LCOV - code coverage report
Current view: top level - gcc/cp - module.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.4 % 11123 10498
Test Date: 2026-10-03 16:17:38 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 interpreted 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 search 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 experimental (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              : /* Provide fallback.  */
     262              : #ifndef EOPNOTSUPP
     263              : #define EOPNOTSUPP ENOTSUP
     264              : #endif
     265              : 
     266              : /* Some open(2) flag differences, what a colourful world it is!  */
     267              : #if defined (O_CLOEXEC)
     268              : // OK
     269              : #elif defined (_O_NOINHERIT)
     270              : /* Windows' _O_NOINHERIT matches O_CLOEXEC flag */
     271              : #define O_CLOEXEC _O_NOINHERIT
     272              : #else
     273              : #define O_CLOEXEC 0
     274              : #endif
     275              : #if defined (O_BINARY)
     276              : // Ok?
     277              : #elif defined (_O_BINARY)
     278              : /* Windows' open(2) call defaults to text!  */
     279              : #define O_BINARY _O_BINARY
     280              : #else
     281              : #define O_BINARY 0
     282              : #endif
     283              : 
     284       294747 : static inline cpp_hashnode *cpp_node (tree id)
     285              : {
     286       294747 :   return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
     287              : }
     288              : 
     289       157666 : static inline tree identifier (const cpp_hashnode *node)
     290              : {
     291              :   /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
     292              :      then subtracts a nonzero constant, deriving a pointer to
     293              :      a different member than ident.  That's strictly undefined
     294              :      and detected by -Warray-bounds.  Suppress it.  See PR 101372.  */
     295       157666 : #pragma GCC diagnostic push
     296       157666 : #pragma GCC diagnostic ignored "-Warray-bounds"
     297       157666 :   return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
     298       157666 : #pragma GCC diagnostic pop
     299              : }
     300              : 
     301              : /* Id for dumping module information.  */
     302              : int module_dump_id;
     303              : 
     304              : /* We have a special module owner.  */
     305              : #define MODULE_UNKNOWN (~0U)    /* Not yet known.  */
     306              : 
     307              : /* Prefix for section names.  */
     308              : #define MOD_SNAME_PFX ".gnu.c++"
     309              : 
     310              : /* Format a version for user consumption.  */
     311              : 
     312              : typedef char verstr_t[32];
     313              : static void
     314         4206 : version2string (unsigned version, verstr_t &out)
     315              : {
     316         4206 :   unsigned major = MODULE_MAJOR (version);
     317         4206 :   unsigned minor = MODULE_MINOR (version);
     318              : 
     319         4206 :   if (IS_EXPERIMENTAL (version))
     320         4206 :     sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
     321         4206 :              2000 + major / 10000, (major / 100) % 100, (major % 100),
     322              :              minor / 100, minor % 100,
     323              :              EXPERIMENT ("", " (experimental)"));
     324              :   else
     325            0 :     sprintf (out, "%u.%u", major, minor);
     326         4206 : }
     327              : 
     328              : /* Include files to note translation for.  */
     329              : static vec<const char *, va_heap, vl_embed> *note_includes;
     330              : 
     331              : /* Modules to note CMI pathnames.  */
     332              : static vec<const char *, va_heap, vl_embed> *note_cmis;
     333              : 
     334              : /* Traits to hash an arbitrary pointer.  Entries are not deletable,
     335              :    and removal is a noop (removal needed upon destruction).  */
     336              : template <typename T>
     337              : struct nodel_ptr_hash : pointer_hash<T>, typed_noop_remove <T *> {
     338              :   /* Nothing is deletable.  Everything is insertable.  */
     339              :   static bool is_deleted (T *) { return false; }
     340              :   static void mark_deleted (T *) { gcc_unreachable (); }
     341              : };
     342              : 
     343              : /* Map from pointer to signed integer.   */
     344              : typedef simple_hashmap_traits<nodel_ptr_hash<void>, int> ptr_int_traits;
     345              : typedef hash_map<void *,signed,ptr_int_traits> ptr_int_hash_map;
     346              : 
     347              : /********************************************************************/
     348              : /* Basic streaming & ELF.  Serialization is usually via mmap.  For
     349              :    writing we slide a buffer over the output file, syncing it
     350              :    approproiately.  For reading we simply map the whole file (as a
     351              :    file-backed read-only map -- it's just address space, leaving the
     352              :    OS pager to deal with getting the data to us).  Some buffers need
     353              :    to be more conventional malloc'd contents.   */
     354              : 
     355              : /* Variable length buffer.  */
     356              : 
     357              : namespace {
     358              : 
     359              : constexpr line_map_uint_t loc_one = 1;
     360              : 
     361              : class data {
     362              : public:
     363         2932 :   class allocator {
     364              :   public:
     365              :     /* Tools tend to moan if the dtor's not virtual.  */
     366       104524 :     virtual ~allocator () {}
     367              : 
     368              :   public:
     369              :     void grow (data &obj, unsigned needed, bool exact);
     370              :     void shrink (data &obj);
     371              : 
     372              :   public:
     373              :     virtual char *grow (char *ptr, unsigned needed);
     374              :     virtual void shrink (char *ptr);
     375              :   };
     376              : 
     377              : public:
     378              :   char *buffer;         /* Buffer being transferred.  */
     379              :   /* Although size_t would be the usual size, we know we never get
     380              :      more than 4GB of buffer -- because that's the limit of the
     381              :      encapsulation format.  And if you need bigger imports, you're
     382              :      doing it wrong.  */
     383              :   unsigned size;        /* Allocated size of buffer.  */
     384              :   unsigned pos;         /* Position in buffer.  */
     385              : 
     386              : public:
     387       933920 :   data ()
     388       933920 :     :buffer (NULL), size (0), pos (0)
     389              :   {
     390              :   }
     391       948548 :   ~data ()
     392              :   {
     393              :     /* Make sure the derived and/or using class know what they're
     394              :        doing.  */
     395       948548 :     gcc_checking_assert (!buffer);
     396       948548 :   }
     397              : 
     398              : protected:
     399    693449970 :   char *use (unsigned count)
     400              :   {
     401    693449970 :     if (size < pos + count)
     402              :       return NULL;
     403    693449970 :     char *res = &buffer[pos];
     404    693449970 :     pos += count;
     405    397349801 :     return res;
     406              :   }
     407              : 
     408              :   unsigned calc_crc (unsigned) const;
     409              : 
     410              : public:
     411     53765608 :   void unuse (unsigned count)
     412              :   {
     413     53765608 :     pos -= count;
     414              :   }
     415              : 
     416              : public:
     417              :   static allocator simple_memory;
     418              : };
     419              : } // anon namespace
     420              : 
     421              : /* The simple data allocator.  */
     422              : data::allocator data::simple_memory;
     423              : 
     424              : /* Grow buffer to at least size NEEDED.  */
     425              : 
     426              : void
     427       864360 : data::allocator::grow (data &obj, unsigned needed, bool exact)
     428              : {
     429       864360 :   gcc_checking_assert (needed ? needed > obj.size : !obj.size);
     430       864360 :   if (!needed)
     431              :     /* Pick a default size.  */
     432       357922 :     needed = EXPERIMENT (100, 1000);
     433              : 
     434       864360 :   if (!exact)
     435       855918 :     needed *= 2;
     436       864360 :   obj.buffer = grow (obj.buffer, needed);
     437       864360 :   if (obj.buffer)
     438       864360 :     obj.size = needed;
     439              :   else
     440            0 :     obj.pos = obj.size = 0;
     441       864360 : }
     442              : 
     443              : /* Free a buffer.  */
     444              : 
     445              : void
     446       372582 : data::allocator::shrink (data &obj)
     447              : {
     448            0 :   shrink (obj.buffer);
     449       372582 :   obj.buffer = NULL;
     450       372582 :   obj.size = 0;
     451            0 : }
     452              : 
     453              : char *
     454        11863 : data::allocator::grow (char *ptr, unsigned needed)
     455              : {
     456        11863 :   return XRESIZEVAR (char, ptr, needed);
     457              : }
     458              : 
     459              : void
     460        14660 : data::allocator::shrink (char *ptr)
     461              : {
     462        14660 :   XDELETEVEC (ptr);
     463        14660 : }
     464              : 
     465              : /* Calculate the crc32 of the buffer.  Note the CRC is stored in the
     466              :    first 4 bytes, so don't include them.  */
     467              : 
     468              : unsigned
     469       584515 : data::calc_crc (unsigned l) const
     470              : {
     471       584515 :   return crc32 (0, (unsigned char *)buffer + 4, l - 4);
     472              : }
     473              : 
     474              : class elf_in;
     475              : 
     476              : /* Byte stream reader.  */
     477              : 
     478              : namespace {
     479              : class bytes_in : public data {
     480              :   typedef data parent;
     481              : 
     482              : protected:
     483              :   bool overrun;  /* Sticky read-too-much flag.  */
     484              : 
     485              : public:
     486       236941 :   bytes_in ()
     487       236941 :     : parent (), overrun (false)
     488              :   {
     489              :   }
     490       239835 :   ~bytes_in ()
     491              :   {
     492        16111 :   }
     493              : 
     494              : public:
     495              :   /* Begin reading a named section.  */
     496              :   bool begin (location_t loc, elf_in *src, const char *name);
     497              :   /* Begin reading a numbered section with optional name.  */
     498              :   bool begin (location_t loc, elf_in *src, unsigned, const char * = NULL);
     499              :   /* Complete reading a buffer.  Propagate errors and return true on
     500              :      success.  */
     501              :   bool end (elf_in *src);
     502              :   /* Return true if there is unread data.  */
     503      1803055 :   bool more_p () const
     504              :   {
     505      1803055 :     return pos != size;
     506              :   }
     507              : 
     508              : public:
     509              :   /* Start reading at OFFSET.  */
     510          813 :   void random_access (unsigned offset)
     511              :   {
     512          813 :     if (offset > size)
     513            0 :       set_overrun ();
     514          813 :     pos = offset;
     515              :   }
     516              : 
     517              : public:
     518      1639996 :   void align (unsigned boundary)
     519              :   {
     520      1639996 :     if (unsigned pad = pos & (boundary - 1))
     521      3176630 :       read (boundary - pad);
     522              :   }
     523              : 
     524              : public:
     525    296100169 :   const char *read (unsigned count)
     526              :   {
     527      1536634 :     char *ptr = use (count);
     528    296100169 :     if (!ptr)
     529            0 :       set_overrun ();
     530    240449558 :     return ptr;
     531              :   }
     532              : 
     533              : public:
     534              :   bool check_crc () const;
     535              :   /* We store the CRC in the first 4 bytes, using host endianness.  */
     536       238086 :   unsigned get_crc () const
     537              :   {
     538       238086 :     return *(const unsigned *)&buffer[0];
     539              :   }
     540              : 
     541              : public:
     542              :   /* Manipulate the overrun flag.  */
     543    172246002 :   bool get_overrun () const
     544              :   {
     545    172246002 :     return overrun;
     546              :   }
     547           26 :   void set_overrun ()
     548              :   {
     549            0 :     overrun = true;
     550              :   }
     551              : 
     552              : public:
     553              :   unsigned u32 ();      /* Read uncompressed integer.  */
     554              : 
     555              : public:
     556              :   int c () ATTRIBUTE_UNUSED;            /* Read a char.  */
     557              :   int i ();             /* Read a signed int.  */
     558              :   unsigned u ();        /* Read an unsigned int.  */
     559              :   size_t z ();          /* Read a size_t.  */
     560              :   location_t loc ();    /* Read a location_t.  */
     561              :   HOST_WIDE_INT wi ();  /* Read a HOST_WIDE_INT.  */
     562              :   unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT.  */
     563              :   const char *str (size_t * = NULL); /* Read a string.  */
     564              :   const void *buf (size_t); /* Read a fixed-length buffer.  */
     565              :   cpp_hashnode *cpp_node (); /* Read a cpp node.  */
     566              : 
     567              :   struct bits_in;
     568              :   bits_in stream_bits ();
     569              : };
     570              : } // anon namespace
     571              : 
     572              : /* Verify the buffer's CRC is correct.  */
     573              : 
     574              : bool
     575       235006 : bytes_in::check_crc () const
     576              : {
     577       235006 :   if (size < 4)
     578              :     return false;
     579              : 
     580       235006 :   unsigned c_crc = calc_crc (size);
     581       235006 :   if (c_crc != get_crc ())
     582            0 :     return false;
     583              : 
     584              :   return true;
     585              : }
     586              : 
     587              : class elf_out;
     588              : 
     589              : /* Byte stream writer.  */
     590              : 
     591              : namespace {
     592              : class bytes_out : public data {
     593              :   typedef data parent;
     594              : 
     595              : public:
     596              :   allocator *memory;    /* Obtainer of memory.  */
     597              : 
     598              : public:
     599       690989 :   bytes_out (allocator *memory)
     600       690989 :     : parent (), memory (memory)
     601              :   {
     602              :   }
     603       690989 :   ~bytes_out ()
     604              :   {
     605       721398 :   }
     606              : 
     607              : public:
     608   1010789471 :   bool streaming_p () const
     609              :   {
     610   1010789471 :     return memory != NULL;
     611              :   }
     612              : 
     613              : public:
     614              :   void set_crc (unsigned *crc_ptr);
     615              : 
     616              : public:
     617              :   /* Begin writing, maybe reserve space for CRC.  */
     618              :   void begin (bool need_crc = true);
     619              :   /* Finish writing.  Spill to section by number.  */
     620              :   unsigned end (elf_out *, unsigned, unsigned *crc_ptr = NULL);
     621              : 
     622              : public:
     623      2352019 :   void align (unsigned boundary)
     624              :   {
     625      2352019 :     if (unsigned pad = pos & (boundary - 1))
     626      2200651 :       write (boundary - pad);
     627      2352019 :   }
     628              : 
     629              : public:
     630    397349801 :   char *write (unsigned count, bool exact = false)
     631              :   {
     632    397349801 :     if (size < pos + count)
     633       491761 :       memory->grow (*this, pos + count, exact);
     634    397349801 :     return use (count);
     635              :   }
     636              : 
     637              : public:
     638              :   void u32 (unsigned);  /* Write uncompressed integer.  */
     639              : 
     640              : public:
     641              :   void c (unsigned char) ATTRIBUTE_UNUSED; /* Write unsigned char.  */
     642              :   void i (int);         /* Write signed int.  */
     643              :   void u (unsigned);    /* Write unsigned int.  */
     644              :   void z (size_t s);    /* Write size_t.  */
     645              :   void loc (location_t); /* Write location_t.  */
     646              :   void wi (HOST_WIDE_INT); /* Write HOST_WIDE_INT.  */
     647              :   void wu (unsigned HOST_WIDE_INT);  /* Write unsigned HOST_WIDE_INT.  */
     648        20186 :   void str (const char *ptr)
     649              :   {
     650        20186 :     str (ptr, strlen (ptr));
     651        20186 :   }
     652       308571 :   void cpp_node (const cpp_hashnode *node)
     653              :   {
     654       308571 :     str ((const char *)NODE_NAME (node), NODE_LEN (node));
     655              :   }
     656              :   void str (const char *, size_t);  /* Write string of known length.  */
     657              :   void buf (const void *, size_t);  /* Write fixed length buffer.  */
     658              :   void *buf (size_t); /* Create a writable buffer */
     659              : 
     660              :   struct bits_out;
     661              :   bits_out stream_bits ();
     662              : 
     663              : public:
     664              :   /* Format a NUL-terminated raw string.  */
     665              :   void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
     666              :   void print_time (const char *, const tm *, const char *);
     667              : 
     668              : public:
     669              :   /* Dump instrumentation.  */
     670              :   static void instrument ();
     671              : 
     672              : protected:
     673              :   /* Instrumentation.  */
     674              :   static unsigned spans[4];
     675              :   static unsigned lengths[4];
     676              : };
     677              : } // anon namespace
     678              : 
     679              : /* Finish bit packet.  Rewind the bytes not used.  */
     680              : 
     681              : static unsigned
     682     53709096 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
     683              : {
     684     53709096 :   gcc_assert (bit_pos);
     685     53709096 :   unsigned bytes = (bit_pos + 7) / 8;
     686     53709096 :   bits.unuse (4 - bytes);
     687     53709096 :   bit_pos = 0;
     688     53709096 :   bit_val = 0;
     689     53709096 :   return bytes;
     690              : }
     691              : 
     692              : /* Bit stream reader (RAII-enabled).  Bools are packed into bytes.  You
     693              :    cannot mix bools and non-bools.  Use bflush to flush the current stream
     694              :    of bools on demand.  Upon destruction bflush is called.
     695              : 
     696              :    When reading, we don't know how many bools we'll read in.  So read
     697              :    4 bytes-worth, and then rewind when flushing if we didn't need them
     698              :    all.  You can't have a block of bools closer than 4 bytes to the
     699              :    end of the buffer.
     700              : 
     701              :    Both bits_in and bits_out maintain the necessary state for bit packing,
     702              :    and since these objects are locally constructed the compiler can more
     703              :    easily track their state across consecutive reads/writes and optimize
     704              :    away redundant buffering checks.  */
     705              : 
     706              : struct bytes_in::bits_in {
     707              :   bytes_in& in;
     708              :   uint32_t bit_val = 0;
     709              :   unsigned bit_pos = 0;
     710              : 
     711     16760431 :   bits_in (bytes_in& in)
     712     16760431 :     : in (in)
     713              :   { }
     714              : 
     715     16760431 :   ~bits_in ()
     716              :   {
     717     15740538 :     bflush ();
     718     16760431 :   }
     719              : 
     720              :   bits_in(bits_in&&) = default;
     721              :   bits_in(const bits_in&) = delete;
     722              :   bits_in& operator=(const bits_in&) = delete;
     723              : 
     724              :   /* Completed a block of bools.  */
     725     34760628 :   void bflush ()
     726              :   {
     727     34760628 :     if (bit_pos)
     728     19020090 :       bit_flush (in, bit_val, bit_pos);
     729     34760628 :   }
     730              : 
     731              :   /* Read one bit.  */
     732    574687665 :   bool b ()
     733              :   {
     734    574687665 :     if (!bit_pos)
     735     25147156 :       bit_val = in.u32 ();
     736    574687665 :     bool x = (bit_val >> bit_pos) & 1;
     737    574687665 :     bit_pos = (bit_pos + 1) % 32;
     738    574687665 :     return x;
     739              :   }
     740              : };
     741              : 
     742              : /* Factory function for bits_in.  */
     743              : 
     744              : bytes_in::bits_in
     745     16760431 : bytes_in::stream_bits ()
     746              : {
     747     16760431 :   return bits_in (*this);
     748              : }
     749              : 
     750              : /* Bit stream writer (RAII-enabled), counterpart to bits_in.  */
     751              : 
     752              : struct bytes_out::bits_out {
     753              :   bytes_out& out;
     754              :   uint32_t bit_val = 0;
     755              :   unsigned bit_pos = 0;
     756              :   char is_set = -1;
     757              : 
     758     23068955 :   bits_out (bytes_out& out)
     759     23068955 :     : out (out)
     760              :   { }
     761              : 
     762     23068955 :   ~bits_out ()
     763              :   {
     764        78790 :     bflush ();
     765              :   }
     766              : 
     767              :   bits_out(bits_out&&) = default;
     768              :   bits_out(const bits_out&) = delete;
     769              :   bits_out& operator=(const bits_out&) = delete;
     770              : 
     771              :   /* Completed a block of bools.  */
     772     47737175 :   void bflush ()
     773              :   {
     774     47737175 :     if (bit_pos)
     775              :       {
     776     26108296 :         out.u32 (bit_val);
     777     26108296 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     778              :       }
     779     47737175 :     out.spans[2]++;
     780     47737175 :     is_set = -1;
     781     47737175 :   }
     782              : 
     783              :   /* Write one bit.
     784              : 
     785              :      It may be worth optimizing for most bools being zero.  Some kind of
     786              :      run-length encoding?  */
     787    789792439 :   void b (bool x)
     788              :   {
     789    789792439 :     if (is_set != x)
     790              :       {
     791     82384130 :         is_set = x;
     792     82384130 :         out.spans[x]++;
     793              :       }
     794    789792439 :     out.lengths[x]++;
     795    789792439 :     bit_val |= unsigned (x) << bit_pos++;
     796    789792439 :     if (bit_pos == 32)
     797              :       {
     798      8580710 :         out.u32 (bit_val);
     799      8580710 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     800              :       }
     801    789792439 :   }
     802              : };
     803              : 
     804              : /* Factory function for bits_out.  */
     805              : 
     806              : bytes_out::bits_out
     807     23068955 : bytes_out::stream_bits ()
     808              : {
     809     23068955 :   return bits_out (*this);
     810              : }
     811              : 
     812              : /* Instrumentation.  */
     813              : unsigned bytes_out::spans[4];
     814              : unsigned bytes_out::lengths[4];
     815              : 
     816              : /* If CRC_PTR non-null, set the CRC of the buffer.  Mix the CRC into
     817              :    that pointed to by CRC_PTR.  */
     818              : 
     819              : void
     820       352294 : bytes_out::set_crc (unsigned *crc_ptr)
     821              : {
     822       352294 :   if (crc_ptr)
     823              :     {
     824       349509 :       gcc_checking_assert (pos >= 4);
     825              : 
     826       349509 :       unsigned crc = calc_crc (pos);
     827       349509 :       unsigned accum = *crc_ptr;
     828              :       /* Only mix the existing *CRC_PTR if it is non-zero.  */
     829       349509 :       accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
     830       349509 :       *crc_ptr = accum;
     831              : 
     832              :       /* Buffer will be sufficiently aligned.  */
     833       349509 :       *(unsigned *)buffer = crc;
     834              :     }
     835       352294 : }
     836              : 
     837              : /* Exactly 4 bytes.  Used internally for bool packing and a few other
     838              :    places.  We can't simply use uint32_t because (a) alignment and
     839              :    (b) we need little-endian for the bool streaming rewinding to make
     840              :    sense.  */
     841              : 
     842              : void
     843     34698041 : bytes_out::u32 (unsigned val)
     844              : {
     845     34698041 :   if (char *ptr = write (4))
     846              :     {
     847     34698041 :       ptr[0] = val;
     848     34698041 :       ptr[1] = val >> 8;
     849     34698041 :       ptr[2] = val >> 16;
     850     34698041 :       ptr[3] = val >> 24;
     851              :     }
     852     34698041 : }
     853              : 
     854              : unsigned
     855     25156834 : bytes_in::u32 ()
     856              : {
     857     25156834 :   unsigned val = 0;
     858     25156834 :   if (const char *ptr = read (4))
     859              :     {
     860     25156834 :       val |= (unsigned char)ptr[0];
     861     25156834 :       val |= (unsigned char)ptr[1] << 8;
     862     25156834 :       val |= (unsigned char)ptr[2] << 16;
     863     25156834 :       val |= (unsigned char)ptr[3] << 24;
     864              :     }
     865              : 
     866     25156834 :   return val;
     867              : }
     868              : 
     869              : /* Chars are unsigned and written as single bytes. */
     870              : 
     871              : void
     872            0 : bytes_out::c (unsigned char v)
     873              : {
     874            0 :   if (char *ptr = write (1))
     875            0 :     *ptr = v;
     876            0 : }
     877              : 
     878              : int
     879            0 : bytes_in::c ()
     880              : {
     881            0 :   int v = 0;
     882            0 :   if (const char *ptr = read (1))
     883            0 :     v = (unsigned char)ptr[0];
     884            0 :   return v;
     885              : }
     886              : 
     887              : /* Ints 7-bit as a byte. Otherwise a 3bit count of following bytes in
     888              :    big-endian form.  4 bits are in the first byte.  */
     889              : 
     890              : void
     891    139900619 : bytes_out::i (int v)
     892              : {
     893    139900619 :   if (char *ptr = write (1))
     894              :     {
     895    139900619 :       if (v <= 0x3f && v >= -0x40)
     896    108913365 :         *ptr = v & 0x7f;
     897              :       else
     898              :         {
     899     30987254 :           unsigned bytes = 0;
     900     30987254 :           int probe;
     901     30987254 :           if (v >= 0)
     902            0 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     903            0 :               bytes++;
     904              :           else
     905     46915205 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     906     15927951 :               bytes++;
     907     30987254 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     908     30987254 :           if ((ptr = write (++bytes)))
     909     77902459 :             for (; bytes--; v >>= 8)
     910     46915205 :               ptr[bytes] = v & 0xff;
     911              :         }
     912              :     }
     913    139900619 : }
     914              : 
     915              : int
     916    102831071 : bytes_in::i ()
     917              : {
     918    102831071 :   int v = 0;
     919    102831071 :   if (const char *ptr = read (1))
     920              :     {
     921    102831071 :       v = *ptr & 0xff;
     922    102831071 :       if (v & 0x80)
     923              :         {
     924     23507031 :           unsigned bytes = (v >> 4) & 0x7;
     925     23507031 :           v &= 0xf;
     926     23507031 :           if (v & 0x8)
     927     23507031 :             v |= -1 ^ 0x7;
     928              :           /* unsigned necessary due to left shifts of -ve values.  */
     929     23507031 :           unsigned uv = unsigned (v);
     930     23507031 :           if ((ptr = read (++bytes)))
     931     60799653 :             while (bytes--)
     932     37292622 :               uv = (uv << 8) | (*ptr++ & 0xff);
     933     23507031 :           v = int (uv);
     934              :         }
     935     79324040 :       else if (v & 0x40)
     936     10007148 :         v |= -1 ^ 0x3f;
     937              :     }
     938              : 
     939    102831071 :   return v;
     940              : }
     941              : 
     942              : void
     943    119757816 : bytes_out::u (unsigned v)
     944              : {
     945    119757816 :   if (char *ptr = write (1))
     946              :     {
     947    119757816 :       if (v <= 0x7f)
     948    104113699 :         *ptr = v;
     949              :       else
     950              :         {
     951     15644117 :           unsigned bytes = 0;
     952     15644117 :           unsigned probe;
     953     18647590 :           for (probe = v >> 8; probe > 0xf; probe >>= 8)
     954      3003473 :             bytes++;
     955     15644117 :           *ptr = 0x80 | bytes << 4 | probe;
     956     15644117 :           if ((ptr = write (++bytes)))
     957     34291707 :             for (; bytes--; v >>= 8)
     958     18647590 :               ptr[bytes] = v & 0xff;
     959              :         }
     960              :     }
     961    119757816 : }
     962              : 
     963              : unsigned
     964     90090169 : bytes_in::u ()
     965              : {
     966     90090169 :   unsigned v = 0;
     967              : 
     968     90090169 :   if (const char *ptr = read (1))
     969              :     {
     970     90090169 :       v = *ptr & 0xff;
     971     90090169 :       if (v & 0x80)
     972              :         {
     973     11843906 :           unsigned bytes = (v >> 4) & 0x7;
     974     11843906 :           v &= 0xf;
     975     11843906 :           if ((ptr = read (++bytes)))
     976     26013286 :             while (bytes--)
     977     14169380 :               v = (v << 8) | (*ptr++ & 0xff);
     978              :         }
     979              :     }
     980              : 
     981     90090169 :   return v;
     982              : }
     983              : 
     984              : void
     985     28613824 : bytes_out::wi (HOST_WIDE_INT v)
     986              : {
     987     28613824 :   if (char *ptr = write (1))
     988              :     {
     989     28613824 :       if (v <= 0x3f && v >= -0x40)
     990      5474876 :         *ptr = v & 0x7f;
     991              :       else
     992              :         {
     993     23138948 :           unsigned bytes = 0;
     994     23138948 :           HOST_WIDE_INT probe;
     995     23138948 :           if (v >= 0)
     996     84804530 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     997     61668944 :               bytes++;
     998              :           else
     999        10662 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
    1000         7300 :               bytes++;
    1001     23138948 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
    1002     23138948 :           if ((ptr = write (++bytes)))
    1003    107954140 :             for (; bytes--; v >>= 8)
    1004     84815192 :               ptr[bytes] = v & 0xff;
    1005              :         }
    1006              :     }
    1007     28613824 : }
    1008              : 
    1009              : HOST_WIDE_INT
    1010     20731488 : bytes_in::wi ()
    1011              : {
    1012     20731488 :   HOST_WIDE_INT v = 0;
    1013     20731488 :   if (const char *ptr = read (1))
    1014              :     {
    1015     20731488 :       v = *ptr & 0xff;
    1016     20731488 :       if (v & 0x80)
    1017              :         {
    1018     18763040 :           unsigned bytes = (v >> 4) & 0x7;
    1019     18763040 :           v &= 0xf;
    1020     18763040 :           if (v & 0x8)
    1021         2122 :             v |= -1 ^ 0x7;
    1022              :           /* unsigned necessary due to left shifts of -ve values.  */
    1023     18763040 :           unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
    1024     18763040 :           if ((ptr = read (++bytes)))
    1025     86772341 :             while (bytes--)
    1026     68009301 :               uv = (uv << 8) | (*ptr++ & 0xff);
    1027     18763040 :           v = (HOST_WIDE_INT) uv;
    1028              :         }
    1029      1968448 :       else if (v & 0x40)
    1030         9032 :         v |= -1 ^ 0x3f;
    1031              :     }
    1032              : 
    1033     20731488 :   return v;
    1034              : }
    1035              : 
    1036              : /* unsigned wide ints are just written as signed wide ints.  */
    1037              : 
    1038              : inline void
    1039     28612900 : bytes_out::wu (unsigned HOST_WIDE_INT v)
    1040              : {
    1041     28612900 :   wi ((HOST_WIDE_INT) v);
    1042              : }
    1043              : 
    1044              : inline unsigned HOST_WIDE_INT
    1045     20730914 : bytes_in::wu ()
    1046              : {
    1047     40856935 :   return (unsigned HOST_WIDE_INT) wi ();
    1048              : }
    1049              : 
    1050              : /* size_t written as unsigned or unsigned wide int.  */
    1051              : 
    1052              : inline void
    1053      2307603 : bytes_out::z (size_t s)
    1054              : {
    1055      2307603 :   if (sizeof (s) == sizeof (unsigned))
    1056              :     u (s);
    1057              :   else
    1058      4581601 :     wu (s);
    1059              : }
    1060              : 
    1061              : inline size_t
    1062      1622651 : bytes_in::z ()
    1063              : {
    1064      1622651 :   if (sizeof (size_t) == sizeof (unsigned))
    1065              :     return u ();
    1066              :   else
    1067      3245302 :     return wu ();
    1068              : }
    1069              : 
    1070              : /* location_t written as 32- or 64-bit as needed.  */
    1071              : 
    1072     25436951 : inline void bytes_out::loc (location_t l)
    1073              : {
    1074     25436951 :   if (sizeof (location_t) > sizeof (unsigned))
    1075     48216426 :     wu (l);
    1076              :   else
    1077              :     u (l);
    1078              : }
    1079              : 
    1080     18506407 : inline location_t bytes_in::loc ()
    1081              : {
    1082     18506407 :   if (sizeof (location_t) > sizeof (unsigned))
    1083     37009747 :     return wu ();
    1084              :   else
    1085              :     return u ();
    1086              : }
    1087              : 
    1088              : /* Buffer simply memcpied.  */
    1089              : void *
    1090      2352019 : bytes_out::buf (size_t len)
    1091              : {
    1092      2352019 :   align (sizeof (void *) * 2);
    1093      2352019 :   return write (len);
    1094              : }
    1095              : 
    1096              : void
    1097      2299270 : bytes_out::buf (const void *src, size_t len)
    1098              : {
    1099      2299270 :   if (void *ptr = buf (len))
    1100      2299270 :     memcpy (ptr, src, len);
    1101      2299270 : }
    1102              : 
    1103              : const void *
    1104      1639996 : bytes_in::buf (size_t len)
    1105              : {
    1106      1639996 :   align (sizeof (void *) * 2);
    1107      1639996 :   const char *ptr = read (len);
    1108              : 
    1109      1639996 :   return ptr;
    1110              : }
    1111              : 
    1112              : /* strings as an size_t length, followed by the buffer.  Make sure
    1113              :    there's a NUL terminator on read.  */
    1114              : 
    1115              : void
    1116      2307585 : bytes_out::str (const char *string, size_t len)
    1117              : {
    1118      2273992 :   z (len);
    1119      2273992 :   if (len)
    1120              :     {
    1121      2273992 :       gcc_checking_assert (!string[len]);
    1122      2273992 :       buf (string, len + 1);
    1123              :     }
    1124      2273992 : }
    1125              : 
    1126              : const char *
    1127      1622645 : bytes_in::str (size_t *len_p)
    1128              : {
    1129      1622645 :   size_t len = z ();
    1130              : 
    1131              :   /* We're about to trust some user data.  */
    1132      1622645 :   if (overrun)
    1133            0 :     len = 0;
    1134      1622645 :   if (len_p)
    1135      1618662 :     *len_p = len;
    1136      1622645 :   const char *str = NULL;
    1137      1622645 :   if (len)
    1138              :     {
    1139      1622372 :       str = reinterpret_cast<const char *> (buf (len + 1));
    1140      1622372 :       if (!str || str[len])
    1141              :         {
    1142            0 :           set_overrun ();
    1143            0 :           str = NULL;
    1144              :         }
    1145              :     }
    1146            0 :   return str ? str : "";
    1147              : }
    1148              : 
    1149              : cpp_hashnode *
    1150       295020 : bytes_in::cpp_node ()
    1151              : {
    1152       295020 :   size_t len;
    1153       295020 :   const char *s = str (&len);
    1154       295020 :   if (!len)
    1155              :     return NULL;
    1156       294747 :   return ::cpp_node (get_identifier_with_length (s, len));
    1157              : }
    1158              : 
    1159              : /* Format a string directly to the buffer, including a terminating
    1160              :    NUL.  Intended for human consumption.  */
    1161              : 
    1162              : void
    1163        28524 : bytes_out::printf (const char *format, ...)
    1164              : {
    1165        28524 :   va_list args;
    1166              :   /* Exercise buffer expansion.  */
    1167        28524 :   size_t len = EXPERIMENT (10, 500);
    1168              : 
    1169        56512 :   while (char *ptr = write (len))
    1170              :     {
    1171        56512 :       va_start (args, format);
    1172        56512 :       size_t actual = vsnprintf (ptr, len, format, args) + 1;
    1173        56512 :       va_end (args);
    1174        56512 :       if (actual <= len)
    1175              :         {
    1176        28524 :           unuse (len - actual);
    1177        28524 :           break;
    1178              :         }
    1179        27988 :       unuse (len);
    1180        27988 :       len = actual;
    1181        27988 :     }
    1182        28524 : }
    1183              : 
    1184              : void
    1185         5570 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
    1186              : {
    1187         5570 :   printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
    1188         5570 :           kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
    1189         5570 :           time->tm_hour, time->tm_min, time->tm_sec, tz);
    1190         5570 : }
    1191              : 
    1192              : /* Encapsulated Lazy Records Of Named Declarations.
    1193              :    Header: Stunningly Elf32_Ehdr-like
    1194              :    Sections: Sectional data
    1195              :      [1-N) : User data sections
    1196              :      N .strtab  : strings, stunningly ELF STRTAB-like
    1197              :    Index: Section table, stunningly ELF32_Shdr-like.   */
    1198              : 
    1199              : class elf {
    1200              : protected:
    1201              :   /* Constants used within the format.  */
    1202              :   enum private_constants {
    1203              :     /* File kind. */
    1204              :     ET_NONE = 0,
    1205              :     EM_NONE = 0,
    1206              :     OSABI_NONE = 0,
    1207              : 
    1208              :     /* File format. */
    1209              :     EV_CURRENT = 1,
    1210              :     CLASS32 = 1,
    1211              :     DATA2LSB = 1,
    1212              :     DATA2MSB = 2,
    1213              : 
    1214              :     /* Section numbering.  */
    1215              :     SHN_UNDEF = 0,
    1216              :     SHN_LORESERVE = 0xff00,
    1217              :     SHN_XINDEX = 0xffff,
    1218              : 
    1219              :     /* Section types.  */
    1220              :     SHT_NONE = 0,       /* No contents.  */
    1221              :     SHT_PROGBITS = 1, /* Random bytes.  */
    1222              :     SHT_STRTAB = 3,     /* A string table.  */
    1223              : 
    1224              :     /* Section flags.  */
    1225              :     SHF_NONE = 0x00,    /* Nothing.  */
    1226              :     SHF_STRINGS = 0x20,  /* NUL-Terminated strings.  */
    1227              : 
    1228              :     /* I really hope we do not get CMI files larger than 4GB.  */
    1229              :     MY_CLASS = CLASS32,
    1230              :     /* It is host endianness that is relevant.  */
    1231              :     MY_ENDIAN = DATA2LSB
    1232              : #ifdef WORDS_BIGENDIAN
    1233              :     ^ DATA2LSB ^ DATA2MSB
    1234              : #endif
    1235              :   };
    1236              : 
    1237              : public:
    1238              :   /* Constants visible to users.  */
    1239              :   enum public_constants {
    1240              :     /* Special error codes.  Breaking layering a bit.  */
    1241              :     E_BAD_DATA = -1,  /* Random unexpected data errors.  */
    1242              :     E_BAD_LAZY = -2,  /* Badly ordered laziness.  */
    1243              :     E_BAD_IMPORT = -3 /* A nested import failed.  */
    1244              :   };
    1245              : 
    1246              : protected:
    1247              :   /* File identification.  On-disk representation.  */
    1248              :   struct ident {
    1249              :     uint8_t magic[4];   /* 0x7f, 'E', 'L', 'F' */
    1250              :     uint8_t klass;      /* 4:CLASS32 */
    1251              :     uint8_t data;       /* 5:DATA2[LM]SB */
    1252              :     uint8_t version;    /* 6:EV_CURRENT  */
    1253              :     uint8_t osabi;      /* 7:OSABI_NONE */
    1254              :     uint8_t abiver;     /* 8: 0 */
    1255              :     uint8_t pad[7];     /* 9-15 */
    1256              :   };
    1257              :   /* File header.  On-disk representation.  */
    1258              :   struct header {
    1259              :     struct ident ident;
    1260              :     uint16_t type;      /* ET_NONE */
    1261              :     uint16_t machine;   /* EM_NONE */
    1262              :     uint32_t version;   /* EV_CURRENT */
    1263              :     uint32_t entry;     /* 0 */
    1264              :     uint32_t phoff;     /* 0 */
    1265              :     uint32_t shoff;     /* Section Header Offset in file */
    1266              :     uint32_t flags;
    1267              :     uint16_t ehsize;    /* ELROND Header SIZE -- sizeof (header) */
    1268              :     uint16_t phentsize; /* 0 */
    1269              :     uint16_t phnum;     /* 0 */
    1270              :     uint16_t shentsize; /* Section Header SIZE -- sizeof (section) */
    1271              :     uint16_t shnum;     /* Section Header NUM */
    1272              :     uint16_t shstrndx;  /* Section Header STRing iNDeX */
    1273              :   };
    1274              :   /* File section.  On-disk representation.  */
    1275              :   struct section {
    1276              :     uint32_t name;      /* String table offset.  */
    1277              :     uint32_t type;      /* SHT_* */
    1278              :     uint32_t flags;     /* SHF_* */
    1279              :     uint32_t addr;      /* 0 */
    1280              :     uint32_t offset;    /* OFFSET in file */
    1281              :     uint32_t size;      /* SIZE of section */
    1282              :     uint32_t link;      /* 0 */
    1283              :     uint32_t info;      /* 0 */
    1284              :     uint32_t addralign; /* 0 */
    1285              :     uint32_t entsize;   /* ENTry SIZE, usually 0 */
    1286              :   };
    1287              : 
    1288              : protected:
    1289              :   data hdr;     /* The header.  */
    1290              :   data sectab;  /* The section table.  */
    1291              :   data strtab;  /* String table.  */
    1292              :   int fd;       /* File descriptor we're reading or writing.  */
    1293              :   int err;      /* Sticky error code.  */
    1294              : 
    1295              : public:
    1296              :   /* Construct from STREAM.  E is errno if STREAM NULL.  */
    1297         5990 :   elf (int fd, int e)
    1298        11980 :     :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
    1299              :   {}
    1300         5908 :   ~elf ()
    1301              :   {
    1302         5908 :     gcc_checking_assert (fd < 0 && !hdr.buffer
    1303              :                          && !sectab.buffer && !strtab.buffer);
    1304         5908 :   }
    1305              : 
    1306              : public:
    1307              :   /* Return the error, if we have an error.  */
    1308       459380 :   int get_error () const
    1309              :   {
    1310       459380 :     return err;
    1311              :   }
    1312              :   /* Set the error, unless it's already been set.  */
    1313           53 :   void set_error (int e = E_BAD_DATA)
    1314              :   {
    1315           53 :     if (!err)
    1316           19 :       err = e;
    1317              :   }
    1318              :   /* Get an error string.  */
    1319              :   const char *get_error (const char *) const;
    1320              : 
    1321              : public:
    1322              :   /* Begin reading/writing file.  Return false on error.  */
    1323         5872 :   bool begin () const
    1324              :   {
    1325         5872 :     return !get_error ();
    1326              :   }
    1327              :   /* Finish reading/writing file.  Return false on error.  */
    1328              :   bool end ();
    1329              : };
    1330              : 
    1331              : /* Return error string.  */
    1332              : 
    1333              : const char *
    1334           40 : elf::get_error (const char *name) const
    1335              : {
    1336           40 :   if (!name)
    1337              :     return "Unknown CMI mapping";
    1338              : 
    1339           40 :   switch (err)
    1340              :     {
    1341            0 :     case 0:
    1342            0 :       gcc_unreachable ();
    1343              :     case E_BAD_DATA:
    1344              :       return "Bad file data";
    1345            6 :     case E_BAD_IMPORT:
    1346            6 :       return "Bad import dependency";
    1347            0 :     case E_BAD_LAZY:
    1348            0 :       return "Bad lazy ordering";
    1349           21 :     default:
    1350           21 :       return xstrerror (err);
    1351              :     }
    1352              : }
    1353              : 
    1354              : /* Finish file, return true if there's an error.  */
    1355              : 
    1356              : bool
    1357         8359 : elf::end ()
    1358              : {
    1359              :   /* Close the stream and free the section table.  */
    1360         8359 :   if (fd >= 0 && close (fd))
    1361            0 :     set_error (errno);
    1362         8359 :   fd = -1;
    1363              : 
    1364         8359 :   return !get_error ();
    1365              : }
    1366              : 
    1367              : /* ELROND reader.  */
    1368              : 
    1369              : class elf_in : public elf {
    1370              :   typedef elf parent;
    1371              : 
    1372              : private:
    1373              :   /* For freezing & defrosting.  */
    1374              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1375              :   dev_t device;
    1376              :   ino_t inode;
    1377              : #endif
    1378              : 
    1379              : public:
    1380         3058 :   elf_in (int fd, int e)
    1381         6116 :     :parent (fd, e)
    1382              :   {
    1383              :   }
    1384         2976 :   ~elf_in ()
    1385              :   {
    1386         2976 :   }
    1387              : 
    1388              : public:
    1389       216300 :   bool is_frozen () const
    1390              :   {
    1391           18 :     return fd < 0 && hdr.pos;
    1392              :   }
    1393           18 :   bool is_freezable () const
    1394              :   {
    1395            9 :     return fd >= 0 && hdr.pos;
    1396              :   }
    1397              :   void freeze ();
    1398              :   bool defrost (const char *);
    1399              : 
    1400              :   /* If BYTES is in the mmapped area, allocate a new buffer for it.  */
    1401            0 :   void preserve (bytes_in &bytes ATTRIBUTE_UNUSED)
    1402              :   {
    1403              : #if MAPPED_READING
    1404            0 :     if (hdr.buffer && bytes.buffer >= hdr.buffer
    1405            0 :         && bytes.buffer < hdr.buffer + hdr.pos)
    1406              :       {
    1407            0 :         char *buf = bytes.buffer;
    1408            0 :         bytes.buffer = data::simple_memory.grow (NULL, bytes.size);
    1409            0 :         memcpy (bytes.buffer, buf, bytes.size);
    1410              :       }
    1411              : #endif
    1412            0 :   }
    1413              :   /* If BYTES is not in SELF's mmapped area, free it.  SELF might be
    1414              :      NULL. */
    1415         1084 :   static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
    1416              :   {
    1417              : #if MAPPED_READING
    1418         1084 :     if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
    1419         1084 :           && bytes.buffer < self->hdr.buffer + self->hdr.pos))
    1420              : #endif
    1421            0 :       data::simple_memory.shrink (bytes.buffer);
    1422         1084 :     bytes.buffer = NULL;
    1423         1084 :     bytes.size = 0;
    1424         1084 :   }
    1425              : 
    1426              : public:
    1427       241074 :   static void grow (data &data, unsigned needed)
    1428              :   {
    1429       241074 :     gcc_checking_assert (!data.buffer);
    1430              : #if !MAPPED_READING
    1431              :     data.buffer = XNEWVEC (char, needed);
    1432              : #endif
    1433       241074 :     data.size = needed;
    1434       241074 :   }
    1435       247746 :   static void shrink (data &data)
    1436              :   {
    1437              : #if !MAPPED_READING
    1438              :     XDELETEVEC (data.buffer);
    1439              : #endif
    1440       247746 :     data.buffer = NULL;
    1441       247746 :     data.size = 0;
    1442              :   }
    1443              : 
    1444              : public:
    1445       238040 :   const section *get_section (unsigned s) const
    1446              :   {
    1447       238040 :     if (s * sizeof (section) < sectab.size)
    1448       238040 :       return reinterpret_cast<const section *>
    1449       238040 :         (&sectab.buffer[s * sizeof (section)]);
    1450              :     else
    1451              :       return NULL;
    1452              :   }
    1453              :   unsigned get_section_limit () const
    1454              :   {
    1455              :     return sectab.size / sizeof (section);
    1456              :   }
    1457              : 
    1458              : protected:
    1459              :   const char *read (data *, unsigned, unsigned);
    1460              : 
    1461              : public:
    1462              :   /* Read section by number.  */
    1463       238040 :   bool read (data *d, const section *s)
    1464              :   {
    1465       238040 :     return s && read (d, s->offset, s->size);
    1466              :   }
    1467              : 
    1468              :   /* Find section by name.  */
    1469              :   unsigned find (const char *name);
    1470              :   /* Find section by index.  */
    1471              :   const section *find (unsigned snum, unsigned type = SHT_PROGBITS);
    1472              : 
    1473              : public:
    1474              :   /* Release the string table, when we're done with it.  */
    1475         8436 :   void release ()
    1476              :   {
    1477         8436 :     shrink (strtab);
    1478              :   }
    1479              : 
    1480              : public:
    1481              :   bool begin (location_t);
    1482         5427 :   bool end ()
    1483              :   {
    1484         5427 :     release ();
    1485              : #if MAPPED_READING
    1486         5427 :     if (hdr.buffer)
    1487         2976 :       munmap (hdr.buffer, hdr.pos);
    1488         5427 :     hdr.buffer = NULL;
    1489              : #endif
    1490         5427 :     shrink (sectab);
    1491              : 
    1492         5427 :     return parent::end ();
    1493              :   }
    1494              : 
    1495              : public:
    1496              :   /* Return string name at OFFSET.  Checks OFFSET range.  Always
    1497              :      returns non-NULL.  We know offset 0 is an empty string.  */
    1498       347462 :   const char *name (unsigned offset)
    1499              :   {
    1500       694924 :     return &strtab.buffer[offset < strtab.size ? offset : 0];
    1501              :   }
    1502              : };
    1503              : 
    1504              : /* ELROND writer.  */
    1505              : 
    1506              : class elf_out : public elf, public data::allocator {
    1507              :   typedef elf parent;
    1508              :   /* Desired section alignment on disk.  */
    1509              :   static const int SECTION_ALIGN = 16;
    1510              : 
    1511              : private:
    1512              :   ptr_int_hash_map identtab;    /* Map of IDENTIFIERS to strtab offsets. */
    1513              :   unsigned pos;                 /* Write position in file.  */
    1514              :   bool began;                   /* True if begin initialized output state.  */
    1515              : #if MAPPED_WRITING
    1516              :   unsigned offset;              /* Offset of the mapping.  */
    1517              :   unsigned extent;              /* Length of mapping.  */
    1518              :   unsigned page_size;           /* System page size.  */
    1519              : #endif
    1520              : 
    1521              : public:
    1522         2932 :   elf_out (int fd, int e)
    1523         5752 :     :parent (fd, e), identtab (500), pos (0), began (false)
    1524              :   {
    1525              : #if MAPPED_WRITING
    1526         2932 :     offset = extent = 0;
    1527         2932 :     page_size = sysconf (_SC_PAGE_SIZE);
    1528         2932 :     if (page_size < SECTION_ALIGN)
    1529              :       /* Something really strange.  */
    1530            0 :       set_error (EINVAL);
    1531              : #endif
    1532         2932 :   }
    1533         2932 :   ~elf_out ()
    1534         2932 :   {
    1535         2932 :     data::simple_memory.shrink (hdr);
    1536         2932 :     data::simple_memory.shrink (sectab);
    1537         2932 :     data::simple_memory.shrink (strtab);
    1538         2932 :   }
    1539              : 
    1540              : #if MAPPED_WRITING
    1541              : private:
    1542              :   void create_mapping (unsigned ext, bool extending = true);
    1543              :   void remove_mapping ();
    1544              : #endif
    1545              : 
    1546              : protected:
    1547              :   using allocator::grow;
    1548              :   char *grow (char *, unsigned needed) final override;
    1549              : #if MAPPED_WRITING
    1550              :   using allocator::shrink;
    1551              :   void shrink (char *) final override;
    1552              : #endif
    1553              : 
    1554              : public:
    1555         5870 :   unsigned get_section_limit () const
    1556              :   {
    1557         5870 :     return sectab.pos / sizeof (section);
    1558              :   }
    1559              : 
    1560              : protected:
    1561              :   unsigned add (unsigned type, unsigned name = 0,
    1562              :                 unsigned off = 0, unsigned size = 0, unsigned flags = SHF_NONE);
    1563              :   unsigned write (const data &);
    1564              : #if MAPPED_WRITING
    1565              :   unsigned write (const bytes_out &);
    1566              : #endif
    1567              : 
    1568              : public:
    1569              :   /* IDENTIFIER to strtab offset.  */
    1570              :   unsigned name (tree ident);
    1571              :   /* String literal to strtab offset.  */
    1572              :   unsigned name (const char *n);
    1573              :   /* Qualified name of DECL to strtab offset.  */
    1574              :   unsigned qualified_name (tree decl, bool is_defn);
    1575              : 
    1576              : private:
    1577              :   unsigned strtab_write (const char *s, unsigned l);
    1578              :   void strtab_write (tree decl, int);
    1579              : 
    1580              : public:
    1581              :   /* Add a section with contents or strings.  */
    1582              :   unsigned add (const bytes_out &, bool string_p, unsigned name);
    1583              : 
    1584              : public:
    1585              :   /* Begin and end writing.  */
    1586              :   bool begin ();
    1587              :   bool end ();
    1588              : };
    1589              : 
    1590              : /* Begin reading section NAME (of type PROGBITS) from SOURCE.
    1591              :    Data always checked for CRC.  */
    1592              : 
    1593              : bool
    1594        21713 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
    1595              : {
    1596        21713 :   unsigned snum = source->find (name);
    1597              : 
    1598        21713 :   return begin (loc, source, snum, name);
    1599              : }
    1600              : 
    1601              : /* Begin reading section numbered SNUM with NAME (may be NULL).  */
    1602              : 
    1603              : bool
    1604       235006 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
    1605              : {
    1606       235006 :   if (!source->read (this, source->find (snum))
    1607       235006 :       || !size || !check_crc ())
    1608              :     {
    1609            0 :       source->set_error (elf::E_BAD_DATA);
    1610            0 :       source->shrink (*this);
    1611            0 :       if (name)
    1612            0 :         error_at (loc, "section %qs is missing or corrupted", name);
    1613              :       else
    1614            0 :         error_at (loc, "section #%u is missing or corrupted", snum);
    1615              :       return false;
    1616              :     }
    1617       235006 :   pos = 4;
    1618       235006 :   return true;
    1619              : }
    1620              : 
    1621              : /* Finish reading a section.  */
    1622              : 
    1623              : bool
    1624       233883 : bytes_in::end (elf_in *src)
    1625              : {
    1626       233883 :   if (more_p ())
    1627           13 :     set_overrun ();
    1628       233883 :   if (overrun)
    1629           13 :     src->set_error ();
    1630              : 
    1631       233883 :   src->shrink (*this);
    1632              : 
    1633       233883 :   return !overrun;
    1634              : }
    1635              : 
    1636              : /* Begin writing buffer.  */
    1637              : 
    1638              : void
    1639       352294 : bytes_out::begin (bool need_crc)
    1640              : {
    1641            0 :   if (need_crc)
    1642            0 :     pos = 4;
    1643            0 :   memory->grow (*this, 0, false);
    1644            0 : }
    1645              : 
    1646              : /* Finish writing buffer.  Stream out to SINK as named section NAME.
    1647              :    Return section number or 0 on failure.  If CRC_PTR is true, crc
    1648              :    the data.  Otherwise it is a string section.  */
    1649              : 
    1650              : unsigned
    1651       352294 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    1652              : {
    1653       352294 :   lengths[3] += pos;
    1654       352294 :   spans[3]++;
    1655              : 
    1656       352294 :   set_crc (crc_ptr);
    1657       352294 :   unsigned sec_num = sink->add (*this, !crc_ptr, name);
    1658       352294 :   memory->shrink (*this);
    1659              : 
    1660       352294 :   return sec_num;
    1661              : }
    1662              : 
    1663              : /* Close and open the file, without destroying it.  */
    1664              : 
    1665              : void
    1666            9 : elf_in::freeze ()
    1667              : {
    1668            9 :   gcc_checking_assert (!is_frozen ());
    1669              : #if MAPPED_READING
    1670            9 :   if (munmap (hdr.buffer, hdr.pos) < 0)
    1671            0 :     set_error (errno);
    1672              : #endif
    1673            9 :   if (close (fd) < 0)
    1674            0 :     set_error (errno);
    1675            9 :   fd = -1;
    1676            9 : }
    1677              : 
    1678              : bool
    1679            9 : elf_in::defrost (const char *name)
    1680              : {
    1681            9 :   gcc_checking_assert (is_frozen ());
    1682            9 :   struct stat stat;
    1683              : 
    1684            9 :   fd = open (name, O_RDONLY | O_CLOEXEC | O_BINARY);
    1685            9 :   if (fd < 0 || fstat (fd, &stat) < 0)
    1686            0 :     set_error (errno);
    1687              :   else
    1688              :     {
    1689            9 :       bool ok = hdr.pos == unsigned (stat.st_size);
    1690              : #ifndef HOST_LACKS_INODE_NUMBERS
    1691            9 :       if (device != stat.st_dev
    1692            9 :           || inode != stat.st_ino)
    1693              :         ok = false;
    1694              : #endif
    1695            9 :       if (!ok)
    1696            0 :         set_error (EMFILE);
    1697              : #if MAPPED_READING
    1698            0 :       if (ok)
    1699              :         {
    1700            9 :           char *mapping = reinterpret_cast<char *>
    1701            9 :             (mmap (NULL, hdr.pos, PROT_READ, MAP_SHARED, fd, 0));
    1702            9 :           if (mapping == MAP_FAILED)
    1703            0 :           fail:
    1704            0 :               set_error (errno);
    1705              :           else
    1706              :             {
    1707            9 :               if (madvise (mapping, hdr.pos, MADV_RANDOM))
    1708            0 :                 goto fail;
    1709              : 
    1710              :               /* These buffers are never NULL in this case.  */
    1711            9 :               strtab.buffer = mapping + strtab.pos;
    1712            9 :               sectab.buffer = mapping + sectab.pos;
    1713            9 :               hdr.buffer = mapping;
    1714              :             }
    1715              :         }
    1716              : #endif
    1717              :     }
    1718              : 
    1719            9 :   return !get_error ();
    1720              : }
    1721              : 
    1722              : /* Read at current position into BUFFER.  Return true on success.  */
    1723              : 
    1724              : const char *
    1725       241074 : elf_in::read (data *data, unsigned pos, unsigned length)
    1726              : {
    1727              : #if MAPPED_READING
    1728       241074 :   if (pos + length > hdr.pos)
    1729              :     {
    1730            0 :       set_error (EINVAL);
    1731              :       return NULL;
    1732              :     }
    1733              : #else
    1734              :   if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
    1735              :     {
    1736              :       set_error (errno);
    1737              :       return NULL;
    1738              :     }
    1739              : #endif
    1740       241074 :   grow (*data, length);
    1741              : #if MAPPED_READING
    1742       241074 :   data->buffer = hdr.buffer + pos;
    1743              : #else
    1744              :   if (::read (fd, data->buffer, data->size) != ssize_t (length))
    1745              :     {
    1746              :       set_error (errno);
    1747              :       shrink (*data);
    1748              :       return NULL;
    1749              :     }
    1750              : #endif
    1751              : 
    1752       241074 :   return data->buffer;
    1753              : }
    1754              : 
    1755              : /* Read section SNUM of TYPE.  Return section pointer or NULL on error.  */
    1756              : 
    1757              : const elf::section *
    1758       238040 : elf_in::find (unsigned snum, unsigned type)
    1759              : {
    1760       238040 :   const section *sec = get_section (snum);
    1761       238040 :   if (!snum || !sec || sec->type != type)
    1762            0 :     return NULL;
    1763              :   return sec;
    1764              : }
    1765              : 
    1766              : /* Find a section NAME and TYPE.  Return section number, or zero on
    1767              :    failure.  */
    1768              : 
    1769              : unsigned
    1770        21758 : elf_in::find (const char *sname)
    1771              : {
    1772       139368 :   for (unsigned pos = sectab.size; pos -= sizeof (section); )
    1773              :     {
    1774       139368 :       const section *sec
    1775       139368 :         = reinterpret_cast<const section *> (&sectab.buffer[pos]);
    1776              : 
    1777       278736 :       if (0 == strcmp (sname, name (sec->name)))
    1778        21758 :         return pos / sizeof (section);
    1779              :     }
    1780              : 
    1781              :   return 0;
    1782              : }
    1783              : 
    1784              : /* Begin reading file.  Verify header.  Pull in section and string
    1785              :    tables.  Return true on success.  */
    1786              : 
    1787              : bool
    1788         3058 : elf_in::begin (location_t loc)
    1789              : {
    1790         3058 :   if (!parent::begin ())
    1791              :     return false;
    1792              : 
    1793         3034 :   struct stat stat;
    1794         3034 :   unsigned size = 0;
    1795         3034 :   if (!fstat (fd, &stat))
    1796              :     {
    1797              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1798         3034 :       device = stat.st_dev;
    1799         3034 :       inode = stat.st_ino;
    1800              : #endif
    1801              :       /* Never generate files > 4GB, check we've not been given one.  */
    1802         3034 :       if (stat.st_size == unsigned (stat.st_size))
    1803         3034 :         size = unsigned (stat.st_size);
    1804              :     }
    1805              : 
    1806              : #if MAPPED_READING
    1807              :   /* MAP_SHARED so that the file is backing store.  If someone else
    1808              :      concurrently writes it, they're wrong.  */
    1809         3034 :   void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    1810         3034 :   if (mapping == MAP_FAILED)
    1811              :     {
    1812            0 :     fail:
    1813            0 :       set_error (errno);
    1814              :       return false;
    1815              :     }
    1816              :   /* We'll be hopping over this randomly.  Some systems declare the
    1817              :      first parm as char *, and other declare it as void *.  */
    1818         3034 :   if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
    1819            0 :     goto fail;
    1820              : 
    1821         3034 :   hdr.buffer = (char *)mapping;
    1822              : #else
    1823              :   read (&hdr, 0, sizeof (header));
    1824              : #endif
    1825         3034 :   hdr.pos = size; /* Record size of the file.  */
    1826              : 
    1827         3034 :   const header *h = reinterpret_cast<const header *> (hdr.buffer);
    1828         3034 :   if (!h)
    1829              :     return false;
    1830              : 
    1831         3034 :   if (h->ident.magic[0] != 0x7f
    1832         3034 :       || h->ident.magic[1] != 'E'
    1833         3034 :       || h->ident.magic[2] != 'L'
    1834         3034 :       || h->ident.magic[3] != 'F')
    1835              :     {
    1836            0 :       error_at (loc, "not Encapsulated Lazy Records of Named Declarations");
    1837            0 :     failed:
    1838            0 :       shrink (hdr);
    1839            0 :       return false;
    1840              :     }
    1841              : 
    1842              :   /* We expect a particular format -- the ELF is not intended to be
    1843              :      distributable.  */
    1844         3034 :   if (h->ident.klass != MY_CLASS
    1845         3034 :       || h->ident.data != MY_ENDIAN
    1846         3034 :       || h->ident.version != EV_CURRENT
    1847         3034 :       || h->type != ET_NONE
    1848         3034 :       || h->machine != EM_NONE
    1849         3034 :       || h->ident.osabi != OSABI_NONE)
    1850              :     {
    1851            0 :       error_at (loc, "unexpected encapsulation format or type");
    1852            0 :       goto failed;
    1853              :     }
    1854              : 
    1855         3034 :   int e = -1;
    1856         3034 :   if (!h->shoff || h->shentsize != sizeof (section))
    1857              :     {
    1858            0 :     malformed:
    1859            0 :       set_error (e);
    1860            0 :       error_at (loc, "encapsulation is malformed");
    1861            0 :       goto failed;
    1862              :     }
    1863              : 
    1864         3034 :   unsigned strndx = h->shstrndx;
    1865         3034 :   unsigned shnum = h->shnum;
    1866         3034 :   if (shnum == SHN_XINDEX)
    1867              :     {
    1868            0 :       if (!read (&sectab, h->shoff, sizeof (section)))
    1869              :         {
    1870            0 :         section_table_fail:
    1871            0 :           e = errno;
    1872            0 :           goto malformed;
    1873              :         }
    1874            0 :       shnum = get_section (0)->size;
    1875              :       /* Freeing does mean we'll re-read it in the case we're not
    1876              :          mapping, but this is going to be rare.  */
    1877            0 :       shrink (sectab);
    1878              :     }
    1879              : 
    1880         3034 :   if (!shnum)
    1881            0 :     goto malformed;
    1882              : 
    1883         3034 :   if (!read (&sectab, h->shoff, shnum * sizeof (section)))
    1884            0 :     goto section_table_fail;
    1885              : 
    1886         3034 :   if (strndx == SHN_XINDEX)
    1887            0 :     strndx = get_section (0)->link;
    1888              : 
    1889         3034 :   if (!read (&strtab, find (strndx, SHT_STRTAB)))
    1890            0 :     goto malformed;
    1891              : 
    1892              :   /* The string table should be at least one byte, with NUL chars
    1893              :      at either end.  */
    1894         3034 :   if (!(strtab.size && !strtab.buffer[0]
    1895         3034 :         && !strtab.buffer[strtab.size - 1]))
    1896            0 :     goto malformed;
    1897              : 
    1898              : #if MAPPED_READING
    1899              :   /* Record the offsets of the section and string tables.  */
    1900         3034 :   sectab.pos = h->shoff;
    1901         3034 :   strtab.pos = shnum * sizeof (section);
    1902              : #else
    1903              :   shrink (hdr);
    1904              : #endif
    1905              : 
    1906         3034 :   return true;
    1907              : }
    1908              : 
    1909              : /* Create a new mapping.  */
    1910              : 
    1911              : #if MAPPED_WRITING
    1912              : void
    1913         3695 : elf_out::create_mapping (unsigned ext, bool extending)
    1914              : {
    1915              :   /* A wrapper around posix_fallocate, falling back to ftruncate
    1916              :      if the underlying filesystem does not support the operation.  */
    1917         7235 :   auto allocate = [](int fd, off_t offset, off_t length)
    1918              :     {
    1919              : #ifdef HAVE_POSIX_FALLOCATE
    1920         3540 :       int result = posix_fallocate (fd, offset, length);
    1921         3540 :       if (result != EINVAL && result != ENOTSUP && result != EOPNOTSUPP)
    1922         3540 :         return result == 0;
    1923              :       /* Not supported by the underlying filesystem, fallback to ftruncate.  */
    1924              : #endif
    1925            0 :       return ftruncate (fd, offset + length) == 0;
    1926              :     };
    1927              : 
    1928         3695 :   void *mapping = MAP_FAILED;
    1929         3695 :   if (extending && ext < 1024 * 1024)
    1930              :     {
    1931         3364 :       if (allocate (fd, offset, ext * 2))
    1932         3364 :         mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
    1933         3364 :                         MAP_SHARED, fd, offset);
    1934         3364 :       if (mapping != MAP_FAILED)
    1935              :         ext *= 2;
    1936              :     }
    1937              :   if (mapping == MAP_FAILED)
    1938              :     {
    1939          331 :       if (!extending || allocate (fd, offset, ext))
    1940          331 :         mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
    1941          331 :                         MAP_SHARED, fd, offset);
    1942          331 :       if (mapping == MAP_FAILED)
    1943              :         {
    1944            0 :           set_error (errno);
    1945              :           mapping = NULL;
    1946              :           ext = 0;
    1947              :         }
    1948              :     }
    1949         3695 :   hdr.buffer = (char *)mapping;
    1950         3695 :   extent = ext;
    1951         3695 : }
    1952              : #endif
    1953              : 
    1954              : /* Flush out the current mapping.  */
    1955              : 
    1956              : #if MAPPED_WRITING
    1957              : void
    1958         3695 : elf_out::remove_mapping ()
    1959              : {
    1960         3695 :   if (hdr.buffer)
    1961              :     {
    1962              :       /* MS_ASYNC dtrt with the removed mapping, including a
    1963              :          subsequent overlapping remap.  */
    1964         3695 :       if (msync (hdr.buffer, extent, MS_ASYNC)
    1965         3695 :           || munmap (hdr.buffer, extent))
    1966              :         /* We're somewhat screwed at this point.  */
    1967            0 :         set_error (errno);
    1968              :     }
    1969              : 
    1970         3695 :   hdr.buffer = NULL;
    1971         3695 : }
    1972              : #endif
    1973              : 
    1974              : /* Grow a mapping of PTR to be NEEDED bytes long.  This gets
    1975              :    interesting if the new size grows the EXTENT.  */
    1976              : 
    1977              : char *
    1978       852497 : elf_out::grow (char *data, unsigned needed)
    1979              : {
    1980       852497 :   if (!data)
    1981              :     {
    1982              :       /* First allocation, check we're aligned.  */
    1983       357922 :       gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    1984              : #if MAPPED_WRITING
    1985       357922 :       data = hdr.buffer + (pos - offset);
    1986              : #endif
    1987              :     }
    1988              : 
    1989              : #if MAPPED_WRITING
    1990       852497 :   unsigned off = data - hdr.buffer;
    1991       852497 :   if (off + needed > extent)
    1992              :     {
    1993              :       /* We need to grow the mapping.  */
    1994          726 :       unsigned lwm = off & ~(page_size - 1);
    1995          726 :       unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
    1996              : 
    1997          726 :       gcc_checking_assert (hwm > extent);
    1998              : 
    1999          726 :       remove_mapping ();
    2000              : 
    2001          726 :       offset += lwm;
    2002          726 :       create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
    2003              : 
    2004          726 :       data = hdr.buffer + (off - lwm);
    2005              :     }
    2006              : #else
    2007              :   data = allocator::grow (data, needed);
    2008              : #endif
    2009              : 
    2010       852497 :   return data;
    2011              : }
    2012              : 
    2013              : #if MAPPED_WRITING
    2014              : /* Shrinking is a NOP.  */
    2015              : void
    2016       357922 : elf_out::shrink (char *)
    2017              : {
    2018       357922 : }
    2019              : #endif
    2020              : 
    2021              : /* Write S of length L to the strtab buffer.  L must include the ending
    2022              :    NUL, if that's what you want.  */
    2023              : 
    2024              : unsigned
    2025      1599121 : elf_out::strtab_write (const char *s, unsigned l)
    2026              : {
    2027      1599121 :   if (strtab.pos + l > strtab.size)
    2028         1327 :     data::simple_memory.grow (strtab, strtab.pos + l, false);
    2029      1599121 :   memcpy (strtab.buffer + strtab.pos, s, l);
    2030      1599121 :   unsigned res = strtab.pos;
    2031      1599121 :   strtab.pos += l;
    2032      1599121 :   return res;
    2033              : }
    2034              : 
    2035              : /* Write qualified name of decl.  INNER >0 if this is a definition, <0
    2036              :    if this is a qualifier of an outer name.  */
    2037              : 
    2038              : void
    2039       641242 : elf_out::strtab_write (tree decl, int inner)
    2040              : {
    2041       641242 :   tree ctx = CP_DECL_CONTEXT (decl);
    2042       641242 :   if (TYPE_P (ctx))
    2043         7759 :     ctx = TYPE_NAME (ctx);
    2044       641242 :   if (ctx != global_namespace)
    2045       329778 :     strtab_write (ctx, -1);
    2046              : 
    2047       641242 :   tree name = DECL_NAME (decl);
    2048       641242 :   if (!name)
    2049          339 :     name = DECL_ASSEMBLER_NAME_RAW (decl);
    2050       641242 :   strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
    2051              : 
    2052       641242 :   if (inner)
    2053       463932 :     strtab_write (&"::{}"[inner+1], 2);
    2054       641242 : }
    2055              : 
    2056              : /* Map IDENTIFIER IDENT to strtab offset.  Inserts into strtab if not
    2057              :    already there.  */
    2058              : 
    2059              : unsigned
    2060       164960 : elf_out::name (tree ident)
    2061              : {
    2062       164960 :   unsigned res = 0;
    2063       164960 :   if (ident)
    2064              :     {
    2065       164905 :       bool existed;
    2066       164905 :       int *slot = &identtab.get_or_insert (ident, &existed);
    2067       164905 :       if (!existed)
    2068       292918 :         *slot = strtab_write (IDENTIFIER_POINTER (ident),
    2069       146459 :                               IDENTIFIER_LENGTH (ident) + 1);
    2070       164905 :       res = *slot;
    2071              :     }
    2072       164960 :   return res;
    2073              : }
    2074              : 
    2075              : /* Map LITERAL to strtab offset.  Does not detect duplicates and
    2076              :    expects LITERAL to remain live until strtab is written out.  */
    2077              : 
    2078              : unsigned
    2079        36024 : elf_out::name (const char *literal)
    2080              : {
    2081        36024 :   return strtab_write (literal, strlen (literal) + 1);
    2082              : }
    2083              : 
    2084              : /* Map a DECL's qualified name to strtab offset.  Does not detect
    2085              :    duplicates.  */
    2086              : 
    2087              : unsigned
    2088       311464 : elf_out::qualified_name (tree decl, bool is_defn)
    2089              : {
    2090       311464 :   gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
    2091       311464 :   unsigned result = strtab.pos;
    2092              : 
    2093       311464 :   strtab_write (decl, is_defn);
    2094       311464 :   strtab_write ("", 1);
    2095              : 
    2096       311464 :   return result;
    2097              : }
    2098              : 
    2099              : /* Add section to file.  Return section number.  TYPE & NAME identify
    2100              :    the section.  OFF and SIZE identify the file location of its
    2101              :    data.  FLAGS contains additional info.  */
    2102              : 
    2103              : unsigned
    2104       357922 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
    2105              :               unsigned flags)
    2106              : {
    2107       357922 :   gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
    2108       357922 :   if (sectab.pos + sizeof (section) > sectab.size)
    2109         4908 :     data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
    2110       357922 :   section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
    2111       357922 :   memset (sec, 0, sizeof (section));
    2112       357922 :   sec->type = type;
    2113       357922 :   sec->flags = flags;
    2114       357922 :   sec->name = name;
    2115       357922 :   sec->offset = off;
    2116       357922 :   sec->size = size;
    2117       357922 :   if (flags & SHF_STRINGS)
    2118         5599 :     sec->entsize = 1;
    2119              : 
    2120       357922 :   unsigned res = sectab.pos;
    2121       357922 :   sectab.pos += sizeof (section);
    2122       357922 :   return res / sizeof (section);
    2123              : }
    2124              : 
    2125              : /* Pad to the next alignment boundary, then write BUFFER to disk.
    2126              :    Return the position of the start of the write, or zero on failure.   */
    2127              : 
    2128              : unsigned
    2129        11256 : elf_out::write (const data &buffer)
    2130              : {
    2131              : #if MAPPED_WRITING
    2132              :   /* HDR is always mapped.  */
    2133        11256 :   if (&buffer != &hdr)
    2134              :     {
    2135         5628 :       bytes_out out (this);
    2136         5628 :       grow (out, buffer.pos, true);
    2137         5628 :       if (out.buffer)
    2138         5628 :         memcpy (out.buffer, buffer.buffer, buffer.pos);
    2139         5628 :       shrink (out);
    2140         5628 :     }
    2141              :   else
    2142              :     /* We should have been aligned during the first allocation.  */
    2143         5628 :     gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    2144              : #else
    2145              :   if (::write (fd, buffer.buffer, buffer.pos) != ssize_t (buffer.pos))
    2146              :     {
    2147              :       set_error (errno);
    2148              :       return 0;
    2149              :     }
    2150              : #endif
    2151        11256 :   unsigned res = pos;
    2152        11256 :   pos += buffer.pos;
    2153              : 
    2154        11256 :   if (unsigned padding = -pos & (SECTION_ALIGN - 1))
    2155              :     {
    2156              : #if !MAPPED_WRITING
    2157              :       /* Align the section on disk, should help the necessary copies.
    2158              :          fseeking to extend is non-portable.  */
    2159              :       static char zero[SECTION_ALIGN];
    2160              :       if (::write (fd, &zero, padding) != ssize_t (padding))
    2161              :         set_error (errno);
    2162              : #endif
    2163         9588 :       pos += padding;
    2164              :     }
    2165        11256 :   return res;
    2166              : }
    2167              : 
    2168              : /* Write a streaming buffer.  It must be using us as an allocator.  */
    2169              : 
    2170              : #if MAPPED_WRITING
    2171              : unsigned
    2172       352294 : elf_out::write (const bytes_out &buf)
    2173              : {
    2174       352294 :   gcc_checking_assert (buf.memory == this);
    2175              :   /* A directly mapped buffer.  */
    2176       352294 :   gcc_checking_assert (buf.buffer - hdr.buffer >= 0
    2177              :                        && buf.buffer - hdr.buffer + buf.size <= extent);
    2178       352294 :   unsigned res = pos;
    2179       352294 :   pos += buf.pos;
    2180              : 
    2181              :   /* Align up.  We're not going to advance into the next page. */
    2182       352294 :   pos += -pos & (SECTION_ALIGN - 1);
    2183              : 
    2184       352294 :   return res;
    2185              : }
    2186              : #endif
    2187              : 
    2188              : /* Write data and add section.  STRING_P is true for a string
    2189              :    section, false for PROGBITS.  NAME identifies the section (0 is the
    2190              :    empty name).  DATA is the contents.  Return section number or 0 on
    2191              :    failure (0 is the undef section).  */
    2192              : 
    2193              : unsigned
    2194       352294 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
    2195              : {
    2196       352294 :   unsigned off = write (data);
    2197              : 
    2198       704588 :   return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
    2199       352294 :               off, data.pos, string_p ? SHF_STRINGS : SHF_NONE);
    2200              : }
    2201              : 
    2202              : /* Begin writing the file.  Initialize the section table and write an
    2203              :    empty header.  Return false on failure.  */
    2204              : 
    2205              : bool
    2206         2814 : elf_out::begin ()
    2207              : {
    2208         2814 :   if (!parent::begin ())
    2209              :     return false;
    2210              : 
    2211              :   /* Let the allocators pick a default.  */
    2212         2814 :   data::simple_memory.grow (strtab, 0, false);
    2213         2814 :   data::simple_memory.grow (sectab, 0, false);
    2214              : 
    2215              :   /* The string table starts with an empty string.  */
    2216         2814 :   name ("");
    2217              : 
    2218              :   /* Create the UNDEF section.  */
    2219         2814 :   add (SHT_NONE);
    2220              : 
    2221              : #if MAPPED_WRITING
    2222              :   /* Start a mapping.  */
    2223         2814 :   create_mapping (EXPERIMENT (page_size,
    2224              :                               (32767 + page_size) & ~(page_size - 1)));
    2225         2814 :   if (!hdr.buffer)
    2226              :     return false;
    2227              : #endif
    2228              : 
    2229              :   /* Write an empty header.  */
    2230         2814 :   grow (hdr, sizeof (header), true);
    2231         2814 :   header *h = reinterpret_cast<header *> (hdr.buffer);
    2232         2814 :   memset (h, 0, sizeof (header));
    2233         2814 :   hdr.pos = hdr.size;
    2234         2814 :   write (hdr);
    2235         2814 :   if (get_error ())
    2236              :     return false;
    2237         2814 :   began = true;
    2238         2814 :   return true;
    2239              : }
    2240              : 
    2241              : /* Finish writing the file.  Write out the string & section tables.
    2242              :    Fill in the header.  Return true on error.  */
    2243              : 
    2244              : bool
    2245         2932 : elf_out::end ()
    2246              : {
    2247         2932 :   if (fd >= 0 && began)
    2248              :     {
    2249              :       /* Write the string table.  */
    2250         2814 :       unsigned strnam = name (".strtab");
    2251         2814 :       unsigned stroff = write (strtab);
    2252         2814 :       unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
    2253              :                              SHF_STRINGS);
    2254              : 
    2255              :       /* Store escape values in section[0].  */
    2256         2814 :       if (strndx >= SHN_LORESERVE)
    2257              :         {
    2258            0 :           reinterpret_cast<section *> (sectab.buffer)->link = strndx;
    2259            0 :           strndx = SHN_XINDEX;
    2260              :         }
    2261         2814 :       unsigned shnum = sectab.pos / sizeof (section);
    2262         2814 :       if (shnum >= SHN_LORESERVE)
    2263              :         {
    2264            0 :           reinterpret_cast<section *> (sectab.buffer)->size = shnum;
    2265            0 :           shnum = SHN_XINDEX;
    2266              :         }
    2267              : 
    2268         2814 :       unsigned shoff = write (sectab);
    2269              : 
    2270              : #if MAPPED_WRITING
    2271         2814 :       if (offset)
    2272              :         {
    2273          155 :           remove_mapping ();
    2274          155 :           offset = 0;
    2275          155 :           create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
    2276              :                           false);
    2277              :         }
    2278         2814 :       unsigned length = pos;
    2279              : #else
    2280              :       if (lseek (fd, 0, SEEK_SET) < 0)
    2281              :         set_error (errno);
    2282              : #endif
    2283              :       /* Write header.  */
    2284         2814 :       if (!get_error ())
    2285              :         {
    2286              :           /* Write the correct header now.  */
    2287         2814 :           header *h = reinterpret_cast<header *> (hdr.buffer);
    2288         2814 :           h->ident.magic[0] = 0x7f;
    2289         2814 :           h->ident.magic[1] = 'E';   /* Elrond */
    2290         2814 :           h->ident.magic[2] = 'L';   /* is an */
    2291         2814 :           h->ident.magic[3] = 'F';   /* elf.  */
    2292         2814 :           h->ident.klass = MY_CLASS;
    2293         2814 :           h->ident.data =  MY_ENDIAN;
    2294         2814 :           h->ident.version = EV_CURRENT;
    2295         2814 :           h->ident.osabi = OSABI_NONE;
    2296         2814 :           h->type = ET_NONE;
    2297         2814 :           h->machine = EM_NONE;
    2298         2814 :           h->version = EV_CURRENT;
    2299         2814 :           h->shoff = shoff;
    2300         2814 :           h->ehsize = sizeof (header);
    2301         2814 :           h->shentsize = sizeof (section);
    2302         2814 :           h->shnum = shnum;
    2303         2814 :           h->shstrndx = strndx;
    2304              : 
    2305         2814 :           pos = 0;
    2306         2814 :           write (hdr);
    2307              :         }
    2308              : 
    2309              : #if MAPPED_WRITING
    2310         2814 :       remove_mapping ();
    2311         2814 :       if (ftruncate (fd, length))
    2312            0 :         set_error (errno);
    2313              : #endif
    2314              :     }
    2315              : 
    2316         2932 :   data::simple_memory.shrink (sectab);
    2317         2932 :   data::simple_memory.shrink (strtab);
    2318              : 
    2319         2932 :   return parent::end ();
    2320              : }
    2321              : 
    2322              : /********************************************************************/
    2323              : 
    2324              : /* A dependency set.  This is used during stream out to determine the
    2325              :    connectivity of the graph.  Every namespace-scope declaration that
    2326              :    needs writing has a depset.  The depset is filled with the (depsets
    2327              :    of) declarations within this module that it references.  For a
    2328              :    declaration that'll generally be named types.  For definitions
    2329              :    it'll also be declarations in the body.
    2330              : 
    2331              :    From that we can convert the graph to a DAG, via determining the
    2332              :    Strongly Connected Clusters.  Each cluster is streamed
    2333              :    independently, and thus we achieve lazy loading.
    2334              : 
    2335              :    Other decls that get a depset are namespaces themselves and
    2336              :    unnameable declarations.   */
    2337              : 
    2338              : class depset {
    2339              : private:
    2340              :   tree entity;  /* Entity, or containing namespace.  */
    2341              :   uintptr_t discriminator;  /* Flags or identifier.  */
    2342              : 
    2343              : public:
    2344              :   /* The kinds of entity the depset could describe.  The ordering is
    2345              :      significant, see entity_kind_name.  */
    2346              :   enum entity_kind
    2347              :   {
    2348              :     EK_DECL,            /* A decl.  */
    2349              :     EK_SPECIALIZATION,  /* A specialization.  */
    2350              :     EK_PARTIAL,         /* A partial specialization.  */
    2351              :     EK_USING,           /* A using declaration (at namespace scope).  */
    2352              :     EK_NAMESPACE,       /* A namespace.  */
    2353              :     EK_TU_LOCAL,        /* A TU-local decl for ADL.  */
    2354              :     EK_REDIRECT,        /* Redirect to a template_decl.  */
    2355              :     EK_EXPLICIT_HWM,
    2356              :     EK_BINDING = EK_EXPLICIT_HWM, /* Implicitly encoded.  */
    2357              :     EK_FOR_BINDING,     /* A decl being inserted for a binding.  */
    2358              :     EK_INNER_DECL,      /* A decl defined outside of its imported
    2359              :                            context.  */
    2360              :     EK_DIRECT_HWM = EK_PARTIAL + 1,
    2361              : 
    2362              :     EK_BITS = 3         /* Only need to encode below EK_EXPLICIT_HWM.  */
    2363              :   };
    2364              :   static_assert (EK_EXPLICIT_HWM < (1u << EK_BITS),
    2365              :                  "not enough bits reserved for entity_kind");
    2366              : 
    2367              : private:
    2368              :   /* Placement of bit fields in discriminator.  */
    2369              :   enum disc_bits
    2370              :   {
    2371              :     DB_ZERO_BIT, /* Set to disambiguate identifier from flags  */
    2372              :     DB_SPECIAL_BIT, /* First dep slot is special.  */
    2373              :     DB_KIND_BIT, /* Kind of the entity.  */
    2374              :     DB_KIND_BITS = EK_BITS,
    2375              :     DB_DEFN_BIT = DB_KIND_BIT + DB_KIND_BITS,
    2376              :     DB_IS_PENDING_BIT,          /* Is a maybe-pending entity.  */
    2377              :     DB_TU_LOCAL_BIT,            /* Is a TU-local entity.  */
    2378              :     DB_REF_GLOBAL_BIT,          /* Refers to a GMF TU-local entity.  */
    2379              :     DB_REF_PURVIEW_BIT,         /* Refers to a purview TU-local entity.  */
    2380              :     DB_EXPOSE_GLOBAL_BIT,       /* Exposes a GMF TU-local entity.  */
    2381              :     DB_EXPOSE_PURVIEW_BIT,      /* Exposes a purview TU-local entity.  */
    2382              :     DB_IGNORED_EXPOSURE_BIT,    /* Only seen where exposures are ignored.  */
    2383              :     DB_IMPORTED_BIT,            /* An imported entity.  */
    2384              :     DB_UNREACHED_BIT,           /* A yet-to-be reached entity.  */
    2385              :     DB_MAYBE_RECURSIVE_BIT,     /* An entity maybe in a recursive cluster.  */
    2386              :     DB_ENTRY_BIT,               /* The first reached recursive dep.  */
    2387              :     DB_HIDDEN_BIT,              /* A hidden binding.  */
    2388              :     /* The following bits are not independent, but enumerating them is
    2389              :        awkward.  */
    2390              :     DB_TYPE_SPEC_BIT,           /* Specialization in the type table.  */
    2391              :     DB_FRIEND_SPEC_BIT,         /* An instantiated template friend.  */
    2392              :     DB_HWM,
    2393              :   };
    2394              :   static_assert (DB_HWM <= sizeof(discriminator) * CHAR_BIT,
    2395              :                  "not enough bits in discriminator");
    2396              : 
    2397              : public:
    2398              :   /* The first slot is special for EK_SPECIALIZATIONS it is a
    2399              :      spec_entry pointer.  It is not relevant for the SCC
    2400              :      determination.  */
    2401              :   vec<depset *> deps;  /* Depsets we reference.  */
    2402              : 
    2403              : public:
    2404              :   unsigned cluster; /* Strongly connected cluster, later entity number  */
    2405              :   unsigned section; /* Section written to.  */
    2406              :   /* During SCC construction, section is lowlink, until the depset is
    2407              :      removed from the stack.  See Tarjan algorithm for details.  */
    2408              : 
    2409              : private:
    2410              :   /* Construction via factories.  Destruction via hash traits.  */
    2411              :   depset (tree entity);
    2412              :   ~depset ();
    2413              : 
    2414              : public:
    2415              :   static depset *make_binding (tree, tree);
    2416              :   static depset *make_entity (tree, entity_kind, bool = false);
    2417              :   /* Late setting a binding name -- /then/ insert into hash!  */
    2418              :   inline void set_binding_name (tree name)
    2419              :   {
    2420              :     gcc_checking_assert (!get_name ());
    2421              :     discriminator = reinterpret_cast<uintptr_t> (name);
    2422              :   }
    2423              : 
    2424              : private:
    2425      6408407 :   template<unsigned I> void set_flag_bit ()
    2426              :   {
    2427            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2428      6408407 :     discriminator |= 1u << I;
    2429      4056359 :   }
    2430      6710607 :   template<unsigned I> void clear_flag_bit ()
    2431              :   {
    2432            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2433      6710607 :     discriminator &= ~(1u << I);
    2434      6710607 :   }
    2435    644376089 :   template<unsigned I> bool get_flag_bit () const
    2436              :   {
    2437            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2438    782782066 :     return bool ((discriminator >> I) & 1);
    2439              :   }
    2440              : 
    2441              : public:
    2442    632051369 :   bool is_binding () const
    2443              :   {
    2444    149172943 :     return !get_flag_bit<DB_ZERO_BIT> ();
    2445              :   }
    2446    327181568 :   entity_kind get_entity_kind () const
    2447              :   {
    2448        42892 :     if (is_binding ())
    2449              :       return EK_BINDING;
    2450    247304380 :     return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
    2451              :   }
    2452              :   const char *entity_kind_name () const;
    2453              : 
    2454              : public:
    2455     10378640 :   bool has_defn () const
    2456              :   {
    2457              :     /* Never consider TU-local entities as having definitions, since
    2458              :        we will never be accessing them from importers anyway.  */
    2459     10378640 :     return get_flag_bit<DB_DEFN_BIT> () && !is_tu_local ();
    2460              :   }
    2461              : 
    2462              : public:
    2463              :   /* This entity might be found other than by namespace-scope lookup;
    2464              :      see module_state::write_pendings for more details.  */
    2465      2581040 :   bool is_pending_entity () const
    2466              :   {
    2467      4095078 :     return (get_entity_kind () == EK_SPECIALIZATION
    2468      1514038 :             || get_entity_kind () == EK_PARTIAL
    2469      4054362 :             || (get_entity_kind () == EK_DECL
    2470      1425255 :                 && get_flag_bit<DB_IS_PENDING_BIT> ()));
    2471              :   }
    2472              : 
    2473              : public:
    2474              :   /* Only consider global module entities as being TU-local
    2475              :      when STRICT is set; otherwise, as an extension we support
    2476              :      emitting declarations referencing TU-local GMF entities
    2477              :      (and only check purview entities), to assist in migration.  */
    2478     55527094 :   bool is_tu_local (bool strict = false) const
    2479              :   {
    2480              :     /* Non-strict is only intended for migration purposes, so
    2481              :        for simplicity's sake we only care about whether this is
    2482              :        a non-purview variable or function at namespace scope;
    2483              :        these are the most common cases (coming from C), and
    2484              :        that way we don't have to care about diagnostics for
    2485              :        nested types and so forth.  */
    2486     24464705 :     tree inner = STRIP_TEMPLATE (get_entity ());
    2487     55527094 :     return (get_flag_bit<DB_TU_LOCAL_BIT> ()
    2488     55527094 :             && (strict
    2489         3348 :                 || !VAR_OR_FUNCTION_DECL_P (inner)
    2490         2218 :                 || !NAMESPACE_SCOPE_P (inner)
    2491         2191 :                 || (DECL_LANG_SPECIFIC (inner)
    2492         2152 :                     && DECL_MODULE_PURVIEW_P (inner))));
    2493              :   }
    2494      1523280 :   bool refs_tu_local (bool strict = false) const
    2495              :   {
    2496       546383 :     return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
    2497       546383 :             || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
    2498              :   }
    2499      3730512 :   bool is_exposure (bool strict = false) const
    2500              :   {
    2501      1865198 :     return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
    2502      3730512 :             || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
    2503              :   }
    2504      2532973 :   bool is_ignored_exposure_context () const
    2505              :   {
    2506      2532973 :     return get_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
    2507              :   }
    2508              : 
    2509              : public:
    2510     33830939 :   bool is_import () const
    2511              :   {
    2512     10341549 :     return get_flag_bit<DB_IMPORTED_BIT> ();
    2513              :   }
    2514     20567949 :   bool is_unreached () const
    2515              :   {
    2516      1309022 :     return get_flag_bit<DB_UNREACHED_BIT> ();
    2517              :   }
    2518      2228684 :   bool is_hidden () const
    2519              :   {
    2520      2228684 :     return get_flag_bit<DB_HIDDEN_BIT> ();
    2521              :   }
    2522      1320389 :   bool is_maybe_recursive () const
    2523              :   {
    2524      1320389 :     return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
    2525              :   }
    2526         1133 :   bool is_entry () const
    2527              :   {
    2528         1133 :     return get_flag_bit<DB_ENTRY_BIT> ();
    2529              :   }
    2530      1600425 :   bool is_type_spec () const
    2531              :   {
    2532      1600425 :     return get_flag_bit<DB_TYPE_SPEC_BIT> ();
    2533              :   }
    2534      1600425 :   bool is_friend_spec () const
    2535              :   {
    2536      1600425 :     return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
    2537              :   }
    2538              : 
    2539              : public:
    2540              :   /* We set these bit outside of depset.  */
    2541           89 :   void set_hidden_binding ()
    2542              :   {
    2543           89 :     set_flag_bit<DB_HIDDEN_BIT> ();
    2544              :   }
    2545           36 :   void clear_hidden_binding ()
    2546              :   {
    2547           36 :     clear_flag_bit<DB_HIDDEN_BIT> ();
    2548              :   }
    2549              : 
    2550              : public:
    2551     12324720 :   bool is_special () const
    2552              :   {
    2553     12324720 :     return get_flag_bit<DB_SPECIAL_BIT> ();
    2554              :   }
    2555      2352048 :   void set_special ()
    2556              :   {
    2557      2352048 :     set_flag_bit<DB_SPECIAL_BIT> ();
    2558              :   }
    2559              : 
    2560              : public:
    2561    206116826 :   tree get_entity () const
    2562              :   {
    2563     55527094 :     return entity;
    2564              :   }
    2565     20306633 :   tree get_name () const
    2566              :   {
    2567     20306633 :     gcc_checking_assert (is_binding ());
    2568     20306633 :     return reinterpret_cast <tree> (discriminator);
    2569              :   }
    2570              : 
    2571              : public:
    2572              :   /* Traits for a hash table of pointers to bindings.  */
    2573              :   struct traits {
    2574              :     /* Each entry is a pointer to a depset. */
    2575              :     typedef depset *value_type;
    2576              :     /* We lookup by container:maybe-identifier pair.  */
    2577              :     typedef std::pair<tree,tree> compare_type;
    2578              : 
    2579              :     static const bool empty_zero_p = true;
    2580              : 
    2581              :     /* hash and equality for compare_type.  */
    2582     20982728 :     inline static hashval_t hash (const compare_type &p)
    2583              :     {
    2584     20982728 :       hashval_t h = pointer_hash<tree_node>::hash (p.first);
    2585     20982728 :       if (p.second)
    2586              :         {
    2587       236755 :           hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
    2588       236755 :           h = iterative_hash_hashval_t (h, nh);
    2589              :         }
    2590     20982728 :       return h;
    2591              :     }
    2592    127075559 :     inline static bool equal (const value_type b, const compare_type &p)
    2593              :     {
    2594    127075559 :       if (b->entity != p.first)
    2595              :         return false;
    2596              : 
    2597     14919852 :       if (p.second)
    2598        42125 :         return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
    2599              :       else
    2600     14877727 :         return !b->is_binding ();
    2601              :     }
    2602              : 
    2603              :     /* (re)hasher for a binding itself.  */
    2604     99557996 :     inline static hashval_t hash (const value_type b)
    2605              :     {
    2606     99557996 :       hashval_t h = pointer_hash<tree_node>::hash (b->entity);
    2607     99557996 :       if (b->is_binding ())
    2608              :         {
    2609      3995419 :           hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
    2610      3995419 :           h = iterative_hash_hashval_t (h, nh);
    2611              :         }
    2612     99557996 :       return h;
    2613              :     }
    2614              : 
    2615              :     /* Empty via NULL.  */
    2616            0 :     static inline void mark_empty (value_type &p) {p = NULL;}
    2617              :     static inline bool is_empty (value_type p) {return !p;}
    2618              : 
    2619              :     /* Nothing is deletable.  Everything is insertable.  */
    2620              :     static bool is_deleted (value_type) { return false; }
    2621              :     static void mark_deleted (value_type) { gcc_unreachable (); }
    2622              : 
    2623              :     /* We own the entities in the hash table.  */
    2624      3337122 :     static void remove (value_type p)
    2625              :     {
    2626      3337122 :       delete (p);
    2627      3337122 :     }
    2628              :   };
    2629              : 
    2630              : public:
    2631              :   class hash : public hash_table<traits> {
    2632              :     typedef traits::compare_type key_t;
    2633              :     typedef hash_table<traits> parent;
    2634              : 
    2635              :   public:
    2636              :     vec<depset *> worklist;  /* Worklist of decls to walk.  */
    2637              :     hash *chain;             /* Original table.  */
    2638              :     depset *current;         /* Current depset being depended.  */
    2639              :     unsigned section;        /* When writing out, the section.  */
    2640              :     bool reached_unreached;  /* We reached an unreached entity.  */
    2641              :     bool writing_merge_key;  /* We're writing merge key information.  */
    2642              : 
    2643              :   private:
    2644              :     bool ignore_exposure;    /* In a context where referencing a TU-local
    2645              :                                 entity is not an exposure.  */
    2646              : 
    2647              :   private:
    2648              :     /* Information needed to do dependent ADL for discovering
    2649              :        more decl-reachable entities.  Cached during walking to
    2650              :        prevent tree marking from interfering with lookup.  */
    2651              :     struct dep_adl_info {
    2652              :       /* The name of the call or operator.  */
    2653              :       tree name = NULL_TREE;
    2654              :       /* If not ERROR_MARK, a rewrite candidate for this operator.  */
    2655              :       tree_code rewrite = ERROR_MARK;
    2656              :       /* Argument list for the call.  */
    2657              :       vec<tree, va_gc>* args = nullptr;
    2658              :     };
    2659              :     vec<dep_adl_info> dep_adl_entity_list;
    2660              : 
    2661              :   public:
    2662       330282 :     hash (size_t size, hash *c = NULL)
    2663       330282 :       : parent (size), chain (c), current (NULL), section (0),
    2664       330282 :         reached_unreached (false), writing_merge_key (false),
    2665       330282 :         ignore_exposure (false)
    2666              :     {
    2667       330282 :       worklist.create (size);
    2668       330282 :       dep_adl_entity_list.create (16);
    2669       330282 :     }
    2670       330282 :     ~hash ()
    2671              :     {
    2672       330282 :       worklist.release ();
    2673       330282 :       dep_adl_entity_list.release ();
    2674       330282 :     }
    2675              : 
    2676              :   public:
    2677    153017690 :     bool is_key_order () const
    2678              :     {
    2679    153017690 :       return chain != NULL;
    2680              :     }
    2681              : 
    2682              :   public:
    2683              :     /* Returns a temporary override that will additionally consider this
    2684              :        to be a context where exposures of TU-local entities are ignored
    2685              :        if COND is true.  */
    2686       972417 :     temp_override<bool> ignore_exposure_if (bool cond)
    2687              :     {
    2688       765847 :       return make_temp_override (ignore_exposure, ignore_exposure || cond);
    2689              :     }
    2690              : 
    2691              :   private:
    2692              :     depset **entity_slot (tree entity, bool = true);
    2693              :     depset **binding_slot (tree ctx, tree name, bool = true);
    2694              :     depset *maybe_add_declaration (tree decl);
    2695              : 
    2696              :   public:
    2697              :     depset *find_dependency (tree entity);
    2698              :     depset *find_binding (tree ctx, tree name);
    2699              :     depset *make_dependency (tree decl, entity_kind);
    2700              :     void add_dependency (depset *);
    2701              : 
    2702              :   public:
    2703              :     void add_mergeable (depset *);
    2704              :     depset *add_dependency (tree decl, entity_kind);
    2705              :     void add_namespace_context (depset *, tree ns);
    2706              : 
    2707              :   private:
    2708              :     static bool add_binding_entity (tree, WMB_Flags, void *);
    2709              : 
    2710              :   public:
    2711              :     bool add_namespace_entities (tree ns, bitmap partitions);
    2712              :     void add_specializations (bool decl_p);
    2713              :     void add_partial_entities (vec<tree, va_gc> *);
    2714              :     void add_class_entities (vec<tree, va_gc> *);
    2715              :     void add_dependent_adl_entities (tree expr);
    2716              : 
    2717              :   private:
    2718              :     void add_deduction_guides (tree decl);
    2719              : 
    2720              :   public:
    2721              :     void find_dependencies (module_state *);
    2722              :     bool finalize_dependencies ();
    2723              :     vec<depset *> connect ();
    2724              : 
    2725              :   private:
    2726              :     bool diagnose_bad_internal_ref (depset *dep, bool strict = false);
    2727              :     bool diagnose_template_names_tu_local (depset *dep, bool strict = false);
    2728              :   };
    2729              : 
    2730              : public:
    2731              :   struct tarjan {
    2732              :     vec<depset *> result;
    2733              :     vec<depset *> stack;
    2734              :     unsigned index;
    2735              : 
    2736       330253 :     tarjan (unsigned size)
    2737       330253 :       : index (0)
    2738              :     {
    2739       330253 :       result.create (size);
    2740       330253 :       stack.create (50);
    2741       330253 :     }
    2742       330253 :     ~tarjan ()
    2743              :     {
    2744       330253 :       gcc_assert (!stack.length ());
    2745       330253 :       stack.release ();
    2746       330253 :     }
    2747              : 
    2748              :   public:
    2749              :     void connect (depset *);
    2750              :   };
    2751              : };
    2752              : 
    2753              : inline
    2754      3337122 : depset::depset (tree entity)
    2755      3337122 :   :entity (entity), discriminator (0), cluster (0), section (0)
    2756              : {
    2757      3337122 :   deps.create (0);
    2758              : }
    2759              : 
    2760              : inline
    2761      3337122 : depset::~depset ()
    2762              : {
    2763      3337122 :   deps.release ();
    2764      3337122 : }
    2765              : 
    2766              : const char *
    2767        45306 : depset::entity_kind_name () const
    2768              : {
    2769              :   /* Same order as entity_kind.  */
    2770        45306 :   static const char *const names[] =
    2771              :     {"decl", "specialization", "partial", "using",
    2772              :      "namespace", "tu-local", "redirect", "binding"};
    2773        45306 :   static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
    2774              :                  "names must have an entry for every explicit entity_kind");
    2775        45306 :   entity_kind kind = get_entity_kind ();
    2776        45306 :   gcc_checking_assert (kind < ARRAY_SIZE (names));
    2777        45306 :   return names[kind];
    2778              : }
    2779              : 
    2780              : /* Create a depset for a namespace binding NS::NAME.  */
    2781              : 
    2782       162519 : depset *depset::make_binding (tree ns, tree name)
    2783              : {
    2784       162519 :   depset *binding = new depset (ns);
    2785              : 
    2786       162519 :   binding->discriminator = reinterpret_cast <uintptr_t> (name);
    2787              : 
    2788       162519 :   return binding;
    2789              : }
    2790              : 
    2791      3174603 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
    2792              : {
    2793      3174603 :   depset *r = new depset (entity);
    2794              : 
    2795      3174603 :   r->discriminator = ((1 << DB_ZERO_BIT)
    2796      3174603 :                       | (ek << DB_KIND_BIT)
    2797      3174603 :                       | is_defn << DB_DEFN_BIT);
    2798              : 
    2799      3174603 :   return r;
    2800              : }
    2801              : 
    2802              : class pending_key
    2803              : {
    2804              : public:
    2805              :   tree ns;
    2806              :   tree id;
    2807              : };
    2808              : 
    2809              : template<>
    2810              : struct default_hash_traits<pending_key>
    2811              : {
    2812              :   using value_type = pending_key;
    2813              : 
    2814              :   static const bool empty_zero_p = false;
    2815     51556739 :   static hashval_t hash (const value_type &k)
    2816              :   {
    2817     51556739 :     hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
    2818     51556739 :     h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
    2819              : 
    2820     51556739 :     return h;
    2821              :   }
    2822     14564577 :   static bool equal (const value_type &k, const value_type &l)
    2823              :   {
    2824     14564577 :     return k.ns == l.ns && k.id == l.id;
    2825              :   }
    2826       229943 :   static void mark_empty (value_type &k)
    2827              :   {
    2828       229943 :     k.ns = k.id = NULL_TREE;
    2829              :   }
    2830         6315 :   static void mark_deleted (value_type &k)
    2831              :   {
    2832         6315 :     k.ns = NULL_TREE;
    2833         6315 :     gcc_checking_assert (k.id);
    2834         6315 :   }
    2835    393470419 :   static bool is_empty (const value_type &k)
    2836              :   {
    2837    393417685 :     return k.ns == NULL_TREE && k.id == NULL_TREE;
    2838              :   }
    2839     20552195 :   static bool is_deleted (const value_type &k)
    2840              :   {
    2841     20552195 :     return k.ns == NULL_TREE && k.id != NULL_TREE;
    2842              :   }
    2843              :   static void remove (value_type &)
    2844              :   {
    2845              :   }
    2846              : };
    2847              : 
    2848              : typedef hash_map<pending_key, auto_vec<unsigned>> pending_map_t;
    2849              : 
    2850              : /* Not-loaded entities that are keyed to a namespace-scope
    2851              :    identifier.  See module_state::write_pendings for details.  */
    2852              : pending_map_t *pending_table;
    2853              : 
    2854              : /* Decls that need some post processing once a batch of lazy loads has
    2855              :    completed.  */
    2856              : vec<tree, va_heap, vl_embed> *post_load_decls;
    2857              : 
    2858              : /* Some entities are keyed to another entity for ODR purposes.
    2859              :    For example, at namespace scope, 'inline auto var = []{};', that
    2860              :    lambda is keyed to 'var', and follows its ODRness.  */
    2861              : typedef hash_map<tree, auto_vec<tree>> keyed_map_t;
    2862              : static keyed_map_t *keyed_table;
    2863              : 
    2864              : static tree get_keyed_decl_scope (tree);
    2865              : 
    2866              : /* Instantiations of temploid friends imported from another module
    2867              :    need to be attached to the same module as the temploid.  This maps
    2868              :    these decls to the temploid they are instantiated from, as there is
    2869              :    no other easy way to get this information.  */
    2870              : static GTY((cache)) decl_tree_cache_map *imported_temploid_friends;
    2871              : 
    2872              : /********************************************************************/
    2873              : /* Tree streaming.   The tree streaming is very specific to the tree
    2874              :    structures themselves.  A tag indicates the kind of tree being
    2875              :    streamed.  -ve tags indicate backreferences to already-streamed
    2876              :    trees.  Backreferences are auto-numbered.  */
    2877              : 
    2878              : /* Tree tags.  */
    2879              : enum tree_tag {
    2880              :   tt_null,              /* NULL_TREE.  */
    2881              :   tt_tu_local,          /* A TU-local entity.  */
    2882              :   tt_fixed,             /* Fixed vector index.  */
    2883              : 
    2884              :   tt_node,              /* By-value node.  */
    2885              :   tt_decl,              /* By-value mergeable decl.  */
    2886              :   tt_tpl_parm,          /* Template parm.  */
    2887              : 
    2888              :   /* The ordering of the following 5 is relied upon in
    2889              :      trees_out::tree_node.  */
    2890              :   tt_id,                /* Identifier node.  */
    2891              :   tt_conv_id,           /* Conversion operator name.  */
    2892              :   tt_anon_id,           /* Anonymous name.  */
    2893              :   tt_lambda_id,         /* Lambda name.  */
    2894              :   tt_internal_id,       /* Internal name.  */
    2895              : 
    2896              :   tt_typedef_type,      /* A (possibly implicit) typedefed type.  */
    2897              :   tt_derived_type,      /* A type derived from another type.  */
    2898              :   tt_variant_type,      /* A variant of another type.  */
    2899              : 
    2900              :   tt_tinfo_var,         /* Typeinfo object. */
    2901              :   tt_tinfo_typedef,     /* Typeinfo typedef.  */
    2902              :   tt_ptrmem_type,       /* Pointer to member type.  */
    2903              :   tt_nttp_var,          /* NTTP_OBJECT VAR_DECL.  */
    2904              : 
    2905              :   tt_parm,              /* Function parameter or result.  */
    2906              :   tt_enum_value,        /* An enum value.  */
    2907              :   tt_enum_decl,         /* An enum decl.  */
    2908              :   tt_data_member,       /* Data member/using-decl.  */
    2909              : 
    2910              :   tt_binfo,             /* A BINFO.  */
    2911              :   tt_vtable,            /* A vtable.  */
    2912              :   tt_thunk,             /* A thunk.  */
    2913              :   tt_clone_ref,
    2914              : 
    2915              :   tt_entity,            /* A extra-cluster entity.  */
    2916              : 
    2917              :   tt_template,          /* The TEMPLATE_RESULT of a template.  */
    2918              : };
    2919              : 
    2920              : enum walk_kind {
    2921              :   WK_none,      /* No walk to do (a back- or fixed-ref happened).  */
    2922              :   WK_normal,    /* Normal walk (by-name if possible).  */
    2923              : 
    2924              :   WK_value,     /* By-value walk.  */
    2925              : };
    2926              : 
    2927              : enum merge_kind
    2928              : {
    2929              :   MK_unique,    /* Known unique.  */
    2930              :   MK_named,     /* Found by CTX, NAME + maybe_arg types etc.  */
    2931              :   MK_field,     /* Found by CTX and index on TYPE_FIELDS  */
    2932              :   MK_vtable,    /* Found by CTX and index on TYPE_VTABLES  */
    2933              :   MK_as_base,   /* Found by CTX.  */
    2934              : 
    2935              :   MK_partial,
    2936              : 
    2937              :   MK_enum,      /* Found by CTX, & 1stMemberNAME.  */
    2938              :   MK_keyed,     /* Found by key & index.  */
    2939              :   MK_local_type, /* Found by CTX, index.  */
    2940              : 
    2941              :   MK_friend_spec,  /* Like named, but has a tmpl & args too.  */
    2942              :   MK_local_friend, /* Found by CTX, index.  */
    2943              : 
    2944              :   MK_indirect_lwm = MK_enum,
    2945              : 
    2946              :   /* Template specialization kinds below. These are all found via
    2947              :      primary template and specialization args.  */
    2948              :   MK_template_mask = 0x10,  /* A template specialization.  */
    2949              : 
    2950              :   MK_tmpl_decl_mask = 0x4, /* In decl table.  */
    2951              : 
    2952              :   MK_tmpl_tmpl_mask = 0x1, /* We want TEMPLATE_DECL.  */
    2953              : 
    2954              :   MK_type_spec = MK_template_mask,
    2955              :   MK_decl_spec = MK_template_mask | MK_tmpl_decl_mask,
    2956              : 
    2957              :   MK_hwm = 0x20
    2958              : };
    2959              : /* This is more than a debugging array.  NULLs are used to determine
    2960              :    an invalid merge_kind number.  */
    2961              : static char const *const merge_kind_name[MK_hwm] =
    2962              :   {
    2963              :     "unique", "named", "field", "vtable",       /* 0...3  */
    2964              :     "asbase", "partial", "enum", "attached",    /* 4...7  */
    2965              : 
    2966              :     "local type", "friend spec", "local friend", NULL,  /* 8...11 */
    2967              :     NULL, NULL, NULL, NULL,
    2968              : 
    2969              :     "type spec", "type tmpl spec",  /* 16,17 type (template).  */
    2970              :     NULL, NULL,
    2971              : 
    2972              :     "decl spec", "decl tmpl spec",  /* 20,21 decl (template).  */
    2973              :     NULL, NULL,
    2974              :     NULL, NULL, NULL, NULL,
    2975              :     NULL, NULL, NULL, NULL,
    2976              :   };
    2977              : 
    2978              : /* Mergeable entity location data.  */
    2979              : struct merge_key {
    2980              :   cp_ref_qualifier ref_q : 2;
    2981              :   unsigned coro_disc : 2;  /* Discriminator for coroutine transforms.  */
    2982              :   unsigned iobj_p : 1;
    2983              :   unsigned xobj_p : 1;
    2984              :   unsigned index;
    2985              : 
    2986              :   tree ret;  /* Return type, if appropriate.  */
    2987              :   tree args; /* Arg types, if appropriate.  */
    2988              : 
    2989              :   tree constraints;  /* Constraints.  */
    2990              : 
    2991      3181322 :   merge_key ()
    2992      2546221 :     :ref_q (REF_QUAL_NONE), coro_disc (0), iobj_p (0), xobj_p (0), index (0),
    2993      3181322 :      ret (NULL_TREE), args (NULL_TREE),
    2994      3181322 :      constraints (NULL_TREE)
    2995              :   {
    2996              :   }
    2997              : };
    2998              : 
    2999              : /* Hashmap of merged duplicates.  Usually decls, but can contain
    3000              :    BINFOs.  */
    3001              : typedef hash_map<tree,uintptr_t,
    3002              :                  simple_hashmap_traits<nodel_ptr_hash<tree_node>,uintptr_t> >
    3003              : duplicate_hash_map;
    3004              : 
    3005              : /* Data needed for post-processing.  */
    3006              : struct post_process_data {
    3007              :   tree decl;
    3008              :   location_t start_locus;
    3009              :   location_t end_locus;
    3010              :   bool returns_value;
    3011              :   bool returns_null;
    3012              :   bool returns_abnormally;
    3013              :   bool infinite_loop;
    3014              : };
    3015              : 
    3016              : /* Tree stream reader.  Note that reading a stream doesn't mark the
    3017              :    read trees with TREE_VISITED.  Thus it's quite safe to have
    3018              :    multiple concurrent readers.  Which is good, because lazy
    3019              :    loading.
    3020              : 
    3021              :    It's important that trees_in/out have internal linkage so that the
    3022              :    compiler knows core_bools, lang_type_bools and lang_decl_bools have
    3023              :    only a single caller (tree_node_bools) and inlines them appropriately.  */
    3024              : namespace {
    3025              : class trees_in : public bytes_in {
    3026              :   typedef bytes_in parent;
    3027              : 
    3028              : private:
    3029              :   module_state *state;          /* Module being imported.  */
    3030              :   vec<tree> back_refs;            /* Back references.  */
    3031              :   duplicate_hash_map *duplicates;       /* Map from existings to duplicate.  */
    3032              :   vec<post_process_data> post_decls;      /* Decls to post process.  */
    3033              :   vec<tree> post_types;           /* Types to post process.  */
    3034              :   unsigned unused;              /* Inhibit any interior TREE_USED
    3035              :                                    marking.  */
    3036              : 
    3037              : public:
    3038              :   trees_in (module_state *);
    3039              :   ~trees_in ();
    3040              : 
    3041              : public:
    3042              :   int insert (tree);
    3043              :   tree back_ref (int);
    3044              : 
    3045              : private:
    3046              :   tree start (unsigned = 0);
    3047              : 
    3048              : public:
    3049              :   /* Needed for binfo writing  */
    3050              :   bool core_bools (tree, bits_in&);
    3051              : 
    3052              : private:
    3053              :   /* Stream tree_core, lang_decl_specific and lang_type_specific
    3054              :      bits.  */
    3055              :   bool core_vals (tree);
    3056              :   bool lang_type_bools (tree, bits_in&);
    3057              :   bool lang_type_vals (tree);
    3058              :   bool lang_decl_bools (tree, bits_in&);
    3059              :   bool lang_decl_vals (tree);
    3060              :   bool lang_vals (tree);
    3061              :   bool tree_node_bools (tree);
    3062              :   bool tree_node_vals (tree);
    3063              :   tree tree_value ();
    3064              :   tree decl_value ();
    3065              :   tree tpl_parm_value ();
    3066              : 
    3067              : private:
    3068              :   tree chained_decls ();  /* Follow DECL_CHAIN.  */
    3069              :   vec<tree, va_heap> *vec_chained_decls ();
    3070              :   vec<tree, va_gc> *tree_vec (); /* vec of tree.  */
    3071              :   vec<tree_pair_s, va_gc> *tree_pair_vec (); /* vec of tree_pair.  */
    3072              :   tree tree_list (bool has_purpose);
    3073              : 
    3074              : public:
    3075              :   /* Read a tree node.  */
    3076              :   tree tree_node (bool is_use = false);
    3077              : 
    3078              : private:
    3079              :   bool install_entity (tree decl);
    3080              :   tree tpl_parms (unsigned &tpl_levels);
    3081              :   bool tpl_parms_fini (tree decl, unsigned tpl_levels);
    3082              :   bool tpl_header (tree decl, unsigned *tpl_levels);
    3083              :   int fn_parms_init (tree);
    3084              :   void fn_parms_fini (int tag, tree fn, tree existing, bool has_defn);
    3085              :   unsigned add_indirect_tpl_parms (tree);
    3086              : public:
    3087              :   bool add_indirects (tree);
    3088              : 
    3089              : public:
    3090              :   /* Serialize various definitions. */
    3091              :   bool read_definition (tree decl);
    3092              : 
    3093              : private:
    3094              :   void check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr);
    3095              :   bool is_matching_decl (tree existing, tree decl, bool is_typedef);
    3096              :   static bool install_implicit_member (tree decl);
    3097              :   bool read_function_def (tree decl, tree maybe_template);
    3098              :   bool read_var_def (tree decl, tree maybe_template);
    3099              :   bool read_class_def (tree decl, tree maybe_template);
    3100              :   bool read_enum_def (tree decl, tree maybe_template);
    3101              : 
    3102              : public:
    3103              :   tree decl_container ();
    3104              :   tree key_mergeable (int tag, merge_kind, tree decl, tree inner, tree type,
    3105              :                       tree container, bool is_attached,
    3106              :                       bool is_imported_temploid_friend);
    3107              :   unsigned binfo_mergeable (tree *);
    3108              : 
    3109              : private:
    3110              :   tree key_local_type (const merge_key&, tree, tree);
    3111              :   uintptr_t *find_duplicate (tree existing);
    3112              :   void register_duplicate (tree decl, tree existing);
    3113              :   /* Mark as an already diagnosed bad duplicate.  */
    3114           54 :   void unmatched_duplicate (tree existing)
    3115              :   {
    3116          108 :     *find_duplicate (existing) |= 1;
    3117           54 :   }
    3118              : 
    3119              : public:
    3120       136008 :   bool is_duplicate (tree decl)
    3121              :   {
    3122       272016 :     return find_duplicate (decl) != NULL;
    3123              :   }
    3124       168247 :   tree maybe_duplicate (tree decl)
    3125              :   {
    3126       168247 :     if (uintptr_t *dup = find_duplicate (decl))
    3127         8974 :       return reinterpret_cast<tree> (*dup & ~uintptr_t (1));
    3128              :     return decl;
    3129              :   }
    3130              :   tree odr_duplicate (tree decl, bool has_defn);
    3131              : 
    3132              : public:
    3133              :   /* Return the decls to postprocess.  */
    3134              :   const vec<post_process_data>& post_process ()
    3135              :   {
    3136              :     return post_decls;
    3137              :   }
    3138              :   /* Return the types to postprocess.  */
    3139              :   const vec<tree>& post_process_type ()
    3140              :   {
    3141              :     return post_types;
    3142              :   }
    3143              : private:
    3144              :   /* Register DATA for postprocessing.  */
    3145       144809 :   void post_process (post_process_data data)
    3146              :   {
    3147       144809 :     post_decls.safe_push (data);
    3148              :   }
    3149              :   /* Register TYPE for postprocessing.  */
    3150           36 :   void post_process_type (tree type)
    3151              :   {
    3152           36 :     gcc_checking_assert (TYPE_P (type));
    3153           36 :     post_types.safe_push (type);
    3154           36 :   }
    3155              : 
    3156              : private:
    3157              :   void assert_definition (tree, bool installing);
    3158              : };
    3159              : } // anon namespace
    3160              : 
    3161       217617 : trees_in::trees_in (module_state *state)
    3162       217617 :   :parent (), state (state), unused (0)
    3163              : {
    3164       217617 :   duplicates = NULL;
    3165       217617 :   back_refs.create (500);
    3166       217617 :   post_decls.create (0);
    3167       217617 :   post_types.create (0);
    3168       217617 : }
    3169              : 
    3170       217617 : trees_in::~trees_in ()
    3171              : {
    3172       321909 :   delete (duplicates);
    3173       217617 :   back_refs.release ();
    3174       217617 :   post_decls.release ();
    3175       217617 :   post_types.release ();
    3176       217617 : }
    3177              : 
    3178              : /* Tree stream writer.  */
    3179              : namespace {
    3180              : class trees_out : public bytes_out {
    3181              :   typedef bytes_out parent;
    3182              : 
    3183              : private:
    3184              :   module_state *state;          /* The module we are writing.  */
    3185              :   ptr_int_hash_map tree_map;    /* Trees to references */
    3186              :   depset::hash *dep_hash;       /* Dependency table.  */
    3187              :   int ref_num;                  /* Back reference number.  */
    3188              :   unsigned section;
    3189              :   bool writing_local_entities;  /* Whether we might walk into a TU-local
    3190              :                                    entity we need to emit placeholders for.  */
    3191              :   bool walking_bit_field_unit;  /* Whether we're walking the underlying
    3192              :                                    storage for a bit field.  There's no other
    3193              :                                    great way to detect this.  */
    3194              : #if CHECKING_P
    3195              :   int importedness;             /* Checker that imports not occurring
    3196              :                                    inappropriately.  +ve imports ok,
    3197              :                                    -ve imports not ok.  */
    3198              : #endif
    3199              : 
    3200              : public:
    3201              :   trees_out (allocator *, module_state *, depset::hash &deps, unsigned sec = 0);
    3202              :   ~trees_out ();
    3203              : 
    3204              : private:
    3205              :   void mark_trees ();
    3206              :   void unmark_trees ();
    3207              : 
    3208              : public:
    3209              :   /* Hey, let's ignore the well known STL iterator idiom.  */
    3210              :   void begin ();
    3211              :   unsigned end (elf_out *sink, unsigned name, unsigned *crc_ptr);
    3212              :   void end ();
    3213              : 
    3214              : public:
    3215              :   enum tags
    3216              :   {
    3217              :     tag_backref = -1,   /* Upper bound on the backrefs.  */
    3218              :     tag_value = 0,      /* Write by value.  */
    3219              :     tag_fixed           /* Lower bound on the fixed trees.  */
    3220              :   };
    3221              : 
    3222              : public:
    3223              :   /* The walk is used for three similar purposes:
    3224              : 
    3225              :       1. The initial scan for dependencies.
    3226              :       2. Once dependencies have been found, ordering them.
    3227              :       3. Writing dependencies to file (streaming_p).
    3228              : 
    3229              :      For cases where it matters, these accessers can be used to determine
    3230              :      which state we're in.  */
    3231    125539807 :   bool is_initial_scan () const
    3232              :   {
    3233    208606998 :     return !streaming_p () && !is_key_order ();
    3234              :   }
    3235    102409304 :   bool is_key_order () const
    3236              :   {
    3237     83067191 :     return dep_hash->is_key_order ();
    3238              :   }
    3239              : 
    3240              : public:
    3241              :   int insert (tree, walk_kind = WK_normal);
    3242              : 
    3243              : private:
    3244              :   void start (tree, bool = false);
    3245              : 
    3246              : private:
    3247              :   walk_kind ref_node (tree);
    3248              : public:
    3249              :   int get_tag (tree);
    3250       654936 :   void set_importing (int i ATTRIBUTE_UNUSED)
    3251              :   {
    3252              : #if CHECKING_P
    3253       654936 :     importedness = i;
    3254              : #endif
    3255              :   }
    3256              : 
    3257              : private:
    3258              :   void core_bools (tree, bits_out&);
    3259              :   void core_vals (tree);
    3260              :   void lang_type_bools (tree, bits_out&);
    3261              :   void lang_type_vals (tree);
    3262              :   void lang_decl_bools (tree, bits_out&);
    3263              :   void lang_decl_vals (tree);
    3264              :   void lang_vals (tree);
    3265              :   void tree_node_bools (tree);
    3266              :   void tree_node_vals (tree);
    3267              : 
    3268              : private:
    3269              :   void chained_decls (tree);
    3270              :   void vec_chained_decls (tree);
    3271              :   void tree_vec (vec<tree, va_gc> *);
    3272              :   void tree_pair_vec (vec<tree_pair_s, va_gc> *);
    3273              :   void tree_list (tree, bool has_purpose);
    3274              : 
    3275              : private:
    3276              :   bool has_tu_local_dep (tree) const;
    3277              :   tree find_tu_local_decl (tree);
    3278              : 
    3279              : public:
    3280              :   /* Mark a node for by-value walking.  */
    3281              :   void mark_by_value (tree);
    3282              : 
    3283              : public:
    3284              :   void tree_node (tree);
    3285              : 
    3286              : private:
    3287              :   void install_entity (tree decl, depset *);
    3288              :   void tpl_parms (tree parms, unsigned &tpl_levels);
    3289              :   void tpl_parms_fini (tree decl, unsigned tpl_levels);
    3290              :   void fn_parms_fini (tree) {}
    3291              :   unsigned add_indirect_tpl_parms (tree);
    3292              : public:
    3293              :   void add_indirects (tree);
    3294              :   void fn_parms_init (tree);
    3295              :   void tpl_header (tree decl, unsigned *tpl_levels);
    3296              : 
    3297              : public:
    3298              :   merge_kind get_merge_kind (tree decl, depset *maybe_dep);
    3299              :   tree decl_container (tree decl);
    3300              :   void key_mergeable (int tag, merge_kind, tree decl, tree inner,
    3301              :                       tree container, depset *maybe_dep);
    3302              :   void binfo_mergeable (tree binfo);
    3303              : 
    3304              : private:
    3305              :   void key_local_type (merge_key&, tree, tree);
    3306              :   bool decl_node (tree, walk_kind ref);
    3307              :   void type_node (tree);
    3308              :   void tree_value (tree);
    3309              :   void tpl_parm_value (tree);
    3310              : 
    3311              : public:
    3312              :   void decl_value (tree, depset *);
    3313              : 
    3314              : public:
    3315              :   /* Serialize various definitions. */
    3316              :   void write_definition (tree decl, bool refs_tu_local = false);
    3317              :   void mark_declaration (tree decl, bool do_defn);
    3318              : 
    3319              : private:
    3320              :   void mark_function_def (tree decl);
    3321              :   void mark_var_def (tree decl);
    3322              :   void mark_class_def (tree decl);
    3323              :   void mark_enum_def (tree decl);
    3324              :   void mark_class_member (tree decl, bool do_defn = true);
    3325              :   void mark_binfos (tree type);
    3326              : 
    3327              : private:
    3328              :   void write_var_def (tree decl);
    3329              :   void write_function_def (tree decl);
    3330              :   void write_class_def (tree decl);
    3331              :   void write_enum_def (tree decl);
    3332              : 
    3333              : private:
    3334              :   static void assert_definition (tree);
    3335              : 
    3336              : public:
    3337              :   static void instrument ();
    3338              : 
    3339              : private:
    3340              :   /* Tree instrumentation. */
    3341              :   static unsigned tree_val_count;
    3342              :   static unsigned decl_val_count;
    3343              :   static unsigned back_ref_count;
    3344              :   static unsigned tu_local_count;
    3345              :   static unsigned null_count;
    3346              : };
    3347              : } // anon namespace
    3348              : 
    3349              : /* Instrumentation counters.  */
    3350              : unsigned trees_out::tree_val_count;
    3351              : unsigned trees_out::decl_val_count;
    3352              : unsigned trees_out::back_ref_count;
    3353              : unsigned trees_out::tu_local_count;
    3354              : unsigned trees_out::null_count;
    3355              : 
    3356       660580 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
    3357              :                       unsigned section)
    3358      1321160 :   :parent (mem), state (state), tree_map (500),
    3359       660580 :    dep_hash (&deps), ref_num (0), section (section),
    3360       660580 :    writing_local_entities (false), walking_bit_field_unit (false)
    3361              : {
    3362              : #if CHECKING_P
    3363       660580 :   importedness = 0;
    3364              : #endif
    3365       660580 : }
    3366              : 
    3367       660580 : trees_out::~trees_out ()
    3368              : {
    3369       660580 : }
    3370              : 
    3371              : /********************************************************************/
    3372              : /* Location.  We're aware of the line-map concept and reproduce it
    3373              :    here.  Each imported module allocates a contiguous span of ordinary
    3374              :    maps, and of macro maps.  adhoc maps are serialized by contents,
    3375              :    not pre-allocated.   The scattered linemaps of a module are
    3376              :    coalesced when writing.  */
    3377              : 
    3378              : 
    3379              : /* I use half-open [first,second) ranges.  */
    3380              : typedef std::pair<line_map_uint_t,line_map_uint_t> range_t;
    3381              : 
    3382              : /* A range of locations.  */
    3383              : typedef std::pair<location_t,location_t> loc_range_t;
    3384              : 
    3385              : /* Spans of the line maps that are occupied by this TU.  I.e. not
    3386              :    within imports.  Only extended when in an interface unit.
    3387              :    Interval zero corresponds to the forced header linemap(s).  This
    3388              :    is a singleton object.  */
    3389              : 
    3390              : class loc_spans {
    3391              : public:
    3392              :   /* An interval of line maps.  The line maps here represent a contiguous
    3393              :      non-imported range.  */
    3394         6062 :   struct span {
    3395              :     loc_range_t ordinary;       /* Ordinary map location range. */
    3396              :     loc_range_t macro;          /* Macro map location range.  */
    3397              :     /* Add to locs to get serialized loc.  */
    3398              :     location_diff_t ordinary_delta;
    3399              :     location_diff_t macro_delta;
    3400              :   };
    3401              : 
    3402              : private:
    3403              :   vec<span> *spans;
    3404              :   bool locs_exhausted_p;
    3405              : 
    3406              : public:
    3407              :   loc_spans ()
    3408              :     /* Do not preallocate spans, as that causes
    3409              :        --enable-detailed-mem-stats problems.  */
    3410              :     : spans (nullptr), locs_exhausted_p (false)
    3411              :   {
    3412              :   }
    3413       101592 :   ~loc_spans ()
    3414              :   {
    3415       101592 :     delete spans;
    3416       101592 :   }
    3417              : 
    3418              : public:
    3419          300 :   span &operator[] (unsigned ix)
    3420              :   {
    3421          600 :     return (*spans)[ix];
    3422              :   }
    3423              :   unsigned length () const
    3424              :   {
    3425              :     return spans->length ();
    3426              :   }
    3427              : 
    3428              : public:
    3429        15848 :   bool init_p () const
    3430              :   {
    3431        15848 :     return spans != nullptr;
    3432              :   }
    3433              :   /* Initializer.  */
    3434              :   void init (const line_maps *lmaps, const line_map_ordinary *map);
    3435              : 
    3436              :   /* Slightly skewed preprocessed files can cause us to miss an
    3437              :      initialization in some places.  Fallback initializer.  */
    3438         5881 :   void maybe_init ()
    3439              :   {
    3440         5881 :     if (!init_p ())
    3441            6 :       init (line_table, nullptr);
    3442         5881 :   }
    3443              : 
    3444              : public:
    3445              :   enum {
    3446              :     SPAN_RESERVED = 0,  /* Reserved (fixed) locations.  */
    3447              :     SPAN_FIRST = 1,     /* LWM of locations to stream  */
    3448              :     SPAN_MAIN = 2       /* Main file and onwards.  */
    3449              :   };
    3450              : 
    3451              : public:
    3452       469638 :   location_t main_start () const
    3453              :   {
    3454       469638 :     return (*spans)[SPAN_MAIN].ordinary.first;
    3455              :   }
    3456              : 
    3457              : public:
    3458              :   void open (location_t);
    3459              :   void close ();
    3460              : 
    3461              : public:
    3462              :   /* Propagate imported linemaps to us, if needed.  */
    3463              :   bool maybe_propagate (module_state *import, location_t loc);
    3464              : 
    3465              : public:
    3466              :   /* Whether we can no longer represent new imported locations.  */
    3467            0 :   bool locations_exhausted_p () const
    3468              :   {
    3469            0 :     return locs_exhausted_p;
    3470              :   }
    3471            0 :   void report_location_exhaustion (location_t loc)
    3472              :   {
    3473            0 :     if (!locs_exhausted_p)
    3474              :       {
    3475              :         /* Just give the notice once.  */
    3476            0 :         locs_exhausted_p = true;
    3477            0 :         inform (loc, "unable to represent further imported source locations");
    3478              :       }
    3479              :   }
    3480              : 
    3481              : public:
    3482              :   const span *ordinary (location_t);
    3483              :   const span *macro (location_t);
    3484              : };
    3485              : 
    3486              : static loc_spans spans;
    3487              : 
    3488              : /* Information about ordinary locations we stream out.  */
    3489              : struct ord_loc_info
    3490              : {
    3491              :   const line_map_ordinary *src; // line map we're based on
    3492              :   line_map_uint_t offset;       // offset to this line
    3493              :   line_map_uint_t span;         // number of locs we span
    3494              :   line_map_uint_t remap;        // serialization
    3495              : 
    3496    328578967 :   static int compare (const void *a_, const void *b_)
    3497              :   {
    3498    328578967 :     auto *a = static_cast<const ord_loc_info *> (a_);
    3499    328578967 :     auto *b = static_cast<const ord_loc_info *> (b_);
    3500              : 
    3501    328578967 :     if (a->src != b->src)
    3502     48281922 :       return a->src < b->src ? -1 : +1;
    3503              : 
    3504              :     // Ensure no overlap
    3505    280297045 :     gcc_checking_assert (a->offset + a->span <= b->offset
    3506              :                          || b->offset + b->span <= a->offset);
    3507              : 
    3508    280297045 :     gcc_checking_assert (a->offset != b->offset);
    3509    280297045 :     return a->offset < b->offset ? -1 : +1;
    3510              :   }
    3511              : };
    3512              : struct ord_loc_traits
    3513              : {
    3514              :   typedef ord_loc_info value_type;
    3515              :   typedef value_type compare_type;
    3516              : 
    3517              :   static const bool empty_zero_p = false;
    3518              : 
    3519    139320758 :   static hashval_t hash (const value_type &v)
    3520              :   {
    3521    117859382 :     auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
    3522    139320758 :     return iterative_hash_hashval_t (v.offset, h);
    3523              :   }
    3524    153688817 :   static bool equal (const value_type &v, const compare_type p)
    3525              :   {
    3526    153688817 :     return v.src == p.src && v.offset == p.offset;
    3527              :   }
    3528              : 
    3529      9940662 :   static void mark_empty (value_type &v)
    3530              :   {
    3531      9940662 :     v.src = nullptr;
    3532              :   }
    3533    451618792 :   static bool is_empty (value_type &v)
    3534              :   {
    3535    451618792 :     return !v.src;
    3536              :   }
    3537              : 
    3538              :   static bool is_deleted (value_type &) { return false; }
    3539              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3540              : 
    3541      2430816 :   static void remove (value_type &) {}
    3542              : };
    3543              : /* Table keyed by ord_loc_info, used for noting.  */
    3544              : static  hash_table<ord_loc_traits> *ord_loc_table;
    3545              : /* Sorted vector, used for writing.  */
    3546              : static vec<ord_loc_info> *ord_loc_remap;
    3547              : 
    3548              : /* Information about macro locations we stream out.  */
    3549              : struct macro_loc_info
    3550              : {
    3551              :   const line_map_macro *src;    // original expansion
    3552              :   line_map_uint_t remap;        // serialization
    3553              : 
    3554      9801868 :   static int compare (const void *a_, const void *b_)
    3555              :   {
    3556      9801868 :     auto *a = static_cast<const macro_loc_info *> (a_);
    3557      9801868 :     auto *b = static_cast<const macro_loc_info *> (b_);
    3558              : 
    3559      9801868 :     gcc_checking_assert (MAP_START_LOCATION (a->src)
    3560              :                          != MAP_START_LOCATION (b->src));
    3561      9801868 :     if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
    3562              :       return -1;
    3563              :     else
    3564      4727358 :       return +1;
    3565              :   }
    3566              : };
    3567              : struct macro_loc_traits
    3568              : {
    3569              :   typedef macro_loc_info value_type;
    3570              :   typedef const line_map_macro *compare_type;
    3571              : 
    3572              :   static const bool empty_zero_p = false;
    3573              : 
    3574     11005332 :   static hashval_t hash (compare_type p)
    3575              :   {
    3576     11005332 :     return pointer_hash<const line_map_macro>::hash (p);
    3577              :   }
    3578      9397708 :   static hashval_t hash (const value_type &v)
    3579              :   {
    3580      9397708 :     return hash (v.src);
    3581              :   }
    3582              :   static bool equal (const value_type &v, const compare_type p)
    3583              :   {
    3584              :     return v.src == p;
    3585              :   }
    3586              : 
    3587       727677 :   static void mark_empty (value_type &v)
    3588              :   {
    3589       727677 :     v.src = nullptr;
    3590              :   }
    3591     34064536 :   static bool is_empty (value_type &v)
    3592              :   {
    3593     34064536 :     return !v.src;
    3594              :   }
    3595              : 
    3596              :   static bool is_deleted (value_type &) { return false; }
    3597              :   static void mark_deleted (value_type &) { gcc_unreachable (); }
    3598              : 
    3599       168247 :   static void remove (value_type &) {}
    3600              : };
    3601              : /* Table keyed by line_map_macro, used for noting.  */
    3602              : static  hash_table<macro_loc_traits> *macro_loc_table;
    3603              : /* Sorted vector, used for writing.  */
    3604              : static vec<macro_loc_info> *macro_loc_remap;
    3605              : 
    3606              : /* Indirection to allow bsearching imports by ordinary location.  */
    3607              : static vec<module_state *> *ool;
    3608              : 
    3609              : /********************************************************************/
    3610              : /* Data needed by a module during the process of loading.  */
    3611              : struct GTY(()) slurping {
    3612              : 
    3613              :   /* Remap import's module numbering to our numbering.  Values are
    3614              :      shifted by 1.  Bit0 encodes if the import is direct.  */
    3615              :   vec<unsigned, va_heap, vl_embed> *
    3616              :     GTY((skip)) remap;                  /* Module owner remapping.  */
    3617              : 
    3618              :   elf_in *GTY((skip)) from;             /* The elf loader.  */
    3619              : 
    3620              :   /* This map is only for header imports themselves -- the global
    3621              :      headers bitmap hold it for the current TU.  */
    3622              :   bitmap headers;       /* Transitive set of direct imports, including
    3623              :                            self.  Used for macro visibility and
    3624              :                            priority.  */
    3625              : 
    3626              :   /* These objects point into the mmapped area, unless we're not doing
    3627              :      that, or we got frozen or closed.  In those cases they point to
    3628              :      buffers we own.  */
    3629              :   bytes_in macro_defs;  /* Macro definitions.  */
    3630              :   bytes_in macro_tbl;   /* Macro table.  */
    3631              : 
    3632              :   /* Location remapping.  first->ordinary, second->macro.  */
    3633              :   range_t GTY((skip)) loc_deltas;
    3634              : 
    3635              :   unsigned current;     /* Section currently being loaded.  */
    3636              :   unsigned remaining;   /* Number of lazy sections yet to read.  */
    3637              :   unsigned lru;         /* An LRU counter.  */
    3638              : 
    3639              :  public:
    3640              :   slurping (elf_in *);
    3641              :   ~slurping ();
    3642              : 
    3643              :  public:
    3644              :   /* Close the ELF file, if it's open.  */
    3645         5427 :   void close ()
    3646              :   {
    3647         5427 :     if (from)
    3648              :       {
    3649         2976 :         from->end ();
    3650         5952 :         delete from;
    3651         2976 :         from = NULL;
    3652              :       }
    3653         5427 :   }
    3654              : 
    3655              :  public:
    3656              :   void release_macros ();
    3657              : 
    3658              :  public:
    3659         3034 :   void alloc_remap (unsigned size)
    3660              :   {
    3661         3034 :     gcc_assert (!remap);
    3662         3034 :     vec_safe_reserve (remap, size);
    3663         9501 :     for (unsigned ix = size; ix--;)
    3664         3433 :       remap->quick_push (0);
    3665         3034 :   }
    3666      1241897 :   unsigned remap_module (unsigned owner)
    3667              :   {
    3668      1241897 :     if (owner < remap->length ())
    3669      1241897 :       return (*remap)[owner] >> 1;
    3670              :     return 0;
    3671              :   }
    3672              : 
    3673              :  public:
    3674              :   /* GC allocation.  But we must explicitly delete it.   */
    3675         3058 :   static void *operator new (size_t x)
    3676              :   {
    3677         6116 :     return ggc_alloc_atomic (x);
    3678              :   }
    3679         2976 :   static void operator delete (void *p)
    3680              :   {
    3681         2976 :     ggc_free (p);
    3682              :   }
    3683              : };
    3684              : 
    3685         3058 : slurping::slurping (elf_in *from)
    3686         3058 :   : remap (NULL), from (from),
    3687         3058 :     headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
    3688         3058 :     loc_deltas (0, 0),
    3689         3058 :     current (~0u), remaining (0), lru (0)
    3690              : {
    3691         3058 : }
    3692              : 
    3693         2976 : slurping::~slurping ()
    3694              : {
    3695         2976 :   vec_free (remap);
    3696         2976 :   remap = NULL;
    3697         2976 :   release_macros ();
    3698         2976 :   close ();
    3699         2976 : }
    3700              : 
    3701         5427 : void slurping::release_macros ()
    3702              : {
    3703         5427 :   if (macro_defs.size)
    3704          902 :     elf_in::release (from, macro_defs);
    3705         5427 :   if (macro_tbl.size)
    3706            0 :     elf_in::release (from, macro_tbl);
    3707         5427 : }
    3708              : 
    3709              : /* Flags for extensions that end up being streamed.  */
    3710              : 
    3711              : enum streamed_extensions {
    3712              :   SE_OPENMP_SIMD = 1 << 0,
    3713              :   SE_OPENMP = 1 << 1,
    3714              :   SE_OPENACC = 1 << 2,
    3715              :   SE_BITS = 3
    3716              : };
    3717              : 
    3718              : /* Counter indices.  */
    3719              : enum module_state_counts
    3720              : {
    3721              :   MSC_sec_lwm,
    3722              :   MSC_sec_hwm,
    3723              :   MSC_pendings,
    3724              :   MSC_entities,
    3725              :   MSC_namespaces,
    3726              :   MSC_using_directives,
    3727              :   MSC_bindings,
    3728              :   MSC_macros,
    3729              :   MSC_inits,
    3730              :   MSC_HWM
    3731              : };
    3732              : 
    3733              : /********************************************************************/
    3734              : struct module_state_config;
    3735              : 
    3736              : /* Increasing levels of loadedness.  */
    3737              : enum module_loadedness {
    3738              :   ML_NONE,              /* Not loaded.  */
    3739              :   ML_CONFIG,            /* Config loaed.  */
    3740              :   ML_PREPROCESSOR,      /* Preprocessor loaded.  */
    3741              :   ML_LANGUAGE,          /* Language loaded.  */
    3742              : };
    3743              : 
    3744              : /* Increasing levels of directness (toplevel) of import.  */
    3745              : enum module_directness {
    3746              :   MD_NONE,              /* Not direct.  */
    3747              :   MD_PARTITION_DIRECT,  /* Direct import of a partition.  */
    3748              :   MD_DIRECT,            /* Direct import.  */
    3749              :   MD_PURVIEW_DIRECT,    /* Direct import in purview.  */
    3750              : };
    3751              : 
    3752              : /* State of a particular module. */
    3753              : 
    3754              : class GTY((chain_next ("%h.parent"), for_user)) module_state {
    3755              :  public:
    3756              :   /* We always import & export ourselves.  */
    3757              :   bitmap imports;       /* Transitive modules we're importing.  */
    3758              :   bitmap exports;       /* Subset of that, that we're exporting.  */
    3759              : 
    3760              :   /* For a named module interface A.B, parent is A and name is B.
    3761              :      For a partition M:P, parent is M and name is P.
    3762              :      For an implementation unit I, parent is I's interface and name is NULL.
    3763              :      Otherwise parent is NULL and name will be the flatname.  */
    3764              :   module_state *parent;
    3765              :   tree name;
    3766              : 
    3767              :   slurping *slurp;      /* Data for loading.  */
    3768              : 
    3769              :   const char *flatname; /* Flatname of module.  */
    3770              :   char *filename;       /* CMI Filename */
    3771              : 
    3772              :   /* Indices into the entity_ary.  */
    3773              :   unsigned entity_lwm;
    3774              :   unsigned entity_num;
    3775              : 
    3776              :   /* Location ranges for this module.  adhoc-locs are decomposed, so
    3777              :      don't have a range.  */
    3778              :   loc_range_t GTY((skip)) ordinary_locs;
    3779              :   loc_range_t GTY((skip)) macro_locs; // [lwm,num)
    3780              : 
    3781              :   /* LOC is first set too the importing location.  When initially
    3782              :      loaded it refers to a module loc whose parent is the importing
    3783              :      location.  */
    3784              :   location_t loc;       /* Location referring to module itself.  */
    3785              :   unsigned crc;         /* CRC we saw reading it in. */
    3786              : 
    3787              :   unsigned mod;         /* Module owner number.  */
    3788              :   unsigned remap;       /* Remapping during writing.  */
    3789              : 
    3790              :   unsigned short subst; /* Mangle subst if !0.  */
    3791              : 
    3792              :   /* How loaded this module is.  */
    3793              :   enum module_loadedness loadedness : 2;
    3794              : 
    3795              :   bool module_p : 1;    /* /The/ module of this TU.  */
    3796              :   bool header_p : 1;    /* Is a header unit.  */
    3797              :   bool interface_p : 1; /* An interface.  */
    3798              :   bool partition_p : 1; /* A partition.  */
    3799              : 
    3800              :   /* How directly this module is imported.  */
    3801              :   enum module_directness directness : 2;
    3802              : 
    3803              :   bool exported_p : 1;  /* directness != MD_NONE && exported.  */
    3804              :   bool cmi_noted_p : 1; /* We've told the user about the CMI, don't
    3805              :                            do it again  */
    3806              :   bool active_init_p : 1; /* This module's global initializer needs
    3807              :                            calling.  */
    3808              :   bool inform_cmi_p : 1; /* Inform of a read/write.  */
    3809              :   bool visited_p : 1;    /* A walk-once flag. */
    3810              :   /* Record extensions emitted or permitted.  */
    3811              :   unsigned extensions : SE_BITS;
    3812              :   /* 16 bits used, 0 bits remain.  */
    3813              : 
    3814              :  public:
    3815              :   module_state (tree name, module_state *, bool);
    3816              :   ~module_state ();
    3817              : 
    3818              :  public:
    3819         3038 :   void release ()
    3820              :   {
    3821         3038 :     imports = exports = NULL;
    3822         3038 :     slurped ();
    3823              :   }
    3824         5489 :   void slurped ()
    3825              :   {
    3826         5489 :     delete slurp;
    3827         5489 :     slurp = NULL;
    3828         5489 :   }
    3829      1335447 :   elf_in *from () const
    3830              :   {
    3831      1335447 :     return slurp->from;
    3832              :   }
    3833              : 
    3834              :  public:
    3835              :   /* Kind of this module.  */
    3836       145328 :   bool is_module () const
    3837              :   {
    3838       145328 :     return module_p;
    3839              :   }
    3840      2590528 :   bool is_header () const
    3841              :   {
    3842      2590528 :     return header_p;
    3843              :   }
    3844          639 :   bool is_interface () const
    3845              :   {
    3846          639 :     return interface_p;
    3847              :   }
    3848       348618 :   bool is_partition () const
    3849              :   {
    3850       348618 :     return partition_p;
    3851              :   }
    3852              : 
    3853              :   /* How this module is used in the current TU.  */
    3854         3258 :   bool is_exported () const
    3855              :   {
    3856         3258 :     return exported_p;
    3857              :   }
    3858        21197 :   bool is_direct () const
    3859              :   {
    3860        21197 :     return directness >= MD_DIRECT;
    3861              :   }
    3862          287 :   bool is_purview_direct () const
    3863              :   {
    3864          287 :     return directness == MD_PURVIEW_DIRECT;
    3865              :   }
    3866          473 :   bool is_partition_direct () const
    3867              :   {
    3868          473 :     return directness == MD_PARTITION_DIRECT;
    3869              :   }
    3870              : 
    3871              :  public:
    3872              :   /* Is this a real module?  */
    3873        16503 :   bool has_location () const
    3874              :   {
    3875        16503 :     return loc != UNKNOWN_LOCATION;
    3876              :   }
    3877              : 
    3878              :  public:
    3879              :   bool check_circular_import (location_t loc);
    3880              : 
    3881              :  public:
    3882              :   void mangle (bool include_partition);
    3883              : 
    3884              :  public:
    3885              :   void set_import (module_state const *, bool is_export);
    3886              :   void announce (const char *) const;
    3887              : 
    3888              :  public:
    3889              :   /* Read and write module.  */
    3890              :   bool write_begin (elf_out *to, cpp_reader *,
    3891              :                     module_state_config &, unsigned &crc);
    3892              :   void write_end (elf_out *to, cpp_reader *,
    3893              :                   module_state_config &, unsigned &crc);
    3894              :   bool read_initial (cpp_reader *);
    3895              :   bool read_preprocessor (bool);
    3896              :   bool read_language (bool);
    3897              : 
    3898              :  public:
    3899              :   /* Read a section.  */
    3900              :   bool load_section (unsigned snum, binding_slot *mslot);
    3901              :   /* Lazily read a section.  */
    3902              :   bool lazy_load (unsigned index, binding_slot *mslot);
    3903              : 
    3904              :  public:
    3905              :   /* Juggle a limited number of file numbers.  */
    3906              :   static void freeze_an_elf ();
    3907              :   bool maybe_defrost ();
    3908              : 
    3909              :  public:
    3910              :   void maybe_completed_reading ();
    3911              :   bool check_read (bool outermost, bool ok);
    3912              : 
    3913              :  private:
    3914              :   /* The README, for human consumption.  */
    3915              :   void write_readme (elf_out *to, cpp_reader *, const char *dialect);
    3916              :   void write_env (elf_out *to);
    3917              : 
    3918              :  private:
    3919              :   /* Import tables. */
    3920              :   void write_imports (bytes_out &cfg, bool direct);
    3921              :   unsigned read_imports (bytes_in &cfg, cpp_reader *, line_maps *maps);
    3922              : 
    3923              :  private:
    3924              :   void write_imports (elf_out *to, unsigned *crc_ptr);
    3925              :   bool read_imports (cpp_reader *, line_maps *);
    3926              : 
    3927              :  private:
    3928              :   void write_partitions (elf_out *to, unsigned, unsigned *crc_ptr);
    3929              :   bool read_partitions (unsigned);
    3930              : 
    3931              :  private:
    3932              :   void write_config (elf_out *to, struct module_state_config &, unsigned crc);
    3933              :   bool read_config (struct module_state_config &, bool = true);
    3934              :   static void write_counts (elf_out *to, unsigned [MSC_HWM], unsigned *crc_ptr);
    3935              :   bool read_counts (unsigned *);
    3936              : 
    3937              :  public:
    3938              :   void note_cmi_name ();
    3939              : 
    3940              :  private:
    3941              :   static unsigned write_bindings (elf_out *to, vec<depset *> depsets,
    3942              :                                   unsigned *crc_ptr);
    3943              :   bool read_bindings (unsigned count, unsigned lwm, unsigned hwm);
    3944              : 
    3945              :   static void write_namespace (bytes_out &sec, depset *ns_dep);
    3946              :   tree read_namespace (bytes_in &sec);
    3947              : 
    3948              :   void write_namespaces (elf_out *to, vec<depset *> spaces,
    3949              :                          unsigned, unsigned *crc_ptr);
    3950              :   bool read_namespaces (unsigned);
    3951              : 
    3952              :   unsigned write_using_directives (elf_out *to, depset::hash &,
    3953              :                                    vec<depset *> spaces, unsigned *crc_ptr);
    3954              :   bool read_using_directives (unsigned);
    3955              : 
    3956              :   void intercluster_seed (trees_out &sec, unsigned index, depset *dep);
    3957              :   unsigned write_cluster (elf_out *to, depset *depsets[], unsigned size,
    3958              :                           depset::hash &, unsigned *counts, unsigned *crc_ptr);
    3959              :   bool read_cluster (unsigned snum);
    3960              :   bool open_slurp (cpp_reader *);
    3961              : 
    3962              :  private:
    3963              :   unsigned write_inits (elf_out *to, depset::hash &, unsigned *crc_ptr);
    3964              :   bool read_inits (unsigned count);
    3965              : 
    3966              :  private:
    3967              :   unsigned write_pendings (elf_out *to, vec<depset *> depsets,
    3968              :                            depset::hash &, unsigned *crc_ptr);
    3969              :   bool read_pendings (unsigned count);
    3970              : 
    3971              :  private:
    3972              :   void write_entities (elf_out *to, vec<depset *> depsets,
    3973              :                        unsigned count, unsigned *crc_ptr);
    3974              :   bool read_entities (unsigned count, unsigned lwm, unsigned hwm);
    3975              : 
    3976              :  private:
    3977              :   void write_init_maps ();
    3978              :   range_t write_prepare_maps (module_state_config *, bool);
    3979              :   bool read_prepare_maps (const module_state_config *);
    3980              : 
    3981              :   void write_ordinary_maps (elf_out *to, range_t &,
    3982              :                             bool, unsigned *crc_ptr);
    3983              :   bool read_ordinary_maps (line_map_uint_t, unsigned);
    3984              :   void write_macro_maps (elf_out *to, range_t &, unsigned *crc_ptr);
    3985              :   bool read_macro_maps (line_map_uint_t);
    3986              : 
    3987              :   void write_diagnostic_classification (elf_out *, diagnostics::context *,
    3988              :                                         unsigned *);
    3989              :   bool read_diagnostic_classification (diagnostics::context *);
    3990              : 
    3991              :  private:
    3992              :   void write_define (bytes_out &, const cpp_macro *);
    3993              :   cpp_macro *read_define (bytes_in &, cpp_reader *) const;
    3994              :   vec<cpp_hashnode *> *prepare_macros (cpp_reader *);
    3995              :   unsigned write_macros (elf_out *to, vec<cpp_hashnode *> *, unsigned *crc_ptr);
    3996              :   bool read_macros ();
    3997              :   void install_macros ();
    3998              : 
    3999              :  public:
    4000              :   void import_macros ();
    4001              : 
    4002              :  public:
    4003              :   static void undef_macro (cpp_reader *, location_t, cpp_hashnode *);
    4004              :   static cpp_macro *deferred_macro (cpp_reader *, location_t, cpp_hashnode *);
    4005              : 
    4006              :  public:
    4007              :   static bool note_location (location_t);
    4008              :   static void write_location (bytes_out &, location_t);
    4009              :   location_t read_location (bytes_in &) const;
    4010              : 
    4011              :  public:
    4012              :   void set_flatname ();
    4013        53632 :   const char *get_flatname () const
    4014              :   {
    4015        53632 :     return flatname;
    4016              :   }
    4017              :   location_t imported_from () const;
    4018              : 
    4019              :  public:
    4020              :   void set_filename (const Cody::Packet &);
    4021              :   bool do_import (cpp_reader *, bool outermost);
    4022              :   bool check_importable (cpp_reader *);
    4023              : };
    4024              : 
    4025              : /* Hash module state by name.  This cannot be a member of
    4026              :    module_state, because of GTY restrictions.  We never delete from
    4027              :    the hash table, but ggc_ptr_hash doesn't support that
    4028              :    simplification.  */
    4029              : 
    4030              : struct module_state_hash : ggc_ptr_hash<module_state> {
    4031              :   typedef std::pair<tree,uintptr_t> compare_type; /* {name,parent} */
    4032              : 
    4033              :   static inline hashval_t hash (const value_type m);
    4034              :   static inline hashval_t hash (const compare_type &n);
    4035              :   static inline bool equal (const value_type existing,
    4036              :                             const compare_type &candidate);
    4037              : };
    4038              : 
    4039        11724 : module_state::module_state (tree name, module_state *parent, bool partition)
    4040        11724 :   : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
    4041        11724 :     parent (parent), name (name), slurp (NULL),
    4042        11724 :     flatname (NULL), filename (NULL),
    4043        11724 :     entity_lwm (~0u >> 1), entity_num (0),
    4044        11724 :     ordinary_locs (0, 0), macro_locs (0, 0),
    4045        11724 :     loc (UNKNOWN_LOCATION),
    4046        11724 :     crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
    4047              : {
    4048        11724 :   loadedness = ML_NONE;
    4049              : 
    4050        11724 :   module_p = header_p = interface_p = partition_p = false;
    4051              : 
    4052        11724 :   directness = MD_NONE;
    4053        11724 :   exported_p = false;
    4054              : 
    4055        11724 :   cmi_noted_p = false;
    4056        11724 :   active_init_p = false;
    4057              : 
    4058        11724 :   partition_p = partition;
    4059              : 
    4060        11724 :   inform_cmi_p = false;
    4061        11724 :   visited_p = false;
    4062              : 
    4063        11724 :   extensions = 0;
    4064        11724 :   if (name && TREE_CODE (name) == STRING_CST)
    4065              :     {
    4066         1934 :       header_p = true;
    4067              : 
    4068         1934 :       const char *string = TREE_STRING_POINTER (name);
    4069         1934 :       gcc_checking_assert (string[0] == '.'
    4070              :                            ? IS_DIR_SEPARATOR (string[1])
    4071              :                            : IS_ABSOLUTE_PATH (string));
    4072              :     }
    4073              : 
    4074        11724 :   gcc_checking_assert (!(parent && header_p));
    4075        11724 : }
    4076              : 
    4077           62 : module_state::~module_state ()
    4078              : {
    4079           62 :   release ();
    4080           62 : }
    4081              : 
    4082              : /* Hash module state.  */
    4083              : static hashval_t
    4084        17374 : module_name_hash (const_tree name)
    4085              : {
    4086        17374 :   if (TREE_CODE (name) == STRING_CST)
    4087         3568 :     return htab_hash_string (TREE_STRING_POINTER (name));
    4088              :   else
    4089        13806 :     return IDENTIFIER_HASH_VALUE (name);
    4090              : }
    4091              : 
    4092              : hashval_t
    4093         4904 : module_state_hash::hash (const value_type m)
    4094              : {
    4095         4904 :   hashval_t ph = pointer_hash<void>::hash
    4096         4904 :     (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
    4097         4904 :                                | m->is_partition ()));
    4098         4904 :   hashval_t nh = module_name_hash (m->name);
    4099         4904 :   return iterative_hash_hashval_t (ph, nh);
    4100              : }
    4101              : 
    4102              : /* Hash a name.  */
    4103              : hashval_t
    4104        12470 : module_state_hash::hash (const compare_type &c)
    4105              : {
    4106        12470 :   hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
    4107        12470 :   hashval_t nh = module_name_hash (c.first);
    4108              : 
    4109        12470 :   return iterative_hash_hashval_t (ph, nh);
    4110              : }
    4111              : 
    4112              : bool
    4113         8615 : module_state_hash::equal (const value_type existing,
    4114              :                           const compare_type &candidate)
    4115              : {
    4116         8615 :   uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
    4117         8615 :                   | existing->is_partition ());
    4118         8615 :   if (ep != candidate.second)
    4119              :     return false;
    4120              : 
    4121              :   /* Identifier comparison is by pointer.  If the string_csts happen
    4122              :      to be the same object, then they're equal too.  */
    4123         7071 :   if (existing->name == candidate.first)
    4124              :     return true;
    4125              : 
    4126              :   /* If neither are string csts, they can't be equal.  */
    4127         1491 :   if (TREE_CODE (candidate.first) != STRING_CST
    4128          499 :       || TREE_CODE (existing->name) != STRING_CST)
    4129              :     return false;
    4130              : 
    4131              :   /* String equality.  */
    4132          425 :   if (TREE_STRING_LENGTH (existing->name)
    4133          425 :       == TREE_STRING_LENGTH (candidate.first)
    4134          425 :       && !memcmp (TREE_STRING_POINTER (existing->name),
    4135          422 :                   TREE_STRING_POINTER (candidate.first),
    4136          422 :                   TREE_STRING_LENGTH (existing->name)))
    4137          139 :     return true;
    4138              : 
    4139              :   return false;
    4140              : }
    4141              : 
    4142              : /********************************************************************/
    4143              : /* Global state */
    4144              : 
    4145              : /* Mapper name.  */
    4146              : static const char *module_mapper_name;
    4147              : 
    4148              : /* Deferred import queue (FIFO).  */
    4149              : static vec<module_state *, va_heap, vl_embed> *pending_imports;
    4150              : 
    4151              : /* CMI repository path and workspace.  */
    4152              : static char *cmi_repo;
    4153              : static size_t cmi_repo_length;
    4154              : static char *cmi_path;
    4155              : static size_t cmi_path_alloc;
    4156              : 
    4157              : /* Count of available and loaded clusters.  */
    4158              : static unsigned available_clusters;
    4159              : static unsigned loaded_clusters;
    4160              : 
    4161              : /* What the current TU is.  */
    4162              : unsigned module_kind;
    4163              : 
    4164              : /* Global trees.  */
    4165              : static const std::pair<tree *, unsigned> global_tree_arys[] =
    4166              :   {
    4167              :     std::pair<tree *, unsigned> (sizetype_tab, stk_type_kind_last),
    4168              :     std::pair<tree *, unsigned> (integer_types, itk_none),
    4169              :     std::pair<tree *, unsigned> (global_trees, TI_MODULE_HWM),
    4170              :     std::pair<tree *, unsigned> (c_global_trees, CTI_MODULE_HWM),
    4171              :     std::pair<tree *, unsigned> (cp_global_trees, CPTI_MODULE_HWM),
    4172              :     std::pair<tree *, unsigned> (NULL, 0)
    4173              :   };
    4174              : static GTY(()) vec<tree, va_gc> *fixed_trees;
    4175              : static unsigned global_crc;
    4176              : 
    4177              : /* Lazy loading can open many files concurrently, there are
    4178              :    per-process limits on that.  We pay attention to the process limit,
    4179              :    and attempt to increase it when we run out.  Otherwise we use an
    4180              :    LRU scheme to figure out who to flush.  Note that if the import
    4181              :    graph /depth/ exceeds lazy_limit, we'll exceed the limit.  */
    4182              : static unsigned lazy_lru;  /* LRU counter.  */
    4183              : static unsigned lazy_open; /* Number of open modules */
    4184              : static unsigned lazy_limit; /* Current limit of open modules.  */
    4185              : static unsigned lazy_hard_limit; /* Hard limit on open modules.  */
    4186              : /* Account for source, assembler and dump files & directory searches.
    4187              :    We don't keep the source file's open, so we don't have to account
    4188              :    for #include depth.  I think dump files are opened and closed per
    4189              :    pass, but ICBW.  */
    4190              : #define LAZY_HEADROOM 15 /* File descriptor headroom.  */
    4191              : 
    4192              : /* Vector of module state.  Indexed by OWNER.  Index 0 is reserved for the
    4193              :    current TU; imports start at 1.  */
    4194              : static GTY(()) vec<module_state *, va_gc> *modules;
    4195              : 
    4196              : /* Get the module state for the current TU's module.  */
    4197              : 
    4198              : static module_state *
    4199       315179 : this_module() {
    4200       315179 :   return (*modules)[0];
    4201              : }
    4202              : 
    4203              : /* Hash of module state, findable by {name, parent}. */
    4204              : static GTY(()) hash_table<module_state_hash> *modules_hash;
    4205              : 
    4206              : /* Map of imported entities.  We map DECL_UID to index of entity
    4207              :    vector.  */
    4208              : typedef hash_map<unsigned/*UID*/, unsigned/*index*/,
    4209              :                  simple_hashmap_traits<int_hash<unsigned,0>, unsigned>
    4210              :                  > entity_map_t;
    4211              : static entity_map_t *entity_map;
    4212              : /* Doesn't need GTYing, because any tree referenced here is also
    4213              :    findable by, symbol table, specialization table, return type of
    4214              :    reachable function.  */
    4215              : static vec<binding_slot, va_heap, vl_embed> *entity_ary;
    4216              : 
    4217              : /* Members entities of imported classes that are defined in this TU.
    4218              :    These are where the entity's context is not from the current TU.
    4219              :    We need to emit the definition (but not the enclosing class).
    4220              : 
    4221              :    We could find these by walking ALL the imported classes that we
    4222              :    could provide a member definition.  But that's expensive,
    4223              :    especially when you consider lazy implicit member declarations,
    4224              :    which could be ANY imported class.  */
    4225              : static GTY(()) vec<tree, va_gc> *class_members;
    4226              : 
    4227              : /* The same problem exists for class template partial
    4228              :    specializations.  Now that we have constraints, the invariant of
    4229              :    expecting them in the instantiation table no longer holds.  One of
    4230              :    the constrained partial specializations will be there, but the
    4231              :    others not so much.  It's not even an unconstrained partial
    4232              :    specialization in the table :(  so any partial template declaration
    4233              :    is added to this list too.  */
    4234              : static GTY(()) vec<tree, va_gc> *partial_specializations;
    4235              : 
    4236              : /********************************************************************/
    4237              : 
    4238              : /* Our module mapper (created lazily).  */
    4239              : module_client *mapper;
    4240              : 
    4241              : static module_client *make_mapper (location_t loc, class mkdeps *deps);
    4242        34524 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
    4243              : {
    4244        34524 :   auto *res = mapper;
    4245          313 :   if (!res)
    4246         4973 :     res = make_mapper (loc, deps);
    4247        34524 :   return res;
    4248              : }
    4249              : 
    4250              : /********************************************************************/
    4251              : static tree
    4252       477461 : get_clone_target (tree decl)
    4253              : {
    4254       477461 :   tree target;
    4255              : 
    4256       477461 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4257              :     {
    4258        62424 :       tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
    4259              : 
    4260        62424 :       target = DECL_TI_TEMPLATE (res_orig);
    4261              :     }
    4262              :   else
    4263       415037 :     target = DECL_CLONED_FUNCTION (decl);
    4264              : 
    4265       477461 :   gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
    4266              : 
    4267       477461 :   return target;
    4268              : }
    4269              : 
    4270              : /* Like FOR_EACH_CLONE, but will walk cloned templates.  */
    4271              : #define FOR_EVERY_CLONE(CLONE, FN)                      \
    4272              :   if (!DECL_MAYBE_IN_CHARGE_CDTOR_P (FN));              \
    4273              :   else                                                  \
    4274              :     for (CLONE = DECL_CHAIN (FN);                       \
    4275              :          CLONE && DECL_CLONED_FUNCTION_P (CLONE);       \
    4276              :          CLONE = DECL_CHAIN (CLONE))
    4277              : 
    4278              : /* It'd be nice if USE_TEMPLATE was a field of template_info
    4279              :    (a) it'd solve the enum case dealt with below,
    4280              :    (b) both class templates and decl templates would store this in the
    4281              :    same place
    4282              :    (c) this function wouldn't need the by-ref arg, which is annoying.  */
    4283              : 
    4284              : static tree
    4285    145000370 : node_template_info (tree decl, int &use)
    4286              : {
    4287    145000370 :   tree ti = NULL_TREE;
    4288    145000370 :   int use_tpl = -1;
    4289    145000370 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    4290              :     {
    4291     26181640 :       tree type = TREE_TYPE (decl);
    4292              : 
    4293     26181640 :       ti = TYPE_TEMPLATE_INFO (type);
    4294     26181640 :       if (ti)
    4295              :         {
    4296      6109476 :           if (TYPE_LANG_SPECIFIC (type))
    4297      6097721 :             use_tpl = CLASSTYPE_USE_TEMPLATE (type);
    4298              :           else
    4299              :             {
    4300              :               /* An enum, where we don't explicitly encode use_tpl.
    4301              :                  If the containing context (a type or a function), is
    4302              :                  an ({im,ex}plicit) instantiation, then this is too.
    4303              :                  If it's a partial or explicit specialization, then
    4304              :                  this is not!.  */
    4305        11755 :               tree ctx = CP_DECL_CONTEXT (decl);
    4306        11755 :               if (TYPE_P (ctx))
    4307        11521 :                 ctx = TYPE_NAME (ctx);
    4308        11755 :               node_template_info (ctx, use);
    4309        11755 :               use_tpl = use != 2 ? use : 0;
    4310              :             }
    4311              :         }
    4312              :     }
    4313    118818730 :   else if (DECL_LANG_SPECIFIC (decl)
    4314    118818730 :            && (VAR_P (decl)
    4315              :                || TREE_CODE (decl) == TYPE_DECL
    4316              :                || TREE_CODE (decl) == FUNCTION_DECL
    4317              :                || TREE_CODE (decl) == FIELD_DECL
    4318              :                || TREE_CODE (decl) == CONCEPT_DECL
    4319              :                || TREE_CODE (decl) == TEMPLATE_DECL))
    4320              :     {
    4321    101862676 :       use_tpl = DECL_USE_TEMPLATE (decl);
    4322    101862676 :       ti = DECL_TEMPLATE_INFO (decl);
    4323              :     }
    4324              : 
    4325    145000370 :   use = use_tpl;
    4326    145000370 :   return ti;
    4327              : }
    4328              : 
    4329              : /* Find the index in entity_ary for an imported DECL.  It should
    4330              :    always be there, but bugs can cause it to be missing, and that can
    4331              :    crash the crash reporting -- let's not do that!  When streaming
    4332              :    out we place entities from this module there too -- with negated
    4333              :    indices.  */
    4334              : 
    4335              : static unsigned
    4336      1873931 : import_entity_index (tree decl, bool null_ok = false)
    4337              : {
    4338      1873931 :   if (unsigned *slot = entity_map->get (DECL_UID (decl)))
    4339      1873886 :     return *slot;
    4340              : 
    4341           45 :   gcc_checking_assert (null_ok);
    4342              :   return ~(~0u >> 1);
    4343              : }
    4344              : 
    4345              : /* Find the module for an imported entity at INDEX in the entity ary.
    4346              :    There must be one.  */
    4347              : 
    4348              : static module_state *
    4349       139139 : import_entity_module (unsigned index)
    4350              : {
    4351       139139 :   if (index > ~(~0u >> 1))
    4352              :     /* This is an index for an exported entity.  */
    4353           60 :     return this_module ();
    4354              : 
    4355              :   /* Do not include the current TU (not an off-by-one error).  */
    4356       139079 :   unsigned pos = 1;
    4357       139079 :   unsigned len = modules->length () - pos;
    4358       317771 :   while (len)
    4359              :     {
    4360       178692 :       unsigned half = len / 2;
    4361       178692 :       module_state *probe = (*modules)[pos + half];
    4362       178692 :       if (index < probe->entity_lwm)
    4363              :         len = half;
    4364       139544 :       else if (index < probe->entity_lwm + probe->entity_num)
    4365              :         return probe;
    4366              :       else
    4367              :         {
    4368          465 :           pos += half + 1;
    4369          465 :           len = len - (half + 1);
    4370              :         }
    4371              :     }
    4372            0 :   gcc_unreachable ();
    4373              : }
    4374              : 
    4375              : 
    4376              : /********************************************************************/
    4377              : /* A dumping machinery.  */
    4378              : 
    4379              : class dumper {
    4380              : public:
    4381              :   enum {
    4382              :     LOCATION = TDF_LINENO,  /* -lineno:Source location streaming.  */
    4383              :     DEPEND = TDF_GRAPH, /* -graph:Dependency graph construction.  */
    4384              :     CLUSTER = TDF_BLOCKS,   /* -blocks:Clusters.  */
    4385              :     TREE = TDF_UID,     /* -uid:Tree streaming.  */
    4386              :     MERGE = TDF_ALIAS,  /* -alias:Mergeable Entities.  */
    4387              :     ELF = TDF_ASMNAME,  /* -asmname:Elf data.  */
    4388              :     MACRO = TDF_VOPS    /* -vops:Macros.  */
    4389              :   };
    4390              : 
    4391              : private:
    4392              :   struct impl {
    4393              :     typedef vec<module_state *, va_heap, vl_embed> stack_t;
    4394              : 
    4395              :     FILE *stream;       /* Dump stream.  */
    4396              :     unsigned indent;    /* Local indentation.  */
    4397              :     bool bol;           /* Beginning of line.  */
    4398              :     stack_t stack;      /* Trailing array of module_state.  */
    4399              : 
    4400              :     bool nested_name (tree);  /* Dump a name following DECL_CONTEXT.  */
    4401              :   };
    4402              : 
    4403              : public:
    4404              :   /* The dumper.  */
    4405              :   impl *dumps;
    4406              :   dump_flags_t flags;
    4407              : 
    4408              : public:
    4409              :   /* Push/pop module state dumping.  */
    4410              :   unsigned push (module_state *);
    4411              :   void pop (unsigned);
    4412              : 
    4413              : public:
    4414              :   /* Change local indentation.  */
    4415    457156177 :   void indent ()
    4416              :   {
    4417          309 :     if (dumps)
    4418       733489 :       dumps->indent++;
    4419              :   }
    4420    457156177 :   void outdent ()
    4421              :   {
    4422    457156177 :     if (dumps)
    4423              :       {
    4424       733489 :         gcc_checking_assert (dumps->indent);
    4425       733489 :         dumps->indent--;
    4426              :       }
    4427    457156177 :   }
    4428              : 
    4429              : public:
    4430              :   /* Is dump enabled?.  */
    4431    275918843 :   bool operator () (int mask = 0)
    4432              :   {
    4433      5465518 :     if (!dumps || !dumps->stream)
    4434              :       return false;
    4435       513667 :     if (mask && !(mask & flags))
    4436         5388 :       return false;
    4437              :     return true;
    4438              :   }
    4439              :   /* Dump some information.  */
    4440              :   bool operator () (const char *, ...);
    4441              : };
    4442              : 
    4443              : /* The dumper.  */
    4444              : static dumper dump = {0, dump_flags_t (0)};
    4445              : 
    4446              : /* Push to dumping M.  Return previous indentation level.  */
    4447              : 
    4448              : unsigned
    4449       136789 : dumper::push (module_state *m)
    4450              : {
    4451       136789 :   FILE *stream = NULL;
    4452       136789 :   if (!dumps || !dumps->stack.length ())
    4453              :     {
    4454       135485 :       stream = dump_begin (module_dump_id, &flags);
    4455       135485 :       if (!stream)
    4456              :         return 0;
    4457              :     }
    4458              : 
    4459         7063 :   if (!dumps || !dumps->stack.space (1))
    4460              :     {
    4461              :       /* Create or extend the dump implementor.  */
    4462         1248 :       unsigned current = dumps ? dumps->stack.length () : 0;
    4463          662 :       unsigned count = current ? current * 2 : EXPERIMENT (1, 20);
    4464         1248 :       size_t alloc = (offsetof (impl, stack)
    4465         1248 :                       + impl::stack_t::embedded_size (count));
    4466         1248 :       dumps = XRESIZEVAR (impl, dumps, alloc);
    4467         1248 :       dumps->stack.embedded_init (count, current);
    4468              :     }
    4469         7063 :   if (stream)
    4470         5759 :     dumps->stream = stream;
    4471              : 
    4472         7063 :   unsigned n = dumps->indent;
    4473         7063 :   dumps->indent = 0;
    4474         7063 :   dumps->bol = true;
    4475         7063 :   dumps->stack.quick_push (m);
    4476         7063 :   if (m)
    4477              :     {
    4478         2109 :       module_state *from = NULL;
    4479              : 
    4480         2109 :       if (dumps->stack.length () > 1)
    4481          682 :         from = dumps->stack[dumps->stack.length () - 2];
    4482              :       else
    4483         1427 :         dump ("");
    4484         3798 :       dump (from ? "Starting module %M (from %M)"
    4485              :             : "Starting module %M", m, from);
    4486              :     }
    4487              : 
    4488              :   return n;
    4489              : }
    4490              : 
    4491              : /* Pop from dumping.  Restore indentation to N.  */
    4492              : 
    4493       136746 : void dumper::pop (unsigned n)
    4494              : {
    4495       136746 :   if (!dumps)
    4496              :     return;
    4497              : 
    4498        14126 :   gcc_checking_assert (dump () && !dumps->indent);
    4499         7063 :   if (module_state *m = dumps->stack[dumps->stack.length () - 1])
    4500              :     {
    4501         2109 :       module_state *from = (dumps->stack.length () > 1
    4502         2109 :                             ? dumps->stack[dumps->stack.length () - 2] : NULL);
    4503         2371 :       dump (from ? "Finishing module %M (returning to %M)"
    4504              :             : "Finishing module %M", m, from);
    4505              :     }
    4506         7063 :   dumps->stack.pop ();
    4507         7063 :   dumps->indent = n;
    4508         7063 :   if (!dumps->stack.length ())
    4509              :     {
    4510         5759 :       dump_end (module_dump_id, dumps->stream);
    4511         5759 :       dumps->stream = NULL;
    4512              :     }
    4513              : }
    4514              : 
    4515              : /* Dump a nested name for arbitrary tree T.  Sometimes it won't have a
    4516              :    name.  */
    4517              : 
    4518              : bool
    4519       537814 : dumper::impl::nested_name (tree t)
    4520              : {
    4521       537814 :   tree ti = NULL_TREE;
    4522       537814 :   int origin = -1;
    4523       537814 :   tree name = NULL_TREE;
    4524              : 
    4525       537814 :   if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
    4526            0 :     t = TU_LOCAL_ENTITY_NAME (t);
    4527              : 
    4528       537784 :   if (t && TREE_CODE (t) == TREE_BINFO)
    4529          384 :     t = BINFO_TYPE (t);
    4530              : 
    4531       537814 :   if (t && TYPE_P (t))
    4532       260885 :     t = TYPE_NAME (t);
    4533              : 
    4534       537772 :   if (t && DECL_P (t))
    4535              :     {
    4536       451471 :       if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
    4537              :         ;
    4538       419358 :       else if (tree ctx = DECL_CONTEXT (t))
    4539       328982 :         if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
    4540       328982 :             || nested_name (ctx))
    4541       328982 :           fputs ("::", stream);
    4542              : 
    4543       451471 :       int use_tpl;
    4544       451471 :       ti = node_template_info (t, use_tpl);
    4545       142073 :       if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    4546       593499 :           && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
    4547              :         t = TI_TEMPLATE (ti);
    4548       451471 :       tree not_tmpl = t;
    4549       451471 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    4550              :         {
    4551        23883 :           fputs ("template ", stream);
    4552        23883 :           not_tmpl = DECL_TEMPLATE_RESULT (t);
    4553              :         }
    4554              : 
    4555        23883 :       if (not_tmpl
    4556       451467 :           && DECL_P (not_tmpl)
    4557       451467 :           && DECL_LANG_SPECIFIC (not_tmpl)
    4558       271562 :           && DECL_MODULE_IMPORT_P (not_tmpl))
    4559              :         {
    4560              :           /* We need to be careful here, so as to not explode on
    4561              :              inconsistent data -- we're probably debugging, because
    4562              :              Something Is Wrong.  */
    4563        24422 :           unsigned index = import_entity_index (t, true);
    4564        24422 :           if (!(index & ~(~0u >> 1)))
    4565        23822 :             origin = import_entity_module (index)->mod;
    4566          600 :           else if (index > ~(~0u >> 1))
    4567              :             /* An imported partition member that we're emitting.  */
    4568              :             origin = 0;
    4569              :           else
    4570           45 :             origin = -2;
    4571              :         }
    4572              : 
    4573       454698 :       name = DECL_NAME (t) ? DECL_NAME (t)
    4574         4306 :         : HAS_DECL_ASSEMBLER_NAME_P (t) ? DECL_ASSEMBLER_NAME_RAW (t)
    4575              :         : NULL_TREE;
    4576              :     }
    4577              :   else
    4578              :     name = t;
    4579              : 
    4580       451471 :   if (name)
    4581       501368 :     switch (TREE_CODE (name))
    4582              :       {
    4583        13681 :       default:
    4584        13681 :         fputs ("#unnamed#", stream);
    4585        13681 :         break;
    4586              : 
    4587       461945 :       case IDENTIFIER_NODE:
    4588       461945 :         fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
    4589       461945 :         break;
    4590              : 
    4591        25650 :       case INTEGER_CST:
    4592        25650 :         print_hex (wi::to_wide (name), stream);
    4593        25650 :         break;
    4594              : 
    4595           92 :       case STRING_CST:
    4596              :         /* If TREE_TYPE is NULL, this is a raw string.  */
    4597          184 :         fwrite (TREE_STRING_POINTER (name), 1,
    4598           92 :                 TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
    4599              :                 stream);
    4600           92 :         break;
    4601              :       }
    4602              :   else
    4603        36446 :     fputs ("#null#", stream);
    4604              : 
    4605       537814 :   if (t && TREE_CODE (t) == FUNCTION_DECL && DECL_COROUTINE_P (t))
    4606           48 :     if (tree ramp = DECL_RAMP_FN (t))
    4607              :       {
    4608           27 :         if (DECL_ACTOR_FN (ramp) == t)
    4609           12 :           fputs (".actor", stream);
    4610           15 :         else if (DECL_DESTROY_FN (ramp) == t)
    4611           15 :           fputs (".destroy", stream);
    4612              :         else
    4613            0 :           gcc_unreachable ();
    4614              :       }
    4615              : 
    4616       537814 :   if (origin >= 0)
    4617              :     {
    4618        24377 :       const module_state *module = (*modules)[origin];
    4619        48754 :       fprintf (stream, "@%s:%d", !module ? "" : !module->name ? "(unnamed)"
    4620        24377 :                : module->get_flatname (), origin);
    4621              :     }
    4622       513437 :   else if (origin == -2)
    4623           45 :     fprintf (stream, "@???");
    4624              : 
    4625       537814 :   if (ti)
    4626              :     {
    4627       142073 :       tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
    4628       142073 :       fputs ("<", stream);
    4629       142073 :       if (args)
    4630       358716 :         for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
    4631              :           {
    4632       216643 :             if (ix)
    4633        74570 :               fputs (",", stream);
    4634       216643 :             nested_name (TREE_VEC_ELT (args, ix));
    4635              :           }
    4636       142073 :       fputs (">", stream);
    4637              :     }
    4638              : 
    4639       537814 :   return true;
    4640              : }
    4641              : 
    4642              : /* Formatted dumping.  FORMAT begins with '+' do not emit a trailing
    4643              :    new line.  (Normally it is appended.)
    4644              :    Escapes:
    4645              :       %C - tree_code
    4646              :       %I - identifier
    4647              :       %K - location_t or line_map_uint_t
    4648              :       %M - module_state
    4649              :       %N - name -- DECL_NAME
    4650              :       %P - context:name pair
    4651              :       %R - unsigned:unsigned ratio
    4652              :       %S - symbol -- DECL_ASSEMBLER_NAME
    4653              :       %U - long unsigned
    4654              :       %V - version
    4655              :       --- the following are printf-like, but without its flexibility
    4656              :       %d - decimal int
    4657              :       %p - pointer
    4658              :       %s - string
    4659              :       %u - unsigned int
    4660              :       %x - hex int
    4661              : 
    4662              :   We do not implement the printf modifiers.  */
    4663              : 
    4664              : bool
    4665       449840 : dumper::operator () (const char *format, ...)
    4666              : {
    4667       449840 :   if (!(*this) ())
    4668              :     return false;
    4669              : 
    4670       385499 :   bool no_nl = format[0] == '+';
    4671       385499 :   format += no_nl;
    4672              : 
    4673       385499 :   if (dumps->bol)
    4674              :     {
    4675              :       /* Module import indent.  */
    4676       199151 :       if (unsigned depth = dumps->stack.length () - 1)
    4677              :         {
    4678        22800 :           const char *prefix = ">>>>";
    4679        45582 :           fprintf (dumps->stream, (depth <= strlen (prefix)
    4680        22782 :                                    ? &prefix[strlen (prefix) - depth]
    4681              :                                    : ">.%d.>"), depth);
    4682              :         }
    4683              : 
    4684              :       /* Local indent.  */
    4685       199151 :       if (unsigned indent = dumps->indent)
    4686              :         {
    4687       106608 :           const char *prefix = "      ";
    4688       208200 :           fprintf (dumps->stream, (indent <= strlen (prefix)
    4689       101592 :                                    ? &prefix[strlen (prefix) - indent]
    4690              :                                    : "  .%d.  "), indent);
    4691              :         }
    4692       199151 :       dumps->bol = false;
    4693              :     }
    4694              : 
    4695       385499 :   va_list args;
    4696       385499 :   va_start (args, format);
    4697      1128298 :   while (const char *esc = strchr (format, '%'))
    4698              :     {
    4699       742799 :       fwrite (format, 1, (size_t)(esc - format), dumps->stream);
    4700       742799 :       format = ++esc;
    4701       742799 :       switch (*format++)
    4702              :         {
    4703            0 :         default:
    4704            0 :           gcc_unreachable ();
    4705              : 
    4706          586 :         case '%':
    4707          586 :           fputc ('%', dumps->stream);
    4708          586 :           break;
    4709              : 
    4710       113276 :         case 'C': /* Code */
    4711       113276 :           {
    4712       113276 :             tree_code code = (tree_code)va_arg (args, unsigned);
    4713       113276 :             fputs (get_tree_code_name (code), dumps->stream);
    4714              :           }
    4715       113276 :           break;
    4716              : 
    4717           81 :         case 'I': /* Identifier.  */
    4718           81 :           {
    4719           81 :             tree t = va_arg (args, tree);
    4720           81 :             dumps->nested_name (t);
    4721              :           }
    4722           81 :           break;
    4723              : 
    4724         4656 :         case 'K': /* location_t, either 32- or 64-bit.  */
    4725         4656 :           {
    4726         4656 :             unsigned long long u = va_arg (args, location_t);
    4727         4656 :             fprintf (dumps->stream, "%llu", u);
    4728              :           }
    4729         4656 :           break;
    4730              : 
    4731         7980 :         case 'M': /* Module. */
    4732         7980 :           {
    4733         7980 :             const char *str = "(none)";
    4734         7980 :             if (module_state *m = va_arg (args, module_state *))
    4735              :               {
    4736         7980 :                 if (!m->has_location ())
    4737              :                   str = "(detached)";
    4738              :                 else
    4739         7980 :                   str = m->get_flatname ();
    4740              :               }
    4741         7980 :             fputs (str, dumps->stream);
    4742              :           }
    4743         7980 :           break;
    4744              : 
    4745       126760 :         case 'N': /* Name.  */
    4746       126760 :           {
    4747       126760 :             tree t = va_arg (args, tree);
    4748       254009 :             while (t && TREE_CODE (t) == OVERLOAD)
    4749          489 :               t = OVL_FUNCTION (t);
    4750       126760 :             fputc ('\'', dumps->stream);
    4751       126760 :             dumps->nested_name (t);
    4752       126760 :             fputc ('\'', dumps->stream);
    4753              :           }
    4754       126760 :           break;
    4755              : 
    4756         7532 :         case 'P': /* Pair.  */
    4757         7532 :           {
    4758         7532 :             tree ctx = va_arg (args, tree);
    4759         7532 :             tree name = va_arg (args, tree);
    4760         7532 :             fputc ('\'', dumps->stream);
    4761         7532 :             dumps->nested_name (ctx);
    4762         7532 :             if (ctx && ctx != global_namespace)
    4763         1096 :               fputs ("::", dumps->stream);
    4764         7532 :             dumps->nested_name (name);
    4765         7532 :             fputc ('\'', dumps->stream);
    4766              :           }
    4767         7532 :           break;
    4768              : 
    4769          900 :         case 'R': /* Ratio */
    4770          900 :           {
    4771          900 :             unsigned a = va_arg (args, unsigned);
    4772          900 :             unsigned b = va_arg (args, unsigned);
    4773          900 :             fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
    4774              :           }
    4775          900 :           break;
    4776              : 
    4777        34704 :         case 'S': /* Symbol name */
    4778        34704 :           {
    4779        34704 :             tree t = va_arg (args, tree);
    4780        34704 :             if (t && TYPE_P (t))
    4781        12631 :               t = TYPE_NAME (t);
    4782        32924 :             if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
    4783        31750 :                 && DECL_ASSEMBLER_NAME_SET_P (t))
    4784              :               {
    4785          172 :                 fputc ('(', dumps->stream);
    4786          172 :                 fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
    4787          172 :                        dumps->stream);
    4788          172 :                 fputc (')', dumps->stream);
    4789              :               }
    4790              :           }
    4791              :           break;
    4792              : 
    4793            0 :         case 'U': /* long unsigned.  */
    4794            0 :           {
    4795            0 :             unsigned long u = va_arg (args, unsigned long);
    4796            0 :             fprintf (dumps->stream, "%lu", u);
    4797              :           }
    4798            0 :           break;
    4799              : 
    4800          835 :         case 'V': /* Version.  */
    4801          835 :           {
    4802          835 :             unsigned v = va_arg (args, unsigned);
    4803          835 :             verstr_t string;
    4804              : 
    4805          835 :             version2string (v, string);
    4806          835 :             fputs (string, dumps->stream);
    4807              :           }
    4808          835 :           break;
    4809              : 
    4810            0 :         case 'c': /* Character.  */
    4811            0 :           {
    4812            0 :             int c = va_arg (args, int);
    4813            0 :             fputc (c, dumps->stream);
    4814              :           }
    4815            0 :           break;
    4816              : 
    4817        63208 :         case 'd': /* Decimal Int.  */
    4818        63208 :           {
    4819        63208 :             int d = va_arg (args, int);
    4820        63208 :             fprintf (dumps->stream, "%d", d);
    4821              :           }
    4822        63208 :           break;
    4823              : 
    4824            0 :         case 'p': /* Pointer. */
    4825            0 :           {
    4826            0 :             void *p = va_arg (args, void *);
    4827            0 :             fprintf (dumps->stream, "%p", p);
    4828              :           }
    4829            0 :           break;
    4830              : 
    4831       128845 :         case 's': /* String. */
    4832       128845 :           {
    4833       128845 :             const char *s = va_arg (args, char *);
    4834       128845 :             gcc_checking_assert (s);
    4835       128845 :             fputs (s, dumps->stream);
    4836              :           }
    4837       128845 :           break;
    4838              : 
    4839       250751 :         case 'u': /* Unsigned.  */
    4840       250751 :           {
    4841       250751 :             unsigned u = va_arg (args, unsigned);
    4842       250751 :             fprintf (dumps->stream, "%u", u);
    4843              :           }
    4844       250751 :           break;
    4845              : 
    4846         2685 :         case 'x': /* Hex. */
    4847         2685 :           {
    4848         2685 :             unsigned x = va_arg (args, unsigned);
    4849         2685 :             fprintf (dumps->stream, "%x", x);
    4850              :           }
    4851         2685 :           break;
    4852              :         }
    4853              :     }
    4854       385499 :   fputs (format, dumps->stream);
    4855       385499 :   va_end (args);
    4856       385499 :   if (!no_nl)
    4857              :     {
    4858       199151 :       dumps->bol = true;
    4859       199151 :       fputc ('\n', dumps->stream);
    4860              :     }
    4861              :   return true;
    4862              : }
    4863              : 
    4864              : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
    4865              : {
    4866       336235 :   static int keep_cache_entry (tree t)
    4867              :   {
    4868       336235 :     if (!CHECKING_P)
    4869              :       /* GTY is unfortunately not clever enough to conditionalize
    4870              :          this.  */
    4871              :       gcc_unreachable ();
    4872              : 
    4873       336235 :     if (ggc_marked_p (t))
    4874              :       return -1;
    4875              : 
    4876            0 :     unsigned n = dump.push (NULL);
    4877              :     /* This might or might not be an error.  We should note its
    4878              :        dropping whichever.  */
    4879            0 :     dump () && dump ("Dropping %N from note_defs table", t);
    4880            0 :     dump.pop (n);
    4881              : 
    4882            0 :     return 0;
    4883              :   }
    4884              : };
    4885              : 
    4886              : /* We should stream each definition at most once.
    4887              :    This needs to be a cache because there are cases where a definition
    4888              :    ends up being not retained, and we need to drop those so we don't
    4889              :    get confused if memory is reallocated.  */
    4890              : typedef hash_table<note_def_cache_hasher> note_defs_table_t;
    4891              : static GTY((cache)) note_defs_table_t *note_defs;
    4892              : 
    4893              : void
    4894       349663 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
    4895              :                              bool installing ATTRIBUTE_UNUSED)
    4896              : {
    4897              : #if CHECKING_P
    4898       349663 :   tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
    4899       349663 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    4900       349663 :   if (installing)
    4901              :     {
    4902              :       /* We must be inserting for the first time.  */
    4903       213655 :       gcc_assert (!*slot);
    4904       213655 :       *slot = decl;
    4905              :     }
    4906              :   else
    4907              :     /* If this is not the mergeable entity, it should not be in the
    4908              :        table.  If it is a non-global-module mergeable entity, it
    4909              :        should be in the table.  Global module entities could have been
    4910              :        defined textually in the current TU and so might or might not
    4911              :        be present.  */
    4912       136008 :     gcc_assert (!is_duplicate (decl)
    4913              :                 ? !slot
    4914              :                 : (slot
    4915              :                    || !DECL_LANG_SPECIFIC (not_tmpl)
    4916              :                    || !DECL_MODULE_PURVIEW_P (not_tmpl)
    4917              :                    || (!DECL_MODULE_IMPORT_P (not_tmpl)
    4918              :                        && header_module_p ())));
    4919              : 
    4920       349663 :   if (not_tmpl != decl)
    4921       207037 :     gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
    4922              : #endif
    4923       349663 : }
    4924              : 
    4925              : void
    4926       492819 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
    4927              : {
    4928              : #if CHECKING_P
    4929       492819 :   tree *slot = note_defs->find_slot (decl, INSERT);
    4930       492819 :   gcc_assert (!*slot);
    4931       492819 :   *slot = decl;
    4932       492819 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4933       279012 :     gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
    4934              : #endif
    4935       492819 : }
    4936              : 
    4937              : /********************************************************************/
    4938              : static bool
    4939        12809 : noisy_p ()
    4940              : {
    4941            0 :   if (quiet_flag)
    4942              :     return false;
    4943              : 
    4944            0 :   pp_needs_newline (global_dc->get_reference_printer ()) = true;
    4945            0 :   diagnostic_set_last_function (global_dc,
    4946              :                                 (diagnostics::diagnostic_info *) nullptr);
    4947              : 
    4948            0 :   return true;
    4949              : }
    4950              : 
    4951              : /* Set the cmi repo.  Strip trailing '/', '.' becomes NULL.  */
    4952              : 
    4953              : static void
    4954       104746 : set_cmi_repo (const char *r)
    4955              : {
    4956       104746 :   XDELETEVEC (cmi_repo);
    4957       104746 :   XDELETEVEC (cmi_path);
    4958       104746 :   cmi_path_alloc = 0;
    4959              : 
    4960       104746 :   cmi_repo = NULL;
    4961       104746 :   cmi_repo_length = 0;
    4962              : 
    4963       104746 :   if (!r || !r[0])
    4964              :     return;
    4965              : 
    4966         4970 :   size_t len = strlen (r);
    4967         4970 :   cmi_repo = XNEWVEC (char, len + 1);
    4968         4970 :   memcpy (cmi_repo, r, len + 1);
    4969              : 
    4970         4970 :   if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
    4971         4970 :     len--;
    4972         4970 :   if (len == 1 && cmi_repo[0] == '.')
    4973           27 :     len--;
    4974         4970 :   cmi_repo[len] = 0;
    4975         4970 :   cmi_repo_length = len;
    4976              : }
    4977              : 
    4978              : /* TO is a repo-relative name.  Provide one that we may use from where
    4979              :    we are.  */
    4980              : 
    4981              : static const char *
    4982         6075 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
    4983              : {
    4984         6075 :   size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
    4985              : 
    4986         6075 :   if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
    4987              :     {
    4988         6048 :       if (cmi_path_alloc < cmi_repo_length + len + 2)
    4989              :         {
    4990         4847 :           XDELETEVEC (cmi_path);
    4991         4847 :           cmi_path_alloc = cmi_repo_length + len * 2 + 2;
    4992         4847 :           cmi_path = XNEWVEC (char, cmi_path_alloc);
    4993              : 
    4994         4847 :           memcpy (cmi_path, cmi_repo, cmi_repo_length);
    4995         4847 :           cmi_path[cmi_repo_length] = DIR_SEPARATOR;
    4996              :         }
    4997              : 
    4998         6048 :       memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
    4999         6048 :       len += cmi_repo_length + 1;
    5000         6048 :       to = cmi_path;
    5001              :     }
    5002              : 
    5003         6075 :   if (len_p)
    5004         2932 :     *len_p = len;
    5005              : 
    5006         6075 :   return to;
    5007              : }
    5008              : 
    5009              : /* Try and create the directories of PATH.  */
    5010              : 
    5011              : static void
    5012           40 : create_dirs (char *path)
    5013              : {
    5014           40 :   char *base = path;
    5015              :   /* Skip past initial slashes of absolute path.  */
    5016           40 :   while (IS_DIR_SEPARATOR (*base))
    5017            0 :     base++;
    5018              : 
    5019              :   /* Try and create the missing directories.  */
    5020         2426 :   for (; *base; base++)
    5021         2386 :     if (IS_DIR_SEPARATOR (*base))
    5022              :       {
    5023          228 :         char sep = *base;
    5024          228 :         *base = 0;
    5025          228 :         int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
    5026          232 :         dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
    5027          228 :         *base = sep;
    5028          228 :         if (failed
    5029              :             /* Maybe racing with another creator (of a *different*
    5030              :                module).  */
    5031           45 :             && errno != EEXIST)
    5032              :           break;
    5033              :       }
    5034           40 : }
    5035              : 
    5036              : /* Given a CLASSTYPE_DECL_LIST VALUE get the template friend decl,
    5037              :    if that's what this is.  */
    5038              : 
    5039              : static tree
    5040       108706 : friend_from_decl_list (tree frnd)
    5041              : {
    5042       108706 :   tree res = frnd;
    5043              : 
    5044       108706 :   if (TREE_CODE (frnd) != TEMPLATE_DECL)
    5045              :     {
    5046        68088 :       tree tmpl = NULL_TREE;
    5047        68088 :       if (TYPE_P (frnd))
    5048              :         {
    5049        11106 :           res = TYPE_NAME (frnd);
    5050        10932 :           if (CLASS_TYPE_P (frnd)
    5051        22038 :               && CLASSTYPE_TEMPLATE_INFO (frnd))
    5052        10923 :             tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
    5053              :         }
    5054        56982 :       else if (DECL_TEMPLATE_INFO (frnd))
    5055              :         {
    5056        56982 :           tmpl = DECL_TI_TEMPLATE (frnd);
    5057        56982 :           if (TREE_CODE (tmpl) != TEMPLATE_DECL)
    5058              :             tmpl = NULL_TREE;
    5059              :         }
    5060              : 
    5061        77891 :       if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
    5062              :         res = tmpl;
    5063              :     }
    5064              : 
    5065       108706 :   return res;
    5066              : }
    5067              : 
    5068              : static tree
    5069        29859 : find_enum_member (tree ctx, tree name)
    5070              : {
    5071        29859 :   for (tree values = TYPE_VALUES (ctx);
    5072       490719 :        values; values = TREE_CHAIN (values))
    5073       482025 :     if (DECL_NAME (TREE_VALUE (values)) == name)
    5074              :       return TREE_VALUE (values);
    5075              : 
    5076              :   return NULL_TREE;
    5077              : }
    5078              : 
    5079              : /********************************************************************/
    5080              : /* Instrumentation gathered writing bytes.  */
    5081              : 
    5082              : void
    5083          300 : bytes_out::instrument ()
    5084              : {
    5085          300 :   dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
    5086          300 :   dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
    5087         1200 :   for (unsigned ix = 0; ix < 2; ix++)
    5088          900 :     dump ("  %u %s spans of %R bits", spans[ix],
    5089              :           ix ? "one" : "zero", lengths[ix], spans[ix]);
    5090          300 :   dump ("  %u blocks with %R bits padding", spans[2],
    5091          300 :         lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
    5092          300 : }
    5093              : 
    5094              : /* Instrumentation gathered writing trees.  */
    5095              : void
    5096         2785 : trees_out::instrument ()
    5097              : {
    5098         2785 :   if (dump (""))
    5099              :     {
    5100          300 :       bytes_out::instrument ();
    5101          300 :       dump ("Wrote:");
    5102          300 :       dump ("  %u decl trees", decl_val_count);
    5103          300 :       dump ("  %u other trees", tree_val_count);
    5104          300 :       dump ("  %u back references", back_ref_count);
    5105          300 :       dump ("  %u TU-local entities", tu_local_count);
    5106          300 :       dump ("  %u null trees", null_count);
    5107              :     }
    5108         2785 : }
    5109              : 
    5110              : /* Setup and teardown for a tree walk.  */
    5111              : 
    5112              : void
    5113      2911481 : trees_out::begin ()
    5114              : {
    5115      2911481 :   gcc_assert (!streaming_p () || !tree_map.elements ());
    5116              : 
    5117      2911481 :   mark_trees ();
    5118      2911481 :   if (streaming_p ())
    5119       330298 :     parent::begin ();
    5120      2911481 : }
    5121              : 
    5122              : unsigned
    5123       330298 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    5124              : {
    5125       330298 :   gcc_checking_assert (streaming_p ());
    5126              : 
    5127       330298 :   unmark_trees ();
    5128       330298 :   return parent::end (sink, name, crc_ptr);
    5129              : }
    5130              : 
    5131              : void
    5132      2581183 : trees_out::end ()
    5133              : {
    5134      2581183 :   gcc_assert (!streaming_p ());
    5135              : 
    5136      2581183 :   unmark_trees ();
    5137              :   /* Do not parent::end -- we weren't streaming.  */
    5138      2581183 : }
    5139              : 
    5140              : void
    5141      2911481 : trees_out::mark_trees ()
    5142              : {
    5143      2911481 :   if (size_t size = tree_map.elements ())
    5144              :     {
    5145              :       /* This isn't our first rodeo, destroy and recreate the
    5146              :          tree_map.  I'm a bad bad man.  Use the previous size as a
    5147              :          guess for the next one (so not all bad).  */
    5148      2267165 :       tree_map.~ptr_int_hash_map ();
    5149      2267165 :       new (&tree_map) ptr_int_hash_map (size);
    5150              :     }
    5151              : 
    5152              :   /* Install the fixed trees, with +ve references.  */
    5153      2911481 :   unsigned limit = fixed_trees->length ();
    5154    561008359 :   for (unsigned ix = 0; ix != limit; ix++)
    5155              :     {
    5156    558096878 :       tree val = (*fixed_trees)[ix];
    5157    558096878 :       bool existed = tree_map.put (val, ix + tag_fixed);
    5158    558096878 :       gcc_checking_assert (!TREE_VISITED (val) && !existed);
    5159    558096878 :       TREE_VISITED (val) = true;
    5160              :     }
    5161              : 
    5162      2911481 :   ref_num = 0;
    5163      2911481 : }
    5164              : 
    5165              : /* Unmark the trees we encountered  */
    5166              : 
    5167              : void
    5168      2911481 : trees_out::unmark_trees ()
    5169              : {
    5170      2911481 :   ptr_int_hash_map::iterator end (tree_map.end ());
    5171    667769526 :   for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
    5172              :     {
    5173    664858045 :       tree node = reinterpret_cast<tree> ((*iter).first);
    5174    664858045 :       int ref = (*iter).second;
    5175              :       /* We should have visited the node, and converted its mergeable
    5176              :          reference to a regular reference.  */
    5177    664858045 :       gcc_checking_assert (TREE_VISITED (node)
    5178              :                            && (ref <= tag_backref || ref >= tag_fixed));
    5179    664858045 :       TREE_VISITED (node) = false;
    5180              :     }
    5181      2911481 : }
    5182              : 
    5183              : /* Mark DECL for by-value walking.  We do this by inserting it into
    5184              :    the tree map with a reference of zero.  May be called multiple
    5185              :    times on the same node.  */
    5186              : 
    5187              : void
    5188      4512131 : trees_out::mark_by_value (tree decl)
    5189              : {
    5190      4512131 :   gcc_checking_assert (DECL_P (decl)
    5191              :                        /* Enum consts are INTEGER_CSTS.  */
    5192              :                        || TREE_CODE (decl) == INTEGER_CST
    5193              :                        || TREE_CODE (decl) == TREE_BINFO);
    5194              : 
    5195      4512131 :   if (TREE_VISITED (decl))
    5196              :     /* Must already be forced or fixed.  */
    5197         4072 :     gcc_checking_assert (*tree_map.get (decl) >= tag_value);
    5198              :   else
    5199              :     {
    5200      4508059 :       bool existed = tree_map.put (decl, tag_value);
    5201      4508059 :       gcc_checking_assert (!existed);
    5202      4508059 :       TREE_VISITED (decl) = true;
    5203              :     }
    5204      4512131 : }
    5205              : 
    5206              : int
    5207    127563000 : trees_out::get_tag (tree t)
    5208              : {
    5209    127563000 :   gcc_checking_assert (TREE_VISITED (t));
    5210    127563000 :   return *tree_map.get (t);
    5211              : }
    5212              : 
    5213              : /* Insert T into the map, return its tag number.    */
    5214              : 
    5215              : int
    5216    106761167 : trees_out::insert (tree t, walk_kind walk)
    5217              : {
    5218    106761167 :   gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
    5219    106761167 :   int tag = --ref_num;
    5220    106761167 :   bool existed;
    5221    106761167 :   int &slot = tree_map.get_or_insert (t, &existed);
    5222    106761167 :   gcc_checking_assert (TREE_VISITED (t) == existed
    5223              :                        && (!existed
    5224              :                            || (walk == WK_value && slot == tag_value)));
    5225    106761167 :   TREE_VISITED (t) = true;
    5226    106761167 :   slot = tag;
    5227              : 
    5228    106761167 :   return tag;
    5229              : }
    5230              : 
    5231              : /* Insert T into the backreference array.  Return its back reference
    5232              :    number.  */
    5233              : 
    5234              : int
    5235     21807602 : trees_in::insert (tree t)
    5236              : {
    5237     21807602 :   gcc_checking_assert (t || get_overrun ());
    5238     21807602 :   back_refs.safe_push (t);
    5239     21807602 :   return -(int)back_refs.length ();
    5240              : }
    5241              : 
    5242              : /* A chained set of decls.  */
    5243              : 
    5244              : void
    5245       167774 : trees_out::chained_decls (tree decls)
    5246              : {
    5247       387559 :   for (; decls; decls = DECL_CHAIN (decls))
    5248       219785 :     tree_node (decls);
    5249       167774 :   tree_node (NULL_TREE);
    5250       167774 : }
    5251              : 
    5252              : tree
    5253        61066 : trees_in::chained_decls ()
    5254              : {
    5255        61066 :   tree decls = NULL_TREE;
    5256        61066 :   for (tree *chain = &decls;;)
    5257       146625 :     if (tree decl = tree_node ())
    5258              :       {
    5259        85559 :         if (!DECL_P (decl) || DECL_CHAIN (decl))
    5260              :           {
    5261            0 :             set_overrun ();
    5262            0 :             break;
    5263              :           }
    5264        85559 :         *chain = decl;
    5265        85559 :         chain = &DECL_CHAIN (decl);
    5266              :       }
    5267              :     else
    5268        85559 :       break;
    5269              : 
    5270        61066 :   return decls;
    5271              : }
    5272              : 
    5273              : /* A vector of decls following DECL_CHAIN.  */
    5274              : 
    5275              : void
    5276       419792 : trees_out::vec_chained_decls (tree decls)
    5277              : {
    5278       419792 :   if (streaming_p ())
    5279              :     {
    5280              :       unsigned len = 0;
    5281              : 
    5282      1334350 :       for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5283      1124500 :         len++;
    5284       209850 :       u (len);
    5285              :     }
    5286              : 
    5287      2669202 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5288              :     {
    5289       451061 :       if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5290      2266501 :           && TYPE_NAME (TREE_TYPE (decl)) != decl)
    5291              :         /* An anonynmous struct with a typedef name.  An odd thing to
    5292              :            write.  */
    5293            8 :         tree_node (NULL_TREE);
    5294              :       else
    5295      2249402 :         tree_node (decl);
    5296              :     }
    5297       419792 : }
    5298              : 
    5299              : vec<tree, va_heap> *
    5300       136437 : trees_in::vec_chained_decls ()
    5301              : {
    5302       136437 :   vec<tree, va_heap> *v = NULL;
    5303              : 
    5304       136437 :   if (unsigned len = u ())
    5305              :     {
    5306        71564 :       vec_alloc (v, len);
    5307              : 
    5308       942186 :       for (unsigned ix = 0; ix < len; ix++)
    5309              :         {
    5310       799058 :           tree decl = tree_node ();
    5311       799058 :           if (decl && !DECL_P (decl))
    5312              :             {
    5313            0 :               set_overrun ();
    5314            0 :               break;
    5315              :             }
    5316       799058 :           v->quick_push (decl);
    5317              :         }
    5318              : 
    5319        71564 :       if (get_overrun ())
    5320              :         {
    5321            0 :           vec_free (v);
    5322            0 :           v = NULL;
    5323              :         }
    5324              :     }
    5325              : 
    5326       136437 :   return v;
    5327              : }
    5328              : 
    5329              : /* A vector of trees.  */
    5330              : 
    5331              : void
    5332       294796 : trees_out::tree_vec (vec<tree, va_gc> *v)
    5333              : {
    5334       294796 :   unsigned len = vec_safe_length (v);
    5335       294796 :   if (streaming_p ())
    5336       147375 :     u (len);
    5337       377492 :   for (unsigned ix = 0; ix != len; ix++)
    5338        82696 :     tree_node ((*v)[ix]);
    5339       294796 : }
    5340              : 
    5341              : vec<tree, va_gc> *
    5342        95405 : trees_in::tree_vec ()
    5343              : {
    5344        95405 :   vec<tree, va_gc> *v = NULL;
    5345        95405 :   if (unsigned len = u ())
    5346              :     {
    5347        24025 :       vec_alloc (v, len);
    5348        74694 :       for (unsigned ix = 0; ix != len; ix++)
    5349        26644 :         v->quick_push (tree_node ());
    5350              :     }
    5351        95405 :   return v;
    5352              : }
    5353              : 
    5354              : /* A vector of tree pairs.  */
    5355              : 
    5356              : void
    5357         7478 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
    5358              : {
    5359         7478 :   unsigned len = vec_safe_length (v);
    5360         7478 :   if (streaming_p ())
    5361         3739 :     u (len);
    5362         7478 :   if (len)
    5363        40568 :     for (unsigned ix = 0; ix != len; ix++)
    5364              :       {
    5365        33224 :         tree_pair_s const &s = (*v)[ix];
    5366        33224 :         tree_node (s.purpose);
    5367        33224 :         tree_node (s.value);
    5368              :       }
    5369         7478 : }
    5370              : 
    5371              : vec<tree_pair_s, va_gc> *
    5372         2696 : trees_in::tree_pair_vec ()
    5373              : {
    5374         2696 :   vec<tree_pair_s, va_gc> *v = NULL;
    5375         2696 :   if (unsigned len = u ())
    5376              :     {
    5377         2642 :       vec_alloc (v, len);
    5378        17311 :       for (unsigned ix = 0; ix != len; ix++)
    5379              :         {
    5380        12027 :           tree_pair_s s;
    5381        12027 :           s.purpose = tree_node ();
    5382        12027 :           s.value = tree_node ();
    5383        12027 :           v->quick_push (s);
    5384              :       }
    5385              :     }
    5386         2696 :   return v;
    5387              : }
    5388              : 
    5389              : void
    5390       447108 : trees_out::tree_list (tree list, bool has_purpose)
    5391              : {
    5392      2014655 :   for (; list; list = TREE_CHAIN (list))
    5393              :     {
    5394      1567547 :       gcc_checking_assert (TREE_VALUE (list));
    5395      1567547 :       tree_node (TREE_VALUE (list));
    5396      1567547 :       if (has_purpose)
    5397      1514239 :         tree_node (TREE_PURPOSE (list));
    5398              :     }
    5399       447108 :   tree_node (NULL_TREE);
    5400       447108 : }
    5401              : 
    5402              : tree
    5403       147609 : trees_in::tree_list (bool has_purpose)
    5404              : {
    5405       147609 :   tree res = NULL_TREE;
    5406              : 
    5407       710120 :   for (tree *chain = &res; tree value = tree_node ();
    5408      1125022 :        chain = &TREE_CHAIN (*chain))
    5409              :     {
    5410       562511 :       tree purpose = has_purpose ? tree_node () : NULL_TREE;
    5411       562511 :       *chain = build_tree_list (purpose, value);
    5412       562511 :     }
    5413              : 
    5414       147609 :   return res;
    5415              : }
    5416              : 
    5417              : #define CASE_OMP_SIMD_CODE \
    5418              :     case OMP_SIMD:                      \
    5419              :     case OMP_STRUCTURED_BLOCK:          \
    5420              :     case OMP_LOOP:                      \
    5421              :     case OMP_ORDERED:                   \
    5422              :     case OMP_TILE:                      \
    5423              :     case OMP_UNROLL
    5424              : #define CASE_OMP_CODE \
    5425              :     case OMP_PARALLEL:                  \
    5426              :     case OMP_TASK:                      \
    5427              :     case OMP_FOR:                       \
    5428              :     case OMP_DISTRIBUTE:                \
    5429              :     case OMP_TASKLOOP:                  \
    5430              :     case OMP_TEAMS:                     \
    5431              :     case OMP_TARGET_DATA:               \
    5432              :     case OMP_TARGET:                    \
    5433              :     case OMP_SECTIONS:                  \
    5434              :     case OMP_CRITICAL:                  \
    5435              :     case OMP_SINGLE:                    \
    5436              :     case OMP_SCOPE:                     \
    5437              :     case OMP_TASKGROUP:                 \
    5438              :     case OMP_MASKED:                    \
    5439              :     case OMP_DISPATCH:                  \
    5440              :     case OMP_INTEROP:                   \
    5441              :     case OMP_MASTER:                    \
    5442              :     case OMP_TARGET_UPDATE:             \
    5443              :     case OMP_TARGET_ENTER_DATA:         \
    5444              :     case OMP_TARGET_EXIT_DATA:          \
    5445              :     case OMP_METADIRECTIVE:             \
    5446              :     case OMP_ATOMIC:                    \
    5447              :     case OMP_ATOMIC_READ:               \
    5448              :     case OMP_ATOMIC_CAPTURE_OLD:        \
    5449              :     case OMP_ATOMIC_CAPTURE_NEW
    5450              : #define CASE_OACC_CODE \
    5451              :     case OACC_PARALLEL:                 \
    5452              :     case OACC_KERNELS:                  \
    5453              :     case OACC_SERIAL:                   \
    5454              :     case OACC_DATA:                     \
    5455              :     case OACC_HOST_DATA:                \
    5456              :     case OACC_LOOP:                     \
    5457              :     case OACC_CACHE:                    \
    5458              :     case OACC_DECLARE:                  \
    5459              :     case OACC_ENTER_DATA:               \
    5460              :     case OACC_EXIT_DATA:                \
    5461              :     case OACC_UPDATE
    5462              : 
    5463              : /* Start tree write.  Write information to allocate the receiving
    5464              :    node.  */
    5465              : 
    5466              : void
    5467     21512246 : trees_out::start (tree t, bool code_streamed)
    5468              : {
    5469     21512246 :   if (TYPE_P (t))
    5470              :     {
    5471       819926 :       enum tree_code code = TREE_CODE (t);
    5472       819926 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5473              :       /* All these types are TYPE_NON_COMMON.  */
    5474       819926 :       gcc_checking_assert (code == RECORD_TYPE
    5475              :                            || code == UNION_TYPE
    5476              :                            || code == ENUMERAL_TYPE
    5477              :                            || code == TEMPLATE_TYPE_PARM
    5478              :                            || code == TEMPLATE_TEMPLATE_PARM
    5479              :                            || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    5480              :     }
    5481              : 
    5482     21512246 :   if (!code_streamed)
    5483     20793319 :     u (TREE_CODE (t));
    5484              : 
    5485     21512246 :   switch (TREE_CODE (t))
    5486              :     {
    5487     19254139 :     default:
    5488     19254139 :       if (VL_EXP_CLASS_P (t))
    5489       830236 :         u (VL_EXP_OPERAND_LENGTH (t));
    5490              :       break;
    5491              : 
    5492       865018 :     case INTEGER_CST:
    5493       865018 :       u (TREE_INT_CST_NUNITS (t));
    5494       865018 :       u (TREE_INT_CST_EXT_NUNITS (t));
    5495       865018 :       break;
    5496              : 
    5497           18 :     case OMP_CLAUSE:
    5498           18 :       u (OMP_CLAUSE_CODE (t));
    5499           18 :       break;
    5500              : 
    5501            6 :     CASE_OMP_SIMD_CODE:
    5502            6 :       state->extensions |= SE_OPENMP_SIMD;
    5503            6 :       break;
    5504              : 
    5505            9 :     CASE_OMP_CODE:
    5506            9 :       state->extensions |= SE_OPENMP;
    5507            9 :       break;
    5508              : 
    5509            6 :     CASE_OACC_CODE:
    5510            6 :       state->extensions |= SE_OPENACC;
    5511            6 :       break;
    5512              : 
    5513        64715 :     case STRING_CST:
    5514        64715 :       str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
    5515        64715 :       break;
    5516              : 
    5517           18 :     case RAW_DATA_CST:
    5518           18 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    5519              :         {
    5520              :           /* Stream RAW_DATA_CST with no owner (i.e. data pointing
    5521              :              into libcpp buffers) as something we can stream in as
    5522              :              STRING_CST which owns the data.  */
    5523            6 :           u (0);
    5524              :           /* Can't use str (RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5525              :              here as there isn't a null termination after it.  */
    5526            6 :           z (RAW_DATA_LENGTH (t));
    5527            6 :           if (RAW_DATA_LENGTH (t))
    5528            6 :             if (void *ptr = buf (RAW_DATA_LENGTH (t) + 1))
    5529              :               {
    5530            6 :                 memcpy (ptr, RAW_DATA_POINTER (t), RAW_DATA_LENGTH (t));
    5531            6 :                 ((char *) ptr)[RAW_DATA_LENGTH (t)] = '\0';
    5532              :               }
    5533              :         }
    5534              :       else
    5535              :         {
    5536           12 :           gcc_assert (RAW_DATA_LENGTH (t));
    5537           12 :           u (RAW_DATA_LENGTH (t));
    5538              :         }
    5539              :       break;
    5540              : 
    5541           18 :     case VECTOR_CST:
    5542           18 :       u (VECTOR_CST_LOG2_NPATTERNS (t));
    5543           18 :       u (VECTOR_CST_NELTS_PER_PATTERN (t));
    5544           18 :       break;
    5545              : 
    5546       143636 :     case TREE_BINFO:
    5547       143636 :       u (BINFO_N_BASE_BINFOS (t));
    5548       143636 :       break;
    5549              : 
    5550      1184663 :     case TREE_VEC:
    5551      1184663 :       u (TREE_VEC_LENGTH (t));
    5552      1184663 :       break;
    5553              : 
    5554            0 :     case FIXED_CST:
    5555            0 :       gcc_unreachable (); /* Not supported in C++.  */
    5556            0 :       break;
    5557              : 
    5558            0 :     case IDENTIFIER_NODE:
    5559            0 :     case SSA_NAME:
    5560            0 :     case TARGET_MEM_REF:
    5561            0 :     case TRANSLATION_UNIT_DECL:
    5562              :       /* We shouldn't meet these.  */
    5563            0 :       gcc_unreachable ();
    5564     21512246 :       break;
    5565              :     }
    5566     21512246 : }
    5567              : 
    5568              : /* Start tree read.  Allocate the receiving node.  */
    5569              : 
    5570              : tree
    5571     15717635 : trees_in::start (unsigned code)
    5572              : {
    5573     15717635 :   tree t = NULL_TREE;
    5574              : 
    5575     15717635 :   if (!code)
    5576     14248290 :     code = u ();
    5577              : 
    5578     15717635 :   switch (code)
    5579              :     {
    5580     14106924 :     default:
    5581     14106924 :       if (code >= MAX_TREE_CODES)
    5582              :         {
    5583            0 :         fail:
    5584            0 :           set_overrun ();
    5585            0 :           return NULL_TREE;
    5586              :         }
    5587     14106924 :       else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
    5588              :         {
    5589       614185 :           unsigned ops = u ();
    5590       614185 :           t = build_vl_exp (tree_code (code), ops);
    5591              :         }
    5592              :       else
    5593     13492739 :         t = make_node (tree_code (code));
    5594              :       break;
    5595              : 
    5596       599619 :     case INTEGER_CST:
    5597       599619 :       {
    5598       599619 :         unsigned n = u ();
    5599       599619 :         unsigned e = u ();
    5600       599619 :         t = make_int_cst (n, e);
    5601              :       }
    5602       599619 :       break;
    5603              : 
    5604           18 :     case OMP_CLAUSE:
    5605           18 :       t = build_omp_clause (UNKNOWN_LOCATION, omp_clause_code (u ()));
    5606           18 :       break;
    5607              : 
    5608            9 :     CASE_OMP_SIMD_CODE:
    5609            9 :       if (!(state->extensions & SE_OPENMP_SIMD))
    5610            0 :         goto fail;
    5611            9 :       t = make_node (tree_code (code));
    5612            9 :       break;
    5613              : 
    5614            9 :     CASE_OMP_CODE:
    5615            9 :       if (!(state->extensions & SE_OPENMP))
    5616            0 :         goto fail;
    5617            9 :       t = make_node (tree_code (code));
    5618            9 :       break;
    5619              : 
    5620            6 :     CASE_OACC_CODE:
    5621            6 :       if (!(state->extensions & SE_OPENACC))
    5622            0 :         goto fail;
    5623            6 :       t = make_node (tree_code (code));
    5624            6 :       break;
    5625              : 
    5626        51020 :     case STRING_CST:
    5627        51020 :       {
    5628        51020 :         size_t l;
    5629        51020 :         const char *chars = str (&l);
    5630        51020 :         t = build_string (l, chars);
    5631              :       }
    5632        51020 :       break;
    5633              : 
    5634            9 :     case RAW_DATA_CST:
    5635            9 :       {
    5636            9 :         size_t l = u ();
    5637            9 :         if (l == 0)
    5638              :           {
    5639              :             /* Stream in RAW_DATA_CST with no owner as STRING_CST
    5640              :                which owns the data.  */
    5641            3 :             const char *chars = str (&l);
    5642            3 :             t = build_string (l, chars);
    5643              :           }
    5644              :         else
    5645              :           {
    5646            6 :             t = make_node (RAW_DATA_CST);
    5647            6 :             RAW_DATA_LENGTH (t) = l;
    5648              :           }
    5649              :       }
    5650            9 :       break;
    5651              : 
    5652           24 :     case VECTOR_CST:
    5653           24 :       {
    5654           24 :         unsigned log2_npats = u ();
    5655           24 :         unsigned elts_per = u ();
    5656           24 :         t = make_vector (log2_npats, elts_per);
    5657              :       }
    5658           24 :       break;
    5659              : 
    5660        92709 :     case TREE_BINFO:
    5661        92709 :       t = make_tree_binfo (u ());
    5662        92709 :       break;
    5663              : 
    5664       867288 :     case TREE_VEC:
    5665       867288 :       t = make_tree_vec (u ());
    5666       867288 :       break;
    5667              : 
    5668            0 :     case FIXED_CST:
    5669            0 :     case IDENTIFIER_NODE:
    5670            0 :     case SSA_NAME:
    5671            0 :     case TARGET_MEM_REF:
    5672            0 :     case TRANSLATION_UNIT_DECL:
    5673            0 :       goto fail;
    5674              :     }
    5675              : 
    5676              :   return t;
    5677              : }
    5678              : 
    5679              : /* The kinds of interface an importer could have for a decl.  */
    5680              : 
    5681              : enum class importer_interface {
    5682              :   unknown,        /* The definition may or may not need to be emitted.  */
    5683              :   external,       /* The definition can always be found in another TU.  */
    5684              :   internal,       /* The definition should be emitted in the importer's TU.  */
    5685              :   always_emit,    /* The definition must be emitted in the importer's TU,
    5686              :                      regardless of if it's used or not. */
    5687              : };
    5688              : 
    5689              : /* Returns what kind of interface an importer will have of DECL.  */
    5690              : 
    5691              : static importer_interface
    5692       814380 : get_importer_interface (tree decl)
    5693              : {
    5694              :   /* Internal linkage entities must be emitted in each importer if
    5695              :      there is a definition available.  */
    5696       814380 :   if (!TREE_PUBLIC (decl))
    5697              :     return importer_interface::internal;
    5698              : 
    5699              :   /* Other entities that aren't vague linkage are either not definitions
    5700              :      or will be publicly emitted in this TU, so importers can just refer
    5701              :      to an external definition.  */
    5702       380599 :   if (!vague_linkage_p (decl))
    5703              :     return importer_interface::external;
    5704              : 
    5705              :   /* For explicit instantiations, importers can always rely on there
    5706              :      being a definition in another TU, unless this is a definition
    5707              :      in a header module: in which case the importer will always need
    5708              :      to emit it.  */
    5709       373981 :   if (DECL_LANG_SPECIFIC (decl)
    5710       373981 :       && DECL_EXPLICIT_INSTANTIATION (decl))
    5711        27792 :     return (header_module_p () && !DECL_EXTERNAL (decl)
    5712        27792 :             ? importer_interface::always_emit
    5713              :             : importer_interface::external);
    5714              : 
    5715              :   /* A gnu_inline function is never emitted in any TU.  */
    5716       346189 :   if (TREE_CODE (decl) == FUNCTION_DECL
    5717       255231 :       && DECL_DECLARED_INLINE_P (decl)
    5718       593057 :       && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (decl)))
    5719              :     return importer_interface::external;
    5720              : 
    5721              :   /* Everything else has vague linkage.  */
    5722              :   return importer_interface::unknown;
    5723              : }
    5724              : 
    5725              : /* The structure streamers access the raw fields, because the
    5726              :    alternative, of using the accessor macros can require using
    5727              :    different accessors for the same underlying field, depending on the
    5728              :    tree code.  That's both confusing and annoying.  */
    5729              : 
    5730              : /* Read & write the core boolean flags.  */
    5731              : 
    5732              : void
    5733     21550089 : trees_out::core_bools (tree t, bits_out& bits)
    5734              : {
    5735              : #define WB(X) (bits.b (X))
    5736              : /* Stream X if COND holds, and if !COND stream a dummy value so that the
    5737              :    overall number of bits streamed is independent of the runtime value
    5738              :    of COND, which allows the compiler to better optimize this function.  */
    5739              : #define WB_IF(COND, X) WB ((COND) ? (X) : false)
    5740     21550089 :   tree_code code = TREE_CODE (t);
    5741              : 
    5742     21550089 :   WB (t->base.side_effects_flag);
    5743     21550089 :   WB (t->base.constant_flag);
    5744     21550089 :   WB (t->base.addressable_flag);
    5745     21550089 :   WB (t->base.volatile_flag);
    5746     21550089 :   WB (t->base.readonly_flag);
    5747              :   /* base.asm_written_flag is a property of the current TU's use of
    5748              :      this decl.  */
    5749     21550089 :   WB (t->base.nowarning_flag);
    5750              :   /* base.visited read as zero (it's set for writer, because that's
    5751              :      how we mark nodes).  */
    5752              :   /* base.used_flag is not streamed.  Readers may set TREE_USED of
    5753              :      decls they use.  */
    5754     21550089 :   WB (t->base.nothrow_flag);
    5755     21550089 :   WB (t->base.static_flag);
    5756              :   /* This is TYPE_CACHED_VALUES_P for types.  */
    5757     21550089 :   WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5758     21550089 :   WB (t->base.private_flag);
    5759     21550089 :   WB (t->base.protected_flag);
    5760     21550089 :   WB (t->base.deprecated_flag);
    5761     21550089 :   WB (t->base.default_def_flag);
    5762              : 
    5763     21550089 :   switch (code)
    5764              :     {
    5765              :     case CALL_EXPR:
    5766              :     case INTEGER_CST:
    5767              :     case SSA_NAME:
    5768              :     case TARGET_MEM_REF:
    5769              :     case TREE_VEC:
    5770              :       /* These use different base.u fields.  */
    5771              :       return;
    5772              : 
    5773     18689792 :     default:
    5774     18689792 :       WB (t->base.u.bits.lang_flag_0);
    5775     18689792 :       bool flag_1 = t->base.u.bits.lang_flag_1;
    5776     18689792 :       if (!flag_1)
    5777              :         ;
    5778       612572 :       else if (code == TEMPLATE_INFO)
    5779              :         /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader.  */
    5780              :         flag_1 = false;
    5781       605691 :       else if (code == VAR_DECL)
    5782              :         {
    5783              :           /* This is DECL_INITIALIZED_P.  */
    5784       110404 :           if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    5785              :             /* We'll set this when reading the definition.  */
    5786     18689792 :             flag_1 = false;
    5787              :         }
    5788     18689792 :       WB (flag_1);
    5789     18689792 :       WB (t->base.u.bits.lang_flag_2);
    5790     18689792 :       WB (t->base.u.bits.lang_flag_3);
    5791     18689792 :       WB (t->base.u.bits.lang_flag_4);
    5792     18689792 :       WB (t->base.u.bits.lang_flag_5);
    5793     18689792 :       WB (t->base.u.bits.lang_flag_6);
    5794     18689792 :       WB (t->base.u.bits.saturating_flag);
    5795     18689792 :       WB (t->base.u.bits.unsigned_flag);
    5796     18689792 :       WB (t->base.u.bits.packed_flag);
    5797     18689792 :       WB (t->base.u.bits.user_align);
    5798     18689792 :       WB (t->base.u.bits.nameless_flag);
    5799     18689792 :       WB (t->base.u.bits.atomic_flag);
    5800     18689792 :       WB (t->base.u.bits.unavailable_flag);
    5801     18689792 :       break;
    5802              :     }
    5803              : 
    5804     18689792 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5805              :     {
    5806       857769 :       WB (t->type_common.no_force_blk_flag);
    5807       857769 :       WB (t->type_common.needs_constructing_flag);
    5808       857769 :       WB (t->type_common.transparent_aggr_flag);
    5809       857769 :       WB (t->type_common.restrict_flag);
    5810       857769 :       WB (t->type_common.string_flag);
    5811       857769 :       WB (t->type_common.lang_flag_0);
    5812       857769 :       WB (t->type_common.lang_flag_1);
    5813       857769 :       WB (t->type_common.lang_flag_2);
    5814       857769 :       WB (t->type_common.lang_flag_3);
    5815       857769 :       WB (t->type_common.lang_flag_4);
    5816       857769 :       WB (t->type_common.lang_flag_5);
    5817       857769 :       WB (t->type_common.lang_flag_6);
    5818       857769 :       WB (t->type_common.typeless_storage);
    5819              :     }
    5820              : 
    5821     18689792 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5822              :     return;
    5823              : 
    5824      4660104 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5825              :     {
    5826      4660104 :       WB (t->decl_common.nonlocal_flag);
    5827      4660104 :       WB (t->decl_common.virtual_flag);
    5828      4660104 :       WB (t->decl_common.ignored_flag);
    5829      4660104 :       WB (t->decl_common.abstract_flag);
    5830      4660104 :       WB (t->decl_common.artificial_flag);
    5831      4660104 :       WB (t->decl_common.preserve_flag);
    5832      4660104 :       WB (t->decl_common.debug_expr_is_from);
    5833      4660104 :       WB (t->decl_common.lang_flag_0);
    5834      4660104 :       WB (t->decl_common.lang_flag_1);
    5835      4660104 :       WB (t->decl_common.lang_flag_2);
    5836      4660104 :       WB (t->decl_common.lang_flag_3);
    5837      4660104 :       WB (t->decl_common.lang_flag_4);
    5838              : 
    5839      4660104 :       {
    5840              :         /* This is DECL_INTERFACE_KNOWN: We should redetermine whether
    5841              :            we need to import or export any vague-linkage entities on
    5842              :            stream-in.  */
    5843      4660104 :         bool interface_known = t->decl_common.lang_flag_5;
    5844      4660104 :         if (interface_known
    5845      4660104 :             && get_importer_interface (t) == importer_interface::unknown)
    5846              :           interface_known = false;
    5847      4660104 :         WB (interface_known);
    5848              :       }
    5849              : 
    5850      4660104 :       WB (t->decl_common.lang_flag_6);
    5851      4660104 :       WB (t->decl_common.lang_flag_7);
    5852      4660104 :       WB (t->decl_common.lang_flag_8);
    5853      4660104 :       WB (t->decl_common.decl_flag_0);
    5854              : 
    5855      4660104 :       {
    5856              :         /* DECL_EXTERNAL -> decl_flag_1
    5857              :              == it is defined elsewhere
    5858              :            DECL_NOT_REALLY_EXTERN -> base.not_really_extern
    5859              :              == that was a lie, it is here  */
    5860              : 
    5861      4660104 :         bool is_external = t->decl_common.decl_flag_1;
    5862              :         /* maybe_emit_vtables relies on vtables being marked as
    5863              :            DECL_EXTERNAL and DECL_NOT_REALLY_EXTERN before processing.  */
    5864      4660104 :         if (!is_external && VAR_P (t) && DECL_VTABLE_OR_VTT_P (t))
    5865              :           is_external = true;
    5866              :         /* Things we emit here might well be external from the POV of an
    5867              :            importer.  */
    5868      4659666 :         if (!is_external
    5869      3916839 :             && VAR_OR_FUNCTION_DECL_P (t)
    5870      5035632 :             && get_importer_interface (t) == importer_interface::external)
    5871              :           is_external = true;
    5872      4660104 :         WB (is_external);
    5873              :       }
    5874              : 
    5875      4660104 :       WB (t->decl_common.decl_flag_2);
    5876      4660104 :       WB (t->decl_common.decl_flag_3);
    5877      4660104 :       WB (t->decl_common.not_gimple_reg_flag);
    5878      4660104 :       WB (t->decl_common.decl_by_reference_flag);
    5879      4660104 :       WB (t->decl_common.decl_read_flag);
    5880      4660104 :       WB (t->decl_common.decl_nonshareable_flag);
    5881      4660104 :       WB (t->decl_common.decl_not_flexarray);
    5882              :     }
    5883              :   else
    5884              :     return;
    5885              : 
    5886      4660104 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5887              :     {
    5888      2195495 :       WB (t->decl_with_vis.defer_output);
    5889      2195495 :       WB (t->decl_with_vis.hard_register);
    5890      2195495 :       WB (t->decl_with_vis.common_flag);
    5891      2195495 :       WB (t->decl_with_vis.in_text_section);
    5892      2195495 :       WB (t->decl_with_vis.in_constant_pool);
    5893      2195495 :       WB (t->decl_with_vis.dllimport_flag);
    5894      2195495 :       WB (t->decl_with_vis.weak_flag);
    5895      2195495 :       WB (t->decl_with_vis.seen_in_bind_expr);
    5896      2195495 :       WB (t->decl_with_vis.comdat_flag);
    5897      2195495 :       WB (t->decl_with_vis.visibility_specified);
    5898      2195495 :       WB (t->decl_with_vis.init_priority_p);
    5899      2195495 :       WB (t->decl_with_vis.shadowed_for_var_p);
    5900      2195495 :       WB (t->decl_with_vis.cxx_constructor);
    5901      2195495 :       WB (t->decl_with_vis.cxx_destructor);
    5902      2195495 :       WB (t->decl_with_vis.final);
    5903      2195495 :       WB (t->decl_with_vis.regdecl_flag);
    5904              :     }
    5905              :   else
    5906              :     return;
    5907              : 
    5908      2195495 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5909              :     {
    5910       644261 :       WB (t->function_decl.static_ctor_flag);
    5911       644261 :       WB (t->function_decl.static_dtor_flag);
    5912       644261 :       WB (t->function_decl.uninlinable);
    5913       644261 :       WB (t->function_decl.possibly_inlined);
    5914       644261 :       WB (t->function_decl.novops_flag);
    5915       644261 :       WB (t->function_decl.returns_twice_flag);
    5916       644261 :       WB (t->function_decl.malloc_flag);
    5917       644261 :       WB (t->function_decl.declared_inline_flag);
    5918       644261 :       WB (t->function_decl.no_inline_warning_flag);
    5919       644261 :       WB (t->function_decl.no_instrument_function_entry_exit);
    5920       644261 :       WB (t->function_decl.no_limit_stack);
    5921       644261 :       WB (t->function_decl.disregard_inline_limits);
    5922       644261 :       WB (t->function_decl.pure_flag);
    5923       644261 :       WB (t->function_decl.looping_const_or_pure_flag);
    5924              : 
    5925       644261 :       WB (t->function_decl.has_debug_args_flag);
    5926       644261 :       WB (t->function_decl.versioned_function);
    5927       644261 :       WB (t->function_decl.replaceable_operator);
    5928              : 
    5929              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5930       644261 :       unsigned kind = (unsigned)t->function_decl.decl_type;
    5931       644261 :       WB ((kind >> 0) & 1);
    5932       644261 :       WB ((kind >> 1) & 1);
    5933              :     }
    5934              : #undef WB_IF
    5935              : #undef WB
    5936              : }
    5937              : 
    5938              : bool
    5939     15739731 : trees_in::core_bools (tree t, bits_in& bits)
    5940              : {
    5941              : #define RB(X) ((X) = bits.b ())
    5942              : /* See the comment for WB_IF in trees_out::core_bools.  */
    5943              : #define RB_IF(COND, X) ((COND) ? RB (X) : bits.b ())
    5944              : 
    5945     15739731 :   tree_code code = TREE_CODE (t);
    5946              : 
    5947     15739731 :   RB (t->base.side_effects_flag);
    5948     15739731 :   RB (t->base.constant_flag);
    5949     15739731 :   RB (t->base.addressable_flag);
    5950     15739731 :   RB (t->base.volatile_flag);
    5951     15739731 :   RB (t->base.readonly_flag);
    5952              :   /* base.asm_written_flag is not streamed.  */
    5953     15739731 :   RB (t->base.nowarning_flag);
    5954              :   /* base.visited is not streamed.  */
    5955              :   /* base.used_flag is not streamed.  */
    5956     15739731 :   RB (t->base.nothrow_flag);
    5957     15739731 :   RB (t->base.static_flag);
    5958     15739731 :   RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5959     15739731 :   RB (t->base.private_flag);
    5960     15739731 :   RB (t->base.protected_flag);
    5961     15739731 :   RB (t->base.deprecated_flag);
    5962     15739731 :   RB (t->base.default_def_flag);
    5963              : 
    5964     15739731 :   switch (code)
    5965              :     {
    5966      2067303 :     case CALL_EXPR:
    5967      2067303 :     case INTEGER_CST:
    5968      2067303 :     case SSA_NAME:
    5969      2067303 :     case TARGET_MEM_REF:
    5970      2067303 :     case TREE_VEC:
    5971              :       /* These use different base.u fields.  */
    5972      2067303 :       goto done;
    5973              : 
    5974     13672428 :     default:
    5975     13672428 :       RB (t->base.u.bits.lang_flag_0);
    5976     13672428 :       RB (t->base.u.bits.lang_flag_1);
    5977     13672428 :       RB (t->base.u.bits.lang_flag_2);
    5978     13672428 :       RB (t->base.u.bits.lang_flag_3);
    5979     13672428 :       RB (t->base.u.bits.lang_flag_4);
    5980     13672428 :       RB (t->base.u.bits.lang_flag_5);
    5981     13672428 :       RB (t->base.u.bits.lang_flag_6);
    5982     13672428 :       RB (t->base.u.bits.saturating_flag);
    5983     13672428 :       RB (t->base.u.bits.unsigned_flag);
    5984     13672428 :       RB (t->base.u.bits.packed_flag);
    5985     13672428 :       RB (t->base.u.bits.user_align);
    5986     13672428 :       RB (t->base.u.bits.nameless_flag);
    5987     13672428 :       RB (t->base.u.bits.atomic_flag);
    5988     13672428 :       RB (t->base.u.bits.unavailable_flag);
    5989     13672428 :       break;
    5990              :     }
    5991              : 
    5992     13672428 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5993              :     {
    5994       592920 :       RB (t->type_common.no_force_blk_flag);
    5995       592920 :       RB (t->type_common.needs_constructing_flag);
    5996       592920 :       RB (t->type_common.transparent_aggr_flag);
    5997       592920 :       RB (t->type_common.restrict_flag);
    5998       592920 :       RB (t->type_common.string_flag);
    5999       592920 :       RB (t->type_common.lang_flag_0);
    6000       592920 :       RB (t->type_common.lang_flag_1);
    6001       592920 :       RB (t->type_common.lang_flag_2);
    6002       592920 :       RB (t->type_common.lang_flag_3);
    6003       592920 :       RB (t->type_common.lang_flag_4);
    6004       592920 :       RB (t->type_common.lang_flag_5);
    6005       592920 :       RB (t->type_common.lang_flag_6);
    6006       592920 :       RB (t->type_common.typeless_storage);
    6007              :     }
    6008              : 
    6009     13672428 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    6010     10314881 :     goto done;
    6011              : 
    6012      3357547 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6013              :     {
    6014      3357547 :       RB (t->decl_common.nonlocal_flag);
    6015      3357547 :       RB (t->decl_common.virtual_flag);
    6016      3357547 :       RB (t->decl_common.ignored_flag);
    6017      3357547 :       RB (t->decl_common.abstract_flag);
    6018      3357547 :       RB (t->decl_common.artificial_flag);
    6019      3357547 :       RB (t->decl_common.preserve_flag);
    6020      3357547 :       RB (t->decl_common.debug_expr_is_from);
    6021      3357547 :       RB (t->decl_common.lang_flag_0);
    6022      3357547 :       RB (t->decl_common.lang_flag_1);
    6023      3357547 :       RB (t->decl_common.lang_flag_2);
    6024      3357547 :       RB (t->decl_common.lang_flag_3);
    6025      3357547 :       RB (t->decl_common.lang_flag_4);
    6026      3357547 :       RB (t->decl_common.lang_flag_5);
    6027      3357547 :       RB (t->decl_common.lang_flag_6);
    6028      3357547 :       RB (t->decl_common.lang_flag_7);
    6029      3357547 :       RB (t->decl_common.lang_flag_8);
    6030      3357547 :       RB (t->decl_common.decl_flag_0);
    6031      3357547 :       RB (t->decl_common.decl_flag_1);
    6032      3357547 :       RB (t->decl_common.decl_flag_2);
    6033      3357547 :       RB (t->decl_common.decl_flag_3);
    6034      3357547 :       RB (t->decl_common.not_gimple_reg_flag);
    6035      3357547 :       RB (t->decl_common.decl_by_reference_flag);
    6036      3357547 :       RB (t->decl_common.decl_read_flag);
    6037      3357547 :       RB (t->decl_common.decl_nonshareable_flag);
    6038      3357547 :       RB (t->decl_common.decl_not_flexarray);
    6039              :     }
    6040              :   else
    6041            0 :     goto done;
    6042              : 
    6043      3357547 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6044              :     {
    6045      1546885 :       RB (t->decl_with_vis.defer_output);
    6046      1546885 :       RB (t->decl_with_vis.hard_register);
    6047      1546885 :       RB (t->decl_with_vis.common_flag);
    6048      1546885 :       RB (t->decl_with_vis.in_text_section);
    6049      1546885 :       RB (t->decl_with_vis.in_constant_pool);
    6050      1546885 :       RB (t->decl_with_vis.dllimport_flag);
    6051      1546885 :       RB (t->decl_with_vis.weak_flag);
    6052      1546885 :       RB (t->decl_with_vis.seen_in_bind_expr);
    6053      1546885 :       RB (t->decl_with_vis.comdat_flag);
    6054      1546885 :       RB (t->decl_with_vis.visibility_specified);
    6055      1546885 :       RB (t->decl_with_vis.init_priority_p);
    6056      1546885 :       RB (t->decl_with_vis.shadowed_for_var_p);
    6057      1546885 :       RB (t->decl_with_vis.cxx_constructor);
    6058      1546885 :       RB (t->decl_with_vis.cxx_destructor);
    6059      1546885 :       RB (t->decl_with_vis.final);
    6060      1546885 :       RB (t->decl_with_vis.regdecl_flag);
    6061              :     }
    6062              :   else
    6063      1810662 :     goto done;
    6064              : 
    6065      1546885 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    6066              :     {
    6067       473467 :       RB (t->function_decl.static_ctor_flag);
    6068       473467 :       RB (t->function_decl.static_dtor_flag);
    6069       473467 :       RB (t->function_decl.uninlinable);
    6070       473467 :       RB (t->function_decl.possibly_inlined);
    6071       473467 :       RB (t->function_decl.novops_flag);
    6072       473467 :       RB (t->function_decl.returns_twice_flag);
    6073       473467 :       RB (t->function_decl.malloc_flag);
    6074       473467 :       RB (t->function_decl.declared_inline_flag);
    6075       473467 :       RB (t->function_decl.no_inline_warning_flag);
    6076       473467 :       RB (t->function_decl.no_instrument_function_entry_exit);
    6077       473467 :       RB (t->function_decl.no_limit_stack);
    6078       473467 :       RB (t->function_decl.disregard_inline_limits);
    6079       473467 :       RB (t->function_decl.pure_flag);
    6080       473467 :       RB (t->function_decl.looping_const_or_pure_flag);
    6081              : 
    6082       473467 :       RB (t->function_decl.has_debug_args_flag);
    6083       473467 :       RB (t->function_decl.versioned_function);
    6084       473467 :       RB (t->function_decl.replaceable_operator);
    6085              : 
    6086              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    6087       473467 :       unsigned kind = 0;
    6088       473467 :       kind |= unsigned (bits.b ()) << 0;
    6089       473467 :       kind |= unsigned (bits.b ()) << 1;
    6090       473467 :       t->function_decl.decl_type = function_decl_type (kind);
    6091              :     }
    6092              : #undef RB_IF
    6093              : #undef RB
    6094      1073418 : done:
    6095     15739731 :   return !get_overrun ();
    6096              : }
    6097              : 
    6098              : void
    6099      2816260 : trees_out::lang_decl_bools (tree t, bits_out& bits)
    6100              : {
    6101              : #define WB(X) (bits.b (X))
    6102      2816260 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6103              : 
    6104      2816260 :   bits.bflush ();
    6105      2816260 :   WB (lang->u.base.language == lang_cplusplus);
    6106      2816260 :   WB ((lang->u.base.use_template >> 0) & 1);
    6107      2816260 :   WB ((lang->u.base.use_template >> 1) & 1);
    6108              :   /* Do not write lang->u.base.not_really_extern, importer will set
    6109              :      when reading the definition (if any).  */
    6110      2816260 :   WB (lang->u.base.initialized_in_class);
    6111              : 
    6112      2816260 :   WB (lang->u.base.threadprivate_or_deleted_p);
    6113      2816260 :   WB (lang->u.base.anticipated_p);
    6114      2816260 :   WB (lang->u.base.friend_or_tls);
    6115      2816260 :   WB (lang->u.base.unknown_bound_p);
    6116              :   /* Do not write lang->u.base.odr_used, importer will recalculate if
    6117              :      they do ODR use this decl.  */
    6118      2816260 :   WB (lang->u.base.concept_p);
    6119      2816260 :   WB (lang->u.base.var_declared_inline_p);
    6120      2816260 :   WB (lang->u.base.dependent_init_p);
    6121              : 
    6122              :   /* When building a header unit, everything is marked as purview, (so
    6123              :      we know which decls to write).  But when we import them we do not
    6124              :      want to mark them as in module purview.  */
    6125      5543660 :   WB (lang->u.base.module_purview_p && !header_module_p ());
    6126      2816260 :   WB (lang->u.base.module_attach_p);
    6127              :   /* Importer will set module_import_p and module_entity_p themselves
    6128              :      as appropriate.  */
    6129      2816260 :   WB (lang->u.base.module_keyed_decls_p);
    6130              : 
    6131      2816260 :   WB (lang->u.base.omp_declare_mapper_p);
    6132              : 
    6133      2816260 :   switch (lang->u.base.selector)
    6134              :     {
    6135            0 :     default:
    6136            0 :       gcc_unreachable ();
    6137              : 
    6138       644261 :     case lds_fn:  /* lang_decl_fn.  */
    6139       644261 :       WB (lang->u.fn.global_ctor_p);
    6140       644261 :       WB (lang->u.fn.global_dtor_p);
    6141              : 
    6142       644261 :       WB (lang->u.fn.static_function);
    6143       644261 :       WB (lang->u.fn.pure_virtual);
    6144       644261 :       WB (lang->u.fn.defaulted_p);
    6145       644261 :       WB (lang->u.fn.has_in_charge_parm_p);
    6146       644261 :       WB (lang->u.fn.has_vtt_parm_p);
    6147              :       /* There shouldn't be a pending inline at this point.  */
    6148       644261 :       gcc_assert (!lang->u.fn.pending_inline_p);
    6149       644261 :       WB (lang->u.fn.nonconverting);
    6150       644261 :       WB (lang->u.fn.thunk_p);
    6151              : 
    6152       644261 :       WB (lang->u.fn.this_thunk_p);
    6153       644261 :       WB (lang->u.fn.omp_declare_reduction_p);
    6154       644261 :       WB (lang->u.fn.has_dependent_explicit_spec_p);
    6155       644261 :       WB (lang->u.fn.immediate_fn_p);
    6156       644261 :       WB (lang->u.fn.maybe_deleted);
    6157       644261 :       WB (lang->u.fn.coroutine_p);
    6158       644261 :       WB (lang->u.fn.implicit_constexpr);
    6159       644261 :       WB (lang->u.fn.escalated_p);
    6160       644261 :       WB (lang->u.fn.xobj_func);
    6161       644261 :       goto lds_min;
    6162              : 
    6163         3406 :     case lds_decomp:  /* lang_decl_decomp.  */
    6164              :       /* No bools.  */
    6165         3406 :       goto lds_min;
    6166              : 
    6167              :     case lds_min:  /* lang_decl_min.  */
    6168      2816260 :     lds_min:
    6169              :       /* No bools.  */
    6170              :       break;
    6171              : 
    6172              :     case lds_ns:  /* lang_decl_ns.  */
    6173              :       /* No bools.  */
    6174              :       break;
    6175              : 
    6176              :     case lds_parm:  /* lang_decl_parm.  */
    6177              :       /* No bools.  */
    6178              :       break;
    6179              :     }
    6180              : #undef WB
    6181      2816260 : }
    6182              : 
    6183              : bool
    6184      2103412 : trees_in::lang_decl_bools (tree t, bits_in& bits)
    6185              : {
    6186              : #define RB(X) ((X) = bits.b ())
    6187      2103412 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6188              : 
    6189      2103412 :   bits.bflush ();
    6190      2103412 :   lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
    6191      2103412 :   unsigned v;
    6192      2103412 :   v = bits.b () << 0;
    6193      2103412 :   v |= bits.b () << 1;
    6194      2103412 :   lang->u.base.use_template = v;
    6195              :   /* lang->u.base.not_really_extern is not streamed.  */
    6196      2103412 :   RB (lang->u.base.initialized_in_class);
    6197              : 
    6198      2103412 :   RB (lang->u.base.threadprivate_or_deleted_p);
    6199      2103412 :   RB (lang->u.base.anticipated_p);
    6200      2103412 :   RB (lang->u.base.friend_or_tls);
    6201      2103412 :   RB (lang->u.base.unknown_bound_p);
    6202              :   /* lang->u.base.odr_used is not streamed.  */
    6203      2103412 :   RB (lang->u.base.concept_p);
    6204      2103412 :   RB (lang->u.base.var_declared_inline_p);
    6205      2103412 :   RB (lang->u.base.dependent_init_p);
    6206              : 
    6207      2103412 :   RB (lang->u.base.module_purview_p);
    6208      2103412 :   RB (lang->u.base.module_attach_p);
    6209              :   /* module_import_p and module_entity_p are not streamed.  */
    6210      2103412 :   RB (lang->u.base.module_keyed_decls_p);
    6211              : 
    6212      2103412 :   RB (lang->u.base.omp_declare_mapper_p);
    6213              : 
    6214      2103412 :   switch (lang->u.base.selector)
    6215              :     {
    6216            0 :     default:
    6217            0 :       gcc_unreachable ();
    6218              : 
    6219       473467 :     case lds_fn:  /* lang_decl_fn.  */
    6220       473467 :       RB (lang->u.fn.global_ctor_p);
    6221       473467 :       RB (lang->u.fn.global_dtor_p);
    6222              : 
    6223       473467 :       RB (lang->u.fn.static_function);
    6224       473467 :       RB (lang->u.fn.pure_virtual);
    6225       473467 :       RB (lang->u.fn.defaulted_p);
    6226       473467 :       RB (lang->u.fn.has_in_charge_parm_p);
    6227       473467 :       RB (lang->u.fn.has_vtt_parm_p);
    6228              :       /* lang->u.f.n.pending_inline_p is not streamed.  */
    6229       473467 :       RB (lang->u.fn.nonconverting);
    6230       473467 :       RB (lang->u.fn.thunk_p);
    6231              : 
    6232       473467 :       RB (lang->u.fn.this_thunk_p);
    6233       473467 :       RB (lang->u.fn.omp_declare_reduction_p);
    6234       473467 :       RB (lang->u.fn.has_dependent_explicit_spec_p);
    6235       473467 :       RB (lang->u.fn.immediate_fn_p);
    6236       473467 :       RB (lang->u.fn.maybe_deleted);
    6237       473467 :       RB (lang->u.fn.coroutine_p);
    6238       473467 :       RB (lang->u.fn.implicit_constexpr);
    6239       473467 :       RB (lang->u.fn.escalated_p);
    6240       473467 :       RB (lang->u.fn.xobj_func);
    6241       473467 :       goto lds_min;
    6242              : 
    6243         3085 :     case lds_decomp:  /* lang_decl_decomp.  */
    6244              :       /* No bools.  */
    6245         3085 :       goto lds_min;
    6246              : 
    6247              :     case lds_min:  /* lang_decl_min.  */
    6248      2103412 :     lds_min:
    6249              :       /* No bools.  */
    6250              :       break;
    6251              : 
    6252              :     case lds_ns:  /* lang_decl_ns.  */
    6253              :       /* No bools.  */
    6254              :       break;
    6255              : 
    6256              :     case lds_parm:  /* lang_decl_parm.  */
    6257              :       /* No bools.  */
    6258              :       break;
    6259              :     }
    6260              : #undef RB
    6261      2103412 :   return !get_overrun ();
    6262              : }
    6263              : 
    6264              : void
    6265       223081 : trees_out::lang_type_bools (tree t, bits_out& bits)
    6266              : {
    6267              : #define WB(X) (bits.b (X))
    6268       223081 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6269              : 
    6270       223081 :   bits.bflush ();
    6271       223081 :   WB (lang->has_type_conversion);
    6272       223081 :   WB (lang->has_copy_ctor);
    6273       223081 :   WB (lang->has_default_ctor);
    6274       223081 :   WB (lang->const_needs_init);
    6275       223081 :   WB (lang->ref_needs_init);
    6276       223081 :   WB (lang->has_const_copy_assign);
    6277       223081 :   WB ((lang->use_template >> 0) & 1);
    6278       223081 :   WB ((lang->use_template >> 1) & 1);
    6279              : 
    6280       223081 :   WB (lang->has_mutable);
    6281       223081 :   WB (lang->com_interface);
    6282       223081 :   WB (lang->non_pod_class);
    6283       223081 :   WB (lang->nearly_empty_p);
    6284       223081 :   WB (lang->user_align);
    6285       223081 :   WB (lang->has_copy_assign);
    6286       223081 :   WB (lang->has_new);
    6287       223081 :   WB (lang->has_array_new);
    6288              : 
    6289       223081 :   WB ((lang->gets_delete >> 0) & 1);
    6290       223081 :   WB ((lang->gets_delete >> 1) & 1);
    6291       223081 :   WB (lang->interface_only);
    6292       223081 :   WB (lang->interface_unknown);
    6293       223081 :   WB (lang->contains_empty_class_p);
    6294       223081 :   WB (lang->anon_aggr);
    6295       223081 :   WB (lang->non_zero_init);
    6296       223081 :   WB (lang->empty_p);
    6297              : 
    6298       223081 :   WB (lang->vec_new_uses_cookie);
    6299       223081 :   WB (lang->declared_class);
    6300       223081 :   WB (lang->diamond_shaped);
    6301       223081 :   WB (lang->repeated_base);
    6302       223081 :   gcc_checking_assert (!lang->being_defined);
    6303              :   // lang->debug_requested
    6304       223081 :   WB (lang->fields_readonly);
    6305       223081 :   WB (lang->ptrmemfunc_flag);
    6306              : 
    6307       223081 :   WB (lang->lazy_default_ctor);
    6308       223081 :   WB (lang->lazy_copy_ctor);
    6309       223081 :   WB (lang->lazy_copy_assign);
    6310       223081 :   WB (lang->lazy_destructor);
    6311       223081 :   WB (lang->has_const_copy_ctor);
    6312       223081 :   WB (lang->has_complex_copy_ctor);
    6313       223081 :   WB (lang->has_complex_copy_assign);
    6314       223081 :   WB (lang->non_aggregate);
    6315              : 
    6316       223081 :   WB (lang->has_complex_dflt);
    6317       223081 :   WB (lang->has_list_ctor);
    6318       223081 :   WB (lang->non_std_layout);
    6319       223081 :   WB (lang->is_literal);
    6320       223081 :   WB (lang->lazy_move_ctor);
    6321       223081 :   WB (lang->lazy_move_assign);
    6322       223081 :   WB (lang->has_complex_move_ctor);
    6323       223081 :   WB (lang->has_complex_move_assign);
    6324              : 
    6325       223081 :   WB (lang->has_constexpr_ctor);
    6326       223081 :   WB (lang->unique_obj_representations);
    6327       223081 :   WB (lang->unique_obj_representations_set);
    6328       223081 :   gcc_checking_assert (!lang->erroneous);
    6329       223081 :   WB (lang->non_pod_aggregate);
    6330       223081 :   WB (lang->non_aggregate_pod);
    6331              : #undef WB
    6332       223081 : }
    6333              : 
    6334              : bool
    6335       156247 : trees_in::lang_type_bools (tree t, bits_in& bits)
    6336              : {
    6337              : #define RB(X) ((X) = bits.b ())
    6338       156247 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6339              : 
    6340       156247 :   bits.bflush ();
    6341       156247 :   RB (lang->has_type_conversion);
    6342       156247 :   RB (lang->has_copy_ctor);
    6343       156247 :   RB (lang->has_default_ctor);
    6344       156247 :   RB (lang->const_needs_init);
    6345       156247 :   RB (lang->ref_needs_init);
    6346       156247 :   RB (lang->has_const_copy_assign);
    6347       156247 :   unsigned v;
    6348       156247 :   v = bits.b () << 0;
    6349       156247 :   v |= bits.b () << 1;
    6350       156247 :   lang->use_template = v;
    6351              : 
    6352       156247 :   RB (lang->has_mutable);
    6353       156247 :   RB (lang->com_interface);
    6354       156247 :   RB (lang->non_pod_class);
    6355       156247 :   RB (lang->nearly_empty_p);
    6356       156247 :   RB (lang->user_align);
    6357       156247 :   RB (lang->has_copy_assign);
    6358       156247 :   RB (lang->has_new);
    6359       156247 :   RB (lang->has_array_new);
    6360              : 
    6361       156247 :   v = bits.b () << 0;
    6362       156247 :   v |= bits.b () << 1;
    6363       156247 :   lang->gets_delete = v;
    6364       156247 :   RB (lang->interface_only);
    6365       156247 :   RB (lang->interface_unknown);
    6366       156247 :   RB (lang->contains_empty_class_p);
    6367       156247 :   RB (lang->anon_aggr);
    6368       156247 :   RB (lang->non_zero_init);
    6369       156247 :   RB (lang->empty_p);
    6370              : 
    6371       156247 :   RB (lang->vec_new_uses_cookie);
    6372       156247 :   RB (lang->declared_class);
    6373       156247 :   RB (lang->diamond_shaped);
    6374       156247 :   RB (lang->repeated_base);
    6375       156247 :   gcc_checking_assert (!lang->being_defined);
    6376       156247 :   gcc_checking_assert (!lang->debug_requested);
    6377       156247 :   RB (lang->fields_readonly);
    6378       156247 :   RB (lang->ptrmemfunc_flag);
    6379              : 
    6380       156247 :   RB (lang->lazy_default_ctor);
    6381       156247 :   RB (lang->lazy_copy_ctor);
    6382       156247 :   RB (lang->lazy_copy_assign);
    6383       156247 :   RB (lang->lazy_destructor);
    6384       156247 :   RB (lang->has_const_copy_ctor);
    6385       156247 :   RB (lang->has_complex_copy_ctor);
    6386       156247 :   RB (lang->has_complex_copy_assign);
    6387       156247 :   RB (lang->non_aggregate);
    6388              : 
    6389       156247 :   RB (lang->has_complex_dflt);
    6390       156247 :   RB (lang->has_list_ctor);
    6391       156247 :   RB (lang->non_std_layout);
    6392       156247 :   RB (lang->is_literal);
    6393       156247 :   RB (lang->lazy_move_ctor);
    6394       156247 :   RB (lang->lazy_move_assign);
    6395       156247 :   RB (lang->has_complex_move_ctor);
    6396       156247 :   RB (lang->has_complex_move_assign);
    6397              : 
    6398       156247 :   RB (lang->has_constexpr_ctor);
    6399       156247 :   RB (lang->unique_obj_representations);
    6400       156247 :   RB (lang->unique_obj_representations_set);
    6401       156247 :   gcc_checking_assert (!lang->erroneous);
    6402       156247 :   RB (lang->non_pod_aggregate);
    6403       156247 :   RB (lang->non_aggregate_pod);
    6404              : #undef RB
    6405       156247 :   return !get_overrun ();
    6406              : }
    6407              : 
    6408              : /* Read & write the core values and pointers.  */
    6409              : 
    6410              : void
    6411     55880551 : trees_out::core_vals (tree t)
    6412              : {
    6413              : #define WU(X) (u (X))
    6414              : #define WT(X) (tree_node (X))
    6415     55880551 :   tree_code code = TREE_CODE (t);
    6416              : 
    6417              :   /* First by shape of the tree.  */
    6418              : 
    6419     55880551 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    6420              :     {
    6421              :       /* Write this early, for better log information.  */
    6422     12227924 :       WT (t->decl_minimal.name);
    6423     12227924 :       if (!DECL_TEMPLATE_PARM_P (t))
    6424      9443973 :         WT (t->decl_minimal.context);
    6425              : 
    6426     12227924 :       if (state)
    6427     10062523 :         state->write_location (*this, t->decl_minimal.locus);
    6428              : 
    6429     12227924 :       if (streaming_p ())
    6430      4660104 :         if (has_warning_spec (t))
    6431          963 :           u (get_warning_spec (t));
    6432              :     }
    6433              : 
    6434     55880551 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    6435              :     {
    6436              :       /* The only types we write also have TYPE_NON_COMMON.  */
    6437      3015898 :       gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
    6438              : 
    6439              :       /* We only stream the main variant.  */
    6440      3015898 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    6441              : 
    6442              :       /* Stream the name & context first, for better log information  */
    6443      3015898 :       WT (t->type_common.name);
    6444      3015898 :       WT (t->type_common.context);
    6445              : 
    6446              :       /* By construction we want to make sure we have the canonical
    6447              :          and main variants already in the type table, so emit them
    6448              :          now.  */
    6449      3015898 :       WT (t->type_common.main_variant);
    6450              : 
    6451      3015898 :       tree canonical = t->type_common.canonical;
    6452      3015898 :       if (canonical && DECL_TEMPLATE_PARM_P (TYPE_NAME (t)))
    6453              :         /* We do not want to wander into different templates.
    6454              :            Reconstructed on stream in.  */
    6455              :         canonical = t;
    6456      3015898 :       WT (canonical);
    6457              : 
    6458              :       /* type_common.next_variant is internally manipulated.  */
    6459              :       /* type_common.pointer_to, type_common.reference_to.  */
    6460              : 
    6461      3015898 :       if (streaming_p ())
    6462              :         {
    6463       819926 :           WU (t->type_common.precision);
    6464       819926 :           WU (t->type_common.contains_placeholder_bits);
    6465       819926 :           WU (t->type_common.mode);
    6466       819926 :           WU (t->type_common.align);
    6467              :         }
    6468              : 
    6469      3015898 :       if (!RECORD_OR_UNION_CODE_P (code))
    6470              :         {
    6471      2506418 :           WT (t->type_common.size);
    6472      2506418 :           WT (t->type_common.size_unit);
    6473              :         }
    6474      3015898 :       WT (t->type_common.attributes);
    6475              : 
    6476      3015898 :       WT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    6477              :     }
    6478              : 
    6479     55880551 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6480              :     {
    6481     12227924 :       if (streaming_p ())
    6482              :         {
    6483      4660104 :           WU (t->decl_common.mode);
    6484      4660104 :           WU (t->decl_common.off_align);
    6485      4660104 :           WU (t->decl_common.align);
    6486              :         }
    6487              : 
    6488              :       /* For templates these hold instantiation (partial and/or
    6489              :          specialization) information.  */
    6490     12227924 :       if (code != TEMPLATE_DECL)
    6491              :         {
    6492     11303865 :           WT (t->decl_common.size);
    6493     11303865 :           WT (t->decl_common.size_unit);
    6494              :         }
    6495              : 
    6496     12227924 :       WT (t->decl_common.attributes);
    6497              :       // FIXME: Does this introduce cross-decl links?  For instance
    6498              :       // from instantiation to the template.  If so, we'll need more
    6499              :       // deduplication logic.  I think we'll need to walk the blocks
    6500              :       // of the owning function_decl's abstract origin in tandem, to
    6501              :       // generate the locating data needed?
    6502     12227924 :       WT (t->decl_common.abstract_origin);
    6503              :     }
    6504              : 
    6505     55880551 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6506              :     {
    6507      5788570 :       WT (t->decl_with_vis.assembler_name);
    6508      5788570 :       if (streaming_p ())
    6509      2195495 :         WU (t->decl_with_vis.visibility);
    6510              :     }
    6511              : 
    6512     55880551 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6513              :     {
    6514      3015898 :       if (code == ENUMERAL_TYPE)
    6515              :         {
    6516              :           /* These fields get set even for opaque enums that lack a
    6517              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    6518              :              We stream TYPE_VALUES as part of the definition.  */
    6519         9916 :           WT (t->type_non_common.maxval);
    6520         9916 :           WT (t->type_non_common.minval);
    6521              :         }
    6522              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    6523              :          things.  */
    6524      3005982 :       else if (!RECORD_OR_UNION_CODE_P (code))
    6525              :         {
    6526              :           // FIXME: These are from tpl_parm_value's 'type' writing.
    6527              :           // Perhaps it should just be doing them directly?
    6528      2496502 :           gcc_checking_assert (code == TEMPLATE_TYPE_PARM
    6529              :                                || code == TEMPLATE_TEMPLATE_PARM
    6530              :                                || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    6531      2496502 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6532      2496502 :           WT (t->type_non_common.values);
    6533      2496502 :           WT (t->type_non_common.maxval);
    6534      2496502 :           WT (t->type_non_common.minval);
    6535              :         }
    6536              : 
    6537      3015898 :       WT (t->type_non_common.lang_1);
    6538              :     }
    6539              : 
    6540     55880551 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6541              :     {
    6542     17079302 :       if (state)
    6543     16745164 :         state->write_location (*this, t->exp.locus);
    6544              : 
    6545     17079302 :       if (streaming_p ())
    6546      8296420 :         if (has_warning_spec (t))
    6547       657554 :           u (get_warning_spec (t));
    6548              : 
    6549     17079302 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6550     17079302 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6551     17079302 :                         : TREE_OPERAND_LENGTH (t));
    6552     17079302 :       unsigned ix = unsigned (vl);
    6553     47105379 :       for (; ix != limit; ix++)
    6554     30026077 :         WT (TREE_OPERAND (t, ix));
    6555              :     }
    6556     38801249 :   else if (code == REQUIRES_EXPR)
    6557              :     {
    6558        24631 :       if (state)
    6559        20771 :         state->write_location (*this, REQUIRES_EXPR_LOCATION (t));
    6560              : 
    6561        24631 :       if (streaming_p ())
    6562        10129 :         if (has_warning_spec (t))
    6563            0 :           u (get_warning_spec (t));
    6564              : 
    6565        24631 :       chained_decls (REQUIRES_EXPR_PARMS (t));
    6566        24631 :       WT (REQUIRES_EXPR_REQS (t));
    6567        24631 :       WT (REQUIRES_EXPR_EXTRA_ARGS (t));
    6568              :     }
    6569              :   else
    6570              :     /* The CODE_CONTAINS tables were inaccurate when I started.  */
    6571     38776618 :     gcc_checking_assert (TREE_CODE_CLASS (code) != tcc_expression
    6572              :                          && TREE_CODE_CLASS (code) != tcc_binary
    6573              :                          && TREE_CODE_CLASS (code) != tcc_unary
    6574              :                          && TREE_CODE_CLASS (code) != tcc_reference
    6575              :                          && TREE_CODE_CLASS (code) != tcc_comparison
    6576              :                          && TREE_CODE_CLASS (code) != tcc_statement
    6577              :                          && TREE_CODE_CLASS (code) != tcc_vl_exp);
    6578              : 
    6579              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    6580              :      correspondence. */
    6581     55880551 :   switch (code)
    6582              :     {
    6583              :     default:
    6584              :       break;
    6585              : 
    6586            0 :     case ARGUMENT_PACK_SELECT:  /* Transient during instantiation.  */
    6587            0 :     case DEFERRED_PARSE:        /* Expanded upon completion of
    6588              :                                    outermost class.  */
    6589            0 :     case IDENTIFIER_NODE:       /* Streamed specially.  */
    6590            0 :     case BINDING_VECTOR:                /* Only in namespace-scope symbol
    6591              :                                    table.  */
    6592            0 :     case SSA_NAME:
    6593            0 :     case TRANSLATION_UNIT_DECL: /* There is only one, it is a
    6594              :                                    global_tree.  */
    6595            0 :     case USERDEF_LITERAL:       /* Expanded during parsing.  */
    6596            0 :       gcc_unreachable (); /* Should never meet.  */
    6597              : 
    6598              :       /* Constants.  */
    6599           18 :     case COMPLEX_CST:
    6600           18 :       WT (TREE_REALPART (t));
    6601           18 :       WT (TREE_IMAGPART (t));
    6602           18 :       break;
    6603              : 
    6604            0 :     case FIXED_CST:
    6605            0 :       gcc_unreachable (); /* Not supported in C++.  */
    6606              : 
    6607      5099642 :     case INTEGER_CST:
    6608      5099642 :       if (streaming_p ())
    6609              :         {
    6610       865018 :           unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6611      1733343 :           for (unsigned ix = 0; ix != num; ix++)
    6612       868325 :             wu (TREE_INT_CST_ELT (t, ix));
    6613              :         }
    6614              :       break;
    6615              : 
    6616            0 :     case POLY_INT_CST:
    6617            0 :       if (streaming_p ())
    6618            0 :         for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    6619            0 :           WT (POLY_INT_CST_COEFF (t, ix));
    6620              :       break;
    6621              : 
    6622        50655 :     case REAL_CST:
    6623        50655 :       if (streaming_p ())
    6624        25278 :         buf (TREE_REAL_CST_PTR (t), sizeof (real_value));
    6625              :       break;
    6626              : 
    6627              :     case STRING_CST:
    6628              :       /* Streamed during start.  */
    6629              :       break;
    6630              : 
    6631           36 :     case RAW_DATA_CST:
    6632           36 :       if (RAW_DATA_OWNER (t) == NULL_TREE)
    6633              :         break; /* Streamed as STRING_CST during start.  */
    6634           24 :       WT (RAW_DATA_OWNER (t));
    6635           24 :       if (streaming_p ())
    6636              :         {
    6637           12 :           if (TREE_CODE (RAW_DATA_OWNER (t)) == RAW_DATA_CST)
    6638            6 :             z (RAW_DATA_POINTER (t) - RAW_DATA_POINTER (RAW_DATA_OWNER (t)));
    6639            6 :           else if (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST)
    6640            6 :             z (RAW_DATA_POINTER (t)
    6641            6 :                - TREE_STRING_POINTER (RAW_DATA_OWNER (t)));
    6642              :           else
    6643            0 :             gcc_unreachable ();
    6644              :         }
    6645              :       break;
    6646              : 
    6647           36 :     case VECTOR_CST:
    6648          102 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    6649           66 :         WT (VECTOR_CST_ENCODED_ELT (t, ix));
    6650              :       break;
    6651              : 
    6652              :       /* Decls.  */
    6653       688622 :     case VAR_DECL:
    6654       688622 :       if (DECL_CONTEXT (t)
    6655       688622 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    6656              :         {
    6657       140077 :           if (DECL_HAS_VALUE_EXPR_P (t))
    6658           18 :             WT (DECL_VALUE_EXPR (t));
    6659              :           break;
    6660              :         }
    6661              :       /* FALLTHROUGH  */
    6662              : 
    6663      5583997 :     case RESULT_DECL:
    6664      5583997 :     case PARM_DECL:
    6665      5583997 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6666        55996 :         WT (DECL_VALUE_EXPR (t));
    6667              :       /* FALLTHROUGH  */
    6668              : 
    6669      5819330 :     case CONST_DECL:
    6670      5819330 :     case IMPORTED_DECL:
    6671      5819330 :       WT (t->decl_common.initial);
    6672      5819330 :       break;
    6673              : 
    6674       170711 :     case FIELD_DECL:
    6675       170711 :       WT (t->field_decl.offset);
    6676       170711 :       WT (t->field_decl.bit_field_type);
    6677       170711 :       {
    6678       170711 :         auto ovr = make_temp_override (walking_bit_field_unit, true);
    6679       170711 :         WT (t->field_decl.qualifier); /* bitfield unit.  */
    6680       170711 :       }
    6681       170711 :       WT (t->field_decl.bit_offset);
    6682       170711 :       WT (t->field_decl.fcontext);
    6683       170711 :       WT (t->decl_common.initial);
    6684       170711 :       break;
    6685              : 
    6686        57483 :     case LABEL_DECL:
    6687        57483 :       if (streaming_p ())
    6688              :         {
    6689        28740 :           WU (t->label_decl.label_decl_uid);
    6690        28740 :           WU (t->label_decl.eh_landing_pad_nr);
    6691              :         }
    6692              :       break;
    6693              : 
    6694      1288736 :     case FUNCTION_DECL:
    6695      1288736 :       if (streaming_p ())
    6696              :         {
    6697              :           /* Builtins can be streamed by value when a header declares
    6698              :              them.  */
    6699       644261 :           WU (DECL_BUILT_IN_CLASS (t));
    6700       644261 :           if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
    6701        12503 :             WU (DECL_UNCHECKED_FUNCTION_CODE (t));
    6702              :         }
    6703              : 
    6704      1288736 :       WT (t->function_decl.personality);
    6705              :       /* Rather than streaming target/optimize nodes, we should reconstruct
    6706              :          them on stream-in from any attributes applied to the function.  */
    6707      1288736 :       if (streaming_p () && t->function_decl.function_specific_target)
    6708            0 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6709              :                     "%<target%> attribute currently unsupported in modules");
    6710      1288736 :       if (streaming_p () && t->function_decl.function_specific_optimization)
    6711            3 :         warning_at (DECL_SOURCE_LOCATION (t), 0,
    6712              :                     "%<optimize%> attribute currently unsupported in modules");
    6713      1288736 :       WT (t->function_decl.vindex);
    6714              : 
    6715      1288736 :       if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6716         8932 :         WT (lookup_explicit_specifier (t));
    6717              :       break;
    6718              : 
    6719       155293 :     case USING_DECL:
    6720              :       /* USING_DECL_DECLS  */
    6721       155293 :       WT (t->decl_common.initial);
    6722              :       /* FALLTHROUGH  */
    6723              : 
    6724      3810779 :     case TYPE_DECL:
    6725              :       /* USING_DECL: USING_DECL_SCOPE  */
    6726              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6727      3810779 :       WT (t->decl_non_common.result);
    6728      3810779 :       break;
    6729              : 
    6730              :       /* Miscellaneous common nodes.  */
    6731       853800 :     case BLOCK:
    6732       853800 :       if (state)
    6733              :         {
    6734       853800 :           state->write_location (*this, t->block.locus);
    6735       853800 :           state->write_location (*this, t->block.end_locus);
    6736              :         }
    6737              : 
    6738              :       /* DECL_LOCAL_DECL_P decls are first encountered here and
    6739              :          streamed by value.  */
    6740      1307678 :       for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
    6741              :         {
    6742       453878 :           if (VAR_OR_FUNCTION_DECL_P (decls)
    6743       453878 :               && DECL_LOCAL_DECL_P (decls))
    6744              :             {
    6745              :               /* Make sure this is the first encounter, and mark for
    6746              :                  walk-by-value.  */
    6747          322 :               gcc_checking_assert (!TREE_VISITED (decls)
    6748              :                                    && !DECL_TEMPLATE_INFO (decls));
    6749          322 :               mark_by_value (decls);
    6750              :             }
    6751       453878 :           tree_node (decls);
    6752              :         }
    6753       853800 :       tree_node (NULL_TREE);
    6754              : 
    6755              :       /* nonlocalized_vars is a middle-end thing.  */
    6756       853800 :       WT (t->block.subblocks);
    6757       853800 :       WT (t->block.supercontext);
    6758              :       // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
    6759       853800 :       WT (t->block.abstract_origin);
    6760              :       /* fragment_origin, fragment_chain are middle-end things.  */
    6761       853800 :       WT (t->block.chain);
    6762              :       /* nonlocalized_vars, block_num & die are middle endy/debug
    6763              :          things.  */
    6764       853800 :       break;
    6765              : 
    6766      1692983 :     case CALL_EXPR:
    6767      1692983 :       if (streaming_p ())
    6768       810616 :         WU (t->base.u.ifn);
    6769              :       break;
    6770              : 
    6771              :     case CONSTRUCTOR:
    6772              :       // This must be streamed /after/ we've streamed the type,
    6773              :       // because it can directly refer to elements of the type. Eg,
    6774              :       // FIELD_DECLs of a RECORD_TYPE.
    6775              :       break;
    6776              : 
    6777           36 :     case OMP_CLAUSE:
    6778           36 :       {
    6779              :         /* The ompcode is serialized in start.  */
    6780           36 :         if (streaming_p ())
    6781           18 :           WU (t->omp_clause.subcode.map_kind);
    6782           36 :         if (state)
    6783           36 :           state->write_location (*this, t->omp_clause.locus);
    6784              : 
    6785           36 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    6786          120 :         for (unsigned ix = 0; ix != len; ix++)
    6787           84 :           WT (t->omp_clause.ops[ix]);
    6788              :       }
    6789              :       break;
    6790              : 
    6791       699057 :     case STATEMENT_LIST:
    6792      2918419 :       for (tree stmt : tsi_range (t))
    6793      2219362 :         if (stmt)
    6794      2219362 :           WT (stmt);
    6795       699057 :       WT (NULL_TREE);
    6796       699057 :       break;
    6797              : 
    6798            0 :     case OPTIMIZATION_NODE:
    6799            0 :     case TARGET_OPTION_NODE:
    6800              :       // FIXME: Our representation for these two nodes is a cache of
    6801              :       // the resulting set of options.  Not a record of the options
    6802              :       // that got changed by a particular attribute or pragma.  Instead
    6803              :       // of recording that, we probably should just rebuild the options
    6804              :       // on stream-in from the function attributes.  This could introduce
    6805              :       // strangeness if the importer has some incompatible set of flags
    6806              :       // but we currently assume users "know what they're doing" in such
    6807              :       // a case anyway.
    6808            0 :       gcc_unreachable ();
    6809       287318 :       break;
    6810              : 
    6811       287318 :     case TREE_BINFO:
    6812       287318 :       {
    6813       287318 :         WT (t->binfo.common.chain);
    6814       287318 :         WT (t->binfo.offset);
    6815       287318 :         WT (t->binfo.inheritance);
    6816       287318 :         WT (t->binfo.vptr_field);
    6817              : 
    6818       287318 :         WT (t->binfo.vtable);
    6819       287318 :         WT (t->binfo.virtuals);
    6820       287318 :         WT (t->binfo.vtt_subvtt);
    6821       287318 :         WT (t->binfo.vtt_vptr);
    6822              : 
    6823       287318 :         tree_vec (BINFO_BASE_ACCESSES (t));
    6824       287318 :         unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6825       366818 :         for (unsigned ix = 0; ix != num; ix++)
    6826        79500 :           WT (BINFO_BASE_BINFO (t, ix));
    6827              :       }
    6828              :       break;
    6829              : 
    6830      4228398 :     case TREE_LIST:
    6831      4228398 :       WT (t->list.purpose);
    6832      4228398 :       WT (t->list.value);
    6833      4228398 :       WT (t->list.common.chain);
    6834      4228398 :       break;
    6835              : 
    6836      3616018 :     case TREE_VEC:
    6837     10041581 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6838      6425563 :         WT (TREE_VEC_ELT (t, ix));
    6839              :       /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN!  */
    6840      3616018 :       gcc_checking_assert (!t->type_common.common.chain
    6841              :                            || (TREE_CODE (t->type_common.common.chain)
    6842              :                                == INTEGER_CST));
    6843      3616018 :       WT (t->type_common.common.chain);
    6844      3616018 :       break;
    6845              : 
    6846              :       /* C++-specific nodes ...  */
    6847       295434 :     case BASELINK:
    6848       295434 :       WT (((lang_tree_node *)t)->baselink.binfo);
    6849       295434 :       WT (((lang_tree_node *)t)->baselink.functions);
    6850       295434 :       WT (((lang_tree_node *)t)->baselink.access_binfo);
    6851       295434 :       WT (((lang_tree_node *)t)->baselink.common.chain);
    6852       295434 :       break;
    6853              : 
    6854       133394 :     case CONSTRAINT_INFO:
    6855       133394 :       WT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6856       133394 :       WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6857       133394 :       WT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6858       133394 :       break;
    6859              : 
    6860        21188 :     case DEFERRED_NOEXCEPT:
    6861        21188 :       WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    6862        21188 :       WT (((lang_tree_node *)t)->deferred_noexcept.args);
    6863        21188 :       break;
    6864              : 
    6865        21440 :     case LAMBDA_EXPR:
    6866        21440 :       WT (((lang_tree_node *)t)->lambda_expression.capture_list);
    6867        21440 :       WT (((lang_tree_node *)t)->lambda_expression.this_capture);
    6868        21440 :       WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    6869        21440 :       WT (((lang_tree_node *)t)->lambda_expression.regen_info);
    6870        21440 :       WT (((lang_tree_node *)t)->lambda_expression.extra_args);
    6871              :       /* pending_proxies is a parse-time thing.  */
    6872        21440 :       gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
    6873        21440 :       if (state)
    6874        21437 :         state->write_location
    6875        21437 :           (*this, ((lang_tree_node *)t)->lambda_expression.locus);
    6876        21440 :       if (streaming_p ())
    6877              :         {
    6878         7359 :           WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    6879         7359 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    6880         7359 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    6881              :         }
    6882              :       break;
    6883              : 
    6884      3108621 :     case OVERLOAD:
    6885      3108621 :       WT (((lang_tree_node *)t)->overload.function);
    6886      3108621 :       WT (t->common.chain);
    6887      3108621 :       break;
    6888              : 
    6889           33 :     case PTRMEM_CST:
    6890           33 :       WT (((lang_tree_node *)t)->ptrmem.member);
    6891           33 :       if (state)
    6892           24 :         state->write_location (*this, ((lang_tree_node *)t)->ptrmem.locus);
    6893              :       break;
    6894              : 
    6895        23720 :     case STATIC_ASSERT:
    6896        23720 :       WT (((lang_tree_node *)t)->static_assertion.condition);
    6897        23720 :       WT (((lang_tree_node *)t)->static_assertion.message);
    6898        23720 :       if (state)
    6899        23720 :         state->write_location
    6900        23720 :           (*this, ((lang_tree_node *)t)->static_assertion.location);
    6901              :       break;
    6902              : 
    6903       924059 :     case TEMPLATE_DECL:
    6904              :       /* Streamed with the template_decl node itself.  */
    6905       924059 :       gcc_checking_assert
    6906              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
    6907       924059 :       gcc_checking_assert
    6908              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
    6909       924059 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6910        18130 :         WT (DECL_CHAIN (t));
    6911              :       break;
    6912              : 
    6913      2096753 :     case TEMPLATE_INFO:
    6914      2096753 :       {
    6915      2096753 :         WT (((lang_tree_node *)t)->template_info.tmpl);
    6916      2096753 :         WT (((lang_tree_node *)t)->template_info.args);
    6917      2096753 :         WT (((lang_tree_node *)t)->template_info.partial);
    6918              : 
    6919      2096753 :         const auto *ac = (((lang_tree_node *)t)
    6920              :                           ->template_info.deferred_access_checks);
    6921      2096753 :         unsigned len = vec_safe_length (ac);
    6922      2096753 :         if (streaming_p ())
    6923      1046189 :           u (len);
    6924      2096753 :         if (len)
    6925              :           {
    6926            0 :             for (unsigned ix = 0; ix != len; ix++)
    6927              :               {
    6928            0 :                 const auto &m = (*ac)[ix];
    6929            0 :                 WT (m.binfo);
    6930            0 :                 WT (m.decl);
    6931            0 :                 WT (m.diag_decl);
    6932            0 :                 if (state)
    6933            0 :                   state->write_location (*this, m.loc);
    6934              :               }
    6935              :           }
    6936              :       }
    6937              :       break;
    6938              : 
    6939      2682237 :     case TEMPLATE_PARM_INDEX:
    6940      2682237 :       if (streaming_p ())
    6941              :         {
    6942       594909 :           WU (((lang_tree_node *)t)->tpi.index);
    6943       594909 :           WU (((lang_tree_node *)t)->tpi.level);
    6944       594909 :           WU (((lang_tree_node *)t)->tpi.orig_level);
    6945              :         }
    6946      2682237 :       WT (((lang_tree_node *)t)->tpi.decl);
    6947              :       /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
    6948              :          cache, do not stream.  */
    6949      2682237 :       break;
    6950              : 
    6951        31798 :     case TRAIT_EXPR:
    6952        31798 :       WT (((lang_tree_node *)t)->trait_expression.type1);
    6953        31798 :       WT (((lang_tree_node *)t)->trait_expression.type2);
    6954        31798 :       if (state)
    6955        25090 :         state->write_location
    6956        25090 :           (*this, ((lang_tree_node *)t)->trait_expression.locus);
    6957        31798 :       if (streaming_p ())
    6958        12369 :         WU (((lang_tree_node *)t)->trait_expression.kind);
    6959              :       break;
    6960              : 
    6961            4 :     case TU_LOCAL_ENTITY:
    6962            4 :       WT (((lang_tree_node *)t)->tu_local_entity.name);
    6963            4 :       if (state)
    6964            4 :         state->write_location
    6965            4 :           (*this, ((lang_tree_node *)t)->tu_local_entity.loc);
    6966              :       break;
    6967              :     }
    6968              : 
    6969     55880551 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    6970              :     {
    6971              :       /* We want to stream the type of a expression-like nodes /after/
    6972              :          we've streamed the operands.  The type often contains (bits
    6973              :          of the) types of the operands, and with things like decltype
    6974              :          and noexcept in play, we really want to stream the decls
    6975              :          defining the type before we try and stream the type on its
    6976              :          own.  Otherwise we can find ourselves trying to read in a
    6977              :          decl, when we're already partially reading in a component of
    6978              :          its type.  And that's bad.  */
    6979     52751692 :       tree type = t->typed.type;
    6980     52751692 :       unsigned prec = 0;
    6981              : 
    6982     52751692 :       switch (code)
    6983              :         {
    6984              :         default:
    6985              :           break;
    6986              : 
    6987              :         case TEMPLATE_DECL:
    6988              :           /* We fill in the template's type separately.  */
    6989     52751692 :           type = NULL_TREE;
    6990              :           break;
    6991              : 
    6992      3655486 :         case TYPE_DECL:
    6993      3655486 :           if (DECL_ORIGINAL_TYPE (t) && t == TYPE_NAME (type))
    6994              :             /* This is a typedef.  We set its type separately.  */
    6995              :             type = NULL_TREE;
    6996              :           break;
    6997              : 
    6998         9916 :         case ENUMERAL_TYPE:
    6999         9916 :           if (type && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    7000              :             {
    7001              :               /* Type is a restricted range integer type derived from the
    7002              :                  integer_types.  Find the right one.  */
    7003         5820 :               prec = TYPE_PRECISION (type);
    7004         5820 :               tree name = DECL_NAME (TYPE_NAME (type));
    7005              : 
    7006        76046 :               for (unsigned itk = itk_none; itk--;)
    7007        76046 :                 if (integer_types[itk]
    7008        76046 :                     && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
    7009              :                   {
    7010              :                     type = integer_types[itk];
    7011              :                     break;
    7012              :                   }
    7013         5820 :               gcc_assert (type != t->typed.type);
    7014              :             }
    7015              :           break;
    7016              :         }
    7017              : 
    7018     52751692 :       WT (type);
    7019     52751692 :       if (prec && streaming_p ())
    7020         2908 :         WU (prec);
    7021              :     }
    7022              : 
    7023     55880551 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7024              :     {
    7025       144572 :       unsigned len = vec_safe_length (t->constructor.elts);
    7026       144572 :       if (streaming_p ())
    7027        71280 :         WU (len);
    7028       144572 :       if (len)
    7029       511386 :         for (unsigned ix = 0; ix != len; ix++)
    7030              :           {
    7031       434594 :             const constructor_elt &elt = (*t->constructor.elts)[ix];
    7032              : 
    7033       434594 :             WT (elt.index);
    7034       434594 :             WT (elt.value);
    7035              :           }
    7036              :     }
    7037              : 
    7038              : #undef WT
    7039              : #undef WU
    7040     55880551 : }
    7041              : 
    7042              : // Streaming in a reference to a decl can cause that decl to be
    7043              : // TREE_USED, which is the mark_used behaviour we need most of the
    7044              : // time.  The trees_in::unused can be incremented to inhibit this,
    7045              : // which is at least needed for vtables.
    7046              : 
    7047              : bool
    7048     15717635 : trees_in::core_vals (tree t)
    7049              : {
    7050              : #define RU(X) ((X) = u ())
    7051              : #define RUC(T,X) ((X) = T (u ()))
    7052              : #define RT(X) ((X) = tree_node ())
    7053              : #define RTU(X) ((X) = tree_node (true))
    7054     15717635 :   tree_code code = TREE_CODE (t);
    7055              : 
    7056              :   /* First by tree shape.  */
    7057     15717635 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    7058              :     {
    7059      3357547 :       RT (t->decl_minimal.name);
    7060      3357547 :       if (!DECL_TEMPLATE_PARM_P (t))
    7061      2922491 :         RT (t->decl_minimal.context);
    7062              : 
    7063              :       /* Don't zap the locus just yet, we don't record it correctly
    7064              :          and thus lose all location information.  */
    7065      3357547 :       t->decl_minimal.locus = state->read_location (*this);
    7066      3357547 :       if (has_warning_spec (t))
    7067          654 :         put_warning_spec (t, u ());
    7068              :     }
    7069              : 
    7070     15717635 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    7071              :     {
    7072       570824 :       RT (t->type_common.name);
    7073       570824 :       RT (t->type_common.context);
    7074              : 
    7075       570824 :       RT (t->type_common.main_variant);
    7076       570824 :       RT (t->type_common.canonical);
    7077              : 
    7078              :       /* type_common.next_variant is internally manipulated.  */
    7079              :       /* type_common.pointer_to, type_common.reference_to.  */
    7080              : 
    7081       570824 :       RU (t->type_common.precision);
    7082       570824 :       RU (t->type_common.contains_placeholder_bits);
    7083       570824 :       RUC (machine_mode, t->type_common.mode);
    7084       570824 :       RU (t->type_common.align);
    7085              : 
    7086       570824 :       if (!RECORD_OR_UNION_CODE_P (code))
    7087              :         {
    7088       394199 :           RT (t->type_common.size);
    7089       394199 :           RT (t->type_common.size_unit);
    7090              :         }
    7091       570824 :       RT (t->type_common.attributes);
    7092              : 
    7093       570824 :       RT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    7094              :     }
    7095              : 
    7096     15717635 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    7097              :     {
    7098      3357547 :       RUC (machine_mode, t->decl_common.mode);
    7099      3357547 :       RU (t->decl_common.off_align);
    7100      3357547 :       RU (t->decl_common.align);
    7101              : 
    7102      3357547 :       if (code != TEMPLATE_DECL)
    7103              :         {
    7104      3021958 :           RT (t->decl_common.size);
    7105      3021958 :           RT (t->decl_common.size_unit);
    7106              :         }
    7107              : 
    7108      3357547 :       RT (t->decl_common.attributes);
    7109      3357547 :       RT (t->decl_common.abstract_origin);
    7110              :     }
    7111              : 
    7112     15717635 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    7113              :     {
    7114      1546885 :       RT (t->decl_with_vis.assembler_name);
    7115      1546885 :       RUC (symbol_visibility, t->decl_with_vis.visibility);
    7116              :     }
    7117              : 
    7118     15717635 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    7119              :     {
    7120       570824 :       if (code == ENUMERAL_TYPE)
    7121              :         {
    7122              :           /* These fields get set even for opaque enums that lack a
    7123              :              definition, so we stream them directly for each ENUMERAL_TYPE.
    7124              :              We stream TYPE_VALUES as part of the definition.  */
    7125         3266 :           RT (t->type_non_common.maxval);
    7126         3266 :           RT (t->type_non_common.minval);
    7127              :         }
    7128              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    7129              :          things.  */
    7130       567558 :       else if (!RECORD_OR_UNION_CODE_P (code))
    7131              :         {
    7132              :           /* This is not clobbering TYPE_CACHED_VALUES, because this
    7133              :              is a type that doesn't have any.  */
    7134       390933 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    7135       390933 :           RT (t->type_non_common.values);
    7136       390933 :           RT (t->type_non_common.maxval);
    7137       390933 :           RT (t->type_non_common.minval);
    7138              :         }
    7139              : 
    7140       570824 :       RT (t->type_non_common.lang_1);
    7141              :     }
    7142              : 
    7143     15717635 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    7144              :     {
    7145      6214541 :       t->exp.locus = state->read_location (*this);
    7146      6214541 :       if (has_warning_spec (t))
    7147       490765 :         put_warning_spec (t, u ());
    7148              : 
    7149      6214541 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    7150      6214541 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    7151      6214541 :                         : TREE_OPERAND_LENGTH (t));
    7152      6214541 :       unsigned ix = unsigned (vl);
    7153     17008381 :       for (; ix != limit; ix++)
    7154     10793840 :         RTU (TREE_OPERAND (t, ix));
    7155              :     }
    7156      9503094 :   else if (code == REQUIRES_EXPR)
    7157              :     {
    7158         7767 :       REQUIRES_EXPR_LOCATION (t) = state->read_location (*this);
    7159         7767 :       if (has_warning_spec (t))
    7160            0 :         put_warning_spec (t, u ());
    7161              : 
    7162         7767 :       REQUIRES_EXPR_PARMS (t) = chained_decls ();
    7163         7767 :       RTU (REQUIRES_EXPR_REQS (t));
    7164         7767 :       RTU (REQUIRES_EXPR_EXTRA_ARGS (t));
    7165              :     }
    7166              : 
    7167              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    7168              :      correspondence. */
    7169     15717635 :   switch (code)
    7170              :     {
    7171              :     default:
    7172              :       break;
    7173              : 
    7174              :     case ARGUMENT_PACK_SELECT:
    7175              :     case DEFERRED_PARSE:
    7176              :     case IDENTIFIER_NODE:
    7177              :     case BINDING_VECTOR:
    7178              :     case SSA_NAME:
    7179              :     case TRANSLATION_UNIT_DECL:
    7180              :     case USERDEF_LITERAL:
    7181              :       return false; /* Should never meet.  */
    7182              : 
    7183              :       /* Constants.  */
    7184            9 :     case COMPLEX_CST:
    7185            9 :       RT (TREE_REALPART (t));
    7186            9 :       RT (TREE_IMAGPART (t));
    7187            9 :       break;
    7188              : 
    7189              :     case FIXED_CST:
    7190              :       /* Not supported in C++.  */
    7191              :       return false;
    7192              : 
    7193       599619 :     case INTEGER_CST:
    7194       599619 :       {
    7195       599619 :         unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    7196      1201445 :         for (unsigned ix = 0; ix != num; ix++)
    7197       601826 :           TREE_INT_CST_ELT (t, ix) = wu ();
    7198              :       }
    7199              :       break;
    7200              : 
    7201              :     case POLY_INT_CST:
    7202            0 :       for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    7203            0 :         RT (POLY_INT_CST_COEFF (t, ix));
    7204              :       break;
    7205              : 
    7206        16998 :     case REAL_CST:
    7207        16998 :       if (const void *bytes = buf (sizeof (real_value)))
    7208        16998 :         memcpy (TREE_REAL_CST_PTR (t), bytes, sizeof (real_value));
    7209              :       break;
    7210              : 
    7211              :     case STRING_CST:
    7212              :       /* Streamed during start.  */
    7213              :       break;
    7214              : 
    7215            6 :     case RAW_DATA_CST:
    7216            6 :       RT (RAW_DATA_OWNER (t));
    7217            6 :       gcc_assert (TREE_CODE (RAW_DATA_OWNER (t)) == STRING_CST
    7218              :                   && TREE_STRING_LENGTH (RAW_DATA_OWNER (t)));
    7219            6 :       RAW_DATA_POINTER (t) = TREE_STRING_POINTER (RAW_DATA_OWNER (t)) + z ();
    7220            6 :       break;
    7221              : 
    7222           24 :     case VECTOR_CST:
    7223           63 :       for (unsigned ix = vector_cst_encoded_nelts (t); ix--;)
    7224           39 :         RT (VECTOR_CST_ENCODED_ELT (t, ix));
    7225              :       break;
    7226              : 
    7227              :       /* Decls.  */
    7228       229933 :     case VAR_DECL:
    7229       229933 :       if (DECL_CONTEXT (t)
    7230       229933 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    7231              :         {
    7232        43018 :           if (DECL_HAS_VALUE_EXPR_P (t))
    7233              :             {
    7234            9 :               tree val = tree_node ();
    7235            9 :               SET_DECL_VALUE_EXPR (t, val);
    7236              :             }
    7237              :           break;
    7238              :         }
    7239              :       /* FALLTHROUGH  */
    7240              : 
    7241      1533599 :     case RESULT_DECL:
    7242      1533599 :     case PARM_DECL:
    7243      1533599 :       if (DECL_HAS_VALUE_EXPR_P (t))
    7244              :         {
    7245              :           /* The DECL_VALUE hash table is a cache, thus if we're
    7246              :              reading a duplicate (which we end up discarding), the
    7247              :              value expr will also be cleaned up at the next gc.  */
    7248        14656 :           tree val = tree_node ();
    7249        14656 :           SET_DECL_VALUE_EXPR (t, val);
    7250              :         }
    7251              :       /* FALLTHROUGH  */
    7252              : 
    7253      1579858 :     case CONST_DECL:
    7254      1579858 :     case IMPORTED_DECL:
    7255      1579858 :       RT (t->decl_common.initial);
    7256      1579858 :       break;
    7257              : 
    7258        57245 :     case FIELD_DECL:
    7259        57245 :       RT (t->field_decl.offset);
    7260        57245 :       RT (t->field_decl.bit_field_type);
    7261        57245 :       RT (t->field_decl.qualifier);
    7262        57245 :       RT (t->field_decl.bit_offset);
    7263        57245 :       RT (t->field_decl.fcontext);
    7264        57245 :       RT (t->decl_common.initial);
    7265        57245 :       break;
    7266              : 
    7267        18489 :     case LABEL_DECL:
    7268        18489 :       RU (t->label_decl.label_decl_uid);
    7269        18489 :       RU (t->label_decl.eh_landing_pad_nr);
    7270        18489 :       break;
    7271              : 
    7272       473467 :     case FUNCTION_DECL:
    7273       473467 :       {
    7274       473467 :         unsigned bltin = u ();
    7275       473467 :         t->function_decl.built_in_class = built_in_class (bltin);
    7276       473467 :         if (bltin != NOT_BUILT_IN)
    7277              :           {
    7278         9622 :             bltin = u ();
    7279         9622 :             DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
    7280              :           }
    7281              : 
    7282       473467 :         RT (t->function_decl.personality);
    7283              :         /* These properties are not streamed, and should be reconstructed
    7284              :            from any function attributes.  */
    7285              :         // t->function_decl.function_specific_target);
    7286              :         // t->function_decl.function_specific_optimization);
    7287       473467 :         RT (t->function_decl.vindex);
    7288              : 
    7289       473467 :         if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    7290              :           {
    7291         3892 :             tree spec;
    7292         3892 :             RT (spec);
    7293         3892 :             store_explicit_specifier (t, spec);
    7294              :           }
    7295              :       }
    7296              :       break;
    7297              : 
    7298        53146 :     case USING_DECL:
    7299              :       /* USING_DECL_DECLS  */
    7300        53146 :       RT (t->decl_common.initial);
    7301              :       /* FALLTHROUGH  */
    7302              : 
    7303       843308 :     case TYPE_DECL:
    7304              :       /* USING_DECL: USING_DECL_SCOPE  */
    7305              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    7306       843308 :       RT (t->decl_non_common.result);
    7307       843308 :       break;
    7308              : 
    7309              :       /* Miscellaneous common nodes.  */
    7310       318769 :     case BLOCK:
    7311       318769 :       t->block.locus = state->read_location (*this);
    7312       318769 :       t->block.end_locus = state->read_location (*this);
    7313              : 
    7314       318769 :       for (tree *chain = &t->block.vars;;)
    7315       480026 :         if (tree decl = tree_node ())
    7316              :           {
    7317              :             /* For a deduplicated local type or enumerator, chain the
    7318              :                duplicate decl instead of the canonical in-TU decl.  Seeing
    7319              :                a duplicate here means the containing function whose body
    7320              :                we're streaming in is a duplicate too, so we'll end up
    7321              :                discarding this BLOCK (and the rest of the duplicate function
    7322              :                body) anyway.  */
    7323       161257 :             decl = maybe_duplicate (decl);
    7324              : 
    7325       161257 :             if (!DECL_P (decl))
    7326              :               {
    7327            0 :                 set_overrun ();
    7328            0 :                 break;
    7329              :               }
    7330              : 
    7331              :             /* If DECL_CHAIN is already set then this was a backreference to a
    7332              :                local type or enumerator from a previous read (PR c++/114630).
    7333              :                Let's copy the node so we can keep building the chain for ODR
    7334              :                checking later.  */
    7335       161257 :             if (DECL_CHAIN (decl))
    7336              :               {
    7337           12 :                 gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    7338              :                                      && find_duplicate (DECL_CONTEXT (decl)));
    7339            6 :                 decl = copy_decl (decl);
    7340              :               }
    7341              : 
    7342       161257 :             *chain = decl;
    7343       161257 :             chain = &DECL_CHAIN (decl);
    7344              :           }
    7345              :         else
    7346       161257 :           break;
    7347              : 
    7348              :       /* nonlocalized_vars is middle-end.  */
    7349       318769 :       RT (t->block.subblocks);
    7350       318769 :       RT (t->block.supercontext);
    7351       318769 :       RT (t->block.abstract_origin);
    7352              :       /* fragment_origin, fragment_chain are middle-end.  */
    7353       318769 :       RT (t->block.chain);
    7354              :       /* nonlocalized_vars, block_num, die are middle endy/debug
    7355              :          things.  */
    7356       318769 :       break;
    7357              : 
    7358       600396 :     case CALL_EXPR:
    7359       600396 :       RUC (internal_fn, t->base.u.ifn);
    7360       600396 :       break;
    7361              : 
    7362              :     case CONSTRUCTOR:
    7363              :       // Streamed after the node's type.
    7364              :       break;
    7365              : 
    7366           18 :     case OMP_CLAUSE:
    7367           18 :       {
    7368           18 :         RU (t->omp_clause.subcode.map_kind);
    7369           18 :         t->omp_clause.locus = state->read_location (*this);
    7370              : 
    7371           18 :         unsigned len = omp_clause_num_ops[OMP_CLAUSE_CODE (t)];
    7372           66 :         for (unsigned ix = 0; ix != len; ix++)
    7373           48 :           RT (t->omp_clause.ops[ix]);
    7374              :       }
    7375              :       break;
    7376              : 
    7377       284699 :     case STATEMENT_LIST:
    7378       284699 :       {
    7379       284699 :         tree_stmt_iterator iter = tsi_start (t);
    7380      1467025 :         for (tree stmt; RT (stmt);)
    7381              :           {
    7382       897627 :             if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
    7383       170246 :                 && !MAY_HAVE_DEBUG_MARKER_STMTS)
    7384            0 :               continue;
    7385       897627 :             tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
    7386              :           }
    7387              :       }
    7388       284699 :       break;
    7389              : 
    7390            0 :     case OPTIMIZATION_NODE:
    7391            0 :     case TARGET_OPTION_NODE:
    7392              :       /* Not implemented, see trees_out::core_vals.  */
    7393            0 :       gcc_unreachable ();
    7394        92709 :       break;
    7395              : 
    7396        92709 :     case TREE_BINFO:
    7397        92709 :       RT (t->binfo.common.chain);
    7398        92709 :       RT (t->binfo.offset);
    7399        92709 :       RT (t->binfo.inheritance);
    7400        92709 :       RT (t->binfo.vptr_field);
    7401              : 
    7402              :       /* Do not mark the vtables as USED in the address expressions
    7403              :          here.  */
    7404        92709 :       unused++;
    7405        92709 :       RT (t->binfo.vtable);
    7406        92709 :       RT (t->binfo.virtuals);
    7407        92709 :       RT (t->binfo.vtt_subvtt);
    7408        92709 :       RT (t->binfo.vtt_vptr);
    7409        92709 :       unused--;
    7410              : 
    7411        92709 :       BINFO_BASE_ACCESSES (t) = tree_vec ();
    7412        92709 :       if (!get_overrun ())
    7413              :         {
    7414        92709 :           unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    7415       117959 :           for (unsigned ix = 0; ix != num; ix++)
    7416        25250 :             BINFO_BASE_APPEND (t, tree_node ());
    7417              :         }
    7418              :       break;
    7419              : 
    7420      1352374 :     case TREE_LIST:
    7421      1352374 :       RT (t->list.purpose);
    7422      1352374 :       RT (t->list.value);
    7423      1352374 :       RT (t->list.common.chain);
    7424      1352374 :       break;
    7425              : 
    7426       867288 :     case TREE_VEC:
    7427      2330944 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    7428      1463656 :         RT (TREE_VEC_ELT (t, ix));
    7429       867288 :       RT (t->type_common.common.chain);
    7430       867288 :       break;
    7431              : 
    7432              :       /* C++-specific nodes ...  */
    7433       110237 :     case BASELINK:
    7434       110237 :       RT (((lang_tree_node *)t)->baselink.binfo);
    7435       110237 :       RTU (((lang_tree_node *)t)->baselink.functions);
    7436       110237 :       RT (((lang_tree_node *)t)->baselink.access_binfo);
    7437       110237 :       RT (((lang_tree_node *)t)->baselink.common.chain);
    7438       110237 :       break;
    7439              : 
    7440        45753 :     case CONSTRAINT_INFO:
    7441        45753 :       RT (((lang_tree_node *)t)->constraint_info.template_reqs);
    7442        45753 :       RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    7443        45753 :       RT (((lang_tree_node *)t)->constraint_info.associated_constr);
    7444        45753 :       break;
    7445              : 
    7446         7775 :     case DEFERRED_NOEXCEPT:
    7447         7775 :       RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    7448         7775 :       RT (((lang_tree_node *)t)->deferred_noexcept.args);
    7449         7775 :       break;
    7450              : 
    7451         4224 :     case LAMBDA_EXPR:
    7452         4224 :       RT (((lang_tree_node *)t)->lambda_expression.capture_list);
    7453         4224 :       RT (((lang_tree_node *)t)->lambda_expression.this_capture);
    7454         4224 :       RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    7455         4224 :       RT (((lang_tree_node *)t)->lambda_expression.regen_info);
    7456         4224 :       RT (((lang_tree_node *)t)->lambda_expression.extra_args);
    7457              :       /* lambda_expression.pending_proxies is NULL  */
    7458         4224 :       ((lang_tree_node *)t)->lambda_expression.locus
    7459         4224 :         = state->read_location (*this);
    7460         4224 :       RUC (cp_lambda_default_capture_mode_type,
    7461              :            ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    7462         4224 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    7463         4224 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    7464         4224 :       break;
    7465              : 
    7466       577050 :     case OVERLOAD:
    7467       577050 :       RT (((lang_tree_node *)t)->overload.function);
    7468       577050 :       RT (t->common.chain);
    7469       577050 :       break;
    7470              : 
    7471           12 :     case PTRMEM_CST:
    7472           12 :       RTU (((lang_tree_node *)t)->ptrmem.member);
    7473           12 :       ((lang_tree_node *)t)->ptrmem.locus = state->read_location (*this);
    7474           12 :       break;
    7475              : 
    7476         8399 :     case STATIC_ASSERT:
    7477         8399 :       RT (((lang_tree_node *)t)->static_assertion.condition);
    7478         8399 :       RT (((lang_tree_node *)t)->static_assertion.message);
    7479         8399 :       ((lang_tree_node *)t)->static_assertion.location
    7480         8399 :         = state->read_location (*this);
    7481         8399 :       break;
    7482              : 
    7483       335589 :     case TEMPLATE_DECL:
    7484              :       /* Streamed when reading the raw template decl itself.  */
    7485       335589 :       gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
    7486       335589 :       gcc_assert (((lang_tree_node *)t)->template_decl.result);
    7487       335589 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    7488         7270 :         RT (DECL_CHAIN (t));
    7489              :       break;
    7490              : 
    7491       750084 :     case TEMPLATE_INFO:
    7492       750084 :       RT (((lang_tree_node *)t)->template_info.tmpl);
    7493       750084 :       RT (((lang_tree_node *)t)->template_info.args);
    7494       750084 :       RT (((lang_tree_node *)t)->template_info.partial);
    7495       750084 :       if (unsigned len = u ())
    7496              :         {
    7497            0 :           auto &ac = (((lang_tree_node *)t)
    7498              :                       ->template_info.deferred_access_checks);
    7499            0 :           vec_alloc (ac, len);
    7500            0 :           for (unsigned ix = 0; ix != len; ix++)
    7501              :             {
    7502            0 :               deferred_access_check m;
    7503              : 
    7504            0 :               RT (m.binfo);
    7505            0 :               RT (m.decl);
    7506            0 :               RT (m.diag_decl);
    7507            0 :               m.loc = state->read_location (*this);
    7508            0 :               ac->quick_push (m);
    7509              :             }
    7510              :         }
    7511              :       break;
    7512              : 
    7513       416734 :     case TEMPLATE_PARM_INDEX:
    7514       416734 :       RU (((lang_tree_node *)t)->tpi.index);
    7515       416734 :       RU (((lang_tree_node *)t)->tpi.level);
    7516       416734 :       RU (((lang_tree_node *)t)->tpi.orig_level);
    7517       416734 :       RT (((lang_tree_node *)t)->tpi.decl);
    7518       416734 :       break;
    7519              : 
    7520         8706 :     case TRAIT_EXPR:
    7521         8706 :       RT (((lang_tree_node *)t)->trait_expression.type1);
    7522         8706 :       RT (((lang_tree_node *)t)->trait_expression.type2);
    7523         8706 :       ((lang_tree_node *)t)->trait_expression.locus
    7524         8706 :         = state->read_location (*this);
    7525         8706 :       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
    7526         8706 :       break;
    7527              : 
    7528            2 :     case TU_LOCAL_ENTITY:
    7529            2 :       RT (((lang_tree_node *)t)->tu_local_entity.name);
    7530            2 :       ((lang_tree_node *)t)->tu_local_entity.loc
    7531            2 :         = state->read_location (*this);
    7532              :     }
    7533              : 
    7534     15717635 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    7535              :     {
    7536     14586853 :       tree type = tree_node ();
    7537              : 
    7538     14586853 :       if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    7539              :         {
    7540         1780 :           unsigned precision = u ();
    7541              : 
    7542         1780 :           type = build_distinct_type_copy (type);
    7543         1780 :           TYPE_PRECISION (type) = precision;
    7544         3560 :           set_min_and_max_values_for_integral_type (type, precision,
    7545         1780 :                                                     TYPE_SIGN (type));
    7546              :         }
    7547              : 
    7548     14586853 :       if (code != TEMPLATE_DECL)
    7549     14251264 :         t->typed.type = type;
    7550              :     }
    7551              : 
    7552     15717635 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7553        54444 :     if (unsigned len = u ())
    7554              :       {
    7555        32891 :         vec_alloc (t->constructor.elts, len);
    7556       242513 :         for (unsigned ix = 0; ix != len; ix++)
    7557              :           {
    7558       176731 :             constructor_elt elt;
    7559              : 
    7560       176731 :             RT (elt.index);
    7561       176731 :             RTU (elt.value);
    7562       176731 :             t->constructor.elts->quick_push (elt);
    7563              :           }
    7564              :       }
    7565              : 
    7566              : #undef RT
    7567              : #undef RM
    7568              : #undef RU
    7569     15717635 :   return !get_overrun ();
    7570              : }
    7571              : 
    7572              : void
    7573      6544931 : trees_out::lang_decl_vals (tree t)
    7574              : {
    7575      6544931 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7576              : #define WU(X) (u (X))
    7577              : #define WT(X) (tree_node (X))
    7578              :   /* Module index already written.  */
    7579      6544931 :   switch (lang->u.base.selector)
    7580              :     {
    7581            0 :     default:
    7582            0 :       gcc_unreachable ();
    7583              : 
    7584      1288736 :     case lds_fn:  /* lang_decl_fn.  */
    7585      1288736 :       if (streaming_p ())
    7586              :         {
    7587       644261 :           if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7588       111736 :             WU (lang->u.fn.ovl_op_code);
    7589              :         }
    7590              : 
    7591      1601724 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7592      1009704 :         WT (lang->u.fn.context);
    7593              : 
    7594      1288736 :       if (lang->u.fn.thunk_p)
    7595              :         {
    7596              :           /* The thunked-to function.  */
    7597         1848 :           WT (lang->u.fn.befriending_classes);
    7598         1848 :           if (streaming_p ())
    7599          924 :             wi (lang->u.fn.u5.fixed_offset);
    7600              :         }
    7601      1286888 :       else if (decl_tls_wrapper_p (t))
    7602              :         /* The wrapped variable.  */
    7603           18 :         WT (lang->u.fn.befriending_classes);
    7604              :       else
    7605      1286870 :         WT (lang->u.fn.u5.cloned_function);
    7606              : 
    7607      1288736 :       if (FNDECL_USED_AUTO (t))
    7608         5209 :         WT (lang->u.fn.u.saved_auto_return_type);
    7609              : 
    7610      1288736 :       goto lds_min;
    7611              : 
    7612         6836 :     case lds_decomp:  /* lang_decl_decomp.  */
    7613         6836 :       WT (lang->u.decomp.base);
    7614         6836 :       goto lds_min;
    7615              : 
    7616      3755960 :     case lds_min:  /* lang_decl_min.  */
    7617      3755960 :     lds_min:
    7618      3755960 :       WT (lang->u.min.template_info);
    7619      3755960 :       {
    7620      3755960 :         tree access = lang->u.min.access;
    7621              : 
    7622              :         /* DECL_ACCESS needs to be maintained by the definition of the
    7623              :            (derived) class that changes the access.  The other users
    7624              :            of DECL_ACCESS need to write it here.  */
    7625      1288736 :         if (!DECL_THUNK_P (t)
    7626      5042848 :             && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
    7627              :           access = NULL_TREE;
    7628              : 
    7629      3755960 :         WT (access);
    7630              :       }
    7631              :       /* A friend template specialisation stashes its owning class on its
    7632              :          DECL_CHAIN; we need to reconstruct this, but it needs to happen
    7633              :          after we stream the template_info so readers can know this is such
    7634              :          an entity.  */
    7635      3755960 :       if (decl_specialization_friend_p (t))
    7636          204 :         WT (t->common.chain);
    7637              :       break;
    7638              : 
    7639              :     case lds_ns:  /* lang_decl_ns.  */
    7640              :       break;
    7641              : 
    7642      2788622 :     case lds_parm:  /* lang_decl_parm.  */
    7643      2788622 :       if (streaming_p ())
    7644              :         {
    7645       948470 :           WU (lang->u.parm.level);
    7646       948470 :           WU (lang->u.parm.index);
    7647              :         }
    7648              :       break;
    7649              :     }
    7650              : #undef WU
    7651              : #undef WT
    7652      6544931 : }
    7653              : 
    7654              : bool
    7655      2103412 : trees_in::lang_decl_vals (tree t)
    7656              : {
    7657      2103412 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    7658              : #define RU(X) ((X) = u ())
    7659              : #define RT(X) ((X) = tree_node ())
    7660              : 
    7661              :   /* Module index already read.  */
    7662      2103412 :   switch (lang->u.base.selector)
    7663              :     {
    7664            0 :     default:
    7665            0 :       gcc_unreachable ();
    7666              : 
    7667       473467 :     case lds_fn:  /* lang_decl_fn.  */
    7668       473467 :       if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7669              :         {
    7670        81862 :           unsigned code = u ();
    7671              : 
    7672              :           /* Check consistency.  */
    7673        81862 :           if (code >= OVL_OP_MAX
    7674        81862 :               || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
    7675        81862 :                   .ovl_op_code) == OVL_OP_ERROR_MARK)
    7676            0 :             set_overrun ();
    7677              :           else
    7678        81862 :             lang->u.fn.ovl_op_code = code;
    7679              :         }
    7680              : 
    7681       585995 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7682       374711 :         RT (lang->u.fn.context);
    7683              : 
    7684       473467 :       if (lang->u.fn.thunk_p)
    7685              :         {
    7686          574 :           RT (lang->u.fn.befriending_classes);
    7687          574 :           lang->u.fn.u5.fixed_offset = wi ();
    7688              :         }
    7689       472893 :       else if (decl_tls_wrapper_p (t))
    7690           15 :         RT (lang->u.fn.befriending_classes);
    7691              :       else
    7692       472878 :         RT (lang->u.fn.u5.cloned_function);
    7693              : 
    7694       473467 :       if (FNDECL_USED_AUTO (t))
    7695         1483 :         RT (lang->u.fn.u.saved_auto_return_type);
    7696       473467 :       goto lds_min;
    7697              : 
    7698         3085 :     case lds_decomp:  /* lang_decl_decomp.  */
    7699         3085 :       RT (lang->u.decomp.base);
    7700         3085 :       goto lds_min;
    7701              : 
    7702      1408223 :     case lds_min:  /* lang_decl_min.  */
    7703      1408223 :     lds_min:
    7704      1408223 :       RT (lang->u.min.template_info);
    7705      1408223 :       RT (lang->u.min.access);
    7706      1408223 :       if (decl_specialization_friend_p (t))
    7707           76 :         RT (t->common.chain);
    7708              :       break;
    7709              : 
    7710              :     case lds_ns:  /* lang_decl_ns.  */
    7711              :       break;
    7712              : 
    7713       695034 :     case lds_parm:  /* lang_decl_parm.  */
    7714       695034 :       RU (lang->u.parm.level);
    7715       695034 :       RU (lang->u.parm.index);
    7716       695034 :       break;
    7717              :     }
    7718              : #undef RU
    7719              : #undef RT
    7720      2103412 :   return !get_overrun ();
    7721              : }
    7722              : 
    7723              : /* Most of the value contents of lang_type is streamed in
    7724              :    define_class.  */
    7725              : 
    7726              : void
    7727       446208 : trees_out::lang_type_vals (tree t)
    7728              : {
    7729       446208 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7730              : #define WU(X) (u (X))
    7731              : #define WT(X) (tree_node (X))
    7732       446208 :   if (streaming_p ())
    7733       223081 :     WU (lang->align);
    7734              : #undef WU
    7735              : #undef WT
    7736       446208 : }
    7737              : 
    7738              : bool
    7739       156247 : trees_in::lang_type_vals (tree t)
    7740              : {
    7741       156247 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7742              : #define RU(X) ((X) = u ())
    7743              : #define RT(X) ((X) = tree_node ())
    7744       156247 :   RU (lang->align);
    7745              : #undef RU
    7746              : #undef RT
    7747       156247 :   return !get_overrun ();
    7748              : }
    7749              : 
    7750              : /* Write out the bools of T, including information about any
    7751              :    LANG_SPECIFIC information.  Including allocation of any lang
    7752              :    specific object.  */
    7753              : 
    7754              : void
    7755     21550089 : trees_out::tree_node_bools (tree t)
    7756              : {
    7757     21550089 :   gcc_checking_assert (streaming_p ());
    7758              : 
    7759              :   /* We should never stream a namespace.  */
    7760     21550089 :   gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
    7761              :                        || DECL_NAMESPACE_ALIAS (t));
    7762              : 
    7763     21550089 :   bits_out bits = stream_bits ();
    7764     21550089 :   core_bools (t, bits);
    7765              : 
    7766     21550089 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7767              :     {
    7768      4660104 :     case tcc_declaration:
    7769      4660104 :       {
    7770      4660104 :         bool specific = DECL_LANG_SPECIFIC (t) != NULL;
    7771      4660104 :         bits.b (specific);
    7772      4660104 :         if (specific && VAR_P (t))
    7773       378088 :           bits.b (DECL_DECOMPOSITION_P (t));
    7774      2816260 :         if (specific)
    7775      2816260 :           lang_decl_bools (t, bits);
    7776              :       }
    7777              :       break;
    7778              : 
    7779       857769 :     case tcc_type:
    7780       857769 :       {
    7781       857769 :         bool specific = (TYPE_MAIN_VARIANT (t) == t
    7782       857769 :                          && TYPE_LANG_SPECIFIC (t) != NULL);
    7783       857769 :         gcc_assert (TYPE_LANG_SPECIFIC (t)
    7784              :                     == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
    7785              : 
    7786       857769 :         bits.b (specific);
    7787       857769 :         if (specific)
    7788       223081 :           lang_type_bools (t, bits);
    7789              :       }
    7790              :       break;
    7791              : 
    7792              :     default:
    7793              :       break;
    7794              :     }
    7795              : 
    7796     21550089 :   bits.bflush ();
    7797     21550089 : }
    7798              : 
    7799              : bool
    7800     15739731 : trees_in::tree_node_bools (tree t)
    7801              : {
    7802     15739731 :   bits_in bits = stream_bits ();
    7803     15739731 :   bool ok = core_bools (t, bits);
    7804              : 
    7805     15739731 :   if (ok)
    7806     15739731 :     switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7807              :       {
    7808      3357547 :       case tcc_declaration:
    7809      3357547 :         if (bits.b ())
    7810              :           {
    7811      2103412 :             bool decomp = VAR_P (t) && bits.b ();
    7812              : 
    7813      2103412 :             ok = maybe_add_lang_decl_raw (t, decomp);
    7814      2103412 :             if (ok)
    7815      2103412 :               ok = lang_decl_bools (t, bits);
    7816              :           }
    7817              :         break;
    7818              : 
    7819       592920 :       case tcc_type:
    7820       592920 :         if (bits.b ())
    7821              :           {
    7822       156247 :             ok = maybe_add_lang_type_raw (t);
    7823       156247 :             if (ok)
    7824       156247 :               ok = lang_type_bools (t, bits);
    7825              :           }
    7826              :         break;
    7827              : 
    7828              :       default:
    7829              :         break;
    7830              :       }
    7831              : 
    7832     15739731 :   bits.bflush ();
    7833     15739731 :   if (!ok || get_overrun ())
    7834            0 :     return false;
    7835              : 
    7836              :   return true;
    7837     15739731 : }
    7838              : 
    7839              : 
    7840              : /* Write out the lang-specific vals of node T.  */
    7841              : 
    7842              : void
    7843     55880551 : trees_out::lang_vals (tree t)
    7844              : {
    7845     55880551 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7846              :     {
    7847     12227924 :     case tcc_declaration:
    7848     12227924 :       if (DECL_LANG_SPECIFIC (t))
    7849      6544931 :         lang_decl_vals (t);
    7850              :       break;
    7851              : 
    7852      3015898 :     case tcc_type:
    7853      3015898 :       if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
    7854       446208 :         lang_type_vals (t);
    7855              :       break;
    7856              : 
    7857              :     default:
    7858              :       break;
    7859              :     }
    7860     55880551 : }
    7861              : 
    7862              : bool
    7863     15717635 : trees_in::lang_vals (tree t)
    7864              : {
    7865     15717635 :   bool ok = true;
    7866              : 
    7867     15717635 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7868              :     {
    7869      3357547 :     case tcc_declaration:
    7870      3357547 :       if (DECL_LANG_SPECIFIC (t))
    7871      2103412 :         ok = lang_decl_vals (t);
    7872              :       break;
    7873              : 
    7874       570824 :     case tcc_type:
    7875       570824 :       if (TYPE_LANG_SPECIFIC (t))
    7876       156247 :         ok = lang_type_vals (t);
    7877              :       else
    7878       414577 :         TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
    7879              :       break;
    7880              : 
    7881              :     default:
    7882              :       break;
    7883              :     }
    7884              : 
    7885     15717635 :   return ok;
    7886              : }
    7887              : 
    7888              : /* Write out the value fields of node T.  */
    7889              : 
    7890              : void
    7891     55880551 : trees_out::tree_node_vals (tree t)
    7892              : {
    7893     55880551 :   core_vals (t);
    7894     55880551 :   lang_vals (t);
    7895     55880551 : }
    7896              : 
    7897              : bool
    7898     15717635 : trees_in::tree_node_vals (tree t)
    7899              : {
    7900     15717635 :   bool ok = core_vals (t);
    7901     15717635 :   if (ok)
    7902     15717635 :     ok = lang_vals (t);
    7903              : 
    7904     15717635 :   return ok;
    7905              : }
    7906              : 
    7907              : 
    7908              : /* If T is a back reference, fixed reference or NULL, write out its
    7909              :    code and return WK_none.  Otherwise return WK_value if we must write
    7910              :    by value, or WK_normal otherwise.  */
    7911              : 
    7912              : walk_kind
    7913    365321674 : trees_out::ref_node (tree t)
    7914              : {
    7915    365321674 :   if (!t)
    7916              :     {
    7917    146514656 :       if (streaming_p ())
    7918              :         {
    7919              :           /* NULL_TREE -> tt_null.  */
    7920     55713563 :           null_count++;
    7921     55713563 :           i (tt_null);
    7922              :         }
    7923              :       return WK_none;
    7924              :     }
    7925              : 
    7926    218807018 :   if (!TREE_VISITED (t))
    7927              :     return WK_normal;
    7928              : 
    7929              :   /* An already-visited tree.  It must be in the map.  */
    7930    127563000 :   int val = get_tag (t);
    7931              : 
    7932    127563000 :   if (val == tag_value)
    7933              :     /* An entry we should walk into.  */
    7934              :     return WK_value;
    7935              : 
    7936    125620720 :   const char *kind;
    7937              : 
    7938    125620720 :   if (val <= tag_backref)
    7939              :     {
    7940              :       /* Back reference -> -ve number  */
    7941     95821759 :       if (streaming_p ())
    7942     45350418 :         i (val);
    7943              :       kind = "backref";
    7944              :     }
    7945     29798961 :   else if (val >= tag_fixed)
    7946              :     {
    7947              :       /* Fixed reference -> tt_fixed */
    7948     29798961 :       val -= tag_fixed;
    7949     29798961 :       if (streaming_p ())
    7950     10478481 :         i (tt_fixed), u (val);
    7951              :       kind = "fixed";
    7952              :     }
    7953              : 
    7954    125620720 :   if (streaming_p ())
    7955              :     {
    7956     55828899 :       back_ref_count++;
    7957     55828899 :       dump (dumper::TREE)
    7958        14184 :         && dump ("Wrote %s:%d %C:%N%S", kind, val, TREE_CODE (t), t, t);
    7959              :     }
    7960              :   return WK_none;
    7961              : }
    7962              : 
    7963              : tree
    7964     32969503 : trees_in::back_ref (int tag)
    7965              : {
    7966     32969503 :   tree res = NULL_TREE;
    7967              : 
    7968     32969503 :   if (tag < 0 && unsigned (~tag) < back_refs.length ())
    7969     32969503 :     res = back_refs[~tag];
    7970              : 
    7971     32969503 :   if (!res
    7972              :       /* Checking TREE_CODE is a dereference, so we know this is not a
    7973              :          wild pointer.  Checking the code provides evidence we've not
    7974              :          corrupted something.  */
    7975     32969503 :       || TREE_CODE (res) >= MAX_TREE_CODES)
    7976            0 :     set_overrun ();
    7977              :   else
    7978     32984043 :     dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
    7979              :                                  TREE_CODE (res), res, res);
    7980     32969503 :   return res;
    7981              : }
    7982              : 
    7983              : unsigned
    7984      5211241 : trees_out::add_indirect_tpl_parms (tree parms)
    7985              : {
    7986      5211241 :   unsigned len = 0;
    7987      9810800 :   for (; parms; parms = TREE_CHAIN (parms), len++)
    7988              :     {
    7989      5517111 :       if (TREE_VISITED (parms))
    7990              :         break;
    7991              : 
    7992      4599559 :       int tag = insert (parms);
    7993      4599559 :       if (streaming_p ())
    7994      4599886 :         dump (dumper::TREE)
    7995          153 :           && dump ("Indirect:%d template's parameter %u %C:%N",
    7996          153 :                    tag, len, TREE_CODE (parms), parms);
    7997              :     }
    7998              : 
    7999      5211241 :   if (streaming_p ())
    8000       864392 :     u (len);
    8001              : 
    8002      5211241 :   return len;
    8003              : }
    8004              : 
    8005              : unsigned
    8006       549437 : trees_in::add_indirect_tpl_parms (tree parms)
    8007              : {
    8008       549437 :   unsigned len = u ();
    8009      1528639 :   for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
    8010              :     {
    8011       429765 :       int tag = insert (parms);
    8012       430245 :       dump (dumper::TREE)
    8013          159 :         && dump ("Indirect:%d template's parameter %u %C:%N",
    8014          159 :                  tag, ix, TREE_CODE (parms), parms);
    8015              :     }
    8016              : 
    8017       549437 :   return len;
    8018              : }
    8019              : 
    8020              : /* We've just found DECL by name.  Insert nodes that come with it, but
    8021              :    cannot be found by name, so we'll not accidentally walk into them.  */
    8022              : 
    8023              : void
    8024     11481301 : trees_out::add_indirects (tree decl)
    8025              : {
    8026     11481301 :   unsigned count = 0;
    8027              : 
    8028              :   // FIXME:OPTIMIZATION We'll eventually want default fn parms of
    8029              :   // templates and perhaps default template parms too.  The former can
    8030              :   // be referenced from instantiations (as they are lazily
    8031              :   // instantiated).  Also (deferred?) exception specifications of
    8032              :   // templates.  See the note about PARM_DECLs in trees_out::decl_node.
    8033     11481301 :   tree inner = decl;
    8034     11481301 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8035              :     {
    8036      5211241 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8037              : 
    8038      5211241 :       inner = DECL_TEMPLATE_RESULT (decl);
    8039      5211241 :       int tag = insert (inner);
    8040      5211241 :       if (streaming_p ())
    8041       864392 :         dump (dumper::TREE)
    8042          219 :           && dump ("Indirect:%d template's result %C:%N",
    8043          219 :                    tag, TREE_CODE (inner), inner);
    8044      5211241 :       count++;
    8045              :     }
    8046              : 
    8047     11481301 :   if (TREE_CODE (inner) == TYPE_DECL)
    8048              :     {
    8049              :       /* Make sure the type is in the map too.  Otherwise we get
    8050              :          different RECORD_TYPEs for the same type, and things go
    8051              :          south.  */
    8052      6035070 :       tree type = TREE_TYPE (inner);
    8053      6035070 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8054              :                            || TYPE_NAME (type) == inner);
    8055      6035070 :       int tag = insert (type);
    8056      6035070 :       if (streaming_p ())
    8057       675767 :         dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
    8058          362 :                                      TREE_CODE (type), type);
    8059      6035070 :       count++;
    8060              :     }
    8061              : 
    8062     11481301 :   if (streaming_p ())
    8063              :     {
    8064      1752903 :       u (count);
    8065      1753432 :       dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8066              :     }
    8067     11481301 : }
    8068              : 
    8069              : bool
    8070      1106661 : trees_in::add_indirects (tree decl)
    8071              : {
    8072      1106661 :   unsigned count = 0;
    8073              : 
    8074      1106661 :   tree inner = decl;
    8075      1106661 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8076              :     {
    8077       549437 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8078              : 
    8079       549437 :       inner = DECL_TEMPLATE_RESULT (decl);
    8080       549437 :       int tag = insert (inner);
    8081       549437 :       dump (dumper::TREE)
    8082          228 :         && dump ("Indirect:%d templates's result %C:%N", tag,
    8083          228 :                  TREE_CODE (inner), inner);
    8084       549437 :       count++;
    8085              :     }
    8086              : 
    8087      1106661 :   if (TREE_CODE (inner) == TYPE_DECL)
    8088              :     {
    8089       427206 :       tree type = TREE_TYPE (inner);
    8090       427206 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8091              :                            || TYPE_NAME (type) == inner);
    8092       427206 :       int tag = insert (type);
    8093       427206 :       dump (dumper::TREE)
    8094          362 :         && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
    8095       427206 :       count++;
    8096              :     }
    8097              : 
    8098      1107266 :   dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8099      1106661 :   return count == u ();
    8100              : }
    8101              : 
    8102              : /* Stream a template parameter.  There are 4.5 kinds of parameter:
    8103              :    a) Template - TEMPLATE_DECL->TYPE_DECL->TEMPLATE_TEMPLATE_PARM
    8104              :         TEMPLATE_TYPE_PARM_INDEX TPI
    8105              :    b) Type - TYPE_DECL->TEMPLATE_TYPE_PARM TEMPLATE_TYPE_PARM_INDEX TPI
    8106              :    c.1) NonTYPE - PARM_DECL DECL_INITIAL TPI We meet this first
    8107              :    c.2) NonTYPE - CONST_DECL DECL_INITIAL Same TPI
    8108              :    d) BoundTemplate - TYPE_DECL->BOUND_TEMPLATE_TEMPLATE_PARM
    8109              :        TEMPLATE_TYPE_PARM_INDEX->TPI
    8110              :        TEMPLATE_TEMPLATE_PARM_INFO->TEMPLATE_INFO
    8111              : 
    8112              :    All of these point to a TEMPLATE_PARM_INDEX, and #B also has a TEMPLATE_INFO
    8113              : */
    8114              : 
    8115              : void
    8116      2783951 : trees_out::tpl_parm_value (tree parm)
    8117              : {
    8118      2783951 :   gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
    8119              : 
    8120      2783951 :   int parm_tag = insert (parm);
    8121      2783951 :   if (streaming_p ())
    8122              :     {
    8123       619998 :       i (tt_tpl_parm);
    8124       619998 :       dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
    8125          114 :                                    parm_tag, TREE_CODE (parm), parm);
    8126       619998 :       start (parm);
    8127       619998 :       tree_node_bools (parm);
    8128              :     }
    8129              : 
    8130      2783951 :   tree inner = parm;
    8131      2783951 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8132              :     {
    8133         7025 :       inner = DECL_TEMPLATE_RESULT (inner);
    8134         7025 :       int inner_tag = insert (inner);
    8135         7025 :       if (streaming_p ())
    8136              :         {
    8137         1851 :           dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
    8138            0 :                                        inner_tag, TREE_CODE (inner), inner);
    8139         1851 :           start (inner);
    8140         1851 :           tree_node_bools (inner);
    8141              :         }
    8142              :     }
    8143              : 
    8144      2783951 :   tree type = NULL_TREE;
    8145      2783951 :   if (TREE_CODE (inner) == TYPE_DECL)
    8146              :     {
    8147      2496502 :       type = TREE_TYPE (inner);
    8148      2496502 :       int type_tag = insert (type);
    8149      2496502 :       if (streaming_p ())
    8150              :         {
    8151       560265 :           dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
    8152          108 :                                        type_tag, TREE_CODE (type), type);
    8153       560265 :           start (type);
    8154       560265 :           tree_node_bools (type);
    8155              :         }
    8156              :     }
    8157              : 
    8158      2783951 :   if (inner != parm)
    8159              :     {
    8160              :       /* This is a template-template parameter.  */
    8161         7025 :       unsigned tpl_levels = 0;
    8162         7025 :       tpl_header (parm, &tpl_levels);
    8163         7025 :       tpl_parms_fini (parm, tpl_levels);
    8164              :     }
    8165              : 
    8166      2783951 :   tree_node_vals (parm);
    8167      2783951 :   if (inner != parm)
    8168         7025 :     tree_node_vals (inner);
    8169      2783951 :   if (type)
    8170              :     {
    8171      2496502 :       tree_node_vals (type);
    8172      2496502 :       if (DECL_NAME (inner) == auto_identifier
    8173      2496502 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8174              :         {
    8175              :           /* Placeholder auto.  */
    8176       116988 :           tree_node (DECL_INITIAL (inner));
    8177       116988 :           tree_node (DECL_SIZE_UNIT (inner));
    8178              :         }
    8179              :     }
    8180              : 
    8181      2783951 :   if (streaming_p ())
    8182       619998 :     dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
    8183          114 :                                  parm_tag, TREE_CODE (parm), parm);
    8184      2783951 : }
    8185              : 
    8186              : tree
    8187       435056 : trees_in::tpl_parm_value ()
    8188              : {
    8189       435056 :   tree parm = start ();
    8190       435056 :   if (!parm || !tree_node_bools (parm))
    8191              :     return NULL_TREE;
    8192              : 
    8193       435056 :   int parm_tag = insert (parm);
    8194       435056 :   dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
    8195          198 :                                parm_tag, TREE_CODE (parm), parm);
    8196              : 
    8197       435056 :   tree inner = parm;
    8198       435056 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8199              :     {
    8200         1229 :       inner = start ();
    8201         1229 :       if (!inner || !tree_node_bools (inner))
    8202              :         return NULL_TREE;
    8203         1229 :       int inner_tag = insert (inner);
    8204         1229 :       dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
    8205            0 :                                    inner_tag, TREE_CODE (inner), inner);
    8206         1229 :       DECL_TEMPLATE_RESULT (parm) = inner;
    8207              :     }
    8208              : 
    8209       435056 :   tree type = NULL_TREE;
    8210       435056 :   if (TREE_CODE (inner) == TYPE_DECL)
    8211              :     {
    8212       390933 :       type = start ();
    8213       390933 :       if (!type || !tree_node_bools (type))
    8214              :         return NULL_TREE;
    8215       390933 :       int type_tag = insert (type);
    8216       390933 :       dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
    8217          120 :                                    type_tag, TREE_CODE (type), type);
    8218              : 
    8219       390933 :       TREE_TYPE (inner) = TREE_TYPE (parm) = type;
    8220       390933 :       TYPE_NAME (type) = parm;
    8221              :     }
    8222              : 
    8223       435056 :   if (inner != parm)
    8224              :     {
    8225              :       /* A template template parameter.  */
    8226         1229 :       unsigned tpl_levels = 0;
    8227         1229 :       tpl_header (parm, &tpl_levels);
    8228         1229 :       tpl_parms_fini (parm, tpl_levels);
    8229              :     }
    8230              : 
    8231       435056 :   tree_node_vals (parm);
    8232       435056 :   if (inner != parm)
    8233         1229 :     tree_node_vals (inner);
    8234       435056 :   if (type)
    8235              :     {
    8236       390933 :       tree_node_vals (type);
    8237       390933 :       if (DECL_NAME (inner) == auto_identifier
    8238       390933 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8239              :         {
    8240              :           /* Placeholder auto.  */
    8241        33921 :           DECL_INITIAL (inner) = tree_node ();
    8242        33921 :           DECL_SIZE_UNIT (inner) = tree_node ();
    8243              :         }
    8244       390933 :       if (TYPE_CANONICAL (type))
    8245              :         {
    8246       390933 :           gcc_checking_assert (TYPE_CANONICAL (type) == type);
    8247       390933 :           TYPE_CANONICAL (type) = canonical_type_parameter (type);
    8248              :         }
    8249              :     }
    8250              : 
    8251       435056 :   dump (dumper::TREE) && dump ("Read template parm:%d %C:%N",
    8252          198 :                                parm_tag, TREE_CODE (parm), parm);
    8253              : 
    8254              :   return parm;
    8255              : }
    8256              : 
    8257              : void
    8258      1752278 : trees_out::install_entity (tree decl, depset *dep)
    8259              : {
    8260      1752278 :   gcc_checking_assert (streaming_p ());
    8261              : 
    8262              :   /* Write the entity index, so we can insert it as soon as we
    8263              :      know this is new.  */
    8264      1752278 :   u (dep ? dep->cluster + 1 : 0);
    8265      1752278 :   if (CHECKING_P && dep)
    8266              :     {
    8267              :       /* Add it to the entity map, such that we can tell it is
    8268              :          part of us.  */
    8269      1266178 :       bool existed;
    8270      1266178 :       unsigned *slot = &entity_map->get_or_insert
    8271      1266178 :         (DECL_UID (decl), &existed);
    8272      1266178 :       if (existed)
    8273              :         /* If it existed, it should match.  */
    8274         1639 :         gcc_checking_assert (decl == (*entity_ary)[*slot]);
    8275      1266178 :       *slot = ~dep->cluster;
    8276              :     }
    8277      1752278 : }
    8278              : 
    8279              : bool
    8280      1239215 : trees_in::install_entity (tree decl)
    8281              : {
    8282      1239215 :   unsigned entity_index = u ();
    8283      1239215 :   if (!entity_index)
    8284              :     return false;
    8285              : 
    8286       898099 :   if (entity_index > state->entity_num)
    8287              :     {
    8288            0 :       set_overrun ();
    8289            0 :       return false;
    8290              :     }
    8291              : 
    8292              :   /* Insert the real decl into the entity ary.  */
    8293       898099 :   unsigned ident = state->entity_lwm + entity_index - 1;
    8294       898099 :   (*entity_ary)[ident] = decl;
    8295              : 
    8296              :   /* And into the entity map, if it's not already there.  */
    8297       898099 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    8298       898099 :   if (!DECL_LANG_SPECIFIC (not_tmpl)
    8299      1698884 :       || !DECL_MODULE_ENTITY_P (not_tmpl))
    8300              :     {
    8301              :       /* We don't want to use retrofit_lang_decl directly so that we aren't
    8302              :          affected by the language state when we load in.  */
    8303       896957 :       if (!DECL_LANG_SPECIFIC (not_tmpl))
    8304              :         {
    8305        97314 :           maybe_add_lang_decl_raw (not_tmpl, false);
    8306        97314 :           SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
    8307              :         }
    8308       896957 :       DECL_MODULE_ENTITY_P (not_tmpl) = true;
    8309              : 
    8310              :       /* Insert into the entity hash (it cannot already be there).  */
    8311       896957 :       bool existed;
    8312       896957 :       unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
    8313       896957 :       gcc_checking_assert (!existed);
    8314       896957 :       slot = ident;
    8315              :     }
    8316              :   else
    8317              :     {
    8318         1142 :       unsigned *slot = entity_map->get (DECL_UID (decl));
    8319              : 
    8320              :       /* The entity must be in the entity map already.  However, DECL may
    8321              :          be the DECL_TEMPLATE_RESULT of an existing partial specialisation
    8322              :          if we matched it while streaming another instantiation; in this
    8323              :          case we already registered that TEMPLATE_DECL.  */
    8324         1142 :       if (!slot)
    8325              :         {
    8326            9 :           tree type = TREE_TYPE (decl);
    8327            9 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
    8328              :                                && CLASS_TYPE_P (type)
    8329              :                                && CLASSTYPE_TEMPLATE_SPECIALIZATION (type));
    8330            9 :           slot = entity_map->get (DECL_UID (CLASSTYPE_TI_TEMPLATE (type)));
    8331              :         }
    8332            9 :       gcc_checking_assert (slot);
    8333              : 
    8334         1142 :       if (state->is_partition ())
    8335              :         {
    8336              :           /* The decl is already in the entity map, but we see it again now
    8337              :              from a partition: we want to overwrite if the original decl
    8338              :              wasn't also from a (possibly different) partition.  Otherwise,
    8339              :              for things like template instantiations, make_dependency might
    8340              :              not realise that this is also provided from a partition and
    8341              :              should be considered part of this module (and thus always
    8342              :              emitted into the primary interface's CMI).  */
    8343          432 :           module_state *imp = import_entity_module (*slot);
    8344          432 :           if (!imp->is_partition ())
    8345          297 :             *slot = ident;
    8346              :         }
    8347              :     }
    8348              : 
    8349              :   return true;
    8350              : }
    8351              : 
    8352              : static bool has_definition (tree decl);
    8353              : 
    8354              : /* DECL is a decl node that must be written by value.  DEP is the
    8355              :    decl's depset.  */
    8356              : 
    8357              : void
    8358      4795206 : trees_out::decl_value (tree decl, depset *dep)
    8359              : {
    8360              :   /* We should not be writing clones or template parms.  */
    8361      4795206 :   gcc_checking_assert (DECL_P (decl)
    8362              :                        && !DECL_CLONED_FUNCTION_P (decl)
    8363              :                        && !DECL_TEMPLATE_PARM_P (decl));
    8364              : 
    8365              :   /* We should never be writing non-typedef ptrmemfuncs by value.  */
    8366      4795206 :   gcc_checking_assert (TREE_CODE (decl) != TYPE_DECL
    8367              :                        || DECL_ORIGINAL_TYPE (decl)
    8368              :                        || !TYPE_PTRMEMFUNC_P (TREE_TYPE (decl)));
    8369              : 
    8370              :   /* There's no need to walk any of the contents of a known TU-local entity,
    8371              :      since importers should never see any of it regardless.  But make sure we
    8372              :      at least note its location so importers can use it for diagnostics.  */
    8373      4795206 :   if (dep && dep->is_tu_local ())
    8374              :     {
    8375          416 :       gcc_checking_assert (is_initial_scan ());
    8376          416 :       insert (decl, WK_value);
    8377          416 :       state->note_location (DECL_SOURCE_LOCATION (decl));
    8378          416 :       return;
    8379              :     }
    8380              : 
    8381      4794790 :   merge_kind mk = get_merge_kind (decl, dep);
    8382              : 
    8383      4794790 :   if (CHECKING_P)
    8384              :     {
    8385              :       /* Never start in the middle of a template.  */
    8386      4794790 :       int use_tpl = -1;
    8387      4794790 :       if (tree ti = node_template_info (decl, use_tpl))
    8388      1673595 :         gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
    8389              :                              || TREE_CODE (TI_TEMPLATE (ti)) == IDENTIFIER_NODE
    8390              :                              || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
    8391              :                              || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
    8392              :                                  != decl));
    8393              :     }
    8394              : 
    8395      4794790 :   if (streaming_p ())
    8396              :     {
    8397              :       /* A new node -> tt_decl.  */
    8398      1752278 :       decl_val_count++;
    8399      1752278 :       i (tt_decl);
    8400      1752278 :       u (mk);
    8401      1752278 :       start (decl);
    8402              : 
    8403      1752278 :       if (mk != MK_unique)
    8404              :         {
    8405      1440076 :           bits_out bits = stream_bits ();
    8406      1440076 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8407              :             {
    8408              :               /* Tell the importer whether this is a global module entity,
    8409              :                  or a module entity.  */
    8410       358923 :               tree o = get_originating_module_decl (decl);
    8411       358923 :               bool is_attached = false;
    8412              : 
    8413       358923 :               tree not_tmpl = STRIP_TEMPLATE (o);
    8414       358923 :               if (DECL_LANG_SPECIFIC (not_tmpl)
    8415       573385 :                   && DECL_MODULE_ATTACH_P (not_tmpl))
    8416              :                 is_attached = true;
    8417              : 
    8418       358923 :               bits.b (is_attached);
    8419              :             }
    8420      1440076 :           bits.b (dep && dep->has_defn ());
    8421      1440076 :         }
    8422      1752278 :       tree_node_bools (decl);
    8423              :     }
    8424              : 
    8425      4794790 :   int tag = insert (decl, WK_value);
    8426      4794790 :   if (streaming_p ())
    8427      1752278 :     dump (dumper::TREE)
    8428          683 :       && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
    8429          683 :                TREE_CODE (decl), decl, decl);
    8430              : 
    8431      4794790 :   tree inner = decl;
    8432      4794790 :   int inner_tag = 0;
    8433      4794790 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8434              :     {
    8435      1375528 :       inner = DECL_TEMPLATE_RESULT (decl);
    8436      1375528 :       inner_tag = insert (inner, WK_value);
    8437              : 
    8438              :       /* On stream-in we assume that a template and its result will
    8439              :          have the same type.  */
    8440      1375528 :       gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
    8441              : 
    8442      1375528 :       if (streaming_p ())
    8443              :         {
    8444       458494 :           int code = TREE_CODE (inner);
    8445       458494 :           u (code);
    8446       458494 :           start (inner, true);
    8447       458494 :           tree_node_bools (inner);
    8448       458494 :           dump (dumper::TREE)
    8449          132 :             && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
    8450          132 :                      TREE_CODE (inner), inner, inner);
    8451              :         }
    8452              :     }
    8453              : 
    8454      4794790 :   tree type = NULL_TREE;
    8455      4794790 :   int type_tag = 0;
    8456      4794790 :   tree stub_decl = NULL_TREE;
    8457      4794790 :   int stub_tag = 0;
    8458      4794790 :   if (TREE_CODE (inner) == TYPE_DECL)
    8459              :     {
    8460      1707839 :       type = TREE_TYPE (inner);
    8461      1707839 :       bool has_type = (type == TYPE_MAIN_VARIANT (type)
    8462      1707839 :                        && TYPE_NAME (type) == inner);
    8463              : 
    8464      1707839 :       if (streaming_p ())
    8465       576892 :         u (has_type ? (unsigned) TREE_CODE (type) : 0);
    8466              : 
    8467      1707839 :       if (has_type)
    8468              :         {
    8469       779015 :           type_tag = insert (type, WK_value);
    8470       779015 :           if (streaming_p ())
    8471              :             {
    8472       259647 :               start (type, true);
    8473       259647 :               tree_node_bools (type);
    8474       259647 :               dump (dumper::TREE)
    8475          155 :                 && dump ("Writing type:%d %C:%N", type_tag,
    8476          155 :                          TREE_CODE (type), type);
    8477              :             }
    8478              : 
    8479       779015 :           stub_decl = TYPE_STUB_DECL (type);
    8480       779015 :           bool has_stub = inner != stub_decl;
    8481       779015 :           if (streaming_p ())
    8482       259647 :             u (has_stub ? (unsigned) TREE_CODE (stub_decl) : 0);
    8483       779015 :           if (has_stub)
    8484              :             {
    8485         2361 :               stub_tag = insert (stub_decl);
    8486         2361 :               if (streaming_p ())
    8487              :                 {
    8488          786 :                   start (stub_decl, true);
    8489          786 :                   tree_node_bools (stub_decl);
    8490          786 :                   dump (dumper::TREE)
    8491            0 :                     && dump ("Writing stub_decl:%d %C:%N", stub_tag,
    8492            0 :                              TREE_CODE (stub_decl), stub_decl);
    8493              :                 }
    8494              :             }
    8495              :           else
    8496              :             stub_decl = NULL_TREE;
    8497              :         }
    8498              :       else
    8499              :         /* Regular typedef.  */
    8500              :         type = NULL_TREE;
    8501              :     }
    8502              : 
    8503              :   /* Stream the container, we want it correctly canonicalized before
    8504              :      we start emitting keys for this decl.  */
    8505      4794790 :   tree container = decl_container (decl);
    8506      4794790 :   unsigned tpl_levels = 0;
    8507              : 
    8508              :   /* Also tell the importer whether this is a temploid friend attached
    8509              :      to a different module (which has implications for merging), so that
    8510              :      importers can reconstruct this information on stream-in.  */
    8511      4794790 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8512              :     {
    8513      3639760 :       tree* temploid_friend_slot = imported_temploid_friends->get (decl);
    8514      3639760 :       gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
    8515      3639760 :       tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
    8516              :     }
    8517              : 
    8518      4794790 :   {
    8519      4794790 :     auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
    8520      4794790 :     if (decl != inner)
    8521      1375528 :       tpl_header (decl, &tpl_levels);
    8522      4794790 :     if (TREE_CODE (inner) == FUNCTION_DECL)
    8523      1931921 :       fn_parms_init (inner);
    8524              : 
    8525              :     /* Now write out the merging information, and then really
    8526              :        install the tag values.  */
    8527      4794790 :     key_mergeable (tag, mk, decl, inner, container, dep);
    8528              : 
    8529      4794790 :     if (streaming_p ())
    8530      4798689 :       dump (dumper::MERGE)
    8531         1101 :         && dump ("Wrote:%d's %s merge key %C:%N", tag,
    8532         1101 :                  merge_kind_name[mk], TREE_CODE (decl), decl);
    8533      4794790 :   }
    8534              : 
    8535      4794790 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8536      4794790 :     fn_parms_fini (inner);
    8537              : 
    8538      4794790 :   if (!is_key_order ())
    8539      3526182 :     tree_node_vals (decl);
    8540              : 
    8541      4794790 :   if (inner_tag)
    8542              :     {
    8543      1375528 :       if (!is_key_order ())
    8544       917034 :         tree_node_vals (inner);
    8545      1375528 :       tpl_parms_fini (decl, tpl_levels);
    8546              :     }
    8547              : 
    8548      4794790 :   if (type && !is_key_order ())
    8549              :     {
    8550       519368 :       tree_node_vals (type);
    8551       519368 :       if (stub_decl)
    8552         1575 :         tree_node_vals (stub_decl);
    8553              :     }
    8554              : 
    8555      4794790 :   if (!is_key_order ())
    8556              :     {
    8557      3526182 :       if (mk & MK_template_mask
    8558      2459232 :           || mk == MK_partial
    8559      2459232 :           || mk == MK_friend_spec)
    8560              :         {
    8561        40716 :           if (mk != MK_partial)
    8562              :             {
    8563              :               // FIXME: We should make use of the merge-key by
    8564              :               // exposing it outside of key_mergeable.  But this gets
    8565              :               // the job done.
    8566      1066950 :               auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
    8567              : 
    8568      1066950 :               if (streaming_p ())
    8569       533475 :                 u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
    8570              :                                                        entry->tmpl, decl));
    8571      1066950 :               tree_node (entry->tmpl);
    8572      1066950 :               tree_node (entry->args);
    8573              :             }
    8574              :           else
    8575              :             {
    8576        40716 :               tree ti = get_template_info (inner);
    8577        40716 :               tree_node (TI_TEMPLATE (ti));
    8578        40716 :               tree_node (TI_ARGS (ti));
    8579              :             }
    8580              :         }
    8581      3526182 :       tree_node (get_constraints (decl));
    8582              :     }
    8583              : 
    8584      4794790 :   if (streaming_p ())
    8585              :     {
    8586              :       /* Do not stray outside this section.  */
    8587      1752278 :       gcc_checking_assert (!dep || dep->section == dep_hash->section);
    8588              : 
    8589              :       /* Write the entity index, so we can insert it as soon as we
    8590              :          know this is new.  */
    8591      1752278 :       install_entity (decl, dep);
    8592              :     }
    8593              : 
    8594      4794790 :   if (DECL_LANG_SPECIFIC (inner)
    8595      4008177 :       && DECL_MODULE_KEYED_DECLS_P (inner)
    8596      4796062 :       && streaming_p ())
    8597              :     {
    8598              :       /* Stream the keyed entities.  There may be keyed entities that we
    8599              :          choose not to stream, such as a lambda in a non-inline variable's
    8600              :          initializer, so don't build dependencies for them here; any deps
    8601              :          we need should be acquired during write_definition (possibly
    8602              :          indirectly).  */
    8603          417 :       auto *attach_vec = keyed_table->get (inner);
    8604          417 :       unsigned num = attach_vec->length ();
    8605          417 :       u (num);
    8606         1288 :       for (unsigned ix = 0; ix != num; ix++)
    8607              :         {
    8608          454 :           tree attached = (*attach_vec)[ix];
    8609          454 :           if (attached)
    8610              :             {
    8611          454 :               tree ti = TYPE_TEMPLATE_INFO (TREE_TYPE (attached));
    8612          454 :               if (!dep_hash->find_dependency (attached)
    8613          454 :                   && !(ti && dep_hash->find_dependency (TI_TEMPLATE (ti))))
    8614              :                 attached = NULL_TREE;
    8615              :             }
    8616              : 
    8617          454 :           tree_node (attached);
    8618          496 :           dump (dumper::MERGE)
    8619           30 :             && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
    8620              :         }
    8621              :     }
    8622              : 
    8623      4794790 :   bool is_typedef = false;
    8624      4794790 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8625              :     {
    8626       928824 :       tree t = TREE_TYPE (inner);
    8627       928824 :       unsigned tdef_flags = 0;
    8628       928824 :       if (DECL_ORIGINAL_TYPE (inner)
    8629       928824 :           && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8630              :         {
    8631       928824 :           tdef_flags |= 1;
    8632       928824 :           if (TYPE_STRUCTURAL_EQUALITY_P (t)
    8633       204818 :               && TYPE_DEPENDENT_P_VALID (t)
    8634      1124420 :               && TYPE_DEPENDENT_P (t))
    8635              :             tdef_flags |= 2;
    8636              :         }
    8637       928824 :       if (streaming_p ())
    8638       317245 :         u (tdef_flags);
    8639              : 
    8640       928824 :       if (tdef_flags & 1)
    8641              :         {
    8642              :           /* A typedef type.  */
    8643       928824 :           int type_tag = insert (t);
    8644       928824 :           if (streaming_p ())
    8645       317245 :             dump (dumper::TREE)
    8646          206 :               && dump ("Cloned:%d %s %C:%N", type_tag,
    8647              :                        tdef_flags & 2 ? "depalias" : "typedef",
    8648          206 :                        TREE_CODE (t), t);
    8649              : 
    8650              :           is_typedef = true;
    8651              :         }
    8652              :     }
    8653              : 
    8654      4794790 :   if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    8655              :     {
    8656       146051 :       bool cloned_p
    8657       146051 :         = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    8658       103886 :       bool needs_vtt_parm_p
    8659       103886 :         = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
    8660       103886 :       bool omit_inherited_parms_p
    8661       103886 :         = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
    8662        83217 :            && base_ctor_omit_inherited_parms (decl));
    8663       146051 :       unsigned flags = (int (cloned_p) << 0
    8664       146051 :                         | int (needs_vtt_parm_p) << 1
    8665       146051 :                         | int (omit_inherited_parms_p) << 2);
    8666       146051 :       u (flags);
    8667       146130 :       dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    8668              :                                    decl, cloned_p ? "" : "not ");
    8669              :     }
    8670              : 
    8671      4794790 :   if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    8672          174 :     u (decl_tls_model (decl));
    8673              : 
    8674      4794790 :   if (streaming_p ())
    8675      1752278 :     dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
    8676          683 :                                  TREE_CODE (decl), decl);
    8677              : 
    8678      4794790 :   if (NAMESPACE_SCOPE_P (inner))
    8679      2676166 :     gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
    8680              :                                   && DECL_LOCAL_DECL_P (inner)));
    8681      3456549 :   else if ((TREE_CODE (inner) == TYPE_DECL
    8682       923756 :             && !is_typedef
    8683       176354 :             && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8684      4203951 :            || TREE_CODE (inner) == FUNCTION_DECL)
    8685              :     {
    8686      1638987 :       bool write_defn = !dep && has_definition (decl);
    8687      1638987 :       if (streaming_p ())
    8688       546578 :         u (write_defn);
    8689      1638987 :       if (write_defn)
    8690            6 :         write_definition (decl);
    8691              :     }
    8692              : }
    8693              : 
    8694              : tree
    8695      1239215 : trees_in::decl_value ()
    8696              : {
    8697      1239215 :   int tag = 0;
    8698      1239215 :   bool is_attached = false;
    8699      1239215 :   bool has_defn = false;
    8700      1239215 :   unsigned mk_u = u ();
    8701      1239215 :   if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
    8702              :     {
    8703            0 :       set_overrun ();
    8704            0 :       return NULL_TREE;
    8705              :     }
    8706              : 
    8707      1239215 :   unsigned saved_unused = unused;
    8708      1239215 :   unused = 0;
    8709              : 
    8710      1239215 :   merge_kind mk = merge_kind (mk_u);
    8711              : 
    8712      1239215 :   tree decl = start ();
    8713      1239215 :   if (decl)
    8714              :     {
    8715      1239215 :       if (mk != MK_unique)
    8716              :         {
    8717      1019893 :           bits_in bits = stream_bits ();
    8718      1019893 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8719        87972 :             is_attached = bits.b ();
    8720              : 
    8721      1019893 :           has_defn = bits.b ();
    8722      1019893 :         }
    8723              : 
    8724      1239215 :       if (!tree_node_bools (decl))
    8725            0 :         decl = NULL_TREE;
    8726              :     }
    8727              : 
    8728              :   /* Insert into map.  */
    8729      1239215 :   tag = insert (decl);
    8730      1239215 :   if (decl)
    8731      1239215 :     dump (dumper::TREE)
    8732          964 :       && dump ("Reading:%d %C", tag, TREE_CODE (decl));
    8733              : 
    8734      1239215 :   tree inner = decl;
    8735      1239215 :   int inner_tag = 0;
    8736      1239215 :   if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
    8737              :     {
    8738       334360 :       int code = u ();
    8739       334360 :       inner = start (code);
    8740       334360 :       if (inner && tree_node_bools (inner))
    8741       334360 :         DECL_TEMPLATE_RESULT (decl) = inner;
    8742              :       else
    8743            0 :         decl = NULL_TREE;
    8744              : 
    8745       334360 :       inner_tag = insert (inner);
    8746       334360 :       if (decl)
    8747       334360 :         dump (dumper::TREE)
    8748          204 :           && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
    8749              :     }
    8750              : 
    8751      1239215 :   tree type = NULL_TREE;
    8752      1239215 :   int type_tag = 0;
    8753      1239215 :   tree stub_decl = NULL_TREE;
    8754      1239215 :   int stub_tag = 0;
    8755      1239215 :   if (decl && TREE_CODE (inner) == TYPE_DECL)
    8756              :     {
    8757       398774 :       if (unsigned type_code = u ())
    8758              :         {
    8759       179877 :           type = start (type_code);
    8760       179877 :           if (type && tree_node_bools (type))
    8761              :             {
    8762       179877 :               TREE_TYPE (inner) = type;
    8763       179877 :               TYPE_NAME (type) = inner;
    8764              :             }
    8765              :           else
    8766            0 :             decl = NULL_TREE;
    8767              : 
    8768       179877 :           type_tag = insert (type);
    8769       179877 :           if (decl)
    8770       179877 :             dump (dumper::TREE)
    8771          212 :               && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
    8772              : 
    8773       179877 :           if (unsigned stub_code = u ())
    8774              :             {
    8775          441 :               stub_decl = start (stub_code);
    8776          441 :               if (stub_decl && tree_node_bools (stub_decl))
    8777              :                 {
    8778          441 :                   TREE_TYPE (stub_decl) = type;
    8779          441 :                   TYPE_STUB_DECL (type) = stub_decl;
    8780              :                 }
    8781              :               else
    8782            0 :                 decl = NULL_TREE;
    8783              : 
    8784          441 :               stub_tag = insert (stub_decl);
    8785          441 :               if (decl)
    8786          441 :                 dump (dumper::TREE)
    8787            0 :                   && dump ("Reading stub_decl:%d %C", stub_tag,
    8788            0 :                            TREE_CODE (stub_decl));
    8789              :             }
    8790              :         }
    8791              :     }
    8792              : 
    8793      1239215 :   if (!decl)
    8794              :     {
    8795            0 :     bail:
    8796            0 :       if (inner_tag != 0)
    8797            0 :         back_refs[~inner_tag] = NULL_TREE;
    8798            0 :       if (type_tag != 0)
    8799            0 :         back_refs[~type_tag] = NULL_TREE;
    8800            0 :       if (stub_tag != 0)
    8801            0 :         back_refs[~stub_tag] = NULL_TREE;
    8802            0 :       if (tag != 0)
    8803            0 :         back_refs[~tag] = NULL_TREE;
    8804            0 :       set_overrun ();
    8805              :       /* Bail.  */
    8806            0 :       unused = saved_unused;
    8807            0 :       return NULL_TREE;
    8808              :     }
    8809              : 
    8810              :   /* Read the container, to ensure it's already been streamed in.  */
    8811      1239215 :   tree container = decl_container ();
    8812      1239215 :   unsigned tpl_levels = 0;
    8813              : 
    8814              :   /* If this is an imported temploid friend, get the owning decl its
    8815              :      attachment is determined by (or NULL_TREE otherwise).  */
    8816      1239215 :   tree temploid_friend = NULL_TREE;
    8817      1239215 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8818       872241 :     temploid_friend = tree_node ();
    8819              : 
    8820              :   /* Figure out if this decl is already known about.  */
    8821      1239215 :   int parm_tag = 0;
    8822              : 
    8823      1239215 :   if (decl != inner)
    8824       334360 :     if (!tpl_header (decl, &tpl_levels))
    8825            0 :       goto bail;
    8826      1239215 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8827       473467 :     parm_tag = fn_parms_init (inner);
    8828              : 
    8829      1239215 :   tree existing = key_mergeable (tag, mk, decl, inner, type, container,
    8830              :                                  is_attached, temploid_friend);
    8831      1239215 :   tree existing_inner = existing;
    8832      1239215 :   if (existing)
    8833              :     {
    8834       442337 :       if (existing == error_mark_node)
    8835            0 :         goto bail;
    8836              : 
    8837       442337 :       if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
    8838              :         {
    8839       164512 :           tree etype = TREE_TYPE (existing);
    8840       164512 :           if (TYPE_LANG_SPECIFIC (etype)
    8841       118563 :               && COMPLETE_TYPE_P (etype)
    8842       233665 :               && !CLASSTYPE_MEMBER_VEC (etype))
    8843              :             /* Give it a member vec, we're likely gonna be looking
    8844              :                inside it.  */
    8845        14392 :             set_class_bindings (etype, -1);
    8846              :         }
    8847              : 
    8848              :       /* Install the existing decl into the back ref array.  */
    8849       442337 :       register_duplicate (decl, existing);
    8850       442337 :       back_refs[~tag] = existing;
    8851       442337 :       if (inner_tag != 0)
    8852              :         {
    8853       147027 :           existing_inner = DECL_TEMPLATE_RESULT (existing);
    8854       147027 :           back_refs[~inner_tag] = existing_inner;
    8855              :         }
    8856              : 
    8857       442337 :       if (type_tag != 0)
    8858              :         {
    8859        78738 :           tree existing_type = TREE_TYPE (existing);
    8860        78738 :           back_refs[~type_tag] = existing_type;
    8861        78738 :           if (stub_tag != 0)
    8862          245 :             back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
    8863              :         }
    8864              :     }
    8865              : 
    8866      1239215 :   if (parm_tag)
    8867       473467 :     fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
    8868              : 
    8869      1239215 :   if (!tree_node_vals (decl))
    8870            0 :     goto bail;
    8871              : 
    8872      1239215 :   if (inner_tag)
    8873              :     {
    8874       334360 :       gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
    8875              : 
    8876       334360 :       if (!tree_node_vals (inner))
    8877            0 :         goto bail;
    8878              : 
    8879       334360 :       if (!tpl_parms_fini (decl, tpl_levels))
    8880            0 :         goto bail;
    8881              :     }
    8882              : 
    8883      1239215 :   if (type && (!tree_node_vals (type)
    8884       179877 :                || (stub_decl && !tree_node_vals (stub_decl))))
    8885            0 :     goto bail;
    8886              : 
    8887      1239215 :   spec_entry spec;
    8888      1239215 :   unsigned spec_flags = 0;
    8889      1239215 :   if (mk & MK_template_mask
    8890       854423 :       || mk == MK_partial
    8891       854423 :       || mk == MK_friend_spec)
    8892              :     {
    8893        11036 :       if (mk == MK_partial)
    8894              :         spec_flags = 2;
    8895              :       else
    8896       384792 :         spec_flags = u ();
    8897              : 
    8898       395828 :       spec.tmpl = tree_node ();
    8899       395828 :       spec.args = tree_node ();
    8900              :     }
    8901              :   /* Hold constraints on the spec field, for a short while.  */
    8902      1239215 :   spec.spec = tree_node ();
    8903              : 
    8904      1240179 :   dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
    8905              : 
    8906      1239215 :   existing = back_refs[~tag];
    8907      1239215 :   bool installed = install_entity (existing);
    8908      1239215 :   bool is_new = existing == decl;
    8909              : 
    8910      1239215 :   if (DECL_LANG_SPECIFIC (inner)
    8911      2325567 :       && DECL_MODULE_KEYED_DECLS_P (inner))
    8912              :     {
    8913              :       /* Read and maybe install the attached entities.  */
    8914          377 :       bool existed;
    8915          377 :       auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
    8916              :                                               &existed);
    8917          377 :       unsigned num = u ();
    8918          377 :       if (is_new == existed)
    8919            0 :         set_overrun ();
    8920          377 :       if (is_new)
    8921          244 :         set.reserve (num);
    8922          803 :       for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
    8923              :         {
    8924          426 :           tree attached = tree_node ();
    8925          426 :           dump (dumper::MERGE)
    8926          105 :             && dump ("Read %d[%u] %s attached decl %N", tag, ix,
    8927              :                      is_new ? "new" : "matched", attached);
    8928          426 :           if (is_new)
    8929          274 :             set.quick_push (attached);
    8930          152 :           else if (set[ix] != attached)
    8931              :             {
    8932            3 :               if (!set[ix] || !attached)
    8933              :                 /* One import left a hole for a lambda dep we chose not
    8934              :                    to stream, but another import chose to stream that lambda.
    8935              :                    Let's not error here: hopefully we'll complain later in
    8936              :                    is_matching_decl about whatever caused us to make a
    8937              :                    different decision.  */
    8938              :                 ;
    8939              :               else
    8940            0 :                 set_overrun ();
    8941              :             }
    8942              :         }
    8943              :     }
    8944              : 
    8945              :   /* Regular typedefs will have a NULL TREE_TYPE at this point.  */
    8946      1239215 :   unsigned tdef_flags = 0;
    8947      1239215 :   bool is_typedef = false;
    8948      1239215 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8949              :     {
    8950       218897 :       tdef_flags = u ();
    8951       218897 :       if (tdef_flags & 1)
    8952       218897 :         is_typedef = true;
    8953              :     }
    8954              : 
    8955      1239215 :   if (is_new)
    8956              :     {
    8957              :       /* A newly discovered node.  */
    8958       796878 :       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
    8959              :         /* Mark this identifier as naming a virtual function --
    8960              :            lookup_overrides relies on this optimization.  */
    8961         6635 :         IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
    8962              : 
    8963       796878 :       if (installed)
    8964              :         {
    8965              :           /* Mark the entity as imported.  */
    8966       509284 :           retrofit_lang_decl (inner);
    8967       509284 :           DECL_MODULE_IMPORT_P (inner) = true;
    8968              :         }
    8969              : 
    8970       796878 :       if (temploid_friend)
    8971           38 :         imported_temploid_friends->put (decl, temploid_friend);
    8972              : 
    8973       796878 :       if (spec.spec)
    8974        34018 :         set_constraints (decl, spec.spec);
    8975              : 
    8976       796878 :       if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
    8977              :         {
    8978            0 :           decl = cache_integer_cst (decl, true);
    8979            0 :           back_refs[~tag] = decl;
    8980              :         }
    8981              : 
    8982       796878 :       if (is_typedef)
    8983              :         {
    8984              :           /* Frob it to be ready for cloning.  */
    8985       133123 :           TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
    8986       133123 :           DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
    8987       133123 :           if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
    8988              :             {
    8989       133120 :               set_underlying_type (inner);
    8990       133120 :               if (tdef_flags & 2)
    8991              :                 {
    8992              :                   /* Match instantiate_alias_template's handling.  */
    8993        33861 :                   tree type = TREE_TYPE (inner);
    8994        33861 :                   TYPE_DEPENDENT_P (type) = true;
    8995        33861 :                   TYPE_DEPENDENT_P_VALID (type) = true;
    8996        33861 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
    8997              :                 }
    8998              :             }
    8999              :         }
    9000              : 
    9001       796878 :       if (inner_tag)
    9002              :         /* Set the TEMPLATE_DECL's type.  */
    9003       187333 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9004              : 
    9005              :       /* Redetermine whether we need to import or export this declaration
    9006              :          for this TU.  But for extern templates we know we must import:
    9007              :          they'll be defined in a different TU.
    9008              :          FIXME: How do dllexport and dllimport interact across a module?
    9009              :          See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
    9010              :          May have to revisit?  */
    9011       796878 :       if (type
    9012       101139 :           && CLASS_TYPE_P (type)
    9013        87280 :           && TYPE_LANG_SPECIFIC (type)
    9014       884158 :           && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
    9015          769 :                && CLASSTYPE_INTERFACE_KNOWN (type)
    9016          769 :                && CLASSTYPE_INTERFACE_ONLY (type)))
    9017              :         {
    9018        86563 :           CLASSTYPE_INTERFACE_ONLY (type) = false;
    9019        86563 :           CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
    9020              :         }
    9021              : 
    9022              :       /* Add to specialization tables now that constraints etc are
    9023              :          added.  */
    9024       796878 :       if (mk == MK_partial)
    9025              :         {
    9026         4813 :           bool is_type = TREE_CODE (inner) == TYPE_DECL;
    9027         4813 :           spec.spec = is_type ? type : inner;
    9028         4813 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9029              :         }
    9030       792065 :       else if (mk & MK_template_mask)
    9031              :         {
    9032       230802 :           bool is_type = !(mk & MK_tmpl_decl_mask);
    9033       230802 :           spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
    9034       230802 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9035              :         }
    9036              : 
    9037       796878 :       if (NAMESPACE_SCOPE_P (decl)
    9038       167645 :           && (mk == MK_named || mk == MK_unique
    9039       167645 :               || mk == MK_enum || mk == MK_friend_spec)
    9040       871761 :           && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
    9041        74782 :         add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
    9042              : 
    9043       796878 :       if (DECL_ARTIFICIAL (decl)
    9044       218152 :           && TREE_CODE (decl) == FUNCTION_DECL
    9045        23162 :           && !DECL_TEMPLATE_INFO (decl)
    9046        22850 :           && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
    9047        22672 :           && TYPE_SIZE (DECL_CONTEXT (decl))
    9048       798464 :           && !DECL_THUNK_P (decl))
    9049              :         /* A new implicit member function, when the class is
    9050              :            complete.  This means the importee declared it, and
    9051              :            we must now add it to the class.  Note that implicit
    9052              :            member fns of template instantiations do not themselves
    9053              :            look like templates.  */
    9054         1012 :         if (!install_implicit_member (inner))
    9055            0 :           set_overrun ();
    9056              : 
    9057              :       /* When importing a TLS wrapper from a header unit, we haven't
    9058              :          actually emitted its definition yet. Remember it so we can
    9059              :          do this later.  */
    9060       796878 :       if (state->is_header ()
    9061       796878 :           && decl_tls_wrapper_p (decl))
    9062            6 :         note_vague_linkage_fn (decl);
    9063              : 
    9064              :       /* Apply relevant attributes.
    9065              :          FIXME should probably use cplus_decl_attributes for this,
    9066              :          but it's not yet ready for modules.  */
    9067              : 
    9068       796878 :       if (VAR_OR_FUNCTION_DECL_P (inner))
    9069       480326 :         if (tree attr = lookup_attribute ("section", DECL_ATTRIBUTES (inner)))
    9070              :           {
    9071            6 :             tree section_name = TREE_VALUE (TREE_VALUE (attr));
    9072            6 :             set_decl_section_name (inner, TREE_STRING_POINTER (section_name));
    9073              :           }
    9074              : 
    9075              :       /* Setup aliases for the declaration.  */
    9076       796878 :       if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
    9077              :         {
    9078            3 :           alias = TREE_VALUE (TREE_VALUE (alias));
    9079            3 :           alias = get_identifier (TREE_STRING_POINTER (alias));
    9080            3 :           assemble_alias (decl, alias);
    9081              :         }
    9082              :     }
    9083              :   else
    9084              :     {
    9085              :       /* DECL is the to-be-discarded decl.  Its internal pointers will
    9086              :          be to the EXISTING's structure.  Frob it to point to its
    9087              :          own other structures, so loading its definition will alter
    9088              :          it, and not the existing decl.  */
    9089       444099 :       dump (dumper::MERGE) && dump ("Deduping %N", existing);
    9090              : 
    9091       442337 :       if (inner_tag)
    9092       147027 :         DECL_TEMPLATE_RESULT (decl) = inner;
    9093              : 
    9094       442337 :       if (type)
    9095              :         {
    9096              :           /* Point at the to-be-discarded type & decl.  */
    9097        78738 :           TYPE_NAME (type) = inner;
    9098        78738 :           TREE_TYPE (inner) = type;
    9099              : 
    9100       157231 :           TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
    9101        78738 :           if (stub_decl)
    9102          245 :             TREE_TYPE (stub_decl) = type;
    9103              : 
    9104        78738 :           tree etype = TREE_TYPE (existing);
    9105              : 
    9106              :           /* Handle separate declarations with different attributes.  */
    9107        78738 :           tree &dattr = TYPE_ATTRIBUTES (type);
    9108        78738 :           tree &eattr = TYPE_ATTRIBUTES (etype);
    9109        78738 :           check_abi_tags (existing, decl, eattr, dattr);
    9110              :           // TODO: handle other conflicting type attributes
    9111        78738 :           eattr = merge_attributes (eattr, dattr);
    9112              : 
    9113              :           /* When merging a partial specialisation, the existing decl may have
    9114              :              had its TYPE_CANONICAL adjusted.  If so we should use structural
    9115              :              equality to ensure is_matching_decl doesn't get confused.  */
    9116        78738 :           if ((spec_flags & 2)
    9117        78738 :               && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
    9118            3 :             SET_TYPE_STRUCTURAL_EQUALITY (type);
    9119              :         }
    9120              : 
    9121       442337 :       if (inner_tag)
    9122              :         /* Set the TEMPLATE_DECL's type.  */
    9123       147027 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9124              : 
    9125       442337 :       if (!is_matching_decl (existing, decl, is_typedef))
    9126           42 :         unmatched_duplicate (existing);
    9127              : 
    9128       442337 :       if (TREE_CODE (inner) == FUNCTION_DECL)
    9129              :         {
    9130       202025 :           tree e_inner = STRIP_TEMPLATE (existing);
    9131       202025 :           for (auto parm = DECL_ARGUMENTS (inner);
    9132       603899 :                parm; parm = DECL_CHAIN (parm))
    9133       401874 :             DECL_CONTEXT (parm) = e_inner;
    9134              :         }
    9135              : 
    9136              :       /* And our result is the existing node.  */
    9137       442337 :       decl = existing;
    9138              :     }
    9139              : 
    9140      1239215 :   if (mk == MK_friend_spec)
    9141              :     {
    9142            0 :       tree e = match_mergeable_specialization (true, &spec);
    9143            0 :       if (!e)
    9144              :         {
    9145            0 :           spec.spec = inner;
    9146            0 :           add_mergeable_specialization (true, &spec, decl, spec_flags);
    9147              :         }
    9148            0 :       else if (e != existing)
    9149            0 :         set_overrun ();
    9150              :     }
    9151              : 
    9152      1239215 :   if (is_typedef)
    9153              :     {
    9154              :       /* Insert the type into the array now.  */
    9155       218897 :       tag = insert (TREE_TYPE (decl));
    9156       218897 :       dump (dumper::TREE)
    9157          247 :         && dump ("Cloned:%d typedef %C:%N",
    9158          247 :                  tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
    9159              :     }
    9160              : 
    9161      1239215 :   unused = saved_unused;
    9162              : 
    9163      1239215 :   if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    9164              :     {
    9165       106254 :       unsigned flags = u ();
    9166              : 
    9167       106254 :       if (is_new)
    9168              :         {
    9169        65515 :           bool cloned_p = flags & 1;
    9170        65615 :           dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    9171              :                                        decl, cloned_p ? "" : "not ");
    9172        65515 :           if (cloned_p)
    9173              :             {
    9174              :               /* Update the member vec, if there is one (we're in a different
    9175              :                  cluster to the class defn) and this isn't a primary template
    9176              :                  specialization (as in tsubst_function_decl).  */
    9177        46315 :               bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
    9178        46315 :                          && !primary_template_specialization_p (decl));
    9179        46315 :               build_cdtor_clones (decl, flags & 2, flags & 4, up);
    9180              :             }
    9181              :         }
    9182              :     }
    9183              : 
    9184      1239215 :   if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    9185              :     {
    9186          160 :       enum tls_model model = tls_model (u ());
    9187          160 :       if (is_new)
    9188          140 :         set_decl_tls_model (decl, model);
    9189              :     }
    9190              : 
    9191      1239215 :   if (!NAMESPACE_SCOPE_P (inner)
    9192       924032 :       && ((TREE_CODE (inner) == TYPE_DECL
    9193       214837 :            && !is_typedef
    9194        38359 :            && TYPE_NAME (TREE_TYPE (inner)) == inner)
    9195       885673 :           || TREE_CODE (inner) == FUNCTION_DECL)
    9196      1638516 :       && u ())
    9197            3 :     read_definition (decl);
    9198              : 
    9199              :   return decl;
    9200              : }
    9201              : 
    9202              : /* DECL is an unnameable member of CTX.  Return a suitable identifying
    9203              :    index.  */
    9204              : 
    9205              : static unsigned
    9206         1641 : get_field_ident (tree ctx, tree decl)
    9207              : {
    9208         1641 :   gcc_checking_assert (TREE_CODE (decl) == USING_DECL
    9209              :                        || !DECL_NAME (decl)
    9210              :                        || IDENTIFIER_ANON_P (DECL_NAME (decl)));
    9211              : 
    9212         1641 :   unsigned ix = 0;
    9213         1641 :   for (tree fields = TYPE_FIELDS (ctx);
    9214        12192 :        fields; fields = DECL_CHAIN (fields))
    9215              :     {
    9216        12192 :       if (fields == decl)
    9217         1641 :         return ix;
    9218              : 
    9219        10551 :       if (DECL_CONTEXT (fields) == ctx
    9220        10551 :           && (TREE_CODE (fields) == USING_DECL
    9221        10530 :               || (TREE_CODE (fields) == FIELD_DECL
    9222           97 :                   && (!DECL_NAME (fields)
    9223           48 :                       || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9224              :         /* Count this field.  */
    9225           52 :         ix++;
    9226              :     }
    9227            0 :   gcc_unreachable ();
    9228              : }
    9229              : 
    9230              : static tree
    9231         1055 : lookup_field_ident (tree ctx, unsigned ix)
    9232              : {
    9233         1055 :   for (tree fields = TYPE_FIELDS (ctx);
    9234         8104 :        fields; fields = DECL_CHAIN (fields))
    9235         8104 :     if (DECL_CONTEXT (fields) == ctx
    9236         8104 :         && (TREE_CODE (fields) == USING_DECL
    9237         8092 :             || (TREE_CODE (fields) == FIELD_DECL
    9238         1120 :                 && (!DECL_NAME (fields)
    9239           25 :                     || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9240         1101 :       if (!ix--)
    9241              :         return fields;
    9242              : 
    9243              :   return NULL_TREE;
    9244              : }
    9245              : 
    9246              : /* Reference DECL.  REF indicates the walk kind we are performing.
    9247              :    Return true if we should write this decl by value.  */
    9248              : 
    9249              : bool
    9250     16849709 : trees_out::decl_node (tree decl, walk_kind ref)
    9251              : {
    9252     16849709 :   gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
    9253              :                        && DECL_CONTEXT (decl));
    9254              : 
    9255     16849709 :   if (ref == WK_value)
    9256              :     {
    9257      1617122 :       depset *dep = dep_hash->find_dependency (decl);
    9258      1617122 :       decl_value (decl, dep);
    9259      1617122 :       return false;
    9260              :     }
    9261              : 
    9262     15232587 :   switch (TREE_CODE (decl))
    9263              :     {
    9264              :     default:
    9265              :       break;
    9266              : 
    9267      1609792 :     case FUNCTION_DECL:
    9268      1609792 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9269              :       break;
    9270              : 
    9271              :     case RESULT_DECL:
    9272              :       /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
    9273              :          referenced when we're inside the function itself.  */
    9274              :       return true;
    9275              : 
    9276       250062 :     case PARM_DECL:
    9277       250062 :       {
    9278       250062 :         if (streaming_p ())
    9279       108200 :           i (tt_parm);
    9280       250062 :         tree_node (DECL_CONTEXT (decl));
    9281              : 
    9282              :         /* That must have put this in the map.  */
    9283       250062 :         walk_kind ref = ref_node (decl);
    9284       250062 :         if (ref != WK_none)
    9285              :           // FIXME:OPTIMIZATION We can wander into bits of the
    9286              :           // template this was instantiated from, for instance
    9287              :           // deferred noexcept and default parms, or references
    9288              :           // to parms from earlier forward-decls (PR c++/119608).
    9289              :           //
    9290              :           // Currently we'll end up cloning those bits of tree.
    9291              :           // It would be nice to reference those specific nodes.
    9292              :           // I think putting those things in the map when we
    9293              :           // reference their template by name.
    9294              :           //
    9295              :           // See the note in add_indirects.
    9296              :           return true;
    9297              : 
    9298            0 :         if (streaming_p ())
    9299            0 :           dump (dumper::TREE)
    9300            0 :             && dump ("Wrote %s reference %N",
    9301            0 :                      TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
    9302              :                      decl);
    9303              :       }
    9304              :       return false;
    9305              : 
    9306              :     case IMPORTED_DECL:
    9307              :       /* This describes a USING_DECL to the ME's debug machinery.  It
    9308              :          originates from the fortran FE, and has nothing to do with
    9309              :          C++ modules.  */
    9310              :       return true;
    9311              : 
    9312              :     case LABEL_DECL:
    9313              :       return true;
    9314              : 
    9315        93747 :     case CONST_DECL:
    9316        93747 :       {
    9317              :         /* If I end up cloning enum decls, implementing C++20 using
    9318              :            E::v, this will need tweaking.   */
    9319        93747 :         if (streaming_p ())
    9320        23256 :           i (tt_enum_decl);
    9321        93747 :         tree ctx = DECL_CONTEXT (decl);
    9322        93747 :         gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
    9323        93747 :         tree_node (ctx);
    9324        93747 :         tree_node (DECL_NAME (decl));
    9325              : 
    9326        93747 :         int tag = insert (decl);
    9327        93747 :         if (streaming_p ())
    9328        23256 :           dump (dumper::TREE)
    9329           21 :             && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
    9330              :         return false;
    9331              :       }
    9332        37376 :       break;
    9333              : 
    9334        37376 :     case USING_DECL:
    9335        37376 :       if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
    9336              :         break;
    9337              :       /* FALLTHROUGH  */
    9338              : 
    9339       233523 :     case FIELD_DECL:
    9340       233523 :       {
    9341       233523 :         if (streaming_p ())
    9342        17329 :           i (tt_data_member);
    9343              : 
    9344       233523 :         tree ctx = DECL_CONTEXT (decl);
    9345       233523 :         tree_node (ctx);
    9346              : 
    9347       233523 :         tree name = NULL_TREE;
    9348              : 
    9349       233523 :         if (TREE_CODE (decl) == USING_DECL)
    9350              :           ;
    9351              :         else
    9352              :           {
    9353       231807 :             name = DECL_NAME (decl);
    9354       450260 :             if (name && IDENTIFIER_ANON_P (name))
    9355              :               name = NULL_TREE;
    9356              :           }
    9357              : 
    9358       233523 :         tree_node (name);
    9359       233523 :         if (!name && streaming_p ())
    9360              :           {
    9361         1641 :             unsigned ix = get_field_ident (ctx, decl);
    9362         1641 :             u (ix);
    9363              :           }
    9364              : 
    9365       233523 :         int tag = insert (decl);
    9366       233523 :         if (streaming_p ())
    9367        17329 :           dump (dumper::TREE)
    9368           26 :             && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
    9369              :         return false;
    9370              :       }
    9371       701892 :       break;
    9372              : 
    9373       701892 :     case VAR_DECL:
    9374       701892 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9375       701892 :       if (DECL_VTABLE_OR_VTT_P (decl))
    9376              :         {
    9377              :           /* VTT or VTABLE, they are all on the vtables list.  */
    9378         4486 :           tree ctx = CP_DECL_CONTEXT (decl);
    9379         4486 :           tree vtable = CLASSTYPE_VTABLES (ctx);
    9380         4606 :           for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
    9381         4606 :             if (vtable == decl)
    9382              :               {
    9383         4486 :                 gcc_checking_assert (DECL_VIRTUAL_P (decl));
    9384         4486 :                 if (streaming_p ())
    9385              :                   {
    9386           44 :                     u (tt_vtable);
    9387           44 :                     u (ix);
    9388           44 :                     dump (dumper::TREE)
    9389            0 :                       && dump ("Writing vtable %N[%u]", ctx, ix);
    9390              :                   }
    9391         4486 :                 tree_node (ctx);
    9392         4486 :                 return false;
    9393              :               }
    9394              :           gcc_unreachable ();
    9395              :         }
    9396              : 
    9397       697406 :       if (DECL_TINFO_P (decl))
    9398              :         {
    9399         8934 :         tinfo:
    9400              :           /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var.  */
    9401        16464 :           bool is_var = VAR_P (decl);
    9402        16464 :           tree type = TREE_TYPE (decl);
    9403        16464 :           unsigned ix = get_pseudo_tinfo_index (type);
    9404        16464 :           if (streaming_p ())
    9405              :             {
    9406        10416 :               i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
    9407         7374 :               u (ix);
    9408              :             }
    9409              : 
    9410        16464 :           if (is_var)
    9411              :             {
    9412              :               /* We also need the type it is for and mangled name, so
    9413              :                  the reader doesn't need to complete the type (which
    9414              :                  would break section ordering).  The type it is for is
    9415              :                  stashed on the name's TREE_TYPE.  */
    9416         8934 :               tree name = DECL_NAME (decl);
    9417         8934 :               tree_node (name);
    9418         8934 :               type = TREE_TYPE (name);
    9419         8934 :               tree_node (type);
    9420              :             }
    9421              : 
    9422        16464 :           int tag = insert (decl);
    9423        16464 :           if (streaming_p ())
    9424         7374 :             dump (dumper::TREE)
    9425           27 :               && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
    9426              :                        tag, ix, type);
    9427              : 
    9428        16464 :           if (!is_var)
    9429              :             {
    9430         7530 :               tag = insert (type);
    9431         7530 :               if (streaming_p ())
    9432         3042 :                 dump (dumper::TREE)
    9433            9 :                   && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
    9434              :             }
    9435              :           return false;
    9436              :         }
    9437              : 
    9438       688472 :       if (DECL_NTTP_OBJECT_P (decl))
    9439              :         {
    9440              :           /* A NTTP parm object.  */
    9441           42 :           if (streaming_p ())
    9442           10 :             i (tt_nttp_var);
    9443           42 :           tree_node (tparm_object_argument (decl));
    9444           42 :           tree_node (DECL_NAME (decl));
    9445           42 :           int tag = insert (decl);
    9446           42 :           if (streaming_p ())
    9447           10 :             dump (dumper::TREE)
    9448            0 :               && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
    9449              :           return false;
    9450              :         }
    9451              : 
    9452              :       break;
    9453              : 
    9454      5203446 :     case TYPE_DECL:
    9455      5203446 :       if (DECL_TINFO_P (decl))
    9456         7530 :         goto tinfo;
    9457              :       /* c++/125768: For an imported typedef, also mark the original type
    9458              :          reachable in case it was instantiated here.  */
    9459      4746361 :       if (!streaming_p () && DECL_ORIGINAL_TYPE (decl)
    9460      6312316 :           && (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl)))
    9461         1675 :         tree_node (DECL_ORIGINAL_TYPE (decl));
    9462              :       break;
    9463              :     }
    9464              : 
    9465     13832034 :   if (DECL_THUNK_P (decl))
    9466              :     {
    9467              :       /* Thunks are similar to binfos -- write the thunked-to decl and
    9468              :          then thunk-specific key info.  */
    9469            0 :       if (streaming_p ())
    9470              :         {
    9471            0 :           i (tt_thunk);
    9472            0 :           i (THUNK_FIXED_OFFSET (decl));
    9473              :         }
    9474              : 
    9475            0 :       tree target = decl;
    9476            0 :       while (DECL_THUNK_P (target))
    9477            0 :         target = THUNK_TARGET (target);
    9478            0 :       tree_node (target);
    9479            0 :       tree_node (THUNK_VIRTUAL_OFFSET (decl));
    9480            0 :       int tag = insert (decl);
    9481            0 :       if (streaming_p ())
    9482            0 :         dump (dumper::TREE)
    9483            0 :           && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
    9484              :       return false;
    9485              :     }
    9486              : 
    9487     13832034 :   if (DECL_CLONED_FUNCTION_P (decl))
    9488              :     {
    9489       477461 :       tree target = get_clone_target (decl);
    9490       477461 :       if (streaming_p ())
    9491       226900 :         i (tt_clone_ref);
    9492              : 
    9493       477461 :       tree_node (target);
    9494       477461 :       tree_node (DECL_NAME (decl));
    9495       477461 :       if (DECL_VIRTUAL_P (decl))
    9496        32187 :         tree_node (DECL_VINDEX (decl));
    9497       477461 :       int tag = insert (decl);
    9498       477461 :       if (streaming_p ())
    9499       226900 :         dump (dumper::TREE)
    9500          164 :           && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
    9501              :       return false;
    9502              :     }
    9503              : 
    9504              :   /* Everything left should be a thing that is in the entity table.
    9505              :      Mostly things that can be defined outside of their (original
    9506              :      declaration) context.  */
    9507     13354573 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
    9508              :                        || VAR_P (decl)
    9509              :                        || TREE_CODE (decl) == FUNCTION_DECL
    9510              :                        || TREE_CODE (decl) == TYPE_DECL
    9511              :                        || TREE_CODE (decl) == USING_DECL
    9512              :                        || TREE_CODE (decl) == CONCEPT_DECL
    9513              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
    9514              : 
    9515     13354573 :   int use_tpl = -1;
    9516     13354573 :   tree ti = node_template_info (decl, use_tpl);
    9517     13354573 :   tree tpl = NULL_TREE;
    9518              : 
    9519              :   /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
    9520              :      TEMPLATE_DECL.  Note TI_TEMPLATE is not a TEMPLATE_DECL for
    9521              :      (some) friends, so we need to check that.  */
    9522              :   // FIXME: Should local friend template specializations be by value?
    9523              :   // They don't get idents so we'll never know they're imported, but I
    9524              :   // think we can only reach them from the TU that defines the
    9525              :   // befriending class?
    9526      4866851 :   if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    9527     18221316 :       && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
    9528              :     {
    9529              :       tpl = TI_TEMPLATE (ti);
    9530      1228161 :     partial_template:
    9531      1228161 :       if (streaming_p ())
    9532              :         {
    9533         4103 :           i (tt_template);
    9534         4103 :           dump (dumper::TREE)
    9535            9 :             && dump ("Writing implicit template %C:%N%S",
    9536            9 :                      TREE_CODE (tpl), tpl, tpl);
    9537              :         }
    9538      1228161 :       tree_node (tpl);
    9539              : 
    9540              :       /* Streaming TPL caused us to visit DECL and maybe its type,
    9541              :          if it wasn't TU-local.  */
    9542      1228161 :       if (CHECKING_P && !has_tu_local_dep (tpl))
    9543              :         {
    9544      1228134 :           gcc_checking_assert (TREE_VISITED (decl));
    9545      1228134 :           if (DECL_IMPLICIT_TYPEDEF_P (decl))
    9546       633468 :             gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
    9547              :         }
    9548              :       return false;
    9549              :     }
    9550              : 
    9551     12251408 :   tree ctx = CP_DECL_CONTEXT (decl);
    9552     12251408 :   depset *dep = NULL;
    9553     12251408 :   if (streaming_p ())
    9554      2063544 :     dep = dep_hash->find_dependency (decl);
    9555     10187864 :   else if (TREE_CODE (ctx) != FUNCTION_DECL
    9556       413612 :            || TREE_CODE (decl) == TEMPLATE_DECL
    9557       373840 :            || DECL_IMPLICIT_TYPEDEF_P (decl)
    9558     10522074 :            || (DECL_LANG_SPECIFIC (decl)
    9559       169602 :                && DECL_MODULE_IMPORT_P (decl)))
    9560              :     {
    9561      9853654 :       auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
    9562       757980 :                    && !DECL_NAMESPACE_ALIAS (decl)
    9563      9853654 :                    ? depset::EK_NAMESPACE : depset::EK_DECL);
    9564      9853654 :       dep = dep_hash->add_dependency (decl, kind);
    9565              :     }
    9566              : 
    9567     11917198 :   if (!dep || dep->is_tu_local ())
    9568              :     {
    9569              :       /* Some internal entity of context.  Do by value.  */
    9570       645111 :       decl_value (decl, dep);
    9571       645111 :       return false;
    9572              :     }
    9573              : 
    9574     11606297 :   if (dep->get_entity_kind () == depset::EK_REDIRECT)
    9575              :     {
    9576              :       /* The DECL_TEMPLATE_RESULT of a partial specialization.
    9577              :          Write the partial specialization's template.  */
    9578       124996 :       depset *redirect = dep->deps[0];
    9579       124996 :       gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
    9580       124996 :       tpl = redirect->get_entity ();
    9581       124996 :       goto partial_template;
    9582              :     }
    9583              : 
    9584     11481301 :   if (streaming_p ())
    9585              :     {
    9586              :       /* Locate the entity.  */
    9587      1752903 :       unsigned index = dep->cluster;
    9588      1752903 :       unsigned import = 0;
    9589              : 
    9590      1752903 :       if (dep->is_import ())
    9591        11100 :         import = dep->section;
    9592      1741803 :       else if (CHECKING_P)
    9593              :         /* It should be what we put there.  */
    9594      1741803 :         gcc_checking_assert (index == ~import_entity_index (decl));
    9595              : 
    9596              : #if CHECKING_P
    9597        11100 :       gcc_assert (!import || importedness >= 0);
    9598              : #endif
    9599      1752903 :       i (tt_entity);
    9600      1752903 :       u (import);
    9601      1752903 :       u (index);
    9602              :     }
    9603              : 
    9604     11481301 :   int tag = insert (decl);
    9605     11481301 :   if (streaming_p () && dump (dumper::TREE))
    9606              :     {
    9607          529 :       char const *kind = "import";
    9608          529 :       module_state *from = this_module ();
    9609          529 :       if (dep->is_import ())
    9610              :         /* Rediscover the unremapped index.  */
    9611           78 :         from = import_entity_module (import_entity_index (decl));
    9612              :       else
    9613              :         {
    9614          451 :           tree o = get_originating_module_decl (decl);
    9615          451 :           o = STRIP_TEMPLATE (o);
    9616          902 :           kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
    9617          451 :                   ? "purview" : "GMF");
    9618              :         }
    9619          529 :       dump ("Wrote %s:%d %C:%N@%M", kind,
    9620          529 :             tag, TREE_CODE (decl), decl, from);
    9621              :     }
    9622              : 
    9623     11481301 :   add_indirects (decl);
    9624              : 
    9625     11481301 :   return false;
    9626              : }
    9627              : 
    9628              : void
    9629     13677391 : trees_out::type_node (tree type)
    9630              : {
    9631     13677391 :   gcc_assert (TYPE_P (type));
    9632              : 
    9633     13677391 :   tree root = (TYPE_NAME (type)
    9634     13677391 :                ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
    9635     13677391 :   gcc_checking_assert (root);
    9636              : 
    9637     13677391 :   if (type != root)
    9638              :     {
    9639      3060061 :       if (streaming_p ())
    9640       636052 :         i (tt_variant_type);
    9641      3060061 :       tree_node (root);
    9642              : 
    9643      3060061 :       int flags = -1;
    9644              : 
    9645      3060061 :       if (TREE_CODE (type) == FUNCTION_TYPE
    9646      3060061 :           || TREE_CODE (type) == METHOD_TYPE)
    9647              :         {
    9648       714887 :           int quals = type_memfn_quals (type);
    9649       714887 :           int rquals = type_memfn_rqual (type);
    9650       714887 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9651       714887 :           bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
    9652              : 
    9653       714887 :           if (raises != TYPE_RAISES_EXCEPTIONS (root)
    9654        23418 :               || rquals != type_memfn_rqual (root)
    9655        16781 :               || quals != type_memfn_quals (root)
    9656       731650 :               || late != TYPE_HAS_LATE_RETURN_TYPE (root))
    9657       714887 :             flags = rquals | (int (late) << 2) | (quals << 3);
    9658              :         }
    9659              :       else
    9660              :         {
    9661      2345174 :           if (TYPE_USER_ALIGN (type))
    9662        26168 :             flags = TYPE_ALIGN_RAW (type);
    9663              :         }
    9664              : 
    9665      3060061 :       if (streaming_p ())
    9666       636052 :         i (flags);
    9667              : 
    9668      3060061 :       if (flags < 0)
    9669              :         ;
    9670       741055 :       else if (TREE_CODE (type) == FUNCTION_TYPE
    9671       741055 :                || TREE_CODE (type) == METHOD_TYPE)
    9672              :         {
    9673       714887 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9674       714887 :           if (raises == TYPE_RAISES_EXCEPTIONS (root))
    9675        23418 :             raises = error_mark_node;
    9676       714887 :           tree_node (raises);
    9677              :         }
    9678              : 
    9679              :       /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
    9680              :          variants should all have the same set of attributes.  */
    9681      3060061 :       gcc_checking_assert (TYPE_ATTRIBUTES (type)
    9682              :                            == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
    9683              : 
    9684      3060061 :       if (streaming_p ())
    9685              :         {
    9686              :           /* Qualifiers.  */
    9687       636052 :           int rquals = cp_type_quals (root);
    9688       636052 :           int quals = cp_type_quals (type);
    9689       636052 :           if (quals == rquals)
    9690       292678 :             quals = -1;
    9691       636052 :           i (quals);
    9692              :         }
    9693              : 
    9694      3060061 :       if (ref_node (type) != WK_none)
    9695              :         {
    9696      3060061 :           int tag = insert (type);
    9697      3060061 :           if (streaming_p ())
    9698              :             {
    9699       636052 :               i (0);
    9700       636052 :               dump (dumper::TREE)
    9701          203 :                 && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
    9702              :             }
    9703              :         }
    9704              :       return;
    9705              :     }
    9706              : 
    9707     10617330 :   if (tree name = TYPE_NAME (type))
    9708      4200869 :     if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
    9709      3301401 :         || DECL_TEMPLATE_PARM_P (name)
    9710      2247711 :         || TREE_CODE (type) == RECORD_TYPE
    9711       394447 :         || TREE_CODE (type) == UNION_TYPE
    9712      4586883 :         || TREE_CODE (type) == ENUMERAL_TYPE)
    9713              :       {
    9714      3928540 :         gcc_checking_assert (DECL_P (name));
    9715              : 
    9716              :         /* We can meet template parms that we didn't meet in the
    9717              :            tpl_parms walk, because we're referring to a derived type
    9718              :            that was previously constructed from equivalent template
    9719              :            parms. */
    9720      3928540 :         if (streaming_p ())
    9721              :           {
    9722       271179 :             i (tt_typedef_type);
    9723       271179 :             dump (dumper::TREE)
    9724           59 :               && dump ("Writing %stypedef %C:%N",
    9725           59 :                        DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
    9726           59 :                        TREE_CODE (name), name);
    9727              :           }
    9728      3928540 :         tree_node (name);
    9729      3928540 :         if (streaming_p ())
    9730       271179 :           dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
    9731           59 :                                        TREE_CODE (name), name, name);
    9732              : 
    9733              :         /* We'll have either visited this type or have newly discovered
    9734              :            that it's TU-local; either way we won't need to visit it again.  */
    9735      3928540 :         gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
    9736              :         return;
    9737              :       }
    9738              : 
    9739      6688790 :   if (TYPE_PTRMEMFUNC_P (type))
    9740              :     {
    9741              :       /* This is a distinct type node, masquerading as a structure. */
    9742         5698 :       tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
    9743         5698 :       if (streaming_p ())
    9744         1591 :         i (tt_ptrmem_type);
    9745         5698 :       tree_node (fn_type);
    9746         5698 :       int tag = insert (type);
    9747         5698 :       if (streaming_p ())
    9748         1594 :         dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
    9749              :       return;
    9750              :     }
    9751              : 
    9752      6683092 :   if (streaming_p ())
    9753              :     {
    9754      2132613 :       u (tt_derived_type);
    9755      2132613 :       u (TREE_CODE (type));
    9756              :     }
    9757              : 
    9758      6683092 :   tree_node (TREE_TYPE (type));
    9759      6683092 :   switch (TREE_CODE (type))
    9760              :     {
    9761            0 :     default:
    9762              :       /* We should never meet a type here that is indescribable in
    9763              :          terms of other types.  */
    9764            0 :       gcc_unreachable ();
    9765              : 
    9766       102027 :     case ARRAY_TYPE:
    9767       102027 :       tree_node (TYPE_DOMAIN (type));
    9768       102027 :       if (streaming_p ())
    9769              :         /* Dependent arrays are constructed with TYPE_DEPENENT_P
    9770              :            already set.  */
    9771        32936 :         u (TYPE_DEPENDENT_P (type));
    9772              :       break;
    9773              : 
    9774              :     case COMPLEX_TYPE:
    9775              :       /* No additional data.  */
    9776              :       break;
    9777              : 
    9778           12 :     case BOOLEAN_TYPE:
    9779              :       /* A non-standard boolean type.  */
    9780           12 :       if (streaming_p ())
    9781            6 :         u (TYPE_PRECISION (type));
    9782              :       break;
    9783              : 
    9784        95104 :     case INTEGER_TYPE:
    9785        95104 :       if (TREE_TYPE (type))
    9786              :         {
    9787              :           /* A range type (representing an array domain).  */
    9788        88450 :           tree_node (TYPE_MIN_VALUE (type));
    9789        88450 :           tree_node (TYPE_MAX_VALUE (type));
    9790              :         }
    9791              :       else
    9792              :         {
    9793              :           /* A new integral type (representing a bitfield).  */
    9794         6654 :           if (streaming_p ())
    9795              :             {
    9796         2043 :               unsigned prec = TYPE_PRECISION (type);
    9797         2043 :               bool unsigned_p = TYPE_UNSIGNED (type);
    9798              : 
    9799         2043 :               u ((prec << 1) | unsigned_p);
    9800              :             }
    9801              :         }
    9802              :       break;
    9803              : 
    9804      1454492 :     case METHOD_TYPE:
    9805      1454492 :     case FUNCTION_TYPE:
    9806      1454492 :       {
    9807      1454492 :         gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
    9808              : 
    9809      1454492 :         tree arg_types = TYPE_ARG_TYPES (type);
    9810      1454492 :         if (TREE_CODE (type) == METHOD_TYPE)
    9811              :           {
    9812       942817 :             tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
    9813       942817 :             arg_types = TREE_CHAIN (arg_types);
    9814              :           }
    9815      1454492 :         tree_node (arg_types);
    9816              :       }
    9817      1454492 :       break;
    9818              : 
    9819         1671 :     case OFFSET_TYPE:
    9820         1671 :       tree_node (TYPE_OFFSET_BASETYPE (type));
    9821         1671 :       break;
    9822              : 
    9823              :     case POINTER_TYPE:
    9824              :       /* No additional data.  */
    9825              :       break;
    9826              : 
    9827      1148181 :     case REFERENCE_TYPE:
    9828      1148181 :       if (streaming_p ())
    9829       255748 :         u (TYPE_REF_IS_RVALUE (type));
    9830              :       break;
    9831              : 
    9832      1359409 :     case DECLTYPE_TYPE:
    9833      1359409 :     case TYPEOF_TYPE:
    9834      1359409 :     case DEPENDENT_OPERATOR_TYPE:
    9835      1359409 :       tree_node (TYPE_VALUES_RAW (type));
    9836      1359409 :       if (TREE_CODE (type) == DECLTYPE_TYPE)
    9837              :         /* We stash a whole bunch of things into decltype's
    9838              :            flags.  */
    9839       111235 :         if (streaming_p ())
    9840        37843 :           tree_node_bools (type);
    9841              :       break;
    9842              : 
    9843         8853 :     case TRAIT_TYPE:
    9844         8853 :       tree_node (TRAIT_TYPE_KIND_RAW (type));
    9845         8853 :       tree_node (TRAIT_TYPE_TYPE1 (type));
    9846         8853 :       tree_node (TRAIT_TYPE_TYPE2 (type));
    9847         8853 :       break;
    9848              : 
    9849              :     case TYPE_ARGUMENT_PACK:
    9850              :       /* No additional data.  */
    9851              :       break;
    9852              : 
    9853       224868 :     case TYPE_PACK_EXPANSION:
    9854       224868 :       if (streaming_p ())
    9855        90714 :         u (PACK_EXPANSION_LOCAL_P (type));
    9856       449736 :       tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
    9857       224868 :       tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
    9858       224868 :       break;
    9859              : 
    9860           49 :     case PACK_INDEX_TYPE:
    9861           49 :       tree_node (PACK_INDEX_PACK (type));
    9862           49 :       tree_node (PACK_INDEX_INDEX (type));
    9863           49 :       break;
    9864              : 
    9865       272956 :     case TYPENAME_TYPE:
    9866       272956 :       {
    9867       272956 :         tree_node (TYPE_CONTEXT (type));
    9868       272956 :         tree_node (DECL_NAME (TYPE_NAME (type)));
    9869       272956 :         tree_node (TYPENAME_TYPE_FULLNAME (type));
    9870       272956 :         if (streaming_p ())
    9871        94558 :           u (get_typename_tag (type));
    9872              :         }
    9873              :       break;
    9874              : 
    9875          270 :     case UNBOUND_CLASS_TEMPLATE:
    9876          270 :       {
    9877          270 :         tree decl = TYPE_NAME (type);
    9878          270 :         tree_node (DECL_CONTEXT (decl));
    9879          270 :         tree_node (DECL_NAME (decl));
    9880          270 :         tree_node (DECL_TEMPLATE_PARMS (decl));
    9881              :       }
    9882          270 :       break;
    9883              : 
    9884           42 :     case VECTOR_TYPE:
    9885           42 :       if (streaming_p ())
    9886              :         {
    9887           21 :           poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
    9888           63 :           for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    9889           21 :             wu (nunits.coeffs[ix]);
    9890              :         }
    9891              :       break;
    9892              : 
    9893         2366 :     case LANG_TYPE:
    9894              :       /* No additional data.  */
    9895         2366 :       gcc_checking_assert (REFLECTION_TYPE_P (type));
    9896              :       break;
    9897              : 
    9898            8 :     case SPLICE_SCOPE:
    9899            8 :       if (streaming_p ())
    9900            4 :         u (SPLICE_SCOPE_TYPE_P (type));
    9901            8 :       tree_node (SPLICE_SCOPE_EXPR (type));
    9902            8 :       break;
    9903              :     }
    9904              : 
    9905      6683092 :   tree_node (TYPE_ATTRIBUTES (type));
    9906              : 
    9907              :   /* We may have met the type during emitting the above.  */
    9908      6683092 :   if (ref_node (type) != WK_none)
    9909              :     {
    9910      6059005 :       int tag = insert (type);
    9911      6059005 :       if (streaming_p ())
    9912              :         {
    9913      1840841 :           i (0);
    9914      1840841 :           dump (dumper::TREE)
    9915          558 :             && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
    9916              :         }
    9917              :     }
    9918              : 
    9919              :   return;
    9920              : }
    9921              : 
    9922              : /* T is (mostly*) a non-mergeable node that must be written by value.
    9923              :    The mergeable case is a BINFO, which are as-if DECLSs.   */
    9924              : 
    9925              : void
    9926     41721826 : trees_out::tree_value (tree t)
    9927              : {
    9928              :   /* We should never be writing a type by value.  tree_type should
    9929              :      have streamed it, or we're going via its TYPE_DECL.  */
    9930     41721826 :   gcc_checking_assert (!TYPE_P (t));
    9931              : 
    9932     41721826 :   if (DECL_P (t))
    9933              :     /* No template, type, var or function, except anonymous
    9934              :        non-context vars and types.  */
    9935      1085097 :     gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
    9936              :                           && (TREE_CODE (t) != TYPE_DECL
    9937              :                               || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
    9938              :                           && (TREE_CODE (t) != VAR_DECL
    9939              :                               || ((!DECL_NAME (t)
    9940              :                                    || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
    9941              :                                   && !DECL_CONTEXT (t)))
    9942              :                           && TREE_CODE (t) != FUNCTION_DECL));
    9943              : 
    9944     63017663 :   if (is_initial_scan () && EXPR_P (t))
    9945      8448744 :     dep_hash->add_dependent_adl_entities (t);
    9946              : 
    9947     41721826 :   if (streaming_p ())
    9948              :     {
    9949              :       /* A new node -> tt_node.  */
    9950     16556595 :       tree_val_count++;
    9951     16556595 :       i (tt_node);
    9952     16556595 :       start (t);
    9953     16556595 :       tree_node_bools (t);
    9954              :     }
    9955              : 
    9956     41721826 :   if (TREE_CODE (t) == TREE_BINFO)
    9957              :     /* Binfos are decl-like and need merging information.  */
    9958       287318 :     binfo_mergeable (t);
    9959              : 
    9960     41721826 :   int tag = insert (t, WK_value);
    9961     41721826 :   if (streaming_p ())
    9962     16556595 :     dump (dumper::TREE)
    9963         2823 :       && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
    9964              : 
    9965     41721826 :   int type_tag = 0;
    9966     41721826 :   tree type = NULL_TREE;
    9967     41721826 :   if (TREE_CODE (t) == TYPE_DECL)
    9968              :     {
    9969           28 :       type = TREE_TYPE (t);
    9970              : 
    9971              :       /* We only support a limited set of features for uncontexted types;
    9972              :          these are typically types created in the language-independent
    9973              :          parts of the frontend (such as ubsan).  */
    9974           28 :       gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
    9975              :                            && TYPE_MAIN_VARIANT (type) == type
    9976              :                            && TYPE_NAME (type) == t
    9977              :                            && TYPE_STUB_DECL (type) == t
    9978              :                            && !TYPE_VFIELD (type)
    9979              :                            && !TYPE_BINFO (type)
    9980              :                            && !CLASS_TYPE_P (type));
    9981              : 
    9982           28 :       if (streaming_p ())
    9983              :         {
    9984           14 :           start (type);
    9985           14 :           tree_node_bools (type);
    9986              :         }
    9987              : 
    9988           28 :       type_tag = insert (type, WK_value);
    9989           28 :       if (streaming_p ())
    9990           14 :         dump (dumper::TREE)
    9991            0 :           && dump ("Writing type: %d %C:%N", type_tag,
    9992            0 :                    TREE_CODE (type), type);
    9993              :     }
    9994              : 
    9995     41721826 :   tree_node_vals (t);
    9996              : 
    9997     41721826 :   if (type)
    9998              :     {
    9999           28 :       tree_node_vals (type);
   10000           28 :       tree_node (TYPE_SIZE (type));
   10001           28 :       tree_node (TYPE_SIZE_UNIT (type));
   10002           28 :       chained_decls (TYPE_FIELDS (type));
   10003           28 :       if (streaming_p ())
   10004           14 :         dump (dumper::TREE)
   10005            0 :           && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10006              :     }
   10007              : 
   10008              :   /* For uncontexted VAR_DECLs we need to stream the definition so that
   10009              :      importers can recreate their value.  */
   10010     41721826 :   if (TREE_CODE (t) == VAR_DECL)
   10011              :     {
   10012          824 :       gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
   10013          824 :       tree_node (DECL_INITIAL (t));
   10014              :     }
   10015              : 
   10016     41721826 :   if (streaming_p ())
   10017     16559418 :     dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
   10018     41721826 : }
   10019              : 
   10020              : tree
   10021     12181843 : trees_in::tree_value ()
   10022              : {
   10023     12181843 :   tree t = start ();
   10024     12181843 :   if (!t || !tree_node_bools (t))
   10025              :     return NULL_TREE;
   10026              : 
   10027     12181843 :   tree existing = t;
   10028     12181843 :   if (TREE_CODE (t) == TREE_BINFO)
   10029              :     {
   10030        92709 :       tree type;
   10031        92709 :       unsigned ix = binfo_mergeable (&type);
   10032        92709 :       if (TYPE_BINFO (type))
   10033              :         {
   10034              :           /* We already have a definition, this must be a duplicate.  */
   10035        41870 :           dump (dumper::MERGE)
   10036          271 :             && dump ("Deduping binfo %N[%u]", type, ix);
   10037        41870 :           existing = TYPE_BINFO (type);
   10038        57226 :           while (existing && ix--)
   10039        15356 :             existing = TREE_CHAIN (existing);
   10040        41870 :           if (existing)
   10041        41870 :             register_duplicate (t, existing);
   10042              :           else
   10043              :             /* Error, mismatch -- diagnose in read_class_def's
   10044              :                checking.  */
   10045              :             existing = t;
   10046              :         }
   10047              :     }
   10048              : 
   10049              :   /* Insert into map.  */
   10050     12181843 :   int tag = insert (existing);
   10051     12181843 :   dump (dumper::TREE)
   10052         3677 :     && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
   10053              : 
   10054     12181843 :   int type_tag = 0;
   10055     12181843 :   tree type = NULL_TREE;
   10056     12181843 :   if (TREE_CODE (t) == TYPE_DECL)
   10057              :     {
   10058           14 :       type = start ();
   10059           14 :       if (!type || !tree_node_bools (type))
   10060              :         t = NULL_TREE;
   10061              : 
   10062           14 :       type_tag = insert (type);
   10063           14 :       if (t)
   10064           14 :         dump (dumper::TREE)
   10065            0 :           && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
   10066              :     }
   10067              : 
   10068              :   if (!t)
   10069              :     {
   10070            0 : bail:
   10071            0 :       back_refs[~tag] = NULL_TREE;
   10072            0 :       if (type_tag)
   10073            0 :         back_refs[~type_tag] = NULL_TREE;
   10074            0 :       set_overrun ();
   10075            0 :       return NULL_TREE;
   10076              :     }
   10077              : 
   10078     12181843 :   if (!tree_node_vals (t))
   10079            0 :     goto bail;
   10080              : 
   10081     12181843 :   if (type)
   10082              :     {
   10083           14 :       if (!tree_node_vals (type))
   10084            0 :         goto bail;
   10085              : 
   10086           14 :       TYPE_SIZE (type) = tree_node ();
   10087           14 :       TYPE_SIZE_UNIT (type) = tree_node ();
   10088           14 :       TYPE_FIELDS (type) = chained_decls ();
   10089           14 :       if (get_overrun ())
   10090            0 :         goto bail;
   10091              : 
   10092           14 :       dump (dumper::TREE)
   10093            0 :         && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10094              :     }
   10095              : 
   10096     12181843 :   if (TREE_CODE (t) == VAR_DECL)
   10097              :     {
   10098          359 :       DECL_INITIAL (t) = tree_node ();
   10099          359 :       if (TREE_STATIC (t))
   10100            8 :         varpool_node::finalize_decl (t);
   10101              :     }
   10102              : 
   10103     12181843 :   if (TREE_CODE (t) == LAMBDA_EXPR
   10104     12181843 :       && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
   10105              :     {
   10106         1982 :       existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
   10107         1982 :       back_refs[~tag] = existing;
   10108              :     }
   10109              : 
   10110     12185520 :   dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
   10111              : 
   10112     12181843 :   if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
   10113              :     {
   10114       599619 :       existing = cache_integer_cst (t, true);
   10115       599619 :       back_refs[~tag] = existing;
   10116              :     }
   10117              : 
   10118              :   return existing;
   10119              : }
   10120              : 
   10121              : /* Whether DECL has a TU-local dependency in the hash.  */
   10122              : 
   10123              : bool
   10124      1228522 : trees_out::has_tu_local_dep (tree decl) const
   10125              : {
   10126              :   /* Only the contexts of fields or enums remember that they're
   10127              :      TU-local.  */
   10128      1228522 :   if (DECL_CONTEXT (decl)
   10129      1228522 :       && (TREE_CODE (decl) == FIELD_DECL
   10130      1228519 :           || TREE_CODE (decl) == CONST_DECL))
   10131            3 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   10132              : 
   10133      1228522 :   depset *dep = dep_hash->find_dependency (decl);
   10134      1228522 :   if (!dep)
   10135              :     {
   10136              :       /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
   10137              :          which we found was TU-local and gave up early.  */
   10138        15201 :       int use_tpl = -1;
   10139        15201 :       if (tree ti = node_template_info (decl, use_tpl))
   10140         2488 :         dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
   10141              :     }
   10142              : 
   10143      1228522 :   return dep && dep->is_tu_local ();
   10144              : }
   10145              : 
   10146              : /* If T depends on a TU-local entity, return that decl.  */
   10147              : 
   10148              : tree
   10149          395 : trees_out::find_tu_local_decl (tree t)
   10150              : {
   10151              :   /* We need to have walked all deps first before we can check.  */
   10152          395 :   gcc_checking_assert (!is_initial_scan ());
   10153              : 
   10154          951 :   auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
   10155              :     {
   10156          556 :       auto self = (trees_out *)data;
   10157              : 
   10158          556 :       tree decl = NULL_TREE;
   10159          556 :       if (TYPE_P (*tp))
   10160              :         {
   10161              :           /* A PMF type is a record type, which we otherwise wouldn't walk;
   10162              :              return whether the function type is TU-local.  */
   10163          370 :           if (TYPE_PTRMEMFUNC_P (*tp))
   10164              :             {
   10165            3 :               *walk_subtrees = 0;
   10166            3 :               return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
   10167              :             }
   10168              :           else
   10169          367 :             decl = TYPE_MAIN_DECL (*tp);
   10170              :         }
   10171          186 :       else if (DECL_P (*tp))
   10172              :         decl = *tp;
   10173              : 
   10174          373 :       if (decl)
   10175              :         {
   10176              :           /* We found a DECL, this will tell us whether we're TU-local.  */
   10177           59 :           *walk_subtrees = 0;
   10178           59 :           return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
   10179              :         }
   10180              :       return NULL_TREE;
   10181              :     };
   10182              : 
   10183              :   /* We need to walk without duplicates so that we step into the pointed-to
   10184              :      types of array types.  */
   10185          395 :   return cp_walk_tree_without_duplicates (&t, walker, this);
   10186              : }
   10187              : 
   10188              : /* Get the name for TU-local decl T to be used in diagnostics.  */
   10189              : 
   10190              : static tree
   10191          206 : name_for_tu_local_decl (tree t)
   10192              : {
   10193          206 :   int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
   10194          206 :   const char *str = decl_as_string (t, flags);
   10195          206 :   return get_identifier (str);
   10196              : }
   10197              : 
   10198              : /* Stream out tree node T.  We automatically create local back
   10199              :    references, which is essentially a single pass lisp
   10200              :    self-referential structure pretty-printer.  */
   10201              : 
   10202              : void
   10203    354688013 : trees_out::tree_node (tree t)
   10204              : {
   10205    354688013 :   dump.indent ();
   10206    354688013 :   walk_kind ref = ref_node (t);
   10207    354688013 :   if (ref == WK_none)
   10208    270870843 :     goto done;
   10209              : 
   10210              :   /* Find TU-local entities and intercept streaming to instead write a
   10211              :      placeholder value; this way we don't need to emit such decls.
   10212              :      We only need to do this when writing a definition of an entity
   10213              :      that we know names a TU-local entity.  */
   10214     98030352 :   if (!is_initial_scan () && writing_local_entities)
   10215              :     {
   10216          952 :       tree local_decl = NULL_TREE;
   10217          952 :       if (DECL_P (t) && has_tu_local_dep (t))
   10218              :         local_decl = t;
   10219              :       /* Consider a type to be TU-local if it refers to any TU-local decl,
   10220              :          no matter how deep.
   10221              : 
   10222              :          This worsens diagnostics slightly, as we often no longer point
   10223              :          directly to the at-fault entity when instantiating.  However, this
   10224              :          reduces the module size slightly and means that much less of pt.cc
   10225              :          needs to know about us.  */
   10226          848 :       else if (TYPE_P (t))
   10227          142 :         local_decl = find_tu_local_decl (t);
   10228          706 :       else if (EXPR_P (t))
   10229          250 :         local_decl = find_tu_local_decl (TREE_TYPE (t));
   10230              : 
   10231          496 :       if (local_decl)
   10232              :         {
   10233          158 :           int tag = insert (t, WK_value);
   10234          158 :           if (streaming_p ())
   10235              :             {
   10236          158 :               tu_local_count++;
   10237          158 :               i (tt_tu_local);
   10238          158 :               dump (dumper::TREE)
   10239            0 :                 && dump ("Writing TU-local entity:%d %C:%N",
   10240            0 :                          tag, TREE_CODE (t), t);
   10241              :             }
   10242          158 :           tree_node (name_for_tu_local_decl (local_decl));
   10243          158 :           if (state)
   10244          158 :             state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
   10245          158 :           goto done;
   10246              :         }
   10247              :     }
   10248              : 
   10249     83817012 :   if (ref != WK_normal)
   10250      1942280 :     goto skip_normal;
   10251              : 
   10252     81874732 :   if (TREE_CODE (t) == IDENTIFIER_NODE)
   10253              :     {
   10254              :       /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
   10255              :          tt_internal_id.  */
   10256      9704012 :       int code = tt_id;
   10257      9704012 :       if (IDENTIFIER_ANON_P (t))
   10258        38418 :         code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
   10259      9665594 :       else if (IDENTIFIER_INTERNAL_P (t))
   10260              :         code = tt_internal_id;
   10261      9665580 :       else if (IDENTIFIER_CONV_OP_P (t))
   10262        14586 :         code = tt_conv_id;
   10263              : 
   10264      9704012 :       if (streaming_p ())
   10265      1896514 :         i (code);
   10266              : 
   10267      9704012 :       if (code == tt_conv_id)
   10268              :         {
   10269        14586 :           tree type = TREE_TYPE (t);
   10270        14586 :           gcc_checking_assert (type || t == conv_op_identifier);
   10271        14586 :           tree_node (type);
   10272              :         }
   10273      9689426 :       else if (code == tt_id && streaming_p ())
   10274      1880520 :         str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
   10275      7808906 :       else if (code == tt_internal_id && streaming_p ())
   10276            7 :         str (prefix_for_internal_label (t));
   10277              : 
   10278      9704012 :       int tag = insert (t);
   10279      9704012 :       if (streaming_p ())
   10280              :         {
   10281              :           /* We know the ordering of the 5 id tags.  */
   10282      1896514 :           static const char *const kinds[] =
   10283              :             {"", "conv_op ", "anon ", "lambda ", "internal "};
   10284      1896514 :           dump (dumper::TREE)
   10285         1074 :             && dump ("Written:%d %sidentifier:%N", tag,
   10286         1071 :                      kinds[code - tt_id],
   10287            3 :                      code == tt_conv_id ? TREE_TYPE (t) : t);
   10288              :         }
   10289      9704012 :       goto done;
   10290              :     }
   10291              : 
   10292     72170720 :   if (TREE_CODE (t) == TREE_BINFO)
   10293              :     {
   10294              :       /* A BINFO -> tt_binfo.
   10295              :          We must do this by reference.  We stream the binfo tree
   10296              :          itself when streaming its owning RECORD_TYPE.  That we got
   10297              :          here means the dominating type is not in this SCC.  */
   10298        84188 :       if (streaming_p ())
   10299         2693 :         i (tt_binfo);
   10300        84188 :       binfo_mergeable (t);
   10301        84188 :       gcc_checking_assert (!TREE_VISITED (t));
   10302        84188 :       int tag = insert (t);
   10303        84188 :       if (streaming_p ())
   10304         2693 :         dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
   10305        84188 :       goto done;
   10306              :     }
   10307              : 
   10308     72086532 :   if (TREE_CODE (t) == INTEGER_CST
   10309      5110028 :       && !TREE_OVERFLOW (t)
   10310     77196560 :       && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
   10311              :     {
   10312              :       /* An integral constant of enumeral type.  See if it matches one
   10313              :          of the enumeration values.  */
   10314        50932 :       for (tree values = TYPE_VALUES (TREE_TYPE (t));
   10315      1045276 :            values; values = TREE_CHAIN (values))
   10316              :         {
   10317      1042570 :           tree decl = TREE_VALUE (values);
   10318      1042570 :           if (tree_int_cst_equal (DECL_INITIAL (decl), t))
   10319              :             {
   10320        48226 :               if (streaming_p ())
   10321        14036 :                 u (tt_enum_value);
   10322        48226 :               tree_node (decl);
   10323        48268 :               dump (dumper::TREE) && dump ("Written enum value %N", decl);
   10324        48226 :               goto done;
   10325              :             }
   10326              :         }
   10327              :       /* It didn't match.  We'll write it a an explicit INTEGER_CST
   10328              :          node.  */
   10329              :     }
   10330              : 
   10331     72038306 :   if (TYPE_P (t))
   10332              :     {
   10333     13677391 :       type_node (t);
   10334     13677391 :       goto done;
   10335              :     }
   10336              : 
   10337     58360915 :   if (DECL_P (t))
   10338              :     {
   10339     18049344 :       if (DECL_TEMPLATE_PARM_P (t))
   10340              :         {
   10341      2783951 :           tpl_parm_value (t);
   10342      2783951 :           goto done;
   10343              :         }
   10344              : 
   10345     15265393 :       if (!DECL_CONTEXT (t))
   10346              :         {
   10347              :           /* There are a few cases of decls with no context.  We'll write
   10348              :              these by value, but first assert they are cases we expect.  */
   10349        32806 :           gcc_checking_assert (ref == WK_normal);
   10350        32806 :           switch (TREE_CODE (t))
   10351              :             {
   10352            0 :             default: gcc_unreachable ();
   10353              : 
   10354        12944 :             case LABEL_DECL:
   10355              :               /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs.  */
   10356        12944 :               gcc_checking_assert (!DECL_NAME (t));
   10357              :               break;
   10358              : 
   10359          824 :             case VAR_DECL:
   10360              :               /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
   10361              :                  and internal vars are created by sanitizers and
   10362              :                  __builtin_source_location.  */
   10363          824 :               gcc_checking_assert ((!DECL_NAME (t)
   10364              :                                     || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
   10365              :                                    && DECL_ARTIFICIAL (t));
   10366              :               break;
   10367              : 
   10368        19010 :             case PARM_DECL:
   10369              :               /* REQUIRES_EXPRs have a chain of uncontexted PARM_DECLS,
   10370              :                  and an implicit this parm in an NSDMI has no context.  */
   10371        19010 :               gcc_checking_assert (CONSTRAINT_VAR_P (t)
   10372              :                                    || DECL_NAME (t) == this_identifier);
   10373              :               break;
   10374              : 
   10375           28 :             case TYPE_DECL:
   10376              :               /* Some parts of the compiler need internal struct types;
   10377              :                  these types may not have an appropriate context to use.
   10378              :                  Walk the whole type (including its definition) by value.  */
   10379           28 :               gcc_checking_assert (DECL_ARTIFICIAL (t)
   10380              :                                    && TYPE_ARTIFICIAL (TREE_TYPE (t))
   10381              :                                    && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
   10382              :                                    && !CLASS_TYPE_P (TREE_TYPE (t)));
   10383              :               break;
   10384              :             }
   10385        32806 :           mark_declaration (t, has_definition (t));
   10386        32806 :           goto by_value;
   10387              :         }
   10388              :     }
   10389              : 
   10390     40311571 :  skip_normal:
   10391     57486438 :   if (DECL_P (t) && !decl_node (t, ref))
   10392     15797418 :     goto done;
   10393              : 
   10394              :   /* Otherwise by value */
   10395     41721826 :  by_value:
   10396     41721826 :   tree_value (t);
   10397              : 
   10398    354688013 :  done:
   10399              :   /* And, breath out.  */
   10400    354688013 :   dump.outdent ();
   10401    354688013 : }
   10402              : 
   10403              : /* Stream in a tree node.  */
   10404              : 
   10405              : tree
   10406     98973120 : trees_in::tree_node (bool is_use)
   10407              : {
   10408     98973120 :   if (get_overrun ())
   10409              :     return NULL_TREE;
   10410              : 
   10411     98973120 :   dump.indent ();
   10412     98973120 :   int tag = i ();
   10413     98973120 :   tree res = NULL_TREE;
   10414     98973120 :   switch (tag)
   10415              :     {
   10416     32546583 :     default:
   10417              :       /* backref, pull it out of the map.  */
   10418     32546583 :       res = back_ref (tag);
   10419     32546583 :       break;
   10420              : 
   10421              :     case tt_null:
   10422              :       /* NULL_TREE.  */
   10423              :       break;
   10424              : 
   10425          158 :     case tt_tu_local:
   10426          158 :       {
   10427              :         /* A translation-unit-local entity.  */
   10428          158 :         res = make_node (TU_LOCAL_ENTITY);
   10429          158 :         int tag = insert (res);
   10430              : 
   10431          158 :         TU_LOCAL_ENTITY_NAME (res) = tree_node ();
   10432          158 :         TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
   10433          158 :         dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
   10434              :       }
   10435              :       break;
   10436              : 
   10437      7613180 :     case tt_fixed:
   10438              :       /* A fixed ref, find it in the fixed_ref array.   */
   10439      7613180 :       {
   10440      7613180 :         unsigned fix = u ();
   10441      7613180 :         if (fix < (*fixed_trees).length ())
   10442              :           {
   10443      7613180 :             res = (*fixed_trees)[fix];
   10444      7613180 :             dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
   10445         5046 :                                          TREE_CODE (res), res, res);
   10446              :           }
   10447              : 
   10448      7613180 :         if (!res)
   10449            0 :           set_overrun ();
   10450              :       }
   10451              :       break;
   10452              : 
   10453        83748 :     case tt_parm:
   10454        83748 :       {
   10455        83748 :         tree fn = tree_node ();
   10456        83748 :         if (fn && TREE_CODE (fn) == FUNCTION_DECL)
   10457        83748 :           res = tree_node ();
   10458        83748 :         if (res)
   10459        83748 :           dump (dumper::TREE)
   10460           21 :             && dump ("Read %s reference %N",
   10461           21 :                      TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
   10462              :                      res);
   10463              :       }
   10464              :       break;
   10465              : 
   10466     12181843 :     case tt_node:
   10467              :       /* A new node.  Stream it in.  */
   10468     12181843 :       res = tree_value ();
   10469     12181843 :       break;
   10470              : 
   10471      1239215 :     case tt_decl:
   10472              :       /* A new decl.  Stream it in.  */
   10473      1239215 :       res = decl_value ();
   10474      1239215 :       break;
   10475              : 
   10476       435056 :     case tt_tpl_parm:
   10477              :       /* A template parameter.  Stream it in.  */
   10478       435056 :       res = tpl_parm_value ();
   10479       435056 :       break;
   10480              : 
   10481      1255343 :     case tt_id:
   10482              :       /* An identifier node.  */
   10483      1255343 :       {
   10484      1255343 :         size_t l;
   10485      1255343 :         const char *chars = str (&l);
   10486      1255343 :         res = get_identifier_with_length (chars, l);
   10487      1255343 :         int tag = insert (res);
   10488      1255343 :         dump (dumper::TREE)
   10489         1488 :           && dump ("Read identifier:%d %N", tag, res);
   10490              :       }
   10491      1255343 :       break;
   10492              : 
   10493         3305 :     case tt_conv_id:
   10494              :       /* A conversion operator.  Get the type and recreate the
   10495              :          identifier.  */
   10496         3305 :       {
   10497         3305 :         tree type = tree_node ();
   10498         3305 :         if (!get_overrun ())
   10499              :           {
   10500         3305 :             res = type ? make_conv_op_name (type) : conv_op_identifier;
   10501         3305 :             int tag = insert (res);
   10502         3305 :             dump (dumper::TREE)
   10503           27 :               && dump ("Created conv_op:%d %S for %N", tag, res, type);
   10504              :           }
   10505              :       }
   10506              :       break;
   10507              : 
   10508         6810 :     case tt_anon_id:
   10509         6810 :     case tt_lambda_id:
   10510              :       /* An anonymous or lambda id.  */
   10511         6810 :       {
   10512         6810 :         res = make_anon_name ();
   10513         6810 :         if (tag == tt_lambda_id)
   10514         4042 :           IDENTIFIER_LAMBDA_P (res) = true;
   10515         6810 :         int tag = insert (res);
   10516         6810 :         dump (dumper::TREE)
   10517            3 :           && dump ("Read %s identifier:%d %N",
   10518            3 :                    IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
   10519              :       }
   10520              :       break;
   10521              : 
   10522            8 :     case tt_internal_id:
   10523              :       /* An internal label.  */
   10524            8 :       {
   10525            8 :         const char *prefix = str ();
   10526            8 :         res = generate_internal_label (prefix);
   10527            8 :         int tag = insert (res);
   10528            8 :         dump (dumper::TREE)
   10529            1 :           && dump ("Read internal identifier:%d %N", tag, res);
   10530              :       }
   10531              :       break;
   10532              : 
   10533       198602 :     case tt_typedef_type:
   10534       198602 :       res = tree_node ();
   10535       198602 :       if (res)
   10536              :         {
   10537       198602 :           dump (dumper::TREE)
   10538           74 :             && dump ("Read %stypedef %C:%N",
   10539           74 :                      DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
   10540           74 :                      TREE_CODE (res), res);
   10541       198602 :           if (TREE_CODE (res) != TU_LOCAL_ENTITY)
   10542       198601 :             res = TREE_TYPE (res);
   10543              :         }
   10544              :       break;
   10545              : 
   10546      1516137 :     case tt_derived_type:
   10547              :       /* A type derived from some other type.  */
   10548      1516137 :       {
   10549      1516137 :         enum tree_code code = tree_code (u ());
   10550      1516137 :         res = tree_node ();
   10551              : 
   10552      1516137 :         switch (code)
   10553              :           {
   10554            0 :           default:
   10555            0 :             set_overrun ();
   10556            0 :             break;
   10557              : 
   10558        21494 :           case ARRAY_TYPE:
   10559        21494 :             {
   10560        21494 :               tree elt_type = res;
   10561        21494 :               tree domain = tree_node ();
   10562        21494 :               int dep = u ();
   10563        21494 :               if (!get_overrun ())
   10564              :                 {
   10565        21494 :                   res = build_cplus_array_type (elt_type, domain, dep);
   10566              :                   /* If we're an array of an incomplete imported type,
   10567              :                      save it for post-processing so that we can attempt
   10568              :                      to complete the type later if it will get a
   10569              :                      definition later in the cluster.  */
   10570        21494 :                   if (!dep
   10571        18635 :                       && !COMPLETE_TYPE_P (elt_type)
   10572           36 :                       && CLASS_TYPE_P (elt_type)
   10573           36 :                       && DECL_LANG_SPECIFIC (TYPE_NAME (elt_type))
   10574        21530 :                       && DECL_MODULE_IMPORT_P (TYPE_NAME (elt_type)))
   10575           36 :                     post_process_type (res);
   10576              :                 }
   10577              :             }
   10578              :             break;
   10579              : 
   10580          266 :           case COMPLEX_TYPE:
   10581          266 :             if (!get_overrun ())
   10582          266 :               res = build_complex_type (res);
   10583              :             break;
   10584              : 
   10585            9 :           case BOOLEAN_TYPE:
   10586            9 :             {
   10587            9 :               unsigned precision = u ();
   10588            9 :               if (!get_overrun ())
   10589            9 :                 res = build_nonstandard_boolean_type (precision);
   10590              :             }
   10591              :             break;
   10592              : 
   10593        19681 :           case INTEGER_TYPE:
   10594        19681 :             if (res)
   10595              :               {
   10596              :                 /* A range type (representing an array domain).  */
   10597        18632 :                 tree min = tree_node ();
   10598        18632 :                 tree max = tree_node ();
   10599              : 
   10600        18632 :                 if (!get_overrun ())
   10601        18632 :                   res = build_range_type (res, min, max);
   10602              :               }
   10603              :             else
   10604              :               {
   10605              :                 /* A new integral type (representing a bitfield).  */
   10606         1049 :                 unsigned enc = u ();
   10607         1049 :                 if (!get_overrun ())
   10608         1049 :                   res = build_nonstandard_integer_type (enc >> 1, enc & 1);
   10609              :               }
   10610              :             break;
   10611              : 
   10612       428667 :           case FUNCTION_TYPE:
   10613       428667 :           case METHOD_TYPE:
   10614       428667 :             {
   10615       428667 :               tree klass =  code == METHOD_TYPE ? tree_node () : NULL_TREE;
   10616       428667 :               tree args = tree_node ();
   10617       428667 :               if (!get_overrun ())
   10618              :                 {
   10619       428667 :                   if (klass)
   10620       272727 :                     res = build_method_type_directly (klass, res, args);
   10621              :                   else
   10622       155940 :                     res = cp_build_function_type (res, args);
   10623              :                 }
   10624              :             }
   10625              :             break;
   10626              : 
   10627          264 :           case OFFSET_TYPE:
   10628          264 :             {
   10629          264 :               tree base = tree_node ();
   10630          264 :               if (!get_overrun ())
   10631          264 :                 res = build_offset_type (base, res);
   10632              :             }
   10633              :             break;
   10634              : 
   10635       206276 :           case POINTER_TYPE:
   10636       206276 :             if (!get_overrun ())
   10637       206276 :               res = build_pointer_type (res);
   10638              :             break;
   10639              : 
   10640       178078 :           case REFERENCE_TYPE:
   10641       178078 :             {
   10642       178078 :               bool rval = bool (u ());
   10643       178078 :               if (!get_overrun ())
   10644       178078 :                 res = cp_build_reference_type (res, rval);
   10645              :             }
   10646              :             break;
   10647              : 
   10648       462593 :           case DECLTYPE_TYPE:
   10649       462593 :           case TYPEOF_TYPE:
   10650       462593 :           case DEPENDENT_OPERATOR_TYPE:
   10651       462593 :             {
   10652       462593 :               tree expr = tree_node ();
   10653       462593 :               if (!get_overrun ())
   10654              :                 {
   10655       462593 :                   res = cxx_make_type (code);
   10656       462593 :                   TYPE_VALUES_RAW (res) = expr;
   10657       462593 :                   if (code == DECLTYPE_TYPE)
   10658        22096 :                     tree_node_bools (res);
   10659       462593 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10660              :                 }
   10661              :             }
   10662              :             break;
   10663              : 
   10664         2307 :           case TRAIT_TYPE:
   10665         2307 :             {
   10666         2307 :               tree kind = tree_node ();
   10667         2307 :               tree type1 = tree_node ();
   10668         2307 :               tree type2 = tree_node ();
   10669         2307 :               if (!get_overrun ())
   10670              :                 {
   10671         2307 :                   res = cxx_make_type (TRAIT_TYPE);
   10672         2307 :                   TRAIT_TYPE_KIND_RAW (res) = kind;
   10673         2307 :                   TRAIT_TYPE_TYPE1 (res) = type1;
   10674         2307 :                   TRAIT_TYPE_TYPE2 (res) = type2;
   10675         2307 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10676              :                 }
   10677              :             }
   10678              :             break;
   10679              : 
   10680        62457 :           case TYPE_ARGUMENT_PACK:
   10681        62457 :             if (!get_overrun ())
   10682              :               {
   10683        62457 :                 tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   10684        62457 :                 ARGUMENT_PACK_ARGS (pack) = res;
   10685        62457 :                 res = pack;
   10686              :               }
   10687              :             break;
   10688              : 
   10689        65811 :           case TYPE_PACK_EXPANSION:
   10690        65811 :             {
   10691        65811 :               bool local = u ();
   10692        65811 :               tree param_packs = tree_node ();
   10693        65811 :               tree extra_args = tree_node ();
   10694        65811 :               if (!get_overrun ())
   10695              :                 {
   10696        65811 :                   tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
   10697        65811 :                   SET_TYPE_STRUCTURAL_EQUALITY (expn);
   10698        65811 :                   PACK_EXPANSION_PATTERN (expn) = res;
   10699       131622 :                   PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
   10700        65811 :                   PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
   10701        65811 :                   PACK_EXPANSION_LOCAL_P (expn) = local;
   10702        65811 :                   res = expn;
   10703              :                 }
   10704              :             }
   10705              :             break;
   10706              : 
   10707           25 :           case PACK_INDEX_TYPE:
   10708           25 :             {
   10709           25 :               tree pack = tree_node ();
   10710           25 :               tree index = tree_node ();
   10711           25 :               if (!get_overrun ())
   10712           25 :                 res = make_pack_index (pack, index);
   10713              :             }
   10714              :             break;
   10715              : 
   10716        68033 :           case TYPENAME_TYPE:
   10717        68033 :             {
   10718        68033 :               tree ctx = tree_node ();
   10719        68033 :               tree name = tree_node ();
   10720        68033 :               tree fullname = tree_node ();
   10721        68033 :               enum tag_types tag_type = tag_types (u ());
   10722              : 
   10723        68033 :               if (!get_overrun ())
   10724        68033 :                 res = build_typename_type (ctx, name, fullname, tag_type);
   10725              :             }
   10726              :             break;
   10727              : 
   10728           52 :           case UNBOUND_CLASS_TEMPLATE:
   10729           52 :             {
   10730           52 :               tree ctx = tree_node ();
   10731           52 :               tree name = tree_node ();
   10732           52 :               tree parms = tree_node ();
   10733              : 
   10734           52 :               if (!get_overrun ())
   10735           52 :                 res = make_unbound_class_template_raw (ctx, name, parms);
   10736              :             }
   10737              :             break;
   10738              : 
   10739              :           case VECTOR_TYPE:
   10740              :             {
   10741              :               poly_uint64 nunits;
   10742           60 :               for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
   10743           30 :                 nunits.coeffs[ix] = wu ();
   10744           30 :               if (!get_overrun ())
   10745           30 :                 res = build_vector_type (res, nunits);
   10746              :             }
   10747              :             break;
   10748              : 
   10749              :           /* LANG_TYPE can be more things, but here we assume it represents
   10750              :              std::meta::info.  Unfortunately here it's not possible to check
   10751              :              REFLECTION_TYPE_P.  */
   10752           90 :           case LANG_TYPE:
   10753           90 :             if (!get_overrun ())
   10754           90 :               res = meta_info_type_node;
   10755              :             break;
   10756              : 
   10757            4 :           case SPLICE_SCOPE:
   10758            4 :             {
   10759            4 :               bool type = u ();
   10760            4 :               tree expr = tree_node ();
   10761              : 
   10762            4 :               if (!get_overrun ())
   10763            4 :                 res = make_splice_scope (expr, type);
   10764              :             }
   10765              :             break;
   10766              :           }
   10767              : 
   10768              :         /* In the exporting TU, a derived type with attributes was built by
   10769              :            build_type_attribute_variant as a distinct copy, with itself as
   10770              :            TYPE_MAIN_VARIANT.  We repeat that on import to get the version
   10771              :            without attributes as TYPE_CANONICAL.  */
   10772      1516137 :         if (tree attribs = tree_node ())
   10773        17314 :           res = cp_build_type_attribute_variant (res, attribs);
   10774              : 
   10775      1516137 :         int tag = i ();
   10776      1516137 :         if (!tag)
   10777              :           {
   10778      1295890 :             tag = insert (res);
   10779      1295890 :             if (res)
   10780      1295890 :               dump (dumper::TREE)
   10781          678 :                 && dump ("Created:%d derived type %C", tag, code);
   10782              :           }
   10783              :         else
   10784       220247 :           res = back_ref (tag);
   10785              :       }
   10786              :       break;
   10787              : 
   10788       435439 :     case tt_variant_type:
   10789              :       /* Variant of some type.  */
   10790       435439 :       {
   10791       435439 :         res = tree_node ();
   10792       435439 :         int flags = i ();
   10793       435439 :         if (get_overrun ())
   10794              :           ;
   10795       435439 :         else if (flags < 0)
   10796              :           /* No change.  */;
   10797       209763 :         else if (TREE_CODE (res) == FUNCTION_TYPE
   10798       209763 :                  || TREE_CODE (res) == METHOD_TYPE)
   10799              :           {
   10800       208190 :             cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
   10801       208190 :             bool late = (flags >> 2) & 1;
   10802       208190 :             cp_cv_quals quals = cp_cv_quals (flags >> 3);
   10803              : 
   10804       208190 :             tree raises = tree_node ();
   10805       208190 :             if (raises == error_mark_node)
   10806         7275 :               raises = TYPE_RAISES_EXCEPTIONS (res);
   10807              : 
   10808       208190 :             res = build_cp_fntype_variant (res, rqual, raises, late);
   10809       208190 :             if (TREE_CODE (res) == FUNCTION_TYPE)
   10810        73458 :               res = apply_memfn_quals (res, quals, rqual);
   10811              :           }
   10812              :         else
   10813              :           {
   10814         1573 :             res = build_aligned_type (res, (1u << flags) >> 1);
   10815         1573 :             TYPE_USER_ALIGN (res) = true;
   10816              :           }
   10817              : 
   10818       435439 :         int quals = i ();
   10819       435439 :         if (quals >= 0 && !get_overrun ())
   10820       226812 :           res = cp_build_qualified_type (res, quals);
   10821              : 
   10822       435439 :         int tag = i ();
   10823       435439 :         if (!tag)
   10824              :           {
   10825       435439 :             tag = insert (res);
   10826       435439 :             if (res)
   10827       435439 :               dump (dumper::TREE)
   10828          292 :                 && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
   10829              :           }
   10830              :         else
   10831            0 :           res = back_ref (tag);
   10832              :       }
   10833              :       break;
   10834              : 
   10835         5102 :     case tt_tinfo_var:
   10836         5102 :     case tt_tinfo_typedef:
   10837              :       /* A tinfo var or typedef.  */
   10838         5102 :       {
   10839         5102 :         bool is_var = tag == tt_tinfo_var;
   10840         5102 :         unsigned ix = u ();
   10841         5102 :         tree type = NULL_TREE;
   10842              : 
   10843         5102 :         if (is_var)
   10844              :           {
   10845         3095 :             tree name = tree_node ();
   10846         3095 :             type = tree_node ();
   10847              : 
   10848         3095 :             if (!get_overrun ())
   10849         3095 :               res = get_tinfo_decl_direct (type, name, int (ix));
   10850              :           }
   10851              :         else
   10852              :           {
   10853         2007 :             if (!get_overrun ())
   10854              :               {
   10855         2007 :                 type = get_pseudo_tinfo_type (ix);
   10856         2007 :                 res = TYPE_NAME (type);
   10857              :               }
   10858              :           }
   10859         5102 :         if (res)
   10860              :           {
   10861         5102 :             int tag = insert (res);
   10862         5102 :             dump (dumper::TREE)
   10863           36 :               && dump ("Created tinfo_%s:%d %S:%u for %N",
   10864              :                        is_var ? "var" : "decl", tag, res, ix, type);
   10865         5102 :             if (!is_var)
   10866              :               {
   10867         2007 :                 tag = insert (type);
   10868         2007 :                 dump (dumper::TREE)
   10869           12 :                   && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
   10870              :               }
   10871              :           }
   10872              :       }
   10873              :       break;
   10874              : 
   10875         1066 :     case tt_ptrmem_type:
   10876              :       /* A pointer to member function.  */
   10877         1066 :       {
   10878         1066 :         tree type = tree_node ();
   10879         1066 :         if (type && TREE_CODE (type) == POINTER_TYPE
   10880         2132 :             && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
   10881              :           {
   10882         1066 :             res = build_ptrmemfunc_type (type);
   10883         1066 :             int tag = insert (res);
   10884         1069 :             dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
   10885              :           }
   10886              :         else
   10887            0 :           set_overrun ();
   10888              :       }
   10889              :       break;
   10890              : 
   10891            9 :     case tt_nttp_var:
   10892              :       /* An NTTP object. */
   10893            9 :       {
   10894            9 :         tree init = tree_node ();
   10895            9 :         tree name = tree_node ();
   10896            9 :         if (!get_overrun ())
   10897              :           {
   10898              :             /* We don't want to check the initializer as that may require
   10899              :                name lookup, which could recursively start lazy loading.
   10900              :                Instead we know that INIT is already valid so we can just
   10901              :                apply that directly.  */
   10902            9 :             res = get_template_parm_object (init, name, /*check_init=*/false);
   10903            9 :             int tag = insert (res);
   10904            9 :             dump (dumper::TREE)
   10905            0 :               && dump ("Created nttp object:%d %N", tag, name);
   10906            9 :             vec_safe_push (post_load_decls, res);
   10907              :           }
   10908              :       }
   10909              :       break;
   10910              : 
   10911         7961 :     case tt_enum_value:
   10912              :       /* An enum const value.  */
   10913         7961 :       {
   10914         7961 :         if (tree decl = tree_node ())
   10915              :           {
   10916         7979 :             dump (dumper::TREE) && dump ("Read enum value %N", decl);
   10917         7961 :             res = DECL_INITIAL (decl);
   10918              :           }
   10919              : 
   10920         7961 :         if (!res)
   10921            0 :           set_overrun ();
   10922              :       }
   10923              :       break;
   10924              : 
   10925        14187 :     case tt_enum_decl:
   10926              :       /* An enum decl.  */
   10927        14187 :       {
   10928        14187 :         tree ctx = tree_node ();
   10929        14187 :         tree name = tree_node ();
   10930              : 
   10931        14187 :         if (!get_overrun ()
   10932        14187 :             && TREE_CODE (ctx) == ENUMERAL_TYPE)
   10933        14187 :           res = find_enum_member (ctx, name);
   10934              : 
   10935        14187 :         if (!res)
   10936            0 :           set_overrun ();
   10937              :         else
   10938              :           {
   10939        14187 :             int tag = insert (res);
   10940        14187 :             dump (dumper::TREE)
   10941           18 :               && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
   10942              :           }
   10943              :       }
   10944              :       break;
   10945              : 
   10946         9118 :     case tt_data_member:
   10947              :       /* A data member.  */
   10948         9118 :       {
   10949         9118 :         tree ctx = tree_node ();
   10950         9118 :         tree name = tree_node ();
   10951              : 
   10952         9118 :         if (!get_overrun ()
   10953         9118 :             && RECORD_OR_UNION_TYPE_P (ctx))
   10954              :           {
   10955         9118 :             if (name)
   10956         8063 :               res = lookup_class_binding (ctx, name);
   10957              :             else
   10958         1055 :               res = lookup_field_ident (ctx, u ());
   10959              : 
   10960         9118 :             if (!res
   10961         9118 :                 || (TREE_CODE (res) != FIELD_DECL
   10962         9118 :                     && TREE_CODE (res) != USING_DECL)
   10963        18236 :                 || DECL_CONTEXT (res) != ctx)
   10964            0 :               res = NULL_TREE;
   10965              :           }
   10966              : 
   10967         9118 :         if (!res)
   10968            0 :           set_overrun ();
   10969              :         else
   10970              :           {
   10971         9118 :             int tag = insert (res);
   10972         9118 :             dump (dumper::TREE)
   10973           26 :               && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
   10974              :           }
   10975              :       }
   10976              :       break;
   10977              : 
   10978         1667 :     case tt_binfo:
   10979              :       /* A BINFO.  Walk the tree of the dominating type.  */
   10980         1667 :       {
   10981         1667 :         tree type;
   10982         1667 :         unsigned ix = binfo_mergeable (&type);
   10983         1667 :         if (type)
   10984              :           {
   10985         1667 :             res = TYPE_BINFO (type);
   10986         1753 :             for (; ix && res; res = TREE_CHAIN (res))
   10987           86 :               ix--;
   10988         1667 :             if (!res)
   10989            0 :               set_overrun ();
   10990              :           }
   10991              : 
   10992         1667 :         if (get_overrun ())
   10993              :           break;
   10994              : 
   10995              :         /* Insert binfo into backreferences.  */
   10996         1667 :         tag = insert (res);
   10997         1667 :         dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
   10998              :       }
   10999         1667 :       break;
   11000              : 
   11001           73 :     case tt_vtable:
   11002           73 :       {
   11003           73 :         unsigned ix = u ();
   11004           73 :         tree ctx = tree_node ();
   11005           73 :         dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
   11006           73 :         if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
   11007           85 :           for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
   11008           85 :             if (!ix--)
   11009              :               break;
   11010           73 :         if (!res)
   11011            0 :           set_overrun ();
   11012              :       }
   11013              :       break;
   11014              : 
   11015            0 :     case tt_thunk:
   11016            0 :       {
   11017            0 :         int fixed = i ();
   11018            0 :         tree target = tree_node ();
   11019            0 :         tree virt = tree_node ();
   11020              : 
   11021            0 :         for (tree thunk = DECL_THUNKS (target);
   11022            0 :              thunk; thunk = DECL_CHAIN (thunk))
   11023            0 :           if (THUNK_FIXED_OFFSET (thunk) == fixed
   11024            0 :               && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
   11025            0 :               && (!virt
   11026            0 :                   || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
   11027              :             {
   11028            0 :               res = thunk;
   11029            0 :               break;
   11030              :             }
   11031              : 
   11032            0 :         int tag = insert (res);
   11033            0 :         if (res)
   11034            0 :           dump (dumper::TREE)
   11035            0 :             && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
   11036              :         else
   11037            0 :           set_overrun ();
   11038              :       }
   11039              :       break;
   11040              : 
   11041       157646 :     case tt_clone_ref:
   11042       157646 :       {
   11043       157646 :         tree target = tree_node ();
   11044       157646 :         tree name = tree_node ();
   11045              : 
   11046       157646 :         if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
   11047              :           {
   11048       157646 :             tree clone;
   11049       244722 :             FOR_EVERY_CLONE (clone, target)
   11050       244722 :               if (DECL_NAME (clone) == name)
   11051              :                 {
   11052       157646 :                   res = clone;
   11053       157646 :                   break;
   11054              :                 }
   11055              :           }
   11056              : 
   11057              :         /* A clone might have a different vtable entry.  */
   11058       157646 :         if (res && DECL_VIRTUAL_P (res))
   11059         8625 :           DECL_VINDEX (res) = tree_node ();
   11060              : 
   11061       157646 :         if (!res)
   11062            0 :           set_overrun ();
   11063       157646 :         int tag = insert (res);
   11064       157646 :         if (res)
   11065       157646 :           dump (dumper::TREE)
   11066          230 :             && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
   11067              :         else
   11068            0 :           set_overrun ();
   11069              :        }
   11070              :       break;
   11071              : 
   11072      1106661 :     case tt_entity:
   11073              :       /* Index into the entity table.  Perhaps not loaded yet!  */
   11074      1106661 :       {
   11075      1106661 :         unsigned origin = state->slurp->remap_module (u ());
   11076      1106661 :         unsigned ident = u ();
   11077      1106661 :         module_state *from = (*modules)[origin];
   11078              : 
   11079      1106661 :         if (!origin || ident >= from->entity_num)
   11080            0 :           set_overrun ();
   11081      1106661 :         if (!get_overrun ())
   11082              :           {
   11083      1106661 :             binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
   11084      1106661 :             if (slot->is_lazy ())
   11085        55849 :               if (!from->lazy_load (ident, slot))
   11086            0 :                 set_overrun ();
   11087      1106661 :             res = *slot;
   11088              :           }
   11089              : 
   11090      1106661 :         if (res)
   11091              :           {
   11092      1106661 :             const char *kind = (origin != state->mod ? "Imported" : "Named");
   11093      1106661 :             int tag = insert (res);
   11094      1106661 :             dump (dumper::TREE)
   11095          605 :               && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
   11096          605 :                        res, (*modules)[origin]);
   11097              : 
   11098      1106661 :             if (!add_indirects (res))
   11099              :               {
   11100            0 :                 set_overrun ();
   11101            0 :                 res = NULL_TREE;
   11102              :               }
   11103              :           }
   11104              :       }
   11105              :       break;
   11106              : 
   11107         3132 :     case tt_template:
   11108              :       /* A template.  */
   11109         3132 :       if (tree tpl = tree_node ())
   11110              :         {
   11111         3132 :           res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
   11112         3132 :                  tpl : DECL_TEMPLATE_RESULT (tpl));
   11113         3132 :           dump (dumper::TREE)
   11114            9 :             && dump ("Read template %C:%N", TREE_CODE (res), res);
   11115              :         }
   11116              :       break;
   11117              :     }
   11118              : 
   11119     98973120 :   if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
   11120              :     {
   11121              :       /* Mark decl used as mark_used does -- we cannot call
   11122              :          mark_used in the middle of streaming, we only need a subset
   11123              :          of its functionality.   */
   11124       772899 :       TREE_USED (res) = true;
   11125              : 
   11126              :       /* And for structured bindings also the underlying decl.  */
   11127       772899 :       if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
   11128         2038 :         TREE_USED (DECL_DECOMP_BASE (res)) = true;
   11129              : 
   11130       772899 :       if (DECL_CLONED_FUNCTION_P (res))
   11131         7381 :         TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
   11132              :     }
   11133              : 
   11134     98973120 :   dump.outdent ();
   11135     98973120 :   return res;
   11136              : }
   11137              : 
   11138              : void
   11139      2277334 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
   11140              : {
   11141      2277334 :   if (!parms)
   11142              :     return;
   11143              : 
   11144      1535227 :   if (TREE_VISITED (parms))
   11145              :     {
   11146       640446 :       ref_node (parms);
   11147       640446 :       return;
   11148              :     }
   11149              : 
   11150       894781 :   tpl_parms (TREE_CHAIN (parms), tpl_levels);
   11151              : 
   11152       894781 :   tree vec = TREE_VALUE (parms);
   11153       894781 :   unsigned len = TREE_VEC_LENGTH (vec);
   11154              :   /* Depth.  */
   11155       894781 :   int tag = insert (parms);
   11156       894781 :   if (streaming_p ())
   11157              :     {
   11158       237839 :       i (len + 1);
   11159       237905 :       dump (dumper::TREE)
   11160           66 :         && dump ("Writing template parms:%d level:%N length:%d",
   11161           66 :                  tag, TREE_PURPOSE (parms), len);
   11162              :     }
   11163       894781 :   tree_node (TREE_PURPOSE (parms));
   11164              : 
   11165      3372555 :   for (unsigned ix = 0; ix != len; ix++)
   11166              :     {
   11167      1582993 :       tree parm = TREE_VEC_ELT (vec, ix);
   11168      1582993 :       tree decl = TREE_VALUE (parm);
   11169              : 
   11170      1582993 :       gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
   11171      1582993 :       if (CHECKING_P)
   11172      1582993 :         switch (TREE_CODE (decl))
   11173              :           {
   11174            0 :           default: gcc_unreachable ();
   11175              : 
   11176         4017 :           case TEMPLATE_DECL:
   11177         4017 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
   11178              :                         && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
   11179              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11180              :             break;
   11181              : 
   11182      1477112 :           case TYPE_DECL:
   11183      1477112 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
   11184              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11185              :             break;
   11186              : 
   11187       101864 :           case PARM_DECL:
   11188       101864 :             gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
   11189              :                         && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
   11190              :                             == CONST_DECL)
   11191              :                         && (DECL_TEMPLATE_PARM_P
   11192              :                             (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
   11193              :             break;
   11194              :           }
   11195              : 
   11196      1582993 :       tree_node (decl);
   11197      1582993 :       tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
   11198              :     }
   11199              : 
   11200       894781 :   tpl_levels++;
   11201              : }
   11202              : 
   11203              : tree
   11204       335589 : trees_in::tpl_parms (unsigned &tpl_levels)
   11205              : {
   11206       335589 :   tree parms = NULL_TREE;
   11207              : 
   11208       708508 :   while (int len = i ())
   11209              :     {
   11210       372919 :       if (len < 0)
   11211              :         {
   11212       202673 :           parms = back_ref (len);
   11213       202673 :           continue;
   11214              :         }
   11215              : 
   11216       170246 :       len -= 1;
   11217       170246 :       parms = tree_cons (NULL_TREE, NULL_TREE, parms);
   11218       170246 :       int tag = insert (parms);
   11219       170246 :       TREE_PURPOSE (parms) = tree_node ();
   11220              : 
   11221       170246 :       dump (dumper::TREE)
   11222          105 :         && dump ("Reading template parms:%d level:%N length:%d",
   11223          105 :                  tag, TREE_PURPOSE (parms), len);
   11224              : 
   11225       170246 :       tree vec = make_tree_vec (len);
   11226       618461 :       for (int ix = 0; ix != len; ix++)
   11227              :         {
   11228       277969 :           tree decl = tree_node ();
   11229       277969 :           if (!decl)
   11230              :             return NULL_TREE;
   11231              : 
   11232       277969 :           tree parm = build_tree_list (NULL, decl);
   11233       277969 :           TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
   11234              : 
   11235       277969 :           TREE_VEC_ELT (vec, ix) = parm;
   11236              :         }
   11237              : 
   11238       170246 :       TREE_VALUE (parms) = vec;
   11239       170246 :       tpl_levels++;
   11240              :     }
   11241              : 
   11242              :   return parms;
   11243              : }
   11244              : 
   11245              : void
   11246      1382553 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11247              : {
   11248      1382553 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11249      2277334 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11250              :     {
   11251       894781 :       tree vec = TREE_VALUE (parms);
   11252              : 
   11253       894781 :       tree_node (TREE_TYPE (vec));
   11254      2477774 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11255              :         {
   11256      1582993 :           tree parm = TREE_VEC_ELT (vec, ix);
   11257      1582993 :           tree dflt = TREE_PURPOSE (parm);
   11258      1582993 :           tree_node (dflt);
   11259              : 
   11260              :           /* Template template parameters need a context of their owning
   11261              :              template. This is quite tricky to infer correctly on stream-in
   11262              :              (see PR c++/98881) so we'll just provide it directly.  */
   11263      1582993 :           tree decl = TREE_VALUE (parm);
   11264      1582993 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11265         4017 :             tree_node (DECL_CONTEXT (decl));
   11266              :         }
   11267              :     }
   11268      1382553 : }
   11269              : 
   11270              : bool
   11271       335589 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11272              : {
   11273       335589 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11274       505835 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11275              :     {
   11276       170246 :       tree vec = TREE_VALUE (parms);
   11277              : 
   11278       170246 :       TREE_TYPE (vec) = tree_node ();
   11279       448215 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11280              :         {
   11281       277969 :           tree parm = TREE_VEC_ELT (vec, ix);
   11282       277969 :           tree dflt = tree_node ();
   11283       277969 :           TREE_PURPOSE (parm) = dflt;
   11284              : 
   11285       277969 :           tree decl = TREE_VALUE (parm);
   11286       277969 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11287          852 :             DECL_CONTEXT (decl) = tree_node ();
   11288              : 
   11289       277969 :           if (get_overrun ())
   11290              :             return false;
   11291              :         }
   11292              :     }
   11293              :   return true;
   11294              : }
   11295              : 
   11296              : /* PARMS is a LIST, one node per level.
   11297              :    TREE_VALUE is a TREE_VEC of parm info for that level.
   11298              :    each ELT is a TREE_LIST
   11299              :    TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
   11300              :    TREE_PURPOSE is the default value.  */
   11301              : 
   11302              : void
   11303      1382553 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
   11304              : {
   11305      1382553 :   tree parms = DECL_TEMPLATE_PARMS (tpl);
   11306      1382553 :   tpl_parms (parms, *tpl_levels);
   11307              : 
   11308              :   /* Mark end.  */
   11309      1382553 :   if (streaming_p ())
   11310       460345 :     u (0);
   11311              : 
   11312      1382553 :   if (*tpl_levels)
   11313       840640 :     tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
   11314      1382553 : }
   11315              : 
   11316              : bool
   11317       335589 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
   11318              : {
   11319       335589 :   tree parms = tpl_parms (*tpl_levels);
   11320       335589 :   if (!parms)
   11321              :     return false;
   11322              : 
   11323       335589 :   DECL_TEMPLATE_PARMS (tpl) = parms;
   11324              : 
   11325       335589 :   if (*tpl_levels)
   11326       168084 :     TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
   11327              : 
   11328              :   return true;
   11329              : }
   11330              : 
   11331              : /* Stream skeleton parm nodes, with their flags, type & parm indices.
   11332              :    All the parms will have consecutive tags.  */
   11333              : 
   11334              : void
   11335      1931921 : trees_out::fn_parms_init (tree fn)
   11336              : {
   11337              :   /* First init them.  */
   11338      1931921 :   int base_tag = ref_num - 1;
   11339      1931921 :   int ix = 0;
   11340      1931921 :   for (tree parm = DECL_ARGUMENTS (fn);
   11341      5838981 :        parm; parm = DECL_CHAIN (parm), ix++)
   11342              :     {
   11343      3907060 :       if (streaming_p ())
   11344              :         {
   11345      1302318 :           start (parm);
   11346      1302318 :           tree_node_bools (parm);
   11347              :         }
   11348      3907060 :       int tag = insert (parm);
   11349      3907060 :       gcc_checking_assert (base_tag - ix == tag);
   11350              :     }
   11351              :   /* Mark the end.  */
   11352      1931921 :   if (streaming_p ())
   11353       644261 :     u (0);
   11354              : 
   11355              :   /* Now stream their contents.  */
   11356      1931921 :   ix = 0;
   11357      1931921 :   for (tree parm = DECL_ARGUMENTS (fn);
   11358      5838981 :        parm; parm = DECL_CHAIN (parm), ix++)
   11359              :     {
   11360      3907060 :       if (streaming_p ())
   11361      1302318 :         dump (dumper::TREE)
   11362          222 :           && dump ("Writing parm:%d %u (%N) of %N",
   11363              :                    base_tag - ix, ix, parm, fn);
   11364      3907060 :       tree_node_vals (parm);
   11365              :     }
   11366              : 
   11367      1931921 :   if (!streaming_p ())
   11368              :     {
   11369              :       /* We must walk contract specifiers so the dependency graph is
   11370              :          complete.  */
   11371      1287660 :       if (tree contracts = get_fn_contract_specifiers (fn))
   11372            0 :         for (tree contract : tree_vec_range (contracts))
   11373            0 :           tree_node (contract);
   11374              :     }
   11375              : 
   11376              :   /* Write a reference to contracts pre/post functions, if any, to avoid
   11377              :      regenerating them in importers.  */
   11378      1931921 :   tree_node (DECL_PRE_FN (fn));
   11379      1931921 :   tree_node (DECL_POST_FN (fn));
   11380      1931921 : }
   11381              : 
   11382              : /* Build skeleton parm nodes, read their flags, type & parm indices.  */
   11383              : 
   11384              : int
   11385       473467 : trees_in::fn_parms_init (tree fn)
   11386              : {
   11387       473467 :   int base_tag = ~(int)back_refs.length ();
   11388              : 
   11389       473467 :   tree *parm_ptr = &DECL_ARGUMENTS (fn);
   11390       473467 :   int ix = 0;
   11391      1428134 :   for (; int code = u (); ix++)
   11392              :     {
   11393       954667 :       tree parm = start (code);
   11394       954667 :       if (!tree_node_bools (parm))
   11395              :         return 0;
   11396              : 
   11397       954667 :       int tag = insert (parm);
   11398       954667 :       gcc_checking_assert (base_tag - ix == tag);
   11399       954667 :       *parm_ptr = parm;
   11400       954667 :       parm_ptr = &DECL_CHAIN (parm);
   11401       954667 :     }
   11402              : 
   11403       473467 :   ix = 0;
   11404       473467 :   for (tree parm = DECL_ARGUMENTS (fn);
   11405      1428134 :        parm; parm = DECL_CHAIN (parm), ix++)
   11406              :     {
   11407       954667 :       dump (dumper::TREE)
   11408          362 :         && dump ("Reading parm:%d %u (%N) of %N",
   11409              :                  base_tag - ix, ix, parm, fn);
   11410       954667 :       if (!tree_node_vals (parm))
   11411              :         return 0;
   11412              : 
   11413              :       /* Apply relevant attributes.
   11414              :          FIXME should probably use cplus_decl_attributes for this,
   11415              :          but it's not yet ready for modules.  */
   11416              : 
   11417              :       /* TREE_USED is deliberately not streamed for most declarations,
   11418              :          but needs to be set if we have the [[maybe_unused]] attribute.  */
   11419       954667 :       if (lookup_attribute ("unused", DECL_ATTRIBUTES (parm))
   11420       954667 :           || lookup_attribute ("maybe_unused", DECL_ATTRIBUTES (parm)))
   11421              :         {
   11422         2155 :           TREE_USED (parm) = true;
   11423         2155 :           DECL_READ_P (parm) = true;
   11424              :         }
   11425              :     }
   11426              : 
   11427              :   /* Reload references to contract functions, if any.  */
   11428       473467 :   tree pre_fn = tree_node ();
   11429       473467 :   tree post_fn = tree_node ();
   11430       473467 :   set_contract_functions (fn, pre_fn, post_fn);
   11431              : 
   11432       473467 :   return base_tag;
   11433              : }
   11434              : 
   11435              : /* Read the remaining parm node data.  Replace with existing (if
   11436              :    non-null) in the map.  */
   11437              : 
   11438              : void
   11439       473467 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
   11440              : {
   11441       675492 :   tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
   11442       473467 :   tree parms = DECL_ARGUMENTS (fn);
   11443      1428134 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   11444              :     {
   11445       954667 :       if (existing_parm)
   11446              :         {
   11447       588057 :           if (is_defn && !DECL_SAVED_TREE (existing))
   11448              :             {
   11449              :               /* If we're about to become the definition, set the
   11450              :                  names of the parms from us.  */
   11451        15380 :               DECL_NAME (existing_parm) = DECL_NAME (parm);
   11452        15380 :               DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
   11453              : 
   11454              :               /* And some other flags important for codegen are only set
   11455              :                  by the definition.  */
   11456        15380 :               TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
   11457        15380 :               DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
   11458        15380 :               DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
   11459        15380 :               DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
   11460              : 
   11461              :               /* Invisiref parms had their types adjusted by cp_genericize. */
   11462        15380 :               if (DECL_BY_REFERENCE (parm))
   11463              :                 {
   11464            6 :                   TREE_TYPE (existing_parm) = TREE_TYPE (parm);
   11465            6 :                   relayout_decl (existing_parm);
   11466              :                 }
   11467              :             }
   11468              : 
   11469       399019 :           back_refs[~tag] = existing_parm;
   11470       399019 :           existing_parm = DECL_CHAIN (existing_parm);
   11471              :         }
   11472       954667 :       tag--;
   11473              :     }
   11474       473467 : }
   11475              : 
   11476              : /* Encode into KEY the position of the local type (class or enum)
   11477              :    declaration DECL within FN.  The position is encoded as the
   11478              :    index of the innermost BLOCK (numbered in BFS order) along with
   11479              :    the index within its BLOCK_VARS list.  */
   11480              : 
   11481              : void
   11482        21930 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
   11483              : {
   11484        21930 :   auto_vec<tree, 4> blocks;
   11485        21930 :   blocks.quick_push (DECL_INITIAL (fn));
   11486        21930 :   unsigned block_ix = 0;
   11487       107910 :   while (block_ix != blocks.length ())
   11488              :     {
   11489        42990 :       tree block = blocks[block_ix];
   11490        42990 :       unsigned decl_ix = 0;
   11491       130449 :       for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11492              :         {
   11493       109389 :           if (TREE_CODE (var) != TYPE_DECL)
   11494        69030 :             continue;
   11495        40359 :           if (var == decl)
   11496              :             {
   11497        21930 :               key.index = (block_ix << 10) | decl_ix;
   11498        21930 :               return;
   11499              :             }
   11500        18429 :           ++decl_ix;
   11501              :         }
   11502        43995 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11503        22935 :         blocks.safe_push (sub);
   11504        21060 :       ++block_ix;
   11505              :     }
   11506              : 
   11507              :   /* Not-found value.  */
   11508            0 :   key.index = 1023;
   11509        21930 : }
   11510              : 
   11511              : /* Look up the local type corresponding at the position encoded by
   11512              :    KEY within FN and named NAME.  */
   11513              : 
   11514              : tree
   11515         4376 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
   11516              : {
   11517         4376 :   if (!DECL_INITIAL (fn))
   11518              :     return NULL_TREE;
   11519              : 
   11520         1947 :   const unsigned block_pos = key.index >> 10;
   11521         1947 :   const unsigned decl_pos = key.index & 1023;
   11522              : 
   11523         1947 :   if (decl_pos == 1023)
   11524              :     return NULL_TREE;
   11525              : 
   11526         1947 :   auto_vec<tree, 4> blocks;
   11527         1947 :   blocks.quick_push (DECL_INITIAL (fn));
   11528         1947 :   unsigned block_ix = 0;
   11529         8993 :   while (block_ix != blocks.length ())
   11530              :     {
   11531         3523 :       tree block = blocks[block_ix];
   11532         3523 :       if (block_ix == block_pos)
   11533              :         {
   11534         1947 :           unsigned decl_ix = 0;
   11535         5270 :           for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11536              :             {
   11537         5270 :               if (TREE_CODE (var) != TYPE_DECL)
   11538         2345 :                 continue;
   11539              :               /* Prefer using the identifier as the key for more robustness
   11540              :                  to ODR violations, except for anonymous types since their
   11541              :                  compiler-generated identifiers aren't stable.  */
   11542         5850 :               if (IDENTIFIER_ANON_P (name)
   11543         2925 :                   ? decl_ix == decl_pos
   11544          323 :                   : DECL_NAME (var) == name)
   11545              :                 return var;
   11546          978 :               ++decl_ix;
   11547              :             }
   11548              :           return NULL_TREE;
   11549              :         }
   11550         3261 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11551         1685 :         blocks.safe_push (sub);
   11552         1576 :       ++block_ix;
   11553              :     }
   11554              : 
   11555              :   return NULL_TREE;
   11556         1947 : }
   11557              : 
   11558              : /* DEP is the depset of some decl we're streaming by value.  Determine
   11559              :    the merging behaviour.  */
   11560              : 
   11561              : merge_kind
   11562      4794790 : trees_out::get_merge_kind (tree decl, depset *dep)
   11563              : {
   11564      4794790 :   if (!dep)
   11565              :     {
   11566       995795 :       if (VAR_OR_FUNCTION_DECL_P (decl))
   11567              :         {
   11568              :           /* Any var or function with template info should have DEP.  */
   11569       560032 :           gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   11570              :                                || !DECL_TEMPLATE_INFO (decl));
   11571       560032 :           if (DECL_LOCAL_DECL_P (decl))
   11572              :             return MK_unique;
   11573              :         }
   11574              : 
   11575              :       /* Either unique, or some member of a class that cannot have an
   11576              :          out-of-class definition.  For instance a FIELD_DECL.  */
   11577       995473 :       tree ctx = CP_DECL_CONTEXT (decl);
   11578       995473 :       if (TREE_CODE (ctx) == FUNCTION_DECL)
   11579              :         {
   11580              :           /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
   11581              :              this isn't permitting them to have one.   */
   11582       644851 :           gcc_checking_assert (TREE_CODE (decl) == USING_DECL
   11583              :                                || TREE_CODE (decl) == NAMESPACE_DECL
   11584              :                                || !DECL_LANG_SPECIFIC (decl)
   11585              :                                || !DECL_TEMPLATE_INFO (decl));
   11586              : 
   11587              :           return MK_unique;
   11588              :         }
   11589              : 
   11590       350622 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   11591       350622 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   11592              :         return MK_local_friend;
   11593              : 
   11594       350622 :       gcc_checking_assert (TYPE_P (ctx));
   11595              : 
   11596              :       /* Internal-only types will not need to dedup their members.  */
   11597       350622 :       if (!DECL_CONTEXT (TYPE_NAME (ctx)))
   11598              :         return MK_unique;
   11599              : 
   11600       350566 :       if (TREE_CODE (decl) == USING_DECL)
   11601              :         return MK_field;
   11602              : 
   11603       230933 :       if (TREE_CODE (decl) == FIELD_DECL)
   11604              :         {
   11605       170655 :           if (DECL_NAME (decl))
   11606              :             {
   11607              :               /* Anonymous FIELD_DECLs have a NULL name.  */
   11608       138356 :               gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
   11609              :               return MK_named;
   11610              :             }
   11611              : 
   11612        32299 :           if (walking_bit_field_unit)
   11613              :             {
   11614              :               /* The underlying storage unit for a bitfield.  We do not
   11615              :                  need to dedup it, because it's only reachable through
   11616              :                  the bitfields it represents.  And those are deduped.  */
   11617              :               // FIXME: Is that assertion correct -- do we ever fish it
   11618              :               // out and put it in an expr?
   11619          554 :               gcc_checking_assert (!DECL_NAME (decl)
   11620              :                                    && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   11621              :                                    && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
   11622          554 :               gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
   11623              :                                     ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   11624              :                                     : TREE_CODE (TREE_TYPE (decl)))
   11625              :                                    == INTEGER_TYPE);
   11626              :               return MK_unique;
   11627              :             }
   11628              : 
   11629              :           return MK_field;
   11630              :         }
   11631              : 
   11632        60278 :       if (TREE_CODE (decl) == CONST_DECL)
   11633              :         return MK_named;
   11634              : 
   11635        10764 :       if (TREE_CODE (decl) == VAR_DECL
   11636        10764 :           && DECL_VTABLE_OR_VTT_P (decl))
   11637              :         return MK_vtable;
   11638              : 
   11639         1848 :       if (DECL_THUNK_P (decl))
   11640              :         /* Thunks are unique-enough, because they're only referenced
   11641              :            from the vtable.  And that's either new (so we want the
   11642              :            thunks), or it's a duplicate (so it will be dropped).  */
   11643              :         return MK_unique;
   11644              : 
   11645              :       /* There should be no other cases.  */
   11646            0 :       gcc_unreachable ();
   11647              :     }
   11648              : 
   11649      3798995 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   11650              :                        && TREE_CODE (decl) != USING_DECL
   11651              :                        && TREE_CODE (decl) != CONST_DECL);
   11652              : 
   11653      3798995 :   if (is_key_order ())
   11654              :     {
   11655              :       /* When doing the mergeablilty graph, there's an indirection to
   11656              :          the actual depset.  */
   11657      1266178 :       gcc_assert (dep->is_special ());
   11658      1266178 :       dep = dep->deps[0];
   11659              :     }
   11660              : 
   11661      3798995 :   gcc_checking_assert (decl == dep->get_entity ());
   11662              : 
   11663      3798995 :   merge_kind mk = MK_named;
   11664      3798995 :   switch (dep->get_entity_kind ())
   11665              :     {
   11666            0 :     default:
   11667            0 :       gcc_unreachable ();
   11668              : 
   11669              :     case depset::EK_PARTIAL:
   11670              :       mk = MK_partial;
   11671              :       break;
   11672              : 
   11673      2137496 :     case depset::EK_DECL:
   11674      2137496 :       {
   11675      2137496 :         tree ctx = CP_DECL_CONTEXT (decl);
   11676              : 
   11677      2137496 :         switch (TREE_CODE (ctx))
   11678              :           {
   11679            0 :           default:
   11680            0 :             gcc_unreachable ();
   11681              : 
   11682        22194 :           case FUNCTION_DECL:
   11683        22194 :             gcc_checking_assert
   11684              :               (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   11685              : 
   11686        22194 :             if (has_definition (ctx))
   11687              :               mk = MK_local_type;
   11688              :             else
   11689              :               /* We're not providing a definition of the context to key
   11690              :                  the local type into; use the keyed map instead.  */
   11691         1288 :               mk = MK_keyed;
   11692              :             break;
   11693              : 
   11694      2115302 :           case RECORD_TYPE:
   11695      2115302 :           case UNION_TYPE:
   11696      2115302 :           case NAMESPACE_DECL:
   11697      2115302 :             if (DECL_NAME (decl) == as_base_identifier)
   11698              :               {
   11699              :                 mk = MK_as_base;
   11700              :                 break;
   11701              :               }
   11702              : 
   11703              :             /* A lambda may have a class as its context, even though it
   11704              :                isn't a member in the traditional sense; see the test
   11705              :                g++.dg/modules/lambda-6_a.C.  */
   11706      2633999 :             if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
   11707      2297504 :                 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   11708              :               {
   11709         1060 :                 if (get_keyed_decl_scope (decl))
   11710              :                   mk = MK_keyed;
   11711              :                 else
   11712              :                   /* Lambdas not attached to any mangling scope are TU-local
   11713              :                      and so cannot be deduplicated.  */
   11714       648144 :                   mk = MK_unique;
   11715              :                 break;
   11716              :               }
   11717              : 
   11718      2019382 :             if (TREE_CODE (decl) == TEMPLATE_DECL
   11719      2019382 :                 ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11720       859062 :                 : decl_specialization_friend_p (decl))
   11721              :               {
   11722              :                 mk = MK_local_friend;
   11723              :                 break;
   11724              :               }
   11725              : 
   11726      1991881 :             if (DECL_DECOMPOSITION_P (decl))
   11727              :               {
   11728              :                 mk = MK_unique;
   11729              :                 break;
   11730              :               }
   11731              : 
   11732      1991404 :             if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   11733              :               {
   11734        35345 :                 if (RECORD_OR_UNION_TYPE_P (ctx))
   11735              :                   mk = MK_field;
   11736         1186 :                 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   11737         1186 :                          && UNSCOPED_ENUM_P (TREE_TYPE (decl))
   11738         2372 :                          && TYPE_VALUES (TREE_TYPE (decl)))
   11739              :                   /* Keyed by first enum value, and underlying type.  */
   11740              :                   mk = MK_enum;
   11741              :                 else
   11742              :                   /* No way to merge it, it is an ODR land-mine.  */
   11743              :                   mk = MK_unique;
   11744              :               }
   11745              :           }
   11746              :       }
   11747              :       break;
   11748              : 
   11749      1600425 :     case depset::EK_SPECIALIZATION:
   11750      1600425 :       {
   11751      1600425 :         gcc_checking_assert (dep->is_special ());
   11752              : 
   11753      1600425 :         if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   11754              :           /* An block-scope classes of templates are themselves
   11755              :              templates.  */
   11756         5835 :           gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
   11757              : 
   11758      1600425 :         if (dep->is_friend_spec ())
   11759              :           mk = MK_friend_spec;
   11760      1600425 :         else if (dep->is_type_spec ())
   11761              :           mk = MK_type_spec;
   11762              :         else
   11763      1138653 :           mk = MK_decl_spec;
   11764              : 
   11765      1600425 :         if (TREE_CODE (decl) == TEMPLATE_DECL)
   11766              :           {
   11767       134514 :             spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11768       134514 :             if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
   11769        14196 :               mk = merge_kind (mk | MK_tmpl_tmpl_mask);
   11770              :           }
   11771              :       }
   11772              :       break;
   11773              :     }
   11774              : 
   11775              :   return mk;
   11776              : }
   11777              : 
   11778              : 
   11779              : /* The container of DECL -- not necessarily its context!  */
   11780              : 
   11781              : tree
   11782      4794790 : trees_out::decl_container (tree decl)
   11783              : {
   11784      4794790 :   int use_tpl;
   11785      4794790 :   tree tpl = NULL_TREE;
   11786      4794790 :   if (tree template_info = node_template_info (decl, use_tpl))
   11787      1673595 :     tpl = TI_TEMPLATE (template_info);
   11788      4794790 :   if (tpl == decl)
   11789            0 :     tpl = nullptr;
   11790              : 
   11791              :   /* Stream the template we're instantiated from.  */
   11792      4794790 :   tree_node (tpl);
   11793              : 
   11794      4794790 :   tree container = NULL_TREE;
   11795      4794790 :   if (TREE_CODE (decl) == TEMPLATE_DECL
   11796      4794790 :       ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11797      3419262 :       : decl_specialization_friend_p (decl))
   11798        27501 :     container = DECL_CHAIN (decl);
   11799              :   else
   11800      4767289 :     container = CP_DECL_CONTEXT (decl);
   11801              : 
   11802      4794790 :   if (TYPE_P (container))
   11803      2811029 :     container = TYPE_NAME (container);
   11804              : 
   11805      4794790 :   tree_node (container);
   11806              : 
   11807      4794790 :   return container;
   11808              : }
   11809              : 
   11810              : tree
   11811      1239215 : trees_in::decl_container ()
   11812              : {
   11813              :   /* The maybe-template.  */
   11814      1239215 :   (void)tree_node ();
   11815              : 
   11816      1239215 :   tree container = tree_node ();
   11817              : 
   11818      1239215 :   return container;
   11819              : }
   11820              : 
   11821              : /* Gets a 2-bit discriminator to distinguish coroutine actor or destroy
   11822              :    functions from a normal function.  */
   11823              : 
   11824              : static int
   11825      1330146 : get_coroutine_discriminator (tree inner)
   11826              : {
   11827      1330146 :   if (DECL_COROUTINE_P (inner))
   11828           75 :     if (tree ramp = DECL_RAMP_FN (inner))
   11829              :       {
   11830           18 :         if (DECL_ACTOR_FN (ramp) == inner)
   11831              :           return 1;
   11832            9 :         else if (DECL_DESTROY_FN (ramp) == inner)
   11833              :           return 2;
   11834              :         else
   11835            0 :           gcc_unreachable ();
   11836              :       }
   11837              :   return 0;
   11838              : }
   11839              : 
   11840              : /* Write out key information about a mergeable DEP.  Does not write
   11841              :    the contents of DEP itself.  The context has already been
   11842              :    written.  The container has already been streamed.  */
   11843              : 
   11844              : void
   11845      4794790 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   11846              :                           tree container, depset *dep)
   11847              : {
   11848      4794790 :   if (dep && is_key_order ())
   11849              :     {
   11850      1266178 :       gcc_checking_assert (dep->is_special ());
   11851      1266178 :       dep = dep->deps[0];
   11852              :     }
   11853              : 
   11854      4794790 :   if (streaming_p ())
   11855      1752278 :     dump (dumper::MERGE)
   11856         1101 :       && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   11857          993 :                dep ? dep->entity_kind_name () : "contained",
   11858         1101 :                TREE_CODE (decl), decl);
   11859              : 
   11860              :   /* Now write the locating information. */
   11861      4794790 :   if (mk & MK_template_mask)
   11862              :     {
   11863              :       /* Specializations are located via their originating template,
   11864              :          and the set of template args they specialize.  */
   11865      1600425 :       gcc_checking_assert (dep && dep->is_special ());
   11866      1600425 :       spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11867              : 
   11868      1600425 :       tree_node (entry->tmpl);
   11869      1600425 :       tree_node (entry->args);
   11870      1600425 :       if (mk & MK_tmpl_decl_mask)
   11871      1138653 :         if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   11872              :           {
   11873              :             /* Variable template partial specializations might need
   11874              :                constraints (see spec_hasher::equal).  It's simpler to
   11875              :                write NULL when we don't need them.  */
   11876        25371 :             tree constraints = NULL_TREE;
   11877              : 
   11878        25371 :             if (uses_template_parms (entry->args))
   11879          879 :               constraints = get_constraints (inner);
   11880        25371 :             tree_node (constraints);
   11881              :           }
   11882              : 
   11883      1600425 :       if (CHECKING_P)
   11884              :         {
   11885              :           /* Make sure we can locate the decl.  */
   11886      1600425 :           tree existing = match_mergeable_specialization
   11887      1600425 :             (bool (mk & MK_tmpl_decl_mask), entry);
   11888              : 
   11889      1600425 :           gcc_assert (existing);
   11890      1600425 :           if (mk & MK_tmpl_decl_mask)
   11891              :             {
   11892      1138653 :               if (mk & MK_tmpl_tmpl_mask)
   11893        11256 :                 existing = DECL_TI_TEMPLATE (existing);
   11894              :             }
   11895              :           else
   11896              :             {
   11897       461772 :               if (mk & MK_tmpl_tmpl_mask)
   11898         2940 :                 existing = CLASSTYPE_TI_TEMPLATE (existing);
   11899              :               else
   11900       458832 :                 existing = TYPE_NAME (existing);
   11901              :             }
   11902              : 
   11903              :           /* The walkabout should have found ourselves.  */
   11904      1600425 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
   11905              :                                ? same_type_p (TREE_TYPE (decl),
   11906              :                                               TREE_TYPE (existing))
   11907              :                                : existing == decl);
   11908              :         }
   11909              :     }
   11910      3194365 :   else if (mk != MK_unique)
   11911              :     {
   11912      2546221 :       merge_key key;
   11913      2546221 :       tree name = DECL_NAME (decl);
   11914              : 
   11915      2546221 :       switch (mk)
   11916              :         {
   11917            0 :         default:
   11918            0 :           gcc_unreachable ();
   11919              : 
   11920      2143929 :         case MK_named:
   11921      2143929 :         case MK_friend_spec:
   11922      2143929 :           if (IDENTIFIER_CONV_OP_P (name))
   11923         7561 :             name = conv_op_identifier;
   11924              : 
   11925      2143929 :           if (TREE_CODE (inner) == FUNCTION_DECL)
   11926              :             {
   11927              :               /* Functions are distinguished by parameter types.  */
   11928      1197805 :               tree fn_type = TREE_TYPE (inner);
   11929              : 
   11930      1197805 :               key.ref_q = type_memfn_rqual (fn_type);
   11931      1197805 :               key.coro_disc = get_coroutine_discriminator (inner);
   11932      1197805 :               key.iobj_p = DECL_IOBJ_MEMBER_FUNCTION_P (inner);
   11933      1197805 :               key.xobj_p = DECL_XOBJ_MEMBER_FUNCTION_P (inner);
   11934      1197805 :               key.args = TYPE_ARG_TYPES (fn_type);
   11935              : 
   11936      1197805 :               if (tree reqs = get_constraints (inner))
   11937              :                 {
   11938        87318 :                   if (cxx_dialect < cxx20)
   11939           48 :                     reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
   11940              :                   else
   11941       174588 :                     reqs = CI_DECLARATOR_REQS (reqs);
   11942        87318 :                   key.constraints = reqs;
   11943              :                 }
   11944              : 
   11945      1197805 :               if (IDENTIFIER_CONV_OP_P (name)
   11946      1197805 :                   || (decl != inner
   11947       660726 :                       && !(name == fun_identifier
   11948              :                            /* In case the user names something _FUN  */
   11949          156 :                            && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
   11950              :                 /* And a function template, or conversion operator needs
   11951              :                    the return type.  Except for the _FUN thunk of a
   11952              :                    generic lambda, which has a recursive decl_type'd
   11953              :                    return type.  */
   11954              :                 // FIXME: What if the return type is a voldemort?
   11955       668209 :                 key.ret = fndecl_declared_return_type (inner);
   11956              :             }
   11957              :           break;
   11958              : 
   11959       185537 :         case MK_field:
   11960       185537 :           {
   11961       185537 :             unsigned ix = 0;
   11962       185537 :             if (TREE_CODE (inner) != FIELD_DECL)
   11963              :               name = NULL_TREE;
   11964              :             else
   11965        31745 :               gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
   11966              : 
   11967       185537 :             for (tree field = TYPE_FIELDS (TREE_TYPE (container));
   11968      3748184 :                  ; field = DECL_CHAIN (field))
   11969              :               {
   11970      3933721 :                 tree finner = STRIP_TEMPLATE (field);
   11971      3933721 :                 if (TREE_CODE (finner) == TREE_CODE (inner))
   11972              :                   {
   11973      1354671 :                     if (finner == inner)
   11974              :                       break;
   11975      1169134 :                     ix++;
   11976              :                   }
   11977      3748184 :               }
   11978       185537 :             key.index = ix;
   11979              :           }
   11980       185537 :           break;
   11981              : 
   11982         8916 :         case MK_vtable:
   11983         8916 :           {
   11984         8916 :             tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
   11985        11724 :             for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
   11986        11724 :               if (vtable == decl)
   11987              :                 {
   11988         8916 :                   key.index = ix;
   11989         8916 :                   break;
   11990              :                 }
   11991         8916 :             name = NULL_TREE;
   11992              :           }
   11993         8916 :           break;
   11994              : 
   11995        94860 :         case MK_as_base:
   11996        94860 :           gcc_checking_assert
   11997              :             (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
   11998              :           break;
   11999              : 
   12000        27501 :         case MK_local_friend:
   12001        27501 :           {
   12002              :             /* Find by index on the class's DECL_LIST.  We set TREE_CHAIN to
   12003              :                point to the class in push_template_decl or grokfndecl.  */
   12004        27501 :             unsigned ix = 0;
   12005        27501 :             for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
   12006       766908 :                  decls; decls = TREE_CHAIN (decls))
   12007       766908 :               if (!TREE_PURPOSE (decls))
   12008              :                 {
   12009       105606 :                   tree frnd = friend_from_decl_list (TREE_VALUE (decls));
   12010       105606 :                   if (frnd == decl)
   12011              :                     break;
   12012        78105 :                   ix++;
   12013              :                 }
   12014        27501 :             key.index = ix;
   12015        27501 :             name = NULL_TREE;
   12016              :           }
   12017        27501 :           break;
   12018              : 
   12019        21930 :         case MK_local_type:
   12020        21930 :           key_local_type (key, STRIP_TEMPLATE (decl), container);
   12021        21930 :           break;
   12022              : 
   12023         1186 :         case MK_enum:
   12024         1186 :           {
   12025              :             /* Anonymous enums are located by their first identifier,
   12026              :                and underlying type.  */
   12027         1186 :             tree type = TREE_TYPE (decl);
   12028              : 
   12029         1186 :             gcc_checking_assert (UNSCOPED_ENUM_P (type));
   12030              :             /* Using the type name drops the bit precision we might
   12031              :                have been using on the enum.  */
   12032         1186 :             key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
   12033         1186 :             if (tree values = TYPE_VALUES (type))
   12034         1186 :               name = DECL_NAME (TREE_VALUE (values));
   12035              :           }
   12036              :           break;
   12037              : 
   12038         1288 :         case MK_keyed:
   12039         1288 :           {
   12040         1288 :             tree scope = get_keyed_decl_scope (inner);
   12041         1288 :             gcc_checking_assert (scope);
   12042              : 
   12043         1288 :             auto *root = keyed_table->get (scope);
   12044         1288 :             unsigned ix = root->length ();
   12045              :             /* If we don't find it, we'll write a really big number
   12046              :                that the reader will ignore.  */
   12047         1409 :             while (ix--)
   12048         1409 :               if ((*root)[ix] == inner)
   12049              :                 break;
   12050              : 
   12051              :             /* Use the keyed-to decl as the 'name'.  */
   12052         1288 :             name = scope;
   12053         1288 :             key.index = ix;
   12054              :           }
   12055         1288 :           break;
   12056              : 
   12057        61074 :         case MK_partial:
   12058        61074 :           {
   12059        61074 :             tree ti = get_template_info (inner);
   12060        61074 :             key.constraints = get_constraints (inner);
   12061        61074 :             key.ret = TI_TEMPLATE (ti);
   12062        61074 :             key.args = TI_ARGS (ti);
   12063              :           }
   12064        61074 :           break;
   12065              :         }
   12066              : 
   12067      2546221 :       tree_node (name);
   12068      2546221 :       if (streaming_p ())
   12069              :         {
   12070              :           /* Check we have enough bits for the index.  */
   12071       906601 :           gcc_checking_assert (key.index < (1u << (sizeof (unsigned) * 8 - 6)));
   12072              : 
   12073       906601 :           unsigned code = ((key.ref_q << 0)
   12074       906601 :                            | (key.coro_disc << 2)
   12075       906601 :                            | (key.iobj_p << 4)
   12076       906601 :                            | (key.xobj_p << 5)
   12077       906601 :                            | (key.index << 6));
   12078       906601 :           u (code);
   12079              :         }
   12080              : 
   12081      2546221 :       if (mk == MK_enum)
   12082         1186 :         tree_node (key.ret);
   12083      2545035 :       else if (mk == MK_partial
   12084      2483961 :                || (mk == MK_named && inner
   12085      2143929 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12086              :         {
   12087      1258879 :           tree_node (key.ret);
   12088      1258879 :           tree arg = key.args;
   12089      1258879 :           if (mk == MK_named)
   12090      3497232 :             while (arg && arg != void_list_node)
   12091              :               {
   12092      2299427 :                 tree_node (TREE_VALUE (arg));
   12093      2299427 :                 arg = TREE_CHAIN (arg);
   12094              :               }
   12095      1258879 :           tree_node (arg);
   12096      1258879 :           tree_node (key.constraints);
   12097              :         }
   12098              :     }
   12099      4794790 : }
   12100              : 
   12101              : /* DECL is a new declaration that may be duplicated in OVL.  Use KEY
   12102              :    to find its clone, or NULL.  If DECL's DECL_NAME is NULL, this
   12103              :    has been found by a proxy.  It will be an enum type located by its
   12104              :    first member.
   12105              : 
   12106              :    We're conservative with matches, so ambiguous decls will be
   12107              :    registered as different, then lead to a lookup error if the two
   12108              :    modules are both visible.  Perhaps we want to do something similar
   12109              :    to duplicate decls to get ODR errors on loading?  We already have
   12110              :    some special casing for namespaces.  */
   12111              : 
   12112              : static tree
   12113       306494 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
   12114              : {
   12115       306494 :   tree found = NULL_TREE;
   12116       946230 :   for (ovl_iterator iter (ovl); !found && iter; ++iter)
   12117              :     {
   12118       700809 :       tree match = *iter;
   12119              : 
   12120       700809 :       tree d_inner = decl;
   12121       700809 :       tree m_inner = match;
   12122              : 
   12123       809226 :     again:
   12124       809226 :       if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
   12125              :         {
   12126       125185 :           if (TREE_CODE (match) == NAMESPACE_DECL
   12127       125185 :               && !DECL_NAMESPACE_ALIAS (match))
   12128              :             /* Namespaces are never overloaded.  */
   12129              :             found = match;
   12130              : 
   12131       125185 :           continue;
   12132              :         }
   12133              : 
   12134       684041 :       switch (TREE_CODE (d_inner))
   12135              :         {
   12136       260230 :         case TEMPLATE_DECL:
   12137       260230 :           if (template_heads_equivalent_p (d_inner, m_inner))
   12138              :             {
   12139       108417 :               d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12140       108417 :               m_inner = DECL_TEMPLATE_RESULT (m_inner);
   12141       108417 :               if (d_inner == error_mark_node
   12142       108417 :                   && TYPE_DECL_ALIAS_P (m_inner))
   12143              :                 {
   12144              :                   found = match;
   12145              :                   break;
   12146              :                 }
   12147       108417 :               goto again;
   12148              :             }
   12149              :           break;
   12150              : 
   12151       322445 :         case FUNCTION_DECL:
   12152       322445 :           if (tree m_type = TREE_TYPE (m_inner))
   12153       322445 :             if ((!key.ret
   12154       159697 :                  || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
   12155       295398 :                 && type_memfn_rqual (m_type) == key.ref_q
   12156       295132 :                 && key.iobj_p == DECL_IOBJ_MEMBER_FUNCTION_P (m_inner)
   12157       590004 :                 && key.xobj_p == DECL_XOBJ_MEMBER_FUNCTION_P (m_inner)
   12158       295111 :                 && compparms (key.args, TYPE_ARG_TYPES (m_type))
   12159       132341 :                 && get_coroutine_discriminator (m_inner) == key.coro_disc
   12160              :                 /* Reject if old is a "C" builtin and new is not "C".
   12161              :                    Matches decls_match behaviour.  */
   12162       132338 :                 && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
   12163         8255 :                     || !DECL_EXTERN_C_P (m_inner)
   12164         8028 :                     || DECL_EXTERN_C_P (d_inner))
   12165              :                 /* Reject if one is a different member of a
   12166              :                    guarded/pre/post fn set.  */
   12167       132313 :                 && (!flag_contracts
   12168       242862 :                     || (DECL_IS_PRE_FN_P (d_inner)
   12169       121431 :                         == DECL_IS_PRE_FN_P (m_inner)))
   12170       454758 :                 && (!flag_contracts
   12171       242862 :                     || (DECL_IS_POST_FN_P (d_inner)
   12172       121431 :                         == DECL_IS_POST_FN_P (m_inner))))
   12173              :               {
   12174       132313 :                 tree m_reqs = get_constraints (m_inner);
   12175       132313 :                 if (m_reqs)
   12176              :                   {
   12177         9791 :                     if (cxx_dialect < cxx20)
   12178            8 :                       m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
   12179              :                     else
   12180        19574 :                       m_reqs = CI_DECLARATOR_REQS (m_reqs);
   12181              :                   }
   12182              : 
   12183       132313 :                 if (cp_tree_equal (key.constraints, m_reqs))
   12184       639736 :                   found = match;
   12185              :               }
   12186              :           break;
   12187              : 
   12188        61238 :         case TYPE_DECL:
   12189       122476 :           if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
   12190        61238 :               == DECL_IMPLICIT_TYPEDEF_P (m_inner))
   12191              :             {
   12192        61202 :               if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
   12193        61073 :                 return match;
   12194          129 :               else if (mk == MK_enum
   12195          129 :                        && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
   12196          129 :                            == key.ret))
   12197              :                 found = match;
   12198              :             }
   12199              :           /* With -freflection, typedef struct { } A is now represented the same
   12200              :              as typedef struct A_ { } A except the TYPE_DECL for A_ is invisible
   12201              :              to name lookup, so we won't be able to find and match it directly.
   12202              :              But we will find the in-TU A (m_inner), through which we can obtain
   12203              :              the in-TU A_ when d_inner is the streamed-in A_.  */
   12204           36 :           else if (flag_reflection
   12205           10 :                    && TYPE_DECL_WAS_UNNAMED (d_inner)
   12206           42 :                    && DECL_ORIGINAL_TYPE (m_inner))
   12207              :             {
   12208            6 :               tree orig = TYPE_NAME (DECL_ORIGINAL_TYPE (m_inner));
   12209            6 :               if (TYPE_DECL_WAS_UNNAMED (orig)
   12210           12 :                   && DECL_NAME (orig) == DECL_NAME (d_inner))
   12211              :                 found = orig;
   12212              :             }
   12213              :           break;
   12214              : 
   12215              :         default:
   12216              :           found = match;
   12217              :           break;
   12218              :         }
   12219              :     }
   12220              : 
   12221       245421 :   return found;
   12222              : }
   12223              : 
   12224              : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl.  Only
   12225              :    the bools have been filled in.  Read its merging key and merge it.
   12226              :    Returns the existing decl if there is one.  */
   12227              : 
   12228              : tree
   12229      1239215 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   12230              :                          tree type, tree container, bool is_attached,
   12231              :                          bool is_imported_temploid_friend)
   12232              : {
   12233      1239215 :   const char *kind = "new";
   12234      1239215 :   tree existing = NULL_TREE;
   12235              : 
   12236      1239215 :   if (mk & MK_template_mask)
   12237              :     {
   12238              :       // FIXME: We could stream the specialization hash?
   12239       384792 :       spec_entry spec;
   12240       384792 :       spec.tmpl = tree_node ();
   12241       384792 :       spec.args = tree_node ();
   12242              : 
   12243       384792 :       if (get_overrun ())
   12244            0 :         return error_mark_node;
   12245              : 
   12246       384792 :       DECL_NAME (decl) = DECL_NAME (spec.tmpl);
   12247       384792 :       DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
   12248       384792 :       DECL_NAME (inner) = DECL_NAME (decl);
   12249       384792 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12250              : 
   12251       384792 :       tree constr = NULL_TREE;
   12252       384792 :       bool is_decl = mk & MK_tmpl_decl_mask;
   12253       384792 :       if (is_decl)
   12254              :         {
   12255       274799 :           if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   12256              :             {
   12257         5617 :               constr = tree_node ();
   12258         5617 :               if (constr)
   12259            0 :                 set_constraints (inner, constr);
   12260              :             }
   12261       546801 :           spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
   12262              :         }
   12263              :       else
   12264       109993 :         spec.spec = type;
   12265       384792 :       existing = match_mergeable_specialization (is_decl, &spec);
   12266       384792 :       if (constr)
   12267              :         /* We'll add these back later, if this is the new decl.  */
   12268            0 :         remove_constraints (inner);
   12269              : 
   12270       384792 :       if (!existing)
   12271              :         ; /* We'll add to the table once read.  */
   12272       153990 :       else if (mk & MK_tmpl_decl_mask)
   12273              :         {
   12274              :           /* A declaration specialization.  */
   12275       106805 :           if (mk & MK_tmpl_tmpl_mask)
   12276         1152 :             existing = DECL_TI_TEMPLATE (existing);
   12277              :         }
   12278              :       else
   12279              :         {
   12280              :           /* A type specialization.  */
   12281        47185 :           if (mk & MK_tmpl_tmpl_mask)
   12282          248 :             existing = CLASSTYPE_TI_TEMPLATE (existing);
   12283              :           else
   12284        46937 :             existing = TYPE_NAME (existing);
   12285              :         }
   12286              :     }
   12287       854423 :   else if (mk == MK_unique)
   12288              :     kind = "unique";
   12289              :   else
   12290              :     {
   12291       635101 :       tree name = tree_node ();
   12292              : 
   12293       635101 :       merge_key key;
   12294       635101 :       unsigned code = u ();
   12295       635101 :       key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
   12296       635101 :       key.coro_disc = (code >> 2) & 3;
   12297       635101 :       key.iobj_p = (code >> 4) & 1;
   12298       635101 :       key.xobj_p = (code >> 5) & 1;
   12299       635101 :       key.index = code >> 6;
   12300              : 
   12301       635101 :       if (mk == MK_enum)
   12302          238 :         key.ret = tree_node ();
   12303       634863 :       else if (mk == MK_partial
   12304       623827 :                || ((mk == MK_named || mk == MK_friend_spec)
   12305       529894 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12306              :         {
   12307       305368 :           key.ret = tree_node ();
   12308       305368 :           tree arg, *arg_ptr = &key.args;
   12309       305368 :           while ((arg = tree_node ())
   12310       863591 :                  && arg != void_list_node
   12311      1435952 :                  && mk != MK_partial)
   12312              :             {
   12313       559774 :               *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
   12314       559774 :               arg_ptr = &TREE_CHAIN (*arg_ptr);
   12315              :             }
   12316       305368 :           *arg_ptr = arg;
   12317       305368 :           key.constraints = tree_node ();
   12318              :         }
   12319              : 
   12320       635101 :       if (get_overrun ())
   12321            0 :         return error_mark_node;
   12322              : 
   12323       635101 :       if (mk < MK_indirect_lwm)
   12324              :         {
   12325       622742 :           DECL_NAME (decl) = name;
   12326       622742 :           DECL_CONTEXT (decl) = FROB_CONTEXT (container);
   12327              :         }
   12328       635101 :       DECL_NAME (inner) = DECL_NAME (decl);
   12329       635101 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12330              : 
   12331       635101 :       if (mk == MK_partial)
   12332              :         {
   12333        11036 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
   12334        55641 :                spec; spec = TREE_CHAIN (spec))
   12335              :             {
   12336        50828 :               tree tmpl = TREE_VALUE (spec);
   12337        50828 :               tree ti = get_template_info (tmpl);
   12338        50828 :               if (template_args_equal (key.args, TI_ARGS (ti))
   12339        57844 :                   && cp_tree_equal (key.constraints,
   12340              :                                     get_constraints
   12341         7016 :                                     (DECL_TEMPLATE_RESULT (tmpl))))
   12342              :                 {
   12343              :                   existing = tmpl;
   12344              :                   break;
   12345              :                 }
   12346              :             }
   12347              :         }
   12348       624065 :       else if (mk == MK_keyed
   12349          399 :                && DECL_LANG_SPECIFIC (name)
   12350       624464 :                && DECL_MODULE_KEYED_DECLS_P (name))
   12351              :         {
   12352          399 :           gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
   12353              :                                || TREE_CODE (container) == TYPE_DECL
   12354              :                                || TREE_CODE (container) == FUNCTION_DECL);
   12355          399 :           if (auto *set = keyed_table->get (name))
   12356       635250 :             if (key.index < set->length ())
   12357              :               {
   12358          149 :                 existing = (*set)[key.index];
   12359          149 :                 if (existing)
   12360              :                   {
   12361          149 :                     gcc_checking_assert
   12362              :                       (DECL_IMPLICIT_TYPEDEF_P (existing));
   12363          149 :                     if (inner != decl)
   12364           91 :                       existing
   12365           91 :                         = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12366              :                   }
   12367              :               }
   12368              :         }
   12369              :       else
   12370       623666 :         switch (TREE_CODE (container))
   12371              :           {
   12372            0 :           default:
   12373            0 :             gcc_unreachable ();
   12374              : 
   12375       152167 :           case NAMESPACE_DECL:
   12376       152167 :             if (is_attached
   12377       152167 :                 && !is_imported_temploid_friend
   12378       152167 :                 && !(state->is_module () || state->is_partition ()))
   12379              :               kind = "unique";
   12380              :             else
   12381              :               {
   12382       150063 :                 gcc_checking_assert (mk == MK_named || mk == MK_enum);
   12383       150063 :                 tree mvec;
   12384       150063 :                 tree *vslot = mergeable_namespace_slots (container, name,
   12385              :                                                          is_attached, &mvec);
   12386       150063 :                 existing = check_mergeable_decl (mk, decl, *vslot, key);
   12387       150063 :                 if (!existing)
   12388        72507 :                   add_mergeable_namespace_entity (vslot, decl);
   12389              :                 else
   12390              :                   {
   12391              :                     /* Note that we now have duplicates to deal with in
   12392              :                        name lookup.  */
   12393        77556 :                     if (is_attached)
   12394           66 :                       BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
   12395              :                     else
   12396        77490 :                       BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
   12397              :                   }
   12398              :               }
   12399       156543 :             break;
   12400              : 
   12401         4376 :           case FUNCTION_DECL:
   12402         4376 :             gcc_checking_assert (mk == MK_local_type);
   12403         4376 :             existing = key_local_type (key, container, name);
   12404         4376 :             if (existing && inner != decl)
   12405         3516 :               existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12406              :             break;
   12407              : 
   12408       467123 :           case TYPE_DECL:
   12409       467123 :             gcc_checking_assert (!is_imported_temploid_friend);
   12410       467123 :             int use_tmpl = 0;
   12411         5985 :             if (is_attached && !(state->is_module () || state->is_partition ())
   12412              :                 /* Implicit or in-class defaulted member functions
   12413              :                    can come from anywhere.  */
   12414         4303 :                 && !(TREE_CODE (decl) == FUNCTION_DECL
   12415         1271 :                      && !DECL_THUNK_P (decl)
   12416         1271 :                      && DECL_DEFAULTED_FN (decl)
   12417          820 :                      && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
   12418              :                 /* As can members of template specialisations.  */
   12419       470606 :                 && !(node_template_info (container, use_tmpl)
   12420         1307 :                      && use_tmpl != 0))
   12421              :               kind = "unique";
   12422              :             else
   12423              :               {
   12424       463888 :                 tree ctx = TREE_TYPE (container);
   12425              : 
   12426              :                 /* For some reason templated enumeral types are not marked
   12427              :                    as COMPLETE_TYPE_P, even though they have members.
   12428              :                    This may well be a bug elsewhere.  */
   12429       463888 :                 if (TREE_CODE (ctx) == ENUMERAL_TYPE)
   12430        15672 :                   existing = find_enum_member (ctx, name);
   12431       448216 :                 else if (COMPLETE_TYPE_P (ctx))
   12432              :                   {
   12433       196759 :                     switch (mk)
   12434              :                       {
   12435            0 :                       default:
   12436            0 :                         gcc_unreachable ();
   12437              : 
   12438       156901 :                       case MK_named:
   12439       156901 :                         existing = lookup_class_binding (ctx, name);
   12440       156901 :                         if (existing)
   12441              :                           {
   12442       156431 :                             tree inner = decl;
   12443       156431 :                             if (TREE_CODE (inner) == TEMPLATE_DECL
   12444       156431 :                                 && !DECL_MEMBER_TEMPLATE_P (inner))
   12445        69441 :                               inner = DECL_TEMPLATE_RESULT (inner);
   12446              : 
   12447       156431 :                             existing = check_mergeable_decl
   12448       156431 :                               (mk, inner, existing, key);
   12449              : 
   12450       156431 :                             if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
   12451              :                               {} // FIXME: Insert into specialization
   12452              :                             // tables, we'll need the arguments for that!
   12453              :                           }
   12454              :                         break;
   12455              : 
   12456        27107 :                       case MK_field:
   12457        27107 :                         {
   12458        27107 :                           unsigned ix = key.index;
   12459        27107 :                           for (tree field = TYPE_FIELDS (ctx);
   12460       893534 :                                field; field = DECL_CHAIN (field))
   12461              :                             {
   12462       893534 :                               tree finner = STRIP_TEMPLATE (field);
   12463       893534 :                               if (TREE_CODE (finner) == TREE_CODE (inner))
   12464       299981 :                                 if (!ix--)
   12465              :                                   {
   12466              :                                     existing = field;
   12467              :                                     break;
   12468              :                                   }
   12469              :                             }
   12470              :                         }
   12471              :                         break;
   12472              : 
   12473         1484 :                       case MK_vtable:
   12474         1484 :                         {
   12475         1484 :                           unsigned ix = key.index;
   12476         1484 :                           for (tree vtable = CLASSTYPE_VTABLES (ctx);
   12477         1823 :                                vtable; vtable = DECL_CHAIN (vtable))
   12478         1570 :                             if (!ix--)
   12479              :                               {
   12480              :                                 existing = vtable;
   12481              :                                 break;
   12482              :                               }
   12483              :                         }
   12484              :                         break;
   12485              : 
   12486         8167 :                       case MK_as_base:
   12487         8167 :                         {
   12488         8167 :                           tree as_base = CLASSTYPE_AS_BASE (ctx);
   12489         8167 :                           if (as_base && as_base != ctx)
   12490         8167 :                             existing = TYPE_NAME (as_base);
   12491              :                         }
   12492              :                         break;
   12493              : 
   12494         3100 :                       case MK_local_friend:
   12495         3100 :                         {
   12496         3100 :                           unsigned ix = key.index;
   12497         3100 :                           for (tree decls = CLASSTYPE_DECL_LIST (ctx);
   12498        86813 :                                decls; decls = TREE_CHAIN (decls))
   12499        86813 :                             if (!TREE_PURPOSE (decls) && !ix--)
   12500              :                               {
   12501         3100 :                                 existing
   12502         3100 :                                   = friend_from_decl_list (TREE_VALUE (decls));
   12503         3100 :                                 break;
   12504              :                               }
   12505              :                         }
   12506              :                         break;
   12507              :                       }
   12508              : 
   12509       196759 :                     if (existing && mk < MK_indirect_lwm && mk != MK_partial
   12510       192394 :                         && TREE_CODE (decl) == TEMPLATE_DECL
   12511       285345 :                         && !DECL_MEMBER_TEMPLATE_P (decl))
   12512              :                       {
   12513        71492 :                         tree ti;
   12514        71492 :                         if (DECL_IMPLICIT_TYPEDEF_P (existing))
   12515          913 :                           ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
   12516              :                         else
   12517        70579 :                           ti = DECL_TEMPLATE_INFO (existing);
   12518        71492 :                         existing = TI_TEMPLATE (ti);
   12519              :                       }
   12520              :                   }
   12521              :               }
   12522              :           }
   12523              :     }
   12524              : 
   12525      1239215 :   dump (dumper::MERGE)
   12526         3208 :     && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   12527         3208 :              existing ? "matched" : kind, TREE_CODE (decl), decl);
   12528              : 
   12529              :   return existing;
   12530              : }
   12531              : 
   12532              : void
   12533       371506 : trees_out::binfo_mergeable (tree binfo)
   12534              : {
   12535       371506 :   tree dom = binfo;
   12536       469457 :   while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
   12537              :     dom = parent;
   12538       371506 :   tree type = BINFO_TYPE (dom);
   12539       371506 :   gcc_checking_assert (TYPE_BINFO (type) == dom);
   12540       371506 :   tree_node (type);
   12541       371506 :   if (streaming_p ())
   12542              :     {
   12543              :       unsigned ix = 0;
   12544       197902 :       for (; dom != binfo; dom = TREE_CHAIN (dom))
   12545        51573 :         ix++;
   12546       146329 :       u (ix);
   12547              :     }
   12548       371506 : }
   12549              : 
   12550              : unsigned
   12551        94376 : trees_in::binfo_mergeable (tree *type)
   12552              : {
   12553        94376 :   *type = tree_node ();
   12554        94376 :   return u ();
   12555              : }
   12556              : 
   12557              : /* DECL is a just streamed declaration with attributes DATTR that should
   12558              :    have matching ABI tags as EXISTING's attributes EATTR.  Check that the
   12559              :    ABI tags match, and report an error if not.  */
   12560              : 
   12561              : void
   12562       301438 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
   12563              : {
   12564       301438 :   tree etags = lookup_attribute ("abi_tag", eattr);
   12565       301438 :   tree dtags = lookup_attribute ("abi_tag", dattr);
   12566       301438 :   if ((etags == nullptr) != (dtags == nullptr)
   12567       301438 :       || (etags && !attribute_value_equal (etags, dtags)))
   12568              :     {
   12569           33 :       if (etags)
   12570           24 :         etags = TREE_VALUE (etags);
   12571           33 :       if (dtags)
   12572           24 :         dtags = TREE_VALUE (dtags);
   12573              : 
   12574              :       /* We only error if mangling wouldn't consider the tags equivalent.
   12575              :          Since tags might have been inherited during mangling, ignore
   12576              :          inherited tags if there's a mangled-ness mismatch.  */
   12577           33 :       bool ignore_inherited_p
   12578           33 :         = (DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (existing))
   12579           33 :            != DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (decl)));
   12580           33 :       if (!equal_abi_tags (etags, dtags, ignore_inherited_p))
   12581              :         {
   12582           21 :           auto_diagnostic_group d;
   12583           21 :           if (dtags)
   12584           15 :             error_at (DECL_SOURCE_LOCATION (decl),
   12585              :                       "mismatching abi tags for %qD with tags %qE",
   12586              :                       decl, dtags);
   12587              :           else
   12588            6 :             error_at (DECL_SOURCE_LOCATION (decl),
   12589              :                       "mismatching abi tags for %qD with no tags", decl);
   12590           21 :           if (etags)
   12591           15 :             inform (DECL_SOURCE_LOCATION (existing),
   12592              :                     "existing declaration here with tags %qE", etags);
   12593              :           else
   12594            6 :             inform (DECL_SOURCE_LOCATION (existing),
   12595              :                     "existing declaration here with no tags");
   12596           21 :         }
   12597              : 
   12598              :       /* Always use the existing abi_tags as the canonical set so that
   12599              :          later processing doesn't get confused.  */
   12600           33 :       if (dtags)
   12601           24 :         dattr = remove_attribute ("abi_tag", dattr);
   12602           33 :       if (etags)
   12603           24 :         duplicate_one_attribute (&dattr, eattr, "abi_tag");
   12604              :     }
   12605       301438 : }
   12606              : 
   12607              : /* DECL is a just streamed mergeable decl that should match EXISTING.  Check
   12608              :    it does and issue an appropriate diagnostic if not.  Merge any
   12609              :    bits from DECL to EXISTING.  This is stricter matching than
   12610              :    decls_match, because we can rely on ODR-sameness, and we cannot use
   12611              :    decls_match because it can cause instantiations of constraints.  */
   12612              : 
   12613              : bool
   12614       442337 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
   12615              : {
   12616              :   // FIXME: We should probably do some duplicate decl-like stuff here
   12617              :   // (beware, default parms should be the same?)  Can we just call
   12618              :   // duplicate_decls and teach it how to handle the module-specific
   12619              :   // permitted/required duplications?
   12620              : 
   12621              :   // We know at this point that the decls have matched by key, so we
   12622              :   // can elide some of the checking
   12623       442337 :   gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
   12624              : 
   12625       442337 :   tree d_inner = decl;
   12626       442337 :   tree e_inner = existing;
   12627       442337 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12628              :     {
   12629       147027 :       d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12630       147027 :       e_inner = DECL_TEMPLATE_RESULT (e_inner);
   12631       147027 :       gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
   12632              :     }
   12633              : 
   12634              :   // FIXME: do more precise errors at point of mismatch
   12635       442337 :   const char *mismatch_msg = nullptr;
   12636              : 
   12637       442337 :   if (VAR_OR_FUNCTION_DECL_P (d_inner)
   12638       442337 :       && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
   12639              :     {
   12640            6 :       mismatch_msg = G_("conflicting language linkage for imported "
   12641              :                         "declaration %#qD");
   12642            6 :       goto mismatch;
   12643              :     }
   12644       442331 :   else if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12645              :     {
   12646       202025 :       tree e_ret = fndecl_declared_return_type (existing);
   12647       202025 :       tree d_ret = fndecl_declared_return_type (decl);
   12648              : 
   12649        86287 :       if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
   12650       202049 :           && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
   12651              :         /* This has a recursive type that will compare different.  */;
   12652       202013 :       else if (!same_type_p (d_ret, e_ret))
   12653              :         {
   12654           25 :           mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12655           25 :           goto mismatch;
   12656              :         }
   12657              : 
   12658       202000 :       tree& e_type = TREE_TYPE (e_inner);
   12659       202000 :       tree d_type = TREE_TYPE (d_inner);
   12660              : 
   12661       202000 :       for (tree e_args = TYPE_ARG_TYPES (e_type),
   12662       202000 :              d_args = TYPE_ARG_TYPES (d_type);
   12663       346618 :            e_args != d_args && (e_args || d_args);
   12664       144618 :            e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
   12665              :         {
   12666       144618 :           if (!(e_args && d_args))
   12667              :             {
   12668            0 :               mismatch_msg = G_("conflicting argument list for imported "
   12669              :                                 "declaration %#qD");
   12670            0 :               goto mismatch;
   12671              :             }
   12672              : 
   12673       144618 :           if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
   12674              :             {
   12675            0 :               mismatch_msg = G_("conflicting argument types for imported "
   12676              :                                 "declaration %#qD");
   12677            0 :               goto mismatch;
   12678              :             }
   12679              :         }
   12680              : 
   12681              :       /* If EXISTING has an undeduced or uninstantiated exception
   12682              :          specification, but DECL does not, propagate the exception
   12683              :          specification.  Otherwise we end up asserting or trying to
   12684              :          instantiate it in the middle of loading.   */
   12685       202000 :       tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
   12686       202000 :       tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
   12687       295790 :       if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
   12688              :         {
   12689        32004 :           if (!(DECL_MAYBE_DELETED (d_inner)
   12690        15944 :                 || DEFERRED_NOEXCEPT_SPEC_P (d_spec))
   12691        47701 :               || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
   12692        25970 :                   && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
   12693              :             {
   12694          155 :               dump (dumper::MERGE)
   12695            6 :                 && dump ("Propagating instantiated noexcept to %N", existing);
   12696          155 :               gcc_checking_assert (existing == e_inner);
   12697          155 :               e_type = build_exception_variant (e_type, d_spec);
   12698              : 
   12699              :               /* Propagate to existing clones.  */
   12700          155 :               tree clone;
   12701          441 :               FOR_EACH_CLONE (clone, existing)
   12702          286 :                 TREE_TYPE (clone)
   12703          572 :                   = build_exception_variant (TREE_TYPE (clone), d_spec);
   12704              :             }
   12705              :         }
   12706       185940 :       else if (!DECL_MAYBE_DELETED (d_inner)
   12707       265451 :                && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   12708       371879 :                && !comp_except_specs (d_spec, e_spec, ce_type))
   12709              :         {
   12710         1736 :           mismatch_msg = G_("conflicting %<noexcept%> specifier for "
   12711              :                             "imported declaration %#qD");
   12712         1736 :           goto mismatch;
   12713              :         }
   12714              : 
   12715              :       /* Similarly if EXISTING has an undeduced return type, but DECL's
   12716              :          is already deduced.  */
   12717       200264 :       bool e_undeduced = undeduced_auto_decl (existing);
   12718       200264 :       bool d_undeduced = undeduced_auto_decl (decl);
   12719       200264 :       if (e_undeduced && !d_undeduced)
   12720              :         {
   12721           16 :           dump (dumper::MERGE)
   12722            0 :             && dump ("Propagating deduced return type to %N", existing);
   12723           16 :           gcc_checking_assert (existing == e_inner);
   12724           16 :           FNDECL_USED_AUTO (existing) = true;
   12725           16 :           DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
   12726           16 :           e_type = change_return_type (TREE_TYPE (d_type), e_type);
   12727              :         }
   12728       200248 :       else if (d_undeduced && !e_undeduced)
   12729              :         /* EXISTING was deduced, leave it alone.  */;
   12730       200245 :       else if (type_uses_auto (d_ret)
   12731       200245 :                && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
   12732              :         {
   12733            9 :           mismatch_msg = G_("conflicting deduced return type for "
   12734              :                             "imported declaration %#qD");
   12735            9 :           goto mismatch;
   12736              :         }
   12737              : 
   12738              :       /* Similarly if EXISTING has undeduced constexpr, but DECL's
   12739              :          is already deduced.  */
   12740       200255 :       if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12741       200255 :           == DECL_DECLARED_CONSTEXPR_P (d_inner))
   12742              :         /* Already matches.  */;
   12743            2 :       else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
   12744            2 :                && (DECL_MAYBE_DELETED (e_inner)
   12745            0 :                    || decl_implicit_constexpr_p (d_inner)))
   12746              :         /* DECL was deduced, copy to EXISTING.  */
   12747              :         {
   12748            1 :           DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
   12749            1 :           if (decl_implicit_constexpr_p (d_inner))
   12750            0 :             DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
   12751              :         }
   12752            1 :       else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12753            1 :                && (DECL_MAYBE_DELETED (d_inner)
   12754            0 :                    || decl_implicit_constexpr_p (e_inner)))
   12755              :         /* EXISTING was deduced, leave it alone.  */;
   12756              :       else
   12757              :         {
   12758            0 :           mismatch_msg = G_("conflicting %<constexpr%> for imported "
   12759              :                             "declaration %#qD");
   12760            0 :           goto mismatch;
   12761              :         }
   12762              : 
   12763              :       /* Don't synthesize a defaulted function if we're importing one
   12764              :          we've already determined.  */
   12765       200255 :       if (!DECL_MAYBE_DELETED (d_inner))
   12766       200138 :         DECL_MAYBE_DELETED (e_inner) = false;
   12767              :     }
   12768       240306 :   else if (is_typedef)
   12769              :     {
   12770        85774 :       if (!DECL_ORIGINAL_TYPE (e_inner)
   12771        85774 :           || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
   12772              :                            DECL_ORIGINAL_TYPE (e_inner)))
   12773              :         {
   12774            3 :           mismatch_msg = G_("conflicting imported declaration %q#D");
   12775            3 :           goto mismatch;
   12776              :         }
   12777              :     }
   12778              :   /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
   12779              :      here. I suspect the entities that directly do that are things
   12780              :      that shouldn't go to duplicate_decls (FIELD_DECLs etc).   */
   12781       154532 :   else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
   12782              :     {
   12783              :       mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12784         2530 :     mismatch:
   12785         2530 :       if (DECL_IS_UNDECLARED_BUILTIN (existing))
   12786              :         /* Just like duplicate_decls, presum the user knows what
   12787              :            they're doing in overriding a builtin.   */
   12788         1758 :         TREE_TYPE (existing) = TREE_TYPE (decl);
   12789          772 :       else if (decl_function_context (decl))
   12790              :         /* The type of a mergeable local entity (such as a function scope
   12791              :            capturing lambda's closure type fields) can depend on an
   12792              :            unmergeable local entity (such as a local variable), so type
   12793              :            equality isn't feasible in general for local entities.  */;
   12794              :       else
   12795              :         {
   12796           21 :           gcc_checking_assert (mismatch_msg);
   12797           21 :           auto_diagnostic_group d;
   12798           21 :           error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
   12799           21 :           inform (DECL_SOURCE_LOCATION (existing),
   12800              :                   "existing declaration %#qD", existing);
   12801           21 :           return false;
   12802           21 :         }
   12803              :     }
   12804              : 
   12805       442316 :   if (DECL_IS_UNDECLARED_BUILTIN (existing)
   12806       442316 :       && !DECL_IS_UNDECLARED_BUILTIN (decl))
   12807              :     {
   12808              :       /* We're matching a builtin that the user has yet to declare.
   12809              :          We are the one!  This is very much duplicate-decl
   12810              :          shenanigans. */
   12811         2041 :       DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
   12812         2041 :       if (TREE_CODE (decl) != TYPE_DECL)
   12813              :         {
   12814              :           /* Propagate exceptions etc.  */
   12815         2018 :           TREE_TYPE (existing) = TREE_TYPE (decl);
   12816         2018 :           TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
   12817              :         }
   12818              :       /* This is actually an import! */
   12819         2041 :       DECL_MODULE_IMPORT_P (existing) = true;
   12820              : 
   12821              :       /* Yay, sliced!  */
   12822         2041 :       existing->base = decl->base;
   12823              : 
   12824         2041 :       if (TREE_CODE (decl) == FUNCTION_DECL)
   12825              :         {
   12826              :           /* Ew :(  */
   12827         2018 :           memcpy (&existing->decl_common.size,
   12828              :                   &decl->decl_common.size,
   12829              :                   (offsetof (tree_decl_common, pt_uid)
   12830              :                    - offsetof (tree_decl_common, size)));
   12831         2018 :           auto bltin_class = DECL_BUILT_IN_CLASS (decl);
   12832         2018 :           existing->function_decl.built_in_class = bltin_class;
   12833         2018 :           auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
   12834         2018 :           DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
   12835         2018 :           if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
   12836              :             {
   12837         1808 :               if (builtin_decl_explicit_p (built_in_function (fncode)))
   12838         1808 :                 switch (fncode)
   12839              :                   {
   12840            0 :                   case BUILT_IN_STPCPY:
   12841            0 :                     set_builtin_decl_implicit_p
   12842            0 :                       (built_in_function (fncode), true);
   12843            0 :                     break;
   12844         1808 :                   default:
   12845         1808 :                     set_builtin_decl_declared_p
   12846         1808 :                       (built_in_function (fncode), true);
   12847         1808 :                     break;
   12848              :                   }
   12849         1808 :               copy_attributes_to_builtin (decl);
   12850              :             }
   12851              :         }
   12852              :     }
   12853              : 
   12854       442316 :   if (VAR_OR_FUNCTION_DECL_P (decl)
   12855       442316 :       && DECL_TEMPLATE_INSTANTIATED (decl))
   12856              :     /* Don't instantiate again!  */
   12857         9305 :     DECL_TEMPLATE_INSTANTIATED (existing) = true;
   12858              : 
   12859       442316 :   if (TREE_CODE (d_inner) == FUNCTION_DECL
   12860       442316 :       && DECL_DECLARED_INLINE_P (d_inner))
   12861              :     {
   12862       164196 :       DECL_DECLARED_INLINE_P (e_inner) = true;
   12863       164196 :       if (!DECL_SAVED_TREE (e_inner)
   12864        81612 :           && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
   12865       164213 :           && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
   12866              :         {
   12867           51 :           DECL_INTERFACE_KNOWN (e_inner)
   12868           17 :             |= DECL_INTERFACE_KNOWN (d_inner);
   12869           17 :           DECL_DISREGARD_INLINE_LIMITS (e_inner)
   12870           17 :             |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
   12871              :           // TODO: we will eventually want to merge all decl attributes
   12872           17 :           duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
   12873           17 :                                    DECL_ATTRIBUTES (d_inner), "gnu_inline");
   12874              :         }
   12875              :     }
   12876       442316 :   if (!DECL_EXTERNAL (d_inner))
   12877       207476 :     DECL_EXTERNAL (e_inner) = false;
   12878              : 
   12879       442316 :   if (VAR_OR_FUNCTION_DECL_P (d_inner))
   12880       445400 :     check_abi_tags (existing, decl,
   12881       222700 :                     DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
   12882              : 
   12883       442316 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12884              :     {
   12885              :       /* Merge default template arguments.  */
   12886       147025 :       tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
   12887       147025 :       tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
   12888       147025 :       gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
   12889              :                            == TREE_VEC_LENGTH (e_parms));
   12890       420807 :       for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
   12891              :         {
   12892       273791 :           tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
   12893       273791 :           tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
   12894       273791 :           if (e_default == NULL_TREE)
   12895       231881 :             e_default = d_default;
   12896        41910 :           else if (d_default != NULL_TREE
   12897        41910 :                    && !cp_tree_equal (d_default, e_default))
   12898              :             {
   12899            9 :               auto_diagnostic_group d;
   12900            9 :               tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
   12901            9 :               tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
   12902            9 :               error_at (DECL_SOURCE_LOCATION (d_parm),
   12903              :                         "conflicting default argument for %#qD", d_parm);
   12904            9 :               inform (DECL_SOURCE_LOCATION (e_parm),
   12905              :                       "existing default declared here");
   12906            9 :               return false;
   12907            9 :             }
   12908              :         }
   12909              :     }
   12910              : 
   12911       442307 :   if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12912              :     {
   12913              :       /* Merge default function arguments.  */
   12914       202016 :       tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
   12915       202016 :       tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
   12916       202016 :       int i = 0;
   12917       683113 :       for (; d_parm && d_parm != void_list_node;
   12918       279081 :            d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
   12919              :         {
   12920       279093 :           tree d_default = TREE_PURPOSE (d_parm);
   12921       279093 :           tree& e_default = TREE_PURPOSE (e_parm);
   12922       279093 :           if (e_default == NULL_TREE)
   12923       262471 :             e_default = d_default;
   12924        16622 :           else if (d_default != NULL_TREE
   12925        16622 :                    && !cp_tree_equal (d_default, e_default))
   12926              :             {
   12927           12 :               auto_diagnostic_group d;
   12928           12 :               error_at (get_fndecl_argument_location (d_inner, i),
   12929              :                         "conflicting default argument for parameter %P of %#qD",
   12930              :                         i, decl);
   12931           12 :               inform (get_fndecl_argument_location (e_inner, i),
   12932              :                       "existing default declared here");
   12933           12 :               return false;
   12934           12 :             }
   12935              :         }
   12936              :     }
   12937              : 
   12938              :   return true;
   12939              : }
   12940              : 
   12941              : /* FN is an implicit member function that we've discovered is new to
   12942              :    the class.  Add it to the TYPE_FIELDS chain and the method vector.
   12943              :    Reset the appropriate classtype lazy flag.   */
   12944              : 
   12945              : bool
   12946         1012 : trees_in::install_implicit_member (tree fn)
   12947              : {
   12948         1012 :   tree ctx = DECL_CONTEXT (fn);
   12949         1012 :   tree name = DECL_NAME (fn);
   12950              :   /* We know these are synthesized, so the set of expected prototypes
   12951              :      is quite restricted.  We're not validating correctness, just
   12952              :      distinguishing between the small set of possibilities.  */
   12953         1012 :   tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
   12954         1012 :   if (IDENTIFIER_CTOR_P (name))
   12955              :     {
   12956          672 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
   12957          672 :           && VOID_TYPE_P (parm_type))
   12958          166 :         CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
   12959          506 :       else if (!TYPE_REF_P (parm_type))
   12960              :         return false;
   12961          506 :       else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
   12962          506 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12963          256 :         CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
   12964          250 :       else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
   12965          250 :         CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
   12966              :       else
   12967              :         return false;
   12968              :     }
   12969          340 :   else if (IDENTIFIER_DTOR_P (name))
   12970              :     {
   12971          268 :       if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
   12972          268 :         CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
   12973              :       else
   12974              :         return false;
   12975          268 :       if (DECL_VIRTUAL_P (fn))
   12976              :         /* A virtual dtor should have been created when the class
   12977              :            became complete.  */
   12978              :         return false;
   12979              :     }
   12980           72 :   else if (name == assign_op_identifier)
   12981              :     {
   12982           72 :       if (!TYPE_REF_P (parm_type))
   12983              :         return false;
   12984           72 :       else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
   12985           72 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12986           36 :         CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
   12987           36 :       else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
   12988           36 :         CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
   12989              :       else
   12990              :         return false;
   12991              :     }
   12992              :   else
   12993              :     return false;
   12994              : 
   12995         1057 :   dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
   12996              : 
   12997         1012 :   DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
   12998         1012 :   TYPE_FIELDS (ctx) = fn;
   12999              : 
   13000         1012 :   add_method (ctx, fn, false);
   13001              : 
   13002              :     /* Propagate TYPE_FIELDS.  */
   13003         1012 :   fixup_type_variants (ctx);
   13004              : 
   13005         1012 :   return true;
   13006              : }
   13007              : 
   13008              : /* Return non-zero if DECL has a definition that would be interesting to
   13009              :    write out.  */
   13010              : 
   13011              : static bool
   13012      2206674 : has_definition (tree decl)
   13013              : {
   13014      2206680 :   bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
   13015      2206680 :   if (is_tmpl)
   13016       486660 :     decl = DECL_TEMPLATE_RESULT (decl);
   13017              : 
   13018      2206680 :   switch (TREE_CODE (decl))
   13019              :     {
   13020              :     default:
   13021              :       break;
   13022              : 
   13023       828126 :     case FUNCTION_DECL:
   13024       828126 :       if (!DECL_SAVED_TREE (decl))
   13025              :         /* Not defined.  */
   13026              :         break;
   13027              : 
   13028       375455 :       if (DECL_DECLARED_INLINE_P (decl))
   13029              :         return true;
   13030              : 
   13031        30217 :       if (header_module_p ())
   13032              :         /* We always need to write definitions in header modules,
   13033              :            since there's no TU to emit them in otherwise.  */
   13034              :         return true;
   13035              : 
   13036        18836 :       if (DECL_TEMPLATE_INFO (decl))
   13037              :         {
   13038        17545 :           int use_tpl = DECL_USE_TEMPLATE (decl);
   13039              : 
   13040              :           // FIXME: Partial specializations have definitions too.
   13041        17545 :           if (use_tpl < 2)
   13042              :             return true;
   13043              :         }
   13044              : 
   13045              :       /* Coroutine transform functions always need to be emitted
   13046              :          into the importing TU if the ramp function will be.  */
   13047         1351 :       if (DECL_COROUTINE_P (decl))
   13048           12 :         if (tree ramp = DECL_RAMP_FN (decl))
   13049              :           return has_definition (ramp);
   13050              :       break;
   13051              : 
   13052       918386 :     case TYPE_DECL:
   13053       918386 :       {
   13054       918386 :         tree type = TREE_TYPE (decl);
   13055       918386 :         if (type == TYPE_MAIN_VARIANT (type)
   13056       420545 :             && decl == TYPE_NAME (type)
   13057      1338931 :             && (TREE_CODE (type) == ENUMERAL_TYPE
   13058       420545 :                 ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
   13059       207713 :           return true;
   13060              :       }
   13061              :       break;
   13062              : 
   13063       116577 :     case VAR_DECL:
   13064              :       /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL.  */
   13065       116577 :       if (DECL_LANG_SPECIFIC (decl)
   13066       115693 :           && DECL_TEMPLATE_INFO (decl)
   13067       191453 :           && DECL_INITIAL (decl))
   13068              :         return true;
   13069              :       else
   13070              :         {
   13071        47068 :           if (!DECL_INITIALIZED_P (decl))
   13072              :             /* Not defined.  */
   13073              :             return false;
   13074              : 
   13075        39365 :           if (header_module_p ())
   13076              :             /* We always need to write definitions in header modules,
   13077              :                since there's no TU to emit them in otherwise.  */
   13078              :             return true;
   13079              : 
   13080        13889 :           if (decl_maybe_constant_var_p (decl))
   13081              :             /* We might need its constant value.  */
   13082              :             return true;
   13083              : 
   13084          499 :           if (vague_linkage_p (decl))
   13085              :             /* These are emitted as needed.  */
   13086              :             return true;
   13087              : 
   13088              :           return false;
   13089              :         }
   13090         8277 :       break;
   13091              : 
   13092         8277 :     case CONCEPT_DECL:
   13093         8277 :       if (DECL_INITIAL (decl))
   13094         8277 :         return true;
   13095              : 
   13096              :       break;
   13097              :     }
   13098              : 
   13099              :   return false;
   13100              : }
   13101              : 
   13102              : uintptr_t *
   13103       653978 : trees_in::find_duplicate (tree existing)
   13104              : {
   13105       304315 :   if (!duplicates)
   13106              :     return NULL;
   13107              : 
   13108       453907 :   return duplicates->get (existing);
   13109              : }
   13110              : 
   13111              : /* We're starting to read a duplicate DECL.  EXISTING is the already
   13112              :    known node.  */
   13113              : 
   13114              : void
   13115       631234 : trees_in::register_duplicate (tree decl, tree existing)
   13116              : {
   13117       631234 :   if (!duplicates)
   13118       104292 :     duplicates = new duplicate_hash_map (40);
   13119              : 
   13120       631234 :   bool existed;
   13121       631234 :   uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
   13122       631234 :   gcc_checking_assert (!existed);
   13123       631234 :   slot = reinterpret_cast<uintptr_t> (decl);
   13124              : 
   13125       631234 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   13126              :     /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
   13127              :        that passing decl's _RESULT to maybe_duplicate naturally
   13128              :        gives us existing's _RESULT back.  */
   13129       294054 :     register_duplicate (DECL_TEMPLATE_RESULT (decl),
   13130       147027 :                         DECL_TEMPLATE_RESULT (existing));
   13131       631234 : }
   13132              : 
   13133              : /* We've read a definition of MAYBE_EXISTING.  If not a duplicate,
   13134              :    return MAYBE_EXISTING (into which the definition should be
   13135              :    installed).  Otherwise return NULL if already known bad, or the
   13136              :    duplicate we read (for ODR checking, or extracting additional merge
   13137              :    information).  */
   13138              : 
   13139              : tree
   13140       349663 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
   13141              : {
   13142       349663 :   tree res = NULL_TREE;
   13143              : 
   13144       563011 :   if (uintptr_t *dup = find_duplicate (maybe_existing))
   13145              :     {
   13146       148929 :       if (!(*dup & 1))
   13147       148926 :         res = reinterpret_cast<tree> (*dup);
   13148              :     }
   13149              :   else
   13150              :     res = maybe_existing;
   13151              : 
   13152       349663 :   assert_definition (maybe_existing, res && !has_defn);
   13153              : 
   13154              :   // FIXME: We probably need to return the template, so that the
   13155              :   // template header can be checked?
   13156       349663 :   return res ? STRIP_TEMPLATE (res) : NULL_TREE;
   13157              : }
   13158              : 
   13159              : /* The following writer functions rely on the current behaviour of
   13160              :    depset::hash::add_dependency making the decl and defn depset nodes
   13161              :    depend on each other.  That way we don't have to worry about seeding
   13162              :    the tree map with named decls that cannot be looked up by name (I.e
   13163              :    template and function parms).  We know the decl and definition will
   13164              :    be in the same cluster, which is what we want.  */
   13165              : 
   13166              : void
   13167       618497 : trees_out::write_function_def (tree decl)
   13168              : {
   13169       618497 :   tree_node (DECL_RESULT (decl));
   13170              : 
   13171       618497 :   {
   13172              :     /* The function body for a non-inline function or function template
   13173              :        is ignored for determining exposures.  This should only matter
   13174              :        for templates (we don't emit the bodies of non-inline functions
   13175              :        to begin with).  */
   13176       618497 :     auto ovr = dep_hash->ignore_exposure_if (!DECL_DECLARED_INLINE_P (decl));
   13177       618497 :     tree_node (DECL_INITIAL (decl));
   13178       618497 :     tree_node (DECL_SAVED_TREE (decl));
   13179       618497 :   }
   13180              : 
   13181      1227960 :   tree_node (DECL_FRIEND_CONTEXT (decl));
   13182              : 
   13183       618497 :   constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
   13184              : 
   13185       618497 :   if (streaming_p ())
   13186       309207 :     u (cexpr != nullptr);
   13187       618497 :   if (cexpr)
   13188              :     {
   13189       141723 :       chained_decls (cexpr->parms);
   13190       141723 :       tree_node (cexpr->result);
   13191       141723 :       tree_node (cexpr->body);
   13192              :     }
   13193              : 
   13194       618497 :   function* f = DECL_STRUCT_FUNCTION (decl);
   13195              : 
   13196       618497 :   if (streaming_p ())
   13197              :     {
   13198       309207 :       unsigned flags = 0;
   13199              : 
   13200              :       /* Whether the importer should emit this definition, if used.  */
   13201       309207 :       flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
   13202       309207 :                     && (get_importer_interface (decl)
   13203              :                         != importer_interface::external));
   13204              : 
   13205              :       /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
   13206              :          on non-templates or we'll crash later in import_export_decl.  */
   13207       207181 :       gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
   13208              :                            || (DECL_LANG_SPECIFIC (decl)
   13209              :                                && DECL_LOCAL_DECL_P (decl)
   13210              :                                && DECL_OMP_DECLARE_REDUCTION_P (decl))
   13211              :                            || (DECL_LANG_SPECIFIC (decl)
   13212              :                                && DECL_TEMPLATE_INFO (decl)
   13213              :                                && uses_template_parms (DECL_TI_ARGS (decl))));
   13214              : 
   13215       309207 :       if (f)
   13216              :         {
   13217       307959 :           flags |= 2;
   13218              :           /* These flags are needed in tsubst_lambda_expr.  */
   13219       307959 :           flags |= 4 * f->language->returns_value;
   13220       307959 :           flags |= 8 * f->language->returns_null;
   13221       307959 :           flags |= 16 * f->language->returns_abnormally;
   13222       307959 :           flags |= 32 * f->language->infinite_loop;
   13223              :         }
   13224              : 
   13225       309207 :       u (flags);
   13226              :     }
   13227              : 
   13228       618497 :   if (state && f)
   13229              :     {
   13230       616001 :       state->write_location (*this, f->function_start_locus);
   13231       616001 :       state->write_location (*this, f->function_end_locus);
   13232              :     }
   13233              : 
   13234       618497 :   if (DECL_COROUTINE_P (decl))
   13235              :     {
   13236           42 :       tree ramp = DECL_RAMP_FN (decl);
   13237           42 :       tree_node (ramp);
   13238           42 :       if (!ramp)
   13239              :         {
   13240           30 :           tree_node (DECL_ACTOR_FN (decl));
   13241           30 :           tree_node (DECL_DESTROY_FN (decl));
   13242              :         }
   13243              :     }
   13244       618497 : }
   13245              : 
   13246              : void
   13247            0 : trees_out::mark_function_def (tree)
   13248              : {
   13249            0 : }
   13250              : 
   13251              : bool
   13252       231842 : trees_in::read_function_def (tree decl, tree maybe_template)
   13253              : {
   13254       232392 :   dump () && dump ("Reading function definition %N", decl);
   13255       231842 :   tree result = tree_node ();
   13256       231842 :   tree initial = tree_node ();
   13257       231842 :   tree saved = tree_node ();
   13258       231842 :   tree context = tree_node ();
   13259       231842 :   post_process_data pdata {};
   13260       231842 :   pdata.decl = maybe_template;
   13261              : 
   13262       231842 :   tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
   13263       463681 :   bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
   13264              : 
   13265       231842 :   constexpr_fundef cexpr;
   13266       231842 :   if (u ())
   13267              :     {
   13268        52847 :       cexpr.parms = chained_decls ();
   13269        52847 :       cexpr.result = tree_node ();
   13270        52847 :       cexpr.body = tree_node ();
   13271        52847 :       cexpr.decl = decl;
   13272              :     }
   13273              :   else
   13274              :     cexpr.decl = NULL_TREE;
   13275              : 
   13276       231842 :   unsigned flags = u ();
   13277       231842 :   if (flags & 2)
   13278              :     {
   13279       230903 :       pdata.start_locus = state->read_location (*this);
   13280       230903 :       pdata.end_locus = state->read_location (*this);
   13281       230903 :       pdata.returns_value = flags & 4;
   13282       230903 :       pdata.returns_null = flags & 8;
   13283       230903 :       pdata.returns_abnormally = flags & 16;
   13284       230903 :       pdata.infinite_loop = flags & 32;
   13285              :     }
   13286              : 
   13287       231842 :   tree coro_actor = NULL_TREE;
   13288       231842 :   tree coro_destroy = NULL_TREE;
   13289       231842 :   tree coro_ramp = NULL_TREE;
   13290       231842 :   if (DECL_COROUTINE_P (decl))
   13291              :     {
   13292           18 :       coro_ramp = tree_node ();
   13293           18 :       if (!coro_ramp)
   13294              :         {
   13295           12 :           coro_actor = tree_node ();
   13296           12 :           coro_destroy = tree_node ();
   13297           12 :           if ((coro_actor == NULL_TREE) != (coro_destroy == NULL_TREE))
   13298            0 :             set_overrun ();
   13299              :         }
   13300              :     }
   13301              : 
   13302       231842 :   if (get_overrun ())
   13303              :     return NULL_TREE;
   13304              : 
   13305       231842 :   if (installing)
   13306              :     {
   13307       144812 :       DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
   13308       144812 :       DECL_RESULT (decl) = result;
   13309       144812 :       DECL_INITIAL (decl) = initial;
   13310       144812 :       DECL_SAVED_TREE (decl) = saved;
   13311              : 
   13312              :       /* Some entities (like anticipated builtins) were declared without
   13313              :          DECL_ARGUMENTS, so update them now.  But don't do it if there's
   13314              :          already an argument list, because we've already built the
   13315              :          definition referencing those merged PARM_DECLs.  */
   13316       144812 :       if (!DECL_ARGUMENTS (decl))
   13317         6893 :         DECL_ARGUMENTS (decl) = DECL_ARGUMENTS (maybe_dup);
   13318              : 
   13319       144812 :       if (context)
   13320         6318 :         SET_DECL_FRIEND_CONTEXT (decl, context);
   13321       144812 :       if (cexpr.decl)
   13322        38335 :         register_constexpr_fundef (cexpr);
   13323              : 
   13324       144812 :       if (coro_ramp)
   13325            6 :         coro_set_ramp_function (decl, coro_ramp);
   13326       144806 :       else if (coro_actor && coro_destroy)
   13327            3 :         coro_set_transform_functions (decl, coro_actor, coro_destroy);
   13328              : 
   13329       144812 :       if (DECL_LOCAL_DECL_P (decl))
   13330              :         /* Block-scope OMP UDRs aren't real functions, and don't need a
   13331              :            function structure to be allocated or to be expanded.  */
   13332            3 :         gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (decl));
   13333              :       else
   13334       144809 :         post_process (pdata);
   13335              :     }
   13336              :   else if (maybe_dup)
   13337              :     {
   13338              :       // FIXME:QOI Check matching defn
   13339              :     }
   13340              : 
   13341              :   return true;
   13342              : }
   13343              : 
   13344              : /* Also for CONCEPT_DECLs.  */
   13345              : 
   13346              : void
   13347       145880 : trees_out::write_var_def (tree decl)
   13348              : {
   13349              :   /* The initializer of a non-inline variable or variable template is
   13350              :      ignored for determining exposures.  */
   13351       145880 :   auto ovr = dep_hash->ignore_exposure_if (VAR_P (decl)
   13352       145880 :                                            && !DECL_INLINE_VAR_P (decl));
   13353              : 
   13354       145880 :   tree init = DECL_INITIAL (decl);
   13355       145880 :   tree_node (init);
   13356       145880 :   if (!init)
   13357              :     {
   13358         1824 :       tree dyn_init = NULL_TREE;
   13359              : 
   13360              :       /* We only need to write initializers in header modules.  */
   13361         3008 :       if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
   13362              :         {
   13363          450 :           dyn_init = value_member (decl,
   13364          450 :                                    CP_DECL_THREAD_LOCAL_P (decl)
   13365              :                                    ? tls_aggregates : static_aggregates);
   13366          450 :           gcc_checking_assert (dyn_init);
   13367              :           /* Mark it so write_inits knows this is needed.  */
   13368          450 :           TREE_LANG_FLAG_0 (dyn_init) = true;
   13369          450 :           dyn_init = TREE_PURPOSE (dyn_init);
   13370              :         }
   13371         1824 :       tree_node (dyn_init);
   13372              :     }
   13373       145880 : }
   13374              : 
   13375              : void
   13376            0 : trees_out::mark_var_def (tree)
   13377              : {
   13378            0 : }
   13379              : 
   13380              : bool
   13381        45731 : trees_in::read_var_def (tree decl, tree maybe_template)
   13382              : {
   13383              :   /* Do not mark the virtual table entries as used.  */
   13384        45731 :   bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
   13385        45731 :   unused += vtable;
   13386        45731 :   tree init = tree_node ();
   13387        45731 :   tree dyn_init = init ? NULL_TREE : tree_node ();
   13388        45731 :   unused -= vtable;
   13389              : 
   13390        45731 :   if (get_overrun ())
   13391              :     return false;
   13392              : 
   13393        45731 :   bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
   13394        45731 :                       : bool (DECL_INITIAL (decl)));
   13395        45731 :   tree maybe_dup = odr_duplicate (maybe_template, initialized);
   13396        45731 :   bool installing = maybe_dup && !initialized;
   13397        45731 :   if (installing)
   13398              :     {
   13399        28651 :       DECL_INITIAL (decl) = init;
   13400        28651 :       if (DECL_EXTERNAL (decl))
   13401         3577 :         DECL_NOT_REALLY_EXTERN (decl) = true;
   13402        28651 :       if (VAR_P (decl))
   13403              :         {
   13404        25047 :           DECL_INITIALIZED_P (decl) = true;
   13405        25047 :           if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
   13406        24636 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   13407        25047 :           tentative_decl_linkage (decl);
   13408        25047 :           if (DECL_EXPLICIT_INSTANTIATION (decl)
   13409        25047 :               && !DECL_EXTERNAL (decl))
   13410            9 :             setup_explicit_instantiation_definition_linkage (decl);
   13411              :           /* Class non-template static members are handled in read_class_def.
   13412              :              But still handle specialisations of member templates.  */
   13413        50094 :           if ((!DECL_CLASS_SCOPE_P (decl)
   13414        16115 :                || primary_template_specialization_p (decl))
   13415        34098 :               && (DECL_IMPLICIT_INSTANTIATION (decl)
   13416         8968 :                   || (DECL_EXPLICIT_INSTANTIATION (decl)
   13417           21 :                       && !DECL_EXTERNAL (decl))))
   13418           92 :             note_vague_linkage_variable (decl);
   13419              :         }
   13420        28651 :       if (!dyn_init)
   13421              :         ;
   13422          216 :       else if (CP_DECL_THREAD_LOCAL_P (decl))
   13423           96 :         tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
   13424              :       else
   13425          120 :         static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
   13426              :     }
   13427              :   else if (maybe_dup)
   13428              :     {
   13429              :       // FIXME:QOI Check matching defn
   13430              :     }
   13431              : 
   13432              :   return true;
   13433              : }
   13434              : 
   13435              : /* If MEMBER doesn't have an independent life outside the class,
   13436              :    return it (or its TEMPLATE_DECL).  Otherwise NULL.  */
   13437              : 
   13438              : static tree
   13439       298058 : member_owned_by_class (tree member)
   13440              : {
   13441       298058 :   gcc_assert (DECL_P (member));
   13442              : 
   13443              :   /* Clones are owned by their origin.  */
   13444       298058 :   if (DECL_CLONED_FUNCTION_P (member))
   13445              :     return NULL;
   13446              : 
   13447       298058 :   if (TREE_CODE (member) == FIELD_DECL)
   13448              :     /* FIELD_DECLS can have template info in some cases.  We always
   13449              :        want the FIELD_DECL though, as there's never a TEMPLATE_DECL
   13450              :        wrapping them.  */
   13451              :     return member;
   13452              : 
   13453       127901 :   int use_tpl = -1;
   13454       127901 :   if (tree ti = node_template_info (member, use_tpl))
   13455              :     {
   13456              :       // FIXME: Don't bail on things that CANNOT have their own
   13457              :       // template header.  No, make sure they're in the same cluster.
   13458            0 :       if (use_tpl > 0)
   13459              :         return NULL_TREE;
   13460              : 
   13461            0 :       if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
   13462       298058 :         member = TI_TEMPLATE (ti);
   13463              :     }
   13464              :   return member;
   13465              : }
   13466              : 
   13467              : void
   13468       211752 : trees_out::write_class_def (tree defn)
   13469              : {
   13470       211752 :   gcc_assert (DECL_P (defn));
   13471       211752 :   if (streaming_p ())
   13472       106273 :     dump () && dump ("Writing class definition %N", defn);
   13473              : 
   13474       211752 :   tree type = TREE_TYPE (defn);
   13475       211752 :   tree_node (TYPE_SIZE (type));
   13476       211752 :   tree_node (TYPE_SIZE_UNIT (type));
   13477       211752 :   tree_node (TYPE_VFIELD (type));
   13478       211752 :   tree_node (TYPE_BINFO (type));
   13479              : 
   13480       211752 :   vec_chained_decls (TYPE_FIELDS (type));
   13481              : 
   13482              :   /* Every class but __as_base has a type-specific.  */
   13483       419792 :   gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
   13484              : 
   13485       211752 :   if (TYPE_LANG_SPECIFIC (type))
   13486              :     {
   13487       208040 :       {
   13488       208040 :         vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
   13489       208040 :         if (!v)
   13490              :           {
   13491        49176 :             gcc_checking_assert (!streaming_p ());
   13492              :             /* Force a class vector.  */
   13493        49176 :             v = set_class_bindings (type, -1);
   13494        49176 :             gcc_checking_assert (v);
   13495              :           }
   13496              : 
   13497       208040 :         unsigned len = v->length ();
   13498       208040 :         if (streaming_p ())
   13499       103997 :           u (len);
   13500      1765250 :         for (unsigned ix = 0; ix != len; ix++)
   13501              :           {
   13502      1557210 :             tree m = (*v)[ix];
   13503      1557210 :             if (TREE_CODE (m) == TYPE_DECL
   13504       451899 :                 && DECL_ARTIFICIAL (m)
   13505      1783167 :                 && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
   13506              :               /* This is a using-decl for a type, or an anonymous
   13507              :                  struct (maybe with a typedef name).  Write the type.  */
   13508        16614 :               m = TREE_TYPE (m);
   13509      1557210 :             tree_node (m);
   13510              :           }
   13511              :       }
   13512       208040 :       tree_node (CLASSTYPE_LAMBDA_EXPR (type));
   13513              : 
   13514              :       /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
   13515              :          reader won't know at this point.  */
   13516       208040 :       int has_vptr = TYPE_CONTAINS_VPTR_P (type);
   13517              : 
   13518       208040 :       if (streaming_p ())
   13519              :         {
   13520       103997 :           unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
   13521       103997 :           u (nvbases);
   13522       103997 :           i (has_vptr);
   13523              :         }
   13524              : 
   13525       208040 :       if (has_vptr)
   13526              :         {
   13527         7478 :           tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
   13528         7478 :           tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
   13529         7478 :           tree_node (CLASSTYPE_KEY_METHOD (type));
   13530              :         }
   13531              :     }
   13532              : 
   13533       211752 :   if (TYPE_LANG_SPECIFIC (type))
   13534              :     {
   13535       208040 :       tree_node (CLASSTYPE_PRIMARY_BINFO (type));
   13536              : 
   13537       208040 :       tree as_base = CLASSTYPE_AS_BASE (type);
   13538       208040 :       if (as_base)
   13539       108195 :         as_base = TYPE_NAME (as_base);
   13540       208040 :       tree_node (as_base);
   13541              : 
   13542              :       /* Write the vtables.  */
   13543       208040 :       tree vtables = CLASSTYPE_VTABLES (type);
   13544       208040 :       vec_chained_decls (vtables);
   13545       424996 :       for (; vtables; vtables = TREE_CHAIN (vtables))
   13546         8916 :         write_definition (vtables);
   13547              : 
   13548       208040 :       {
   13549              :         /* Friend declarations in class definitions are ignored when
   13550              :            determining exposures.  */
   13551       208040 :         auto ovr = dep_hash->ignore_exposure_if (true);
   13552              : 
   13553              :         /* Write the friend classes.  */
   13554       208040 :         tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
   13555              : 
   13556              :         /* Write the friend functions.  */
   13557       208040 :         for (tree friends = DECL_FRIENDLIST (defn);
   13558       239068 :              friends; friends = TREE_CHAIN (friends))
   13559              :           {
   13560        31028 :             tree_node (FRIEND_NAME (friends));
   13561        31028 :             tree_list (FRIEND_DECLS (friends), false);
   13562              :           }
   13563              :         /* End of friend fns.  */
   13564       208040 :         tree_node (NULL_TREE);
   13565       208040 :       }
   13566              : 
   13567              :       /* Write the decl list.  We don't need to ignore exposures of friend
   13568              :          decls here as any such decls should already have been added and
   13569              :          ignored above.  */
   13570       208040 :       tree_list (CLASSTYPE_DECL_LIST (type), true);
   13571              : 
   13572       208040 :       if (TYPE_CONTAINS_VPTR_P (type))
   13573              :         {
   13574              :           /* Write the thunks.  */
   13575         7478 :           for (tree decls = TYPE_FIELDS (type);
   13576       212420 :                decls; decls = DECL_CHAIN (decls))
   13577       204942 :             if (TREE_CODE (decls) == FUNCTION_DECL
   13578       147886 :                 && DECL_VIRTUAL_P (decls)
   13579       243730 :                 && DECL_THUNKS (decls))
   13580              :               {
   13581         1392 :                 tree_node (decls);
   13582              :                 /* Thunks are always unique, so chaining is ok.  */
   13583         1392 :                 chained_decls (DECL_THUNKS (decls));
   13584              :               }
   13585         7478 :           tree_node (NULL_TREE);
   13586              :         }
   13587              :     }
   13588       211752 : }
   13589              : 
   13590              : void
   13591       298058 : trees_out::mark_class_member (tree member, bool do_defn)
   13592              : {
   13593       298058 :   gcc_assert (DECL_P (member));
   13594              : 
   13595       298058 :   member = member_owned_by_class (member);
   13596       298058 :   if (member)
   13597       596116 :     mark_declaration (member, do_defn && has_definition (member));
   13598       298058 : }
   13599              : 
   13600              : void
   13601       211780 : trees_out::mark_class_def (tree defn)
   13602              : {
   13603       211780 :   gcc_assert (DECL_P (defn));
   13604       211780 :   tree type = TREE_TYPE (defn);
   13605              :   /* Mark the class members that are not type-decls and cannot have
   13606              :      independent definitions.  */
   13607      2452330 :   for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
   13608      2240550 :     if (TREE_CODE (member) == FIELD_DECL
   13609      2240550 :         || TREE_CODE (member) == USING_DECL
   13610              :         /* A cloned enum-decl from 'using enum unrelated;'   */
   13611      2240550 :         || (TREE_CODE (member) == CONST_DECL
   13612        17364 :             && DECL_CONTEXT (member) == type))
   13613              :       {
   13614       298058 :         mark_class_member (member);
   13615       298058 :         if (TREE_CODE (member) == FIELD_DECL)
   13616       170157 :           if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
   13617              :             /* If we're marking a class template definition, then
   13618              :                this'll contain the width (as set by grokbitfield)
   13619              :                instead of a decl.  */
   13620         3140 :             if (DECL_P (repr))
   13621         2440 :               mark_declaration (repr, false);
   13622              :       }
   13623              : 
   13624              :   /* Mark the binfo hierarchy.  */
   13625       499098 :   for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
   13626       287318 :     mark_by_value (child);
   13627              : 
   13628       211780 :   if (TYPE_LANG_SPECIFIC (type))
   13629              :     {
   13630       208044 :       for (tree vtable = CLASSTYPE_VTABLES (type);
   13631       216960 :            vtable; vtable = TREE_CHAIN (vtable))
   13632         8916 :         mark_declaration (vtable, true);
   13633              : 
   13634       208044 :       if (TYPE_CONTAINS_VPTR_P (type))
   13635              :         /* Mark the thunks, they belong to the class definition,
   13636              :            /not/ the thunked-to function.  */
   13637         7478 :         for (tree decls = TYPE_FIELDS (type);
   13638       212420 :              decls; decls = DECL_CHAIN (decls))
   13639       204942 :           if (TREE_CODE (decls) == FUNCTION_DECL)
   13640       147886 :             for (tree thunks = DECL_THUNKS (decls);
   13641       149734 :                  thunks; thunks = DECL_CHAIN (thunks))
   13642         1848 :               mark_declaration (thunks, false);
   13643              :     }
   13644       211780 : }
   13645              : 
   13646              : /* Nop sorting, needed for resorting the member vec.  */
   13647              : 
   13648              : static void
   13649     11568124 : nop (void *, void *, void *)
   13650              : {
   13651     11568124 : }
   13652              : 
   13653              : bool
   13654        68910 : trees_in::read_class_def (tree defn, tree maybe_template)
   13655              : {
   13656        68910 :   gcc_assert (DECL_P (defn));
   13657        69565 :   dump () && dump ("Reading class definition %N", defn);
   13658        68910 :   tree type = TREE_TYPE (defn);
   13659        68910 :   tree size = tree_node ();
   13660        68910 :   tree size_unit = tree_node ();
   13661        68910 :   tree vfield = tree_node ();
   13662        68910 :   tree binfo = tree_node ();
   13663        68910 :   vec<tree, va_gc> *vbase_vec = NULL;
   13664        68910 :   vec<tree, va_gc> *member_vec = NULL;
   13665        68910 :   vec<tree, va_gc> *pure_virts = NULL;
   13666        68910 :   vec<tree_pair_s, va_gc> *vcall_indices = NULL;
   13667        68910 :   tree key_method = NULL_TREE;
   13668        68910 :   tree lambda = NULL_TREE;
   13669              : 
   13670              :   /* Read the fields.  */
   13671        68910 :   vec<tree, va_heap> *fields = vec_chained_decls ();
   13672              : 
   13673        68910 :   if (TYPE_LANG_SPECIFIC (type))
   13674              :     {
   13675        67527 :       if (unsigned len = u ())
   13676              :         {
   13677        67527 :           vec_alloc (member_vec, len);
   13678       678980 :           for (unsigned ix = 0; ix != len; ix++)
   13679              :             {
   13680       543926 :               tree m = tree_node ();
   13681       543926 :               if (get_overrun ())
   13682              :                 break;
   13683       543926 :               if (TYPE_P (m))
   13684         5726 :                 m = TYPE_STUB_DECL (m);
   13685       543926 :               member_vec->quick_push (m);
   13686              :             }
   13687              :         }
   13688        67527 :       lambda = tree_node ();
   13689              : 
   13690        67527 :       if (!get_overrun ())
   13691              :         {
   13692        67527 :           unsigned nvbases = u ();
   13693        67527 :           if (nvbases)
   13694              :             {
   13695          285 :               vec_alloc (vbase_vec, nvbases);
   13696         1300 :               for (tree child = binfo; child; child = TREE_CHAIN (child))
   13697         1015 :                 if (BINFO_VIRTUAL_P (child))
   13698          285 :                   vbase_vec->quick_push (child);
   13699              :             }
   13700              :         }
   13701              : 
   13702        67527 :       if (!get_overrun ())
   13703              :         {
   13704        67527 :           int has_vptr = i ();
   13705        67527 :           if (has_vptr)
   13706              :             {
   13707         2696 :               pure_virts = tree_vec ();
   13708         2696 :               vcall_indices = tree_pair_vec ();
   13709         2696 :               key_method = tree_node ();
   13710              :             }
   13711              :         }
   13712              :     }
   13713              : 
   13714        68910 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
   13715        68910 :   bool installing = maybe_dup && !TYPE_SIZE (type);
   13716        38556 :   if (installing)
   13717              :     {
   13718        38556 :       if (maybe_dup != defn)
   13719              :         {
   13720              :           // FIXME: This is needed on other defns too, almost
   13721              :           // duplicate-decl like?  See is_matching_decl too.
   13722              :           /* Copy flags from the duplicate.  */
   13723          303 :           tree type_dup = TREE_TYPE (maybe_dup);
   13724              : 
   13725              :           /* Core pieces.  */
   13726          303 :           TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
   13727          303 :           TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
   13728          606 :           TYPE_WARN_IF_NOT_ALIGN_RAW (type)
   13729          303 :             = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
   13730          303 :           TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
   13731              : 
   13732          303 :           SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
   13733          303 :           DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
   13734          303 :           DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
   13735          303 :           DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
   13736          606 :           DECL_WARN_IF_NOT_ALIGN_RAW (defn)
   13737          303 :             = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
   13738          303 :           DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
   13739              : 
   13740          303 :           TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
   13741          303 :           TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
   13742          303 :           TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
   13743          303 :           TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
   13744          606 :           TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
   13745          303 :             = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
   13746              : 
   13747          303 :           TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
   13748          303 :           TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
   13749              : 
   13750              :           /* C++ pieces.  */
   13751          303 :           TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
   13752          303 :           CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
   13753              : 
   13754          606 :           TYPE_HAS_USER_CONSTRUCTOR (type)
   13755          303 :             = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
   13756          606 :           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   13757          303 :             = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
   13758          606 :           TYPE_NEEDS_CONSTRUCTING (type)
   13759          303 :             = TYPE_NEEDS_CONSTRUCTING (type_dup);
   13760              : 
   13761          303 :           if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
   13762              :             {
   13763          303 :               if (TYPE_LANG_SPECIFIC (type))
   13764              :                 {
   13765          909 :                   CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
   13766          303 :                     = CLASSTYPE_BEFRIENDING_CLASSES (type);
   13767          606 :                   SET_CLASSTYPE_TYPEINFO_VAR (type_dup,
   13768              :                                               CLASSTYPE_TYPEINFO_VAR (type));
   13769              :                 }
   13770         1255 :               for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
   13771          952 :                 TYPE_LANG_SPECIFIC (v) = ls;
   13772              :             }
   13773              :         }
   13774              : 
   13775        38556 :       TYPE_SIZE (type) = size;
   13776        38556 :       TYPE_SIZE_UNIT (type) = size_unit;
   13777              : 
   13778        38556 :       if (fields)
   13779              :         {
   13780        38556 :           tree *chain = &TYPE_FIELDS (type);
   13781        38556 :           unsigned len = fields->length ();
   13782       504233 :           for (unsigned ix = 0; ix != len; ix++)
   13783              :             {
   13784       465677 :               tree decl = (*fields)[ix];
   13785              : 
   13786       465677 :               if (!decl)
   13787              :                 {
   13788              :                   /* An anonymous struct with typedef name.  */
   13789            3 :                   tree tdef = (*fields)[ix+1];
   13790            3 :                   decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
   13791            3 :                   gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
   13792              :                                        && decl != tdef);
   13793              :                 }
   13794              : 
   13795       841362 :               gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
   13796       465677 :               *chain = decl;
   13797       465677 :               chain = &DECL_CHAIN (decl);
   13798              : 
   13799       465677 :               if (TREE_CODE (decl) == FIELD_DECL
   13800       465677 :                   && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   13801              :                 {
   13802          285 :                   tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
   13803          285 :                   if (DECL_NAME (defn) == as_base_identifier)
   13804              :                     /* ANON_AGGR_TYPE_FIELD should already point to the
   13805              :                        original FIELD_DECL; don't overwrite it to point
   13806              :                        to the as-base FIELD_DECL copy.  */
   13807           26 :                     gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
   13808              :                   else
   13809          272 :                     SET_ANON_AGGR_TYPE_FIELD (anon_type, decl);
   13810              :                 }
   13811              : 
   13812       465677 :               if (TREE_CODE (decl) == USING_DECL
   13813       465677 :                   && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
   13814              :                 {
   13815              :                   /* Reconstruct DECL_ACCESS.  */
   13816        19225 :                   tree decls = USING_DECL_DECLS (decl);
   13817        19225 :                   tree access = declared_access (decl);
   13818              : 
   13819        22734 :                   for (ovl_iterator iter (decls); iter; ++iter)
   13820              :                     {
   13821         2284 :                       tree d = *iter;
   13822              : 
   13823         2284 :                       retrofit_lang_decl (d);
   13824         2284 :                       tree list = DECL_ACCESS (d);
   13825              : 
   13826         2284 :                       if (!purpose_member (type, list))
   13827         2749 :                         DECL_ACCESS (d) = tree_cons (type, access, list);
   13828              :                     }
   13829              :                 }
   13830              : 
   13831       465677 :               if (TREE_CODE (decl) == VAR_DECL
   13832        13499 :                   && TREE_CODE (maybe_template) != TEMPLATE_DECL)
   13833        11604 :                 note_vague_linkage_variable (decl);
   13834              :             }
   13835              :         }
   13836              : 
   13837        38556 :       TYPE_VFIELD (type) = vfield;
   13838        38556 :       TYPE_BINFO (type) = binfo;
   13839              : 
   13840        38556 :       if (TYPE_LANG_SPECIFIC (type))
   13841              :         {
   13842        37701 :           if (!TYPE_POLYMORPHIC_P (type))
   13843        36107 :             SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
   13844              :           else
   13845         1594 :             gcc_checking_assert (lambda == NULL_TREE);
   13846              : 
   13847        37701 :           CLASSTYPE_MEMBER_VEC (type) = member_vec;
   13848        37701 :           CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
   13849        37701 :           CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
   13850              : 
   13851        37701 :           if (TYPE_POLYMORPHIC_P (type))
   13852         1594 :             SET_CLASSTYPE_KEY_METHOD (type, key_method);
   13853              :           else
   13854        36107 :             gcc_checking_assert (key_method == NULL_TREE);
   13855              : 
   13856        37701 :           CLASSTYPE_VBASECLASSES (type) = vbase_vec;
   13857              : 
   13858              :           /* Resort the member vector.  */
   13859        37701 :           resort_type_member_vec (member_vec, NULL, nop, NULL);
   13860              :         }
   13861              :     }
   13862              :   else if (maybe_dup)
   13863              :     {
   13864              :       // FIXME:QOI Check matching defn
   13865              :     }
   13866              : 
   13867        68910 :   if (TYPE_LANG_SPECIFIC (type))
   13868              :     {
   13869        67527 :       tree primary = tree_node ();
   13870        67527 :       tree as_base = tree_node ();
   13871              : 
   13872        67527 :       if (as_base)
   13873        34793 :         as_base = TREE_TYPE (as_base);
   13874              : 
   13875              :       /* Read the vtables.  */
   13876        67527 :       vec<tree, va_heap> *vtables = vec_chained_decls ();
   13877        67527 :       if (vtables)
   13878              :         {
   13879         2654 :           unsigned len = vtables->length ();
   13880         5789 :           for (unsigned ix = 0; ix != len; ix++)
   13881              :             {
   13882         3135 :               tree vtable = (*vtables)[ix];
   13883         3135 :               read_var_def (vtable, vtable);
   13884              :             }
   13885              :         }
   13886              : 
   13887        67527 :       tree friend_classes = tree_list (false);
   13888        67527 :       tree friend_functions = NULL_TREE;
   13889        67527 :       for (tree *chain = &friend_functions;
   13890        80082 :            tree name = tree_node (); chain = &TREE_CHAIN (*chain))
   13891              :         {
   13892        12555 :           tree val = tree_list (false);
   13893        12555 :           *chain = build_tree_list (name, val);
   13894        12555 :         }
   13895        67527 :       tree decl_list = tree_list (true);
   13896              : 
   13897        67527 :       if (installing)
   13898              :         {
   13899        37701 :           CLASSTYPE_PRIMARY_BINFO (type) = primary;
   13900        37701 :           CLASSTYPE_AS_BASE (type) = as_base;
   13901              : 
   13902        37701 :           if (vtables)
   13903              :             {
   13904         1606 :               if ((!CLASSTYPE_KEY_METHOD (type)
   13905              :                    /* Sneaky user may have defined it inline
   13906              :                       out-of-class.  */
   13907         1158 :                    || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
   13908              :                   /* An imported non-template class attached to a module
   13909              :                      doesn't need to have its vtables emitted here.  */
   13910         1762 :                   && (CLASSTYPE_USE_TEMPLATE (type)
   13911          295 :                       || !DECL_MODULE_ATTACH_P (defn)))
   13912         1135 :                 vec_safe_push (keyed_classes, type);
   13913         1606 :               unsigned len = vtables->length ();
   13914         1606 :               tree *chain = &CLASSTYPE_VTABLES (type);
   13915         3510 :               for (unsigned ix = 0; ix != len; ix++)
   13916              :                 {
   13917         1904 :                   tree vtable = (*vtables)[ix];
   13918         1904 :                   gcc_checking_assert (!*chain);
   13919         1904 :                   *chain = vtable;
   13920         1904 :                   chain = &DECL_CHAIN (vtable);
   13921              :                 }
   13922              :             }
   13923        37701 :           CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
   13924        37701 :           DECL_FRIENDLIST (defn) = friend_functions;
   13925        37701 :           CLASSTYPE_DECL_LIST (type) = decl_list;
   13926              : 
   13927        40813 :           for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
   13928              :             {
   13929         3112 :               tree f = TREE_VALUE (friend_classes);
   13930         3112 :               if (TREE_CODE (f) == TEMPLATE_DECL)
   13931         1271 :                 f = TREE_TYPE (f);
   13932              : 
   13933         3112 :               if (CLASS_TYPE_P (f))
   13934              :                 {
   13935         3074 :                   CLASSTYPE_BEFRIENDING_CLASSES (f)
   13936         6148 :                     = tree_cons (NULL_TREE, type,
   13937         3074 :                                  CLASSTYPE_BEFRIENDING_CLASSES (f));
   13938         3118 :                   dump () && dump ("Class %N befriending %C:%N",
   13939            6 :                                    type, TREE_CODE (f), f);
   13940              :                 }
   13941              :             }
   13942              : 
   13943        45233 :           for (; friend_functions;
   13944         7532 :                friend_functions = TREE_CHAIN (friend_functions))
   13945         7532 :             for (tree friend_decls = TREE_VALUE (friend_functions);
   13946        17152 :                  friend_decls; friend_decls = TREE_CHAIN (friend_decls))
   13947              :               {
   13948         9620 :                 tree f = TREE_VALUE (friend_decls);
   13949         9620 :                 if (TREE_CODE (f) == TU_LOCAL_ENTITY)
   13950           36 :                   continue;
   13951              : 
   13952         9584 :                 DECL_BEFRIENDING_CLASSES (f)
   13953         9584 :                   = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
   13954         9650 :                 dump () && dump ("Class %N befriending %C:%N",
   13955           30 :                                  type, TREE_CODE (f), f);
   13956              :               }
   13957              :         }
   13958              : 
   13959        67527 :       if (TYPE_CONTAINS_VPTR_P (type))
   13960              :         /* Read and install the thunks.  */
   13961         3134 :         while (tree vfunc = tree_node ())
   13962              :           {
   13963          438 :             tree thunks = chained_decls ();
   13964          438 :             if (installing)
   13965          270 :               SET_DECL_THUNKS (vfunc, thunks);
   13966              :           }
   13967              : 
   13968        67527 :       vec_free (vtables);
   13969              :     }
   13970              : 
   13971              :   /* Propagate to all variants.  */
   13972        68910 :   if (installing)
   13973        38556 :     fixup_type_variants (type);
   13974              : 
   13975              :   /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
   13976              :      the fake base, we've not hooked it into the containing class's
   13977              :      data structure yet.  Fortunately it has a unique name.  */
   13978        38556 :   if (installing
   13979        38556 :       && DECL_NAME (defn) != as_base_identifier
   13980        37701 :       && (!CLASSTYPE_TEMPLATE_INFO (type)
   13981        31812 :           || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
   13982              :     /* Emit debug info.  It'd be nice to know if the interface TU
   13983              :        already emitted this.  */
   13984        20897 :     rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
   13985              : 
   13986        68910 :   vec_free (fields);
   13987              : 
   13988        68910 :   return !get_overrun ();
   13989              : }
   13990              : 
   13991              : void
   13992         9690 : trees_out::write_enum_def (tree decl)
   13993              : {
   13994         9690 :   tree type = TREE_TYPE (decl);
   13995              : 
   13996         9690 :   tree_node (TYPE_VALUES (type));
   13997              :   /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   13998              :      ENUMERAL_TYPE.  */
   13999         9690 : }
   14000              : 
   14001              : void
   14002         9690 : trees_out::mark_enum_def (tree decl)
   14003              : {
   14004         9690 :   tree type = TREE_TYPE (decl);
   14005              : 
   14006        50936 :   for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
   14007              :     {
   14008        41246 :       tree cst = TREE_VALUE (values);
   14009        41246 :       mark_by_value (cst);
   14010              :       /* We must mark the init to avoid circularity in tt_enum_int.  */
   14011        41246 :       if (tree init = DECL_INITIAL (cst))
   14012        40774 :         if (TREE_CODE (init) == INTEGER_CST)
   14013        40026 :           mark_by_value (init);
   14014              :     }
   14015         9690 : }
   14016              : 
   14017              : bool
   14018         3180 : trees_in::read_enum_def (tree defn, tree maybe_template)
   14019              : {
   14020         3180 :   tree type = TREE_TYPE (defn);
   14021         3180 :   tree values = tree_node ();
   14022              : 
   14023         3180 :   if (get_overrun ())
   14024              :     return false;
   14025              : 
   14026         3180 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
   14027         6360 :   bool installing = maybe_dup && !TYPE_VALUES (type);
   14028              : 
   14029         3180 :   if (installing)
   14030              :     {
   14031         1636 :       TYPE_VALUES (type) = values;
   14032              :       /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   14033              :          ENUMERAL_TYPE.  */
   14034              : 
   14035         2651 :       rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
   14036              :     }
   14037         1544 :   else if (maybe_dup)
   14038              :     {
   14039         1544 :       tree known = TYPE_VALUES (type);
   14040         8519 :       for (; known && values;
   14041         6975 :            known = TREE_CHAIN (known), values = TREE_CHAIN (values))
   14042              :         {
   14043         6984 :           tree known_decl = TREE_VALUE (known);
   14044         6984 :           tree new_decl = TREE_VALUE (values);
   14045              : 
   14046         6984 :           if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
   14047              :             break;
   14048              : 
   14049         6978 :           new_decl = maybe_duplicate (new_decl);
   14050              : 
   14051         6978 :           if (!cp_tree_equal (DECL_INITIAL (known_decl),
   14052         6978 :                               DECL_INITIAL (new_decl)))
   14053              :             break;
   14054              :         }
   14055              : 
   14056         1544 :       if (known || values)
   14057              :         {
   14058           12 :           auto_diagnostic_group d;
   14059           12 :           error_at (DECL_SOURCE_LOCATION (maybe_dup),
   14060              :                     "definition of %qD does not match", maybe_dup);
   14061           12 :           inform (DECL_SOURCE_LOCATION (defn),
   14062              :                   "existing definition %qD", defn);
   14063              : 
   14064           12 :           tree known_decl = NULL_TREE, new_decl = NULL_TREE;
   14065              : 
   14066           12 :           if (known)
   14067            9 :             known_decl = TREE_VALUE (known);
   14068           12 :           if (values)
   14069           12 :             new_decl = maybe_duplicate (TREE_VALUE (values));
   14070              : 
   14071           12 :           if (known_decl && new_decl)
   14072              :             {
   14073            9 :               inform (DECL_SOURCE_LOCATION (new_decl),
   14074              :                       "enumerator %qD does not match ...", new_decl);
   14075            9 :               inform (DECL_SOURCE_LOCATION (known_decl),
   14076              :                       "... this enumerator %qD", known_decl);
   14077              :             }
   14078            3 :           else if (known_decl || new_decl)
   14079              :             {
   14080            3 :               tree extra = known_decl ? known_decl : new_decl;
   14081            3 :               inform (DECL_SOURCE_LOCATION (extra),
   14082              :                       "additional enumerators beginning with %qD", extra);
   14083              :             }
   14084              :           else
   14085            0 :             inform (DECL_SOURCE_LOCATION (maybe_dup),
   14086              :                     "enumeration range differs");
   14087              : 
   14088              :           /* Mark it bad.  */
   14089           12 :           unmatched_duplicate (maybe_template);
   14090           12 :         }
   14091              :     }
   14092              : 
   14093              :   return true;
   14094              : }
   14095              : 
   14096              : /* Write out the body of DECL.  See above circularity note.  */
   14097              : 
   14098              : void
   14099       985819 : trees_out::write_definition (tree decl, bool refs_tu_local)
   14100              : {
   14101       985819 :   auto ovr = make_temp_override (writing_local_entities,
   14102       985819 :                                  writing_local_entities || refs_tu_local);
   14103              : 
   14104       985819 :   if (streaming_p ())
   14105              :     {
   14106       492819 :       assert_definition (decl);
   14107       492819 :       dump ()
   14108          952 :         && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
   14109              :     }
   14110              :   else
   14111       493000 :     dump (dumper::DEPEND)
   14112           96 :       && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
   14113              : 
   14114       985819 :  again:
   14115      1543882 :   switch (TREE_CODE (decl))
   14116              :     {
   14117            0 :     default:
   14118            0 :       gcc_unreachable ();
   14119              : 
   14120       558063 :     case TEMPLATE_DECL:
   14121       558063 :       decl = DECL_TEMPLATE_RESULT (decl);
   14122       558063 :       goto again;
   14123              : 
   14124       618497 :     case FUNCTION_DECL:
   14125       618497 :       write_function_def (decl);
   14126       618497 :       break;
   14127              : 
   14128       221442 :     case TYPE_DECL:
   14129       221442 :       {
   14130       221442 :         tree type = TREE_TYPE (decl);
   14131       221442 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14132              :                     && TYPE_NAME (type) == decl);
   14133       221442 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14134         9690 :           write_enum_def (decl);
   14135              :         else
   14136       211752 :           write_class_def (decl);
   14137              :       }
   14138              :       break;
   14139              : 
   14140       145880 :     case VAR_DECL:
   14141       145880 :     case CONCEPT_DECL:
   14142       145880 :       write_var_def (decl);
   14143       145880 :       break;
   14144              :     }
   14145       985819 : }
   14146              : 
   14147              : /* Mark a declaration for by-value walking.  If DO_DEFN is true, mark
   14148              :    its body too.  */
   14149              : 
   14150              : void
   14151      4143219 : trees_out::mark_declaration (tree decl, bool do_defn)
   14152              : {
   14153      4143219 :   mark_by_value (decl);
   14154              : 
   14155      4143219 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14156      1375551 :     decl = DECL_TEMPLATE_RESULT (decl);
   14157              : 
   14158      4143219 :   if (!do_defn)
   14159              :     return;
   14160              : 
   14161       985841 :   switch (TREE_CODE (decl))
   14162              :     {
   14163            0 :     default:
   14164            0 :       gcc_unreachable ();
   14165              : 
   14166              :     case FUNCTION_DECL:
   14167              :       mark_function_def (decl);
   14168              :       break;
   14169              : 
   14170       221470 :     case TYPE_DECL:
   14171       221470 :       {
   14172       221470 :         tree type = TREE_TYPE (decl);
   14173       221470 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14174              :                     && TYPE_NAME (type) == decl);
   14175       221470 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14176         9690 :           mark_enum_def (decl);
   14177              :         else
   14178       211780 :           mark_class_def (decl);
   14179              :       }
   14180              :       break;
   14181              : 
   14182              :     case VAR_DECL:
   14183              :     case CONCEPT_DECL:
   14184              :       mark_var_def (decl);
   14185              :       break;
   14186              :     }
   14187              : }
   14188              : 
   14189              : /* Read in the body of DECL.  See above circularity note.  */
   14190              : 
   14191              : bool
   14192       346528 : trees_in::read_definition (tree decl)
   14193              : {
   14194       347868 :   dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
   14195              : 
   14196       346528 :   tree maybe_template = decl;
   14197              : 
   14198       553565 :  again:
   14199       553565 :   switch (TREE_CODE (decl))
   14200              :     {
   14201              :     default:
   14202              :       break;
   14203              : 
   14204       207037 :     case TEMPLATE_DECL:
   14205       207037 :       decl = DECL_TEMPLATE_RESULT (decl);
   14206       207037 :       goto again;
   14207              : 
   14208       231842 :     case FUNCTION_DECL:
   14209       231842 :       return read_function_def (decl, maybe_template);
   14210              : 
   14211        72090 :     case TYPE_DECL:
   14212        72090 :       {
   14213        72090 :         tree type = TREE_TYPE (decl);
   14214        72090 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14215              :                     && TYPE_NAME (type) == decl);
   14216        72090 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14217         3180 :           return read_enum_def (decl, maybe_template);
   14218              :         else
   14219        68910 :           return read_class_def (decl, maybe_template);
   14220              :       }
   14221        42596 :       break;
   14222              : 
   14223        42596 :     case VAR_DECL:
   14224        42596 :     case CONCEPT_DECL:
   14225        42596 :       return read_var_def (decl, maybe_template);
   14226              :     }
   14227              : 
   14228              :   return false;
   14229              : }
   14230              : 
   14231              : /* Lookup an maybe insert a slot for depset for KEY.  */
   14232              : 
   14233              : depset **
   14234     20745973 : depset::hash::entity_slot (tree entity, bool insert)
   14235              : {
   14236     20745973 :   traits::compare_type key (entity, NULL);
   14237     31307181 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14238              :                                        insert ? INSERT : NO_INSERT);
   14239              : 
   14240     20745973 :   return slot;
   14241              : }
   14242              : 
   14243              : depset **
   14244       236755 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
   14245              : {
   14246       236755 :   traits::compare_type key (ctx, name);
   14247       310991 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14248              :                                        insert ? INSERT : NO_INSERT);
   14249              : 
   14250       236755 :   return slot;
   14251              : }
   14252              : 
   14253              : depset *
   14254     10074548 : depset::hash::find_dependency (tree decl)
   14255              : {
   14256     10074548 :   depset **slot = entity_slot (decl, false);
   14257              : 
   14258     10074548 :   return slot ? *slot : NULL;
   14259              : }
   14260              : 
   14261              : depset *
   14262        74236 : depset::hash::find_binding (tree ctx, tree name)
   14263              : {
   14264        74236 :   depset **slot = binding_slot (ctx, name, false);
   14265              : 
   14266        74236 :   return slot ? *slot : NULL;
   14267              : }
   14268              : 
   14269              : static bool is_tu_local_entity (tree decl, bool explain = false);
   14270              : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
   14271              : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
   14272              : 
   14273              : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
   14274              :    If EXPLAIN is true, emit an informative note about why DECL is TU-local.  */
   14275              : 
   14276              : static bool
   14277      5045874 : is_tu_local_entity (tree decl, bool explain/*=false*/)
   14278              : {
   14279      5238140 :   gcc_checking_assert (DECL_P (decl));
   14280      5238140 :   location_t loc = DECL_SOURCE_LOCATION (decl);
   14281      5238140 :   tree type = TREE_TYPE (decl);
   14282              : 
   14283              :   /* Only types, functions, variables, and template (specialisations)
   14284              :      can be TU-local.  */
   14285      5238140 :   if (TREE_CODE (decl) != TYPE_DECL
   14286              :       && TREE_CODE (decl) != FUNCTION_DECL
   14287              :       && TREE_CODE (decl) != VAR_DECL
   14288              :       && TREE_CODE (decl) != TEMPLATE_DECL)
   14289              :     return false;
   14290              : 
   14291              :   /* An explicit type alias is not an entity; we don't want to stream
   14292              :      such aliases if they refer to TU-local entities, so propagate this
   14293              :      from the original type. The built-in declarations of 'int' and such
   14294              :      are never TU-local.  */
   14295      5234239 :   if (TREE_CODE (decl) == TYPE_DECL
   14296      1771854 :       && !DECL_SELF_REFERENCE_P (decl)
   14297      6933505 :       && !DECL_IMPLICIT_TYPEDEF_P (decl))
   14298              :     {
   14299       907448 :       tree orig = DECL_ORIGINAL_TYPE (decl);
   14300       907448 :       if (orig && TYPE_NAME (orig))
   14301              :         {
   14302       192266 :           if (explain)
   14303           11 :             inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
   14304       192266 :           return is_tu_local_entity (TYPE_NAME (orig), explain);
   14305              :         }
   14306              :       else
   14307              :         return false;
   14308              :     }
   14309              : 
   14310              :   /* Check specializations first for slightly better explanations.  */
   14311      4326791 :   int use_tpl = -1;
   14312      4326791 :   tree ti = node_template_info (decl, use_tpl);
   14313      5303946 :   if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
   14314              :     {
   14315              :       /* A specialization of a TU-local template.  */
   14316       976980 :       tree tmpl = TI_TEMPLATE (ti);
   14317       976980 :       if (is_tu_local_entity (tmpl))
   14318              :         {
   14319           72 :           if (explain)
   14320              :             {
   14321           18 :               inform (loc, "%qD is a specialization of TU-local template %qD",
   14322              :                       decl, tmpl);
   14323           18 :               is_tu_local_entity (tmpl, /*explain=*/true);
   14324              :             }
   14325              :           return true;
   14326              :         }
   14327              : 
   14328              :       /* A specialization of a template with any TU-local template argument.  */
   14329       976908 :       if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
   14330              :         return true;
   14331              : 
   14332              :       /* FIXME A specialization of a template whose (possibly instantiated)
   14333              :          declaration is an exposure.  This should always be covered by the
   14334              :          above cases??  */
   14335              :     }
   14336              : 
   14337              :   /* A type, function, variable, or template with internal linkage.  */
   14338      4326687 :   linkage_kind kind = decl_linkage (decl);
   14339      4326687 :   if (kind == lk_internal
   14340              :       /* But although weakrefs are marked static, don't consider them
   14341              :          to be TU-local.  */
   14342      4326687 :       && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
   14343              :     {
   14344          851 :       if (explain)
   14345          168 :         inform (loc, "%qD declared with internal linkage", decl);
   14346              :       return true;
   14347              :     }
   14348              : 
   14349              :   /* Does not have a name with linkage and is declared, or introduced by a
   14350              :      lambda-expression, within the definition of a TU-local entity.  */
   14351      4325836 :   if (kind == lk_none)
   14352              :     {
   14353       445375 :       tree ctx = CP_DECL_CONTEXT (decl);
   14354       568159 :       if (LAMBDA_TYPE_P (type))
   14355        86742 :         if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
   14356       445375 :           ctx = extra;
   14357              : 
   14358       445375 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   14359              :         {
   14360           31 :           if (!TREE_PUBLIC (ctx))
   14361              :             {
   14362            0 :               if (explain)
   14363            0 :                 inform (loc, "%qD has no linkage and is declared in an "
   14364              :                         "anonymous namespace", decl);
   14365              :               return true;
   14366              :             }
   14367              :         }
   14368       445344 :       else if (TYPE_P (ctx))
   14369              :         {
   14370        50209 :           tree ctx_decl = TYPE_MAIN_DECL (ctx);
   14371        50209 :           if (is_tu_local_entity (ctx_decl))
   14372              :             {
   14373            6 :               if (explain)
   14374              :                 {
   14375            0 :                   inform (loc, "%qD has no linkage and is declared within "
   14376              :                           "TU-local entity %qT", decl, ctx);
   14377            0 :                   is_tu_local_entity (ctx_decl, /*explain=*/true);
   14378              :                 }
   14379              :               return true;
   14380              :             }
   14381              :         }
   14382       395135 :       else if (is_tu_local_entity (ctx))
   14383              :         {
   14384           33 :           if (explain)
   14385              :             {
   14386            6 :               inform (loc, "%qD has no linkage and is declared within "
   14387              :                       "TU-local entity %qD", decl, ctx);
   14388            6 :               is_tu_local_entity (ctx, /*explain=*/true);
   14389              :             }
   14390              :           return true;
   14391              :         }
   14392              :     }
   14393              : 
   14394              :   /* A type with no name that is defined outside a class-specifier, function
   14395              :      body, or initializer; or is introduced by a defining-type-specifier that
   14396              :      is used to declare only TU-local entities.
   14397              : 
   14398              :      We consider types with names for linkage purposes as having names, since
   14399              :      these aren't really TU-local.  */
   14400      4325797 :   tree inner = STRIP_TEMPLATE (decl);
   14401      1832487 :   if (inner
   14402      4325797 :       && TREE_CODE (inner) == TYPE_DECL
   14403      3284082 :       && TYPE_ANON_P (type)
   14404        47110 :       && !DECL_SELF_REFERENCE_P (inner)
   14405              :       /* An enum with an enumerator name for linkage.  */
   14406      1874968 :       && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
   14407              :     {
   14408        40266 :       tree main_decl = TYPE_MAIN_DECL (type);
   14409        79983 :       if (LAMBDA_TYPE_P (type))
   14410              :         {
   14411              :           /* A lambda expression is, in practice, TU-local iff it has no
   14412              :              mangling scope.  This currently doesn't line up exactly with
   14413              :              the standard's definition due to some ABI issues, but it's
   14414              :              pretty close, and avoids other issues down the line.  */
   14415        79296 :           if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
   14416              :             {
   14417            4 :               if (explain)
   14418            2 :                 inform (loc, "%qT has no name and cannot be differentiated "
   14419              :                         "from similar lambdas in other TUs", type);
   14420              :               return true;
   14421              :             }
   14422              :         }
   14423         1236 :       else if (!DECL_CLASS_SCOPE_P (main_decl)
   14424          648 :                && !decl_function_context (main_decl))
   14425              :         {
   14426           30 :           if (explain)
   14427           12 :             inform (loc, "%qT has no name and is not defined within a class, "
   14428              :                     "function, or initializer", type);
   14429              :           return true;
   14430              :         }
   14431              : 
   14432              :       // FIXME introduced by a defining-type-specifier only declaring TU-local
   14433              :       // entities; does this refer to e.g. 'static struct {} a;"?  I can't
   14434              :       // think of any cases where this isn't covered by earlier cases.  */
   14435              :     }
   14436              : 
   14437              :   return false;
   14438              : }
   14439              : 
   14440              : /* Helper for is_tu_local_entity.  Returns true if one of the ARGS of
   14441              :    DECL is TU-local.  Emits an explanation if EXPLAIN is true.  */
   14442              : 
   14443              : static bool
   14444      1112765 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
   14445              : {
   14446      1112765 :   if (!args || TREE_CODE (args) != TREE_VEC)
   14447              :     return false;
   14448              : 
   14449      3009386 :   for (tree a : tree_vec_range (args))
   14450              :     {
   14451      1896653 :       if (TREE_CODE (a) == TREE_VEC)
   14452              :         {
   14453       135857 :           if (has_tu_local_tmpl_arg (decl, a, explain))
   14454           32 :             return true;
   14455              :         }
   14456              :       else if (!WILDCARD_TYPE_P (a))
   14457              :         {
   14458      1600385 :           if (DECL_P (a) && is_tu_local_entity (a))
   14459              :             {
   14460            0 :               if (explain)
   14461              :                 {
   14462            0 :                   inform (DECL_SOURCE_LOCATION (decl),
   14463              :                           "%qD has TU-local template argument %qD",
   14464              :                           decl, a);
   14465            0 :                   is_tu_local_entity (a, /*explain=*/true);
   14466              :                 }
   14467              :               return true;
   14468              :             }
   14469              : 
   14470      1600385 :           if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
   14471              :             {
   14472           17 :               if (explain)
   14473              :                 {
   14474            1 :                   inform (DECL_SOURCE_LOCATION (decl),
   14475              :                           "%qD has TU-local template argument %qT",
   14476              :                           decl, a);
   14477            1 :                   is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
   14478              :                 }
   14479              :               return true;
   14480              :             }
   14481              : 
   14482      1600368 :           if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
   14483              :             return true;
   14484              :         }
   14485              :     }
   14486              : 
   14487      1112733 :   return false;
   14488              : }
   14489              : 
   14490              : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
   14491              :    or object.  Emits an explanation if EXPLAIN is true.  */
   14492              : 
   14493              : static bool
   14494       117185 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
   14495              : {
   14496       117185 :   if (!expr)
   14497              :     return false;
   14498              : 
   14499       115131 :   tree e = expr;
   14500       115131 :   STRIP_ANY_LOCATION_WRAPPER (e);
   14501       115131 :   STRIP_NOPS (e);
   14502       115131 :   if (TREE_CODE (e) == TARGET_EXPR)
   14503            0 :     e = TARGET_EXPR_INITIAL (e);
   14504            0 :   if (!e)
   14505              :     return false;
   14506              : 
   14507              :   /* It is, or is a pointer to, a TU-local function or the object associated
   14508              :      with a TU-local variable.  */
   14509       115131 :   tree object = NULL_TREE;
   14510       115131 :   if (TREE_CODE (e) == ADDR_EXPR)
   14511         3691 :     object = TREE_OPERAND (e, 0);
   14512       111440 :   else if (TREE_CODE (e) == PTRMEM_CST)
   14513            0 :     object = PTRMEM_CST_MEMBER (e);
   14514       111440 :   else if (VAR_OR_FUNCTION_DECL_P (e))
   14515              :     object = e;
   14516              : 
   14517         3691 :   if (object
   14518         4494 :       && VAR_OR_FUNCTION_DECL_P (object)
   14519         4581 :       && is_tu_local_entity (object))
   14520              :     {
   14521           54 :       if (explain)
   14522              :         {
   14523              :           /* We've lost a lot of location information by the time we get here,
   14524              :              so let's just do our best effort.  */
   14525           18 :           auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
   14526           18 :           if (VAR_P (object))
   14527            9 :             inform (loc, "%qD refers to TU-local object %qD", decl, object);
   14528              :           else
   14529            9 :             inform (loc, "%qD refers to TU-local function %qD", decl, object);
   14530           18 :           is_tu_local_entity (object, true);
   14531              :         }
   14532              :       return true;
   14533              :     }
   14534              : 
   14535              :   /* It is an object of class or array type and any of its subobjects or
   14536              :      any of the objects or functions to which its non-static data members
   14537              :      of reference type refer is TU-local and is usable in constant
   14538              :      expressions.  */
   14539       115077 :   if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
   14540        60680 :     for (auto &f : CONSTRUCTOR_ELTS (e))
   14541        47064 :       if (is_tu_local_value (decl, f.value, explain))
   14542              :         return true;
   14543              : 
   14544              :   return false;
   14545              : }
   14546              : 
   14547              : /* Complains if DECL is a TU-local entity imported from a named module.
   14548              :    Returns TRUE if instantiation should fail.  */
   14549              : 
   14550              : bool
   14551   9295131416 : instantiating_tu_local_entity (tree decl)
   14552              : {
   14553   9295131416 :   if (!modules_p ())
   14554              :     return false;
   14555              : 
   14556     36303016 :   if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
   14557              :     {
   14558           92 :       auto_diagnostic_group d;
   14559           92 :       error ("instantiation exposes TU-local entity %qD",
   14560           92 :              TU_LOCAL_ENTITY_NAME (decl));
   14561           92 :       inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
   14562           92 :       return true;
   14563           92 :     }
   14564              : 
   14565              :   /* Currently, only TU-local variables and functions, or possibly
   14566              :      templates thereof, will be emitted from named modules.  */
   14567     36302924 :   tree inner = STRIP_TEMPLATE (decl);
   14568     36302924 :   if (!VAR_OR_FUNCTION_DECL_P (inner))
   14569              :     return false;
   14570              : 
   14571              :   /* From this point we will only be emitting warnings; if we're not
   14572              :      warning about this case then there's no need to check further.  */
   14573      1534456 :   if (!warn_expose_global_module_tu_local
   14574      3068912 :       || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   14575      1534456 :                               OPT_Wexpose_global_module_tu_local))
   14576              :     return false;
   14577              : 
   14578      1522900 :   if (!is_tu_local_entity (decl))
   14579              :     return false;
   14580              : 
   14581           65 :   if (!DECL_LANG_SPECIFIC (inner)
   14582          120 :       || !DECL_MODULE_IMPORT_P (inner))
   14583              :     return false;
   14584              : 
   14585              :   /* Referencing TU-local entities from a header is generally OK.
   14586              :      We don't have an easy way to detect if this declaration came
   14587              :      from a header via a separate named module, but we can just
   14588              :      ignore that case for warning purposes.  */
   14589            9 :   unsigned index = import_entity_index (decl);
   14590            9 :   module_state *mod = import_entity_module (index);
   14591            9 :   if (mod->is_header ())
   14592              :     return false;
   14593              : 
   14594            9 :   auto_diagnostic_group d;
   14595            9 :   pedwarn (input_location, OPT_Wexpose_global_module_tu_local,
   14596              :            "instantiation exposes TU-local entity %qD", decl);
   14597            9 :   inform (DECL_SOURCE_LOCATION (decl), "declared here");
   14598              : 
   14599              :   /* We treat TU-local entities from the GMF as not actually being
   14600              :      TU-local as an extension, so allow instantiation to proceed.  */
   14601            9 :   return false;
   14602            9 : }
   14603              : 
   14604              : /* DECL is a newly discovered dependency.  Create the depset, if it
   14605              :    doesn't already exist.  Add it to the worklist if so.
   14606              : 
   14607              :    DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
   14608              :    a using decl.
   14609              : 
   14610              :    We do not have to worry about adding the same dependency more than
   14611              :    once.  First it's harmless, but secondly the TREE_VISITED marking
   14612              :    prevents us wanting to do it anyway.  */
   14613              : 
   14614              : depset *
   14615      8918587 : depset::hash::make_dependency (tree decl, entity_kind ek)
   14616              : {
   14617              :   /* Make sure we're being told consistent information.  */
   14618     16695994 :   gcc_checking_assert ((ek == EK_NAMESPACE)
   14619              :                        == (TREE_CODE (decl) == NAMESPACE_DECL
   14620              :                            && !DECL_NAMESPACE_ALIAS (decl)));
   14621      8918587 :   gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
   14622      8918587 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   14623              :                        && (TREE_CODE (decl) != USING_DECL
   14624              :                            || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
   14625      8918587 :   gcc_checking_assert (!is_key_order ());
   14626      8918587 :   if (ek == EK_USING)
   14627        42847 :     gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
   14628      8918587 :   if (ek == EK_TU_LOCAL)
   14629           93 :     gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
   14630              : 
   14631      8918587 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14632              :     /* The template should have copied these from its result decl.  */
   14633      3440783 :     gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
   14634              :                          == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
   14635              : 
   14636      8918587 :   depset **slot = entity_slot (decl, true);
   14637      8918587 :   depset *dep = *slot;
   14638      8918587 :   bool for_binding = ek == EK_FOR_BINDING;
   14639              : 
   14640      8918587 :   if (!dep)
   14641              :     {
   14642       767962 :       if ((DECL_IMPLICIT_TYPEDEF_P (decl)
   14643              :            /* ... not an enum, for instance.  */
   14644       379425 :            && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   14645       374259 :            && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
   14646       342602 :            && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   14647      2635762 :           || (VAR_P (decl)
   14648       102266 :               && DECL_LANG_SPECIFIC (decl)
   14649       102206 :               && DECL_USE_TEMPLATE (decl) == 2))
   14650              :         {
   14651              :           /* A partial or explicit specialization. Partial
   14652              :              specializations might not be in the hash table, because
   14653              :              there can be multiple differently-constrained variants.
   14654              : 
   14655              :              template<typename T> class silly;
   14656              :              template<typename T> requires true class silly {};
   14657              : 
   14658              :              We need to find them, insert their TEMPLATE_DECL in the
   14659              :              dep_hash, and then convert the dep we just found into a
   14660              :              redirect.  */
   14661              : 
   14662        49341 :           tree ti = get_template_info (decl);
   14663        49341 :           tree tmpl = TI_TEMPLATE (ti);
   14664        49341 :           tree partial = NULL_TREE;
   14665        49341 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   14666       178153 :                spec; spec = TREE_CHAIN (spec))
   14667       155250 :             if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
   14668              :               {
   14669              :                 partial = TREE_VALUE (spec);
   14670              :                 break;
   14671              :               }
   14672              : 
   14673        49341 :           if (partial)
   14674              :             {
   14675              :               /* Eagerly create an empty redirect.  The following
   14676              :                  make_dependency call could cause hash reallocation,
   14677              :                  and invalidate slot's value.  */
   14678        26438 :               depset *redirect = make_entity (decl, EK_REDIRECT);
   14679              : 
   14680              :               /* Redirects are never reached -- always snap to their target.  */
   14681        26438 :               redirect->set_flag_bit<DB_UNREACHED_BIT> ();
   14682              : 
   14683        26438 :               *slot = redirect;
   14684              : 
   14685        26438 :               depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
   14686        26438 :               gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
   14687              : 
   14688        26438 :               redirect->deps.safe_push (tmpl_dep);
   14689              : 
   14690        26438 :               return redirect;
   14691              :             }
   14692              :         }
   14693              : 
   14694      1881987 :       bool has_def = ek != EK_USING && has_definition (decl);
   14695      1839140 :       if (ek > EK_BINDING)
   14696       156014 :         ek = EK_DECL;
   14697              : 
   14698              :       /* The only OVERLOADS we should see are USING decls from
   14699              :          bindings.  */
   14700      1881987 :       *slot = dep = make_entity (decl, ek, has_def);
   14701              : 
   14702      1881987 :       if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
   14703              :         /* The template_result should otherwise not be in the
   14704              :            table, or be an empty redirect (created above).  */
   14705       486660 :         if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
   14706        26438 :           gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
   14707              :                                && !(*eslot)->deps.length ());
   14708              : 
   14709      1881987 :       if (ignore_exposure)
   14710        50515 :         dep->set_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14711              : 
   14712      1881987 :       if (ek != EK_USING)
   14713              :         {
   14714      1839140 :           tree not_tmpl = STRIP_TEMPLATE (decl);
   14715      1839140 :           bool imported_from_module_p = false;
   14716              : 
   14717      1839140 :           if (DECL_LANG_SPECIFIC (not_tmpl)
   14718      3505361 :               && DECL_MODULE_IMPORT_P (not_tmpl))
   14719              :             {
   14720              :               /* Store the module number and index in cluster/section,
   14721              :                  so we don't have to look them up again.  */
   14722        89779 :               unsigned index = import_entity_index (decl);
   14723        89779 :               module_state *from = import_entity_module (index);
   14724              :               /* Remap will be zero for imports from partitions, which
   14725              :                  we want to treat as-if declared in this TU.  */
   14726        89779 :               if (from->remap)
   14727              :                 {
   14728        89022 :                   dep->cluster = index - from->entity_lwm;
   14729        89022 :                   dep->section = from->remap;
   14730        89022 :                   dep->set_flag_bit<DB_IMPORTED_BIT> ();
   14731              : 
   14732        89022 :                   if (!from->is_header ())
   14733      1839140 :                     imported_from_module_p = true;
   14734              :                 }
   14735              :             }
   14736              : 
   14737              :           /* Check for TU-local entities.  This is unnecessary in header
   14738              :              units because we can export internal-linkage decls, and
   14739              :              no declarations are exposures.  Similarly, if the decl was
   14740              :              imported from a non-header module we know it cannot have
   14741              :              been TU-local.  */
   14742      1839140 :           if (!header_module_p () && !imported_from_module_p)
   14743              :             {
   14744       905151 :               if (is_tu_local_entity (decl))
   14745          307 :                 dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
   14746              : 
   14747       905151 :               if (VAR_P (decl)
   14748        47487 :                   && decl_maybe_constant_var_p (decl)
   14749       951302 :                   && is_tu_local_value (decl, DECL_INITIAL (decl)))
   14750              :                 {
   14751              :                   /* A potentially-constant variable initialized to a TU-local
   14752              :                      value is not usable in constant expressions within other
   14753              :                      translation units.  We can achieve this by simply not
   14754              :                      streaming the definition in such cases.  */
   14755           24 :                   dep->clear_flag_bit<DB_DEFN_BIT> ();
   14756              : 
   14757           24 :                   if (DECL_DECLARED_CONSTEXPR_P (decl)
   14758           39 :                       || DECL_INLINE_VAR_P (decl))
   14759              :                     /* A constexpr variable initialized to a TU-local value,
   14760              :                        or an inline value (PR c++/119996), is an exposure.
   14761              : 
   14762              :                        For simplicity, we don't support "non-strict" TU-local
   14763              :                        values: even if the TU-local entity we refer to in the
   14764              :                        initialiser is in the GMF, we still won't consider this
   14765              :                        valid in constant expressions in other TUs, and so
   14766              :                        complain accordingly.  */
   14767           15 :                     dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14768              :                 }
   14769              :             }
   14770              : 
   14771              :           /* A namespace-scope type may be declared in one module unit
   14772              :              and defined in another; make sure that we're found when
   14773              :              completing the class.  */
   14774      1839140 :           if (ek == EK_DECL
   14775       721469 :               && !dep->is_import ()
   14776       712910 :               && dep->has_defn ()
   14777       378669 :               && DECL_NAMESPACE_SCOPE_P (not_tmpl)
   14778       137213 :               && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
   14779              :               /* Anonymous types can't be forward-declared.  */
   14780      1875110 :               && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
   14781        35457 :             dep->set_flag_bit<DB_IS_PENDING_BIT> ();
   14782              : 
   14783              :           /* Namespace-scope functions can be found by ADL by template
   14784              :              instantiations in this module.  We need to create bindings
   14785              :              for them so that name lookup recognises they exist, if they
   14786              :              won't be discarded.  add_binding_entity is too early to do
   14787              :              this for GM functions, because if nobody ends up using them
   14788              :              we'll have leftover bindings laying around, and it's tricky
   14789              :              to delete them and any namespaces they've implicitly created
   14790              :              deps on.  The downside is this means we don't pick up on
   14791              :              using-decls, but by [module.global.frag] p3.6 we don't have
   14792              :              to.  */
   14793      1839140 :           if (ek == EK_DECL
   14794      1839140 :               && !for_binding
   14795       565467 :               && !dep->is_import ()
   14796       556914 :               && !dep->is_tu_local ()
   14797       556810 :               && DECL_NAMESPACE_SCOPE_P (decl)
   14798        69078 :               && DECL_DECLARES_FUNCTION_P (decl)
   14799              :               /* Compiler-generated functions won't participate in ADL.  */
   14800        53602 :               && !DECL_ARTIFICIAL (decl)
   14801              :               /* A hidden friend doesn't need a binding.  */
   14802      1884777 :               && !(DECL_LANG_SPECIFIC (not_tmpl)
   14803        45637 :                    && DECL_UNIQUE_FRIEND_P (not_tmpl)))
   14804              :             {
   14805              :               /* This will only affect GM functions.  */
   14806        57538 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
   14807              :                                    || !DECL_MODULE_PURVIEW_P (not_tmpl));
   14808              :               /* We shouldn't see any instantiations or specialisations.  */
   14809        28769 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   14810              :                                    || !DECL_USE_TEMPLATE (decl));
   14811              : 
   14812        28769 :               tree ns = CP_DECL_CONTEXT (decl);
   14813        28769 :               tree name = DECL_NAME (decl);
   14814        28769 :               depset *binding = find_binding (ns, name);
   14815        28769 :               if (!binding)
   14816              :                 {
   14817         8985 :                   binding = make_binding (ns, name);
   14818         8985 :                   add_namespace_context (binding, ns);
   14819              : 
   14820         8985 :                   depset **slot = binding_slot (ns, name, /*insert=*/true);
   14821         8985 :                   *slot = binding;
   14822              :                 }
   14823              : 
   14824        28769 :               binding->deps.safe_push (dep);
   14825        28769 :               dep->deps.safe_push (binding);
   14826        28804 :               dump (dumper::DEPEND)
   14827            9 :                 && dump ("Built ADL binding for %C:%N",
   14828            9 :                          TREE_CODE (decl), decl);
   14829              :             }
   14830              :         }
   14831              : 
   14832      1881987 :       if (!dep->is_import ())
   14833      1792965 :         worklist.safe_push (dep);
   14834              :     }
   14835      7010162 :   else if (!ignore_exposure)
   14836      6202268 :     dep->clear_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14837              : 
   14838      8892149 :   dump (dumper::DEPEND)
   14839        37527 :     && dump ("%s on %s %C:%N found",
   14840              :              ek == EK_REDIRECT ? "Redirect"
   14841        37527 :              : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
   14842              :              : "Dependency",
   14843        37527 :              dep->entity_kind_name (), TREE_CODE (decl), decl);
   14844              : 
   14845      8892149 :   return dep;
   14846              : }
   14847              : 
   14848              : /* Whether REF is an exposure of a member type of SOURCE.
   14849              : 
   14850              :    This comes up with exposures of class-scope lambdas, that we currently
   14851              :    treat as TU-local due to ABI reasons.  In such a case the type of the
   14852              :    lambda will be exposed in two places, first by the class type it is in
   14853              :    the TYPE_FIELDS list of, and second by the actual member declaring that
   14854              :    lambda.  We only want the second case to warn.  */
   14855              : 
   14856              : static bool
   14857          253 : is_exposure_of_member_type (depset *source, depset *ref)
   14858              : {
   14859          253 :   gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
   14860              :                        && ref->is_tu_local (/*strict=*/true));
   14861          253 :   tree source_entity = STRIP_TEMPLATE (source->get_entity ());
   14862          253 :   tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
   14863              : 
   14864          253 :   if (!source->is_tu_local (/*strict=*/true)
   14865          241 :       && source_entity
   14866          241 :       && ref_entity
   14867          241 :       && DECL_IMPLICIT_TYPEDEF_P (source_entity)
   14868            2 :       && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
   14869            2 :       && DECL_CLASS_SCOPE_P (ref_entity)
   14870          255 :       && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
   14871              :     {
   14872            4 :       gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
   14873              :       return true;
   14874              :     }
   14875              :   else
   14876              :     return false;
   14877              : }
   14878              : 
   14879              : /* DEP is a newly discovered dependency.  Append it to current's
   14880              :    depset.  */
   14881              : 
   14882              : void
   14883      6680745 : depset::hash::add_dependency (depset *dep)
   14884              : {
   14885      6680745 :   gcc_checking_assert (current && !is_key_order ());
   14886      6680745 :   current->deps.safe_push (dep);
   14887              : 
   14888      6680745 :   if (dep->is_tu_local (/*strict=*/true))
   14889              :     {
   14890          327 :       if (dep->is_tu_local ())
   14891          260 :         current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
   14892              :       else
   14893           67 :         current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
   14894              : 
   14895          327 :       if (!ignore_exposure && !is_exposure_of_member_type (current, dep))
   14896              :         {
   14897          133 :           if (dep->is_tu_local ())
   14898           91 :             current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14899              :           else
   14900           42 :             current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
   14901              :         }
   14902              :     }
   14903              : 
   14904      6680745 :   if (current->get_entity_kind () == EK_USING
   14905        43210 :       && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
   14906      6686445 :       && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
   14907              :     {
   14908              :       /* CURRENT is an unwrapped using-decl and DECL is an enum's
   14909              :          implicit typedef.  Is CURRENT a member of the enum?  */
   14910         5059 :       tree c_decl = OVL_FUNCTION (current->get_entity ());
   14911              : 
   14912         5059 :       if (TREE_CODE (c_decl) == CONST_DECL
   14913        10073 :           && (current->deps[0]->get_entity ()
   14914         5014 :               == CP_DECL_CONTEXT (dep->get_entity ())))
   14915              :         /* Make DECL depend on CURRENT.  */
   14916         4960 :         dep->deps.safe_push (current);
   14917              :     }
   14918              : 
   14919              :   /* If two dependencies recursively depend on each other existing within
   14920              :      their own merge keys, we must ensure that the first dep we saw while
   14921              :      walking is written first in this cluster.  See sort_cluster for more
   14922              :      details.  */
   14923      6680745 :   if (writing_merge_key)
   14924              :     {
   14925      1202694 :       if (!dep->is_maybe_recursive () && !current->is_maybe_recursive ())
   14926        63608 :         current->set_flag_bit<DB_ENTRY_BIT> ();
   14927      1202694 :       dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14928      1202694 :       current->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14929              :     }
   14930              : 
   14931      6680745 :   if (dep->is_unreached ())
   14932              :     {
   14933              :       /* The dependency is reachable now.  */
   14934       488538 :       reached_unreached = true;
   14935       488538 :       dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   14936       488538 :       dump (dumper::DEPEND)
   14937           30 :         && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
   14938           30 :                  TREE_CODE (dep->get_entity ()), dep->get_entity ());
   14939              :     }
   14940      6680745 : }
   14941              : 
   14942              : depset *
   14943      9853654 : depset::hash::add_dependency (tree decl, entity_kind ek)
   14944              : {
   14945      9853654 :   depset *dep;
   14946              : 
   14947      9853654 :   if (is_key_order ())
   14948              :     {
   14949      3108762 :       dep = find_dependency (decl);
   14950      3108762 :       if (dep)
   14951              :         {
   14952      1408657 :           current->deps.safe_push (dep);
   14953      1408657 :           dump (dumper::MERGE)
   14954          723 :             && dump ("Key dependency on %s %C:%N found",
   14955          723 :                      dep->entity_kind_name (), TREE_CODE (decl), decl);
   14956              :         }
   14957              :       else
   14958              :         {
   14959              :           /* It's not a mergeable decl, look for it in the original
   14960              :              table.  */
   14961      1700105 :           dep = chain->find_dependency (decl);
   14962      1700105 :           gcc_checking_assert (dep);
   14963              :         }
   14964              :     }
   14965              :   else
   14966              :     {
   14967      6744892 :       dep = make_dependency (decl, ek);
   14968      6744892 :       if (dep->get_entity_kind () != EK_REDIRECT)
   14969      6680745 :         add_dependency (dep);
   14970              :     }
   14971              : 
   14972      9853654 :   return dep;
   14973              : }
   14974              : 
   14975              : void
   14976       754589 : depset::hash::add_namespace_context (depset *dep, tree ns)
   14977              : {
   14978       754589 :   depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
   14979       754589 :   dep->deps.safe_push (ns_dep);
   14980              : 
   14981              :   /* Mark it as special if imported so we don't walk connect when
   14982              :      SCCing.  */
   14983       754589 :   if (!dep->is_binding () && ns_dep->is_import ())
   14984            0 :     dep->set_special ();
   14985       754589 : }
   14986              : 
   14987              : struct add_binding_data
   14988              : {
   14989              :   tree ns;
   14990              :   bitmap partitions;
   14991              :   depset *binding;
   14992              :   depset::hash *hash;
   14993              :   bool met_namespace;
   14994              : };
   14995              : 
   14996              : /* Return true if we are, or contain something that is exported.  */
   14997              : 
   14998              : bool
   14999      5934686 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
   15000              : {
   15001      5934686 :   auto data = static_cast <add_binding_data *> (data_);
   15002      5934686 :   decl = strip_using_decl (decl);
   15003              : 
   15004      5934686 :   if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
   15005              :     {
   15006      5926001 :       tree inner = decl;
   15007              : 
   15008      5926001 :       if (TREE_CODE (inner) == CONST_DECL
   15009        10381 :           && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
   15010              :           /* A using-decl could make a CONST_DECL purview for a non-purview
   15011              :              enumeration.  */
   15012      5936382 :           && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
   15013        10340 :         inner = TYPE_NAME (DECL_CONTEXT (inner));
   15014      5915661 :       else if (TREE_CODE (inner) == TEMPLATE_DECL)
   15015       148542 :         inner = DECL_TEMPLATE_RESULT (inner);
   15016              : 
   15017     11664209 :       if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
   15018     11487528 :           && !((flags & WMB_Using) && (flags & WMB_Purview)))
   15019              :         /* Ignore entities not within the module purview.  We'll need to
   15020              :            create bindings for any non-discarded function calls for ADL,
   15021              :            but it's simpler to handle that at the point of use rather
   15022              :            than trying to clear out bindings after the fact.  */
   15023              :         return false;
   15024              : 
   15025       203271 :       if ((flags & WMB_Hidden)
   15026         5609 :           && DECL_LANG_SPECIFIC (inner)
   15027       208880 :           && DECL_UNIQUE_FRIEND_P (inner))
   15028              :         /* Hidden friends will be found via ADL on the class type,
   15029              :            and so do not need to have bindings.  Anticipated builtin
   15030              :            functions and the hidden decl underlying a DECL_LOCAL_DECL_P
   15031              :            also don't need exporting, but we should create a binding
   15032              :            anyway so that we can have a common decl to match against.  */
   15033              :         return false;
   15034              : 
   15035       197751 :       bool internal_decl = false;
   15036       197751 :       if (!header_module_p () && is_tu_local_entity (decl)
   15037       198015 :           && !((flags & WMB_Using) && (flags & WMB_Export)))
   15038              :         {
   15039              :           /* A TU-local entity.  For ADL we still need to create bindings
   15040              :              for internal-linkage functions attached to a named module.  */
   15041          153 :           if (DECL_DECLARES_FUNCTION_P (inner)
   15042          105 :               && DECL_LANG_SPECIFIC (inner)
   15043          363 :               && DECL_MODULE_ATTACH_P (inner))
   15044              :             {
   15045           93 :               gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
   15046              :               internal_decl = true;
   15047              :             }
   15048              :           else
   15049              :             return false;
   15050              :         }
   15051              : 
   15052       197586 :       if ((TREE_CODE (decl) == VAR_DECL
   15053       197586 :            || TREE_CODE (decl) == TYPE_DECL)
   15054       197586 :           && DECL_TINFO_P (decl))
   15055              :         /* Ignore TINFO things.  */
   15056              :         return false;
   15057              : 
   15058       197586 :       if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
   15059              :         /* Ignore NTTP objects.  */
   15060              :         return false;
   15061              : 
   15062       197586 :       if (deduction_guide_p (decl))
   15063              :         {
   15064              :           /* Ignore deduction guides, bindings for them will be created within
   15065              :              find_dependencies for their class template.  But still build a dep
   15066              :              for them so that we don't discard them.  */
   15067         1616 :           data->hash->make_dependency (decl, EK_FOR_BINDING);
   15068         1616 :           return false;
   15069              :         }
   15070              : 
   15071       195970 :       if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
   15072              :         {
   15073              :           /* An unscoped enum constant implicitly brought into the containing
   15074              :              namespace.  We treat this like a using-decl.  */
   15075         4136 :           gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
   15076              : 
   15077         4136 :           flags = WMB_Flags (flags | WMB_Using);
   15078         4136 :           if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
   15079              :               /* A using-decl can make an enum constant exported for a
   15080              :                  non-exported enumeration.  */
   15081         4136 :               || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
   15082         3905 :             flags = WMB_Flags (flags | WMB_Export);
   15083              :         }
   15084              : 
   15085       195970 :       if (!data->binding)
   15086              :         /* No binding to check.  */;
   15087        43488 :       else if (flags & WMB_Using)
   15088              :         {
   15089              :           /* Look in the binding to see if we already have this
   15090              :              using.  */
   15091       181866 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15092              :             {
   15093       147233 :               depset *d = data->binding->deps[ix];
   15094       294466 :               if (d->get_entity_kind () == EK_USING
   15095       147233 :                   && OVL_FUNCTION (d->get_entity ()) == decl)
   15096              :                 {
   15097            3 :                   if (!(flags & WMB_Hidden))
   15098            3 :                     d->clear_hidden_binding ();
   15099            3 :                   OVL_PURVIEW_P (d->get_entity ()) = true;
   15100            3 :                   if (flags & WMB_Export)
   15101            3 :                     OVL_EXPORT_P (d->get_entity ()) = true;
   15102            3 :                   return bool (flags & WMB_Export);
   15103              :                 }
   15104              :             }
   15105              :         }
   15106        26170 :       else if (flags & WMB_Dups)
   15107              :         {
   15108              :           /* Look in the binding to see if we already have this decl.  */
   15109           78 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15110              :             {
   15111           39 :               depset *d = data->binding->deps[ix];
   15112           39 :               if (d->get_entity () == decl)
   15113              :                 {
   15114           33 :                   if (!(flags & WMB_Hidden))
   15115           33 :                     d->clear_hidden_binding ();
   15116              :                   return false;
   15117              :                 }
   15118              :             }
   15119              :         }
   15120              : 
   15121              :       /* We're adding something.  */
   15122       195934 :       if (!data->binding)
   15123              :         {
   15124       152482 :           data->binding = make_binding (data->ns, DECL_NAME (decl));
   15125       152482 :           data->hash->add_namespace_context (data->binding, data->ns);
   15126              : 
   15127       152482 :           depset **slot = data->hash->binding_slot (data->ns,
   15128       152482 :                                                     DECL_NAME (decl), true);
   15129       152482 :           gcc_checking_assert (!*slot);
   15130       152482 :           *slot = data->binding;
   15131              :         }
   15132              : 
   15133              :       /* Make sure nobody left a tree visited lying about.  */
   15134       195934 :       gcc_checking_assert (!TREE_VISITED (decl));
   15135              : 
   15136       195934 :       if (flags & WMB_Using)
   15137              :         {
   15138        42847 :           decl = ovl_make (decl, NULL_TREE);
   15139        42847 :           OVL_USING_P (decl) = true;
   15140        42847 :           OVL_PURVIEW_P (decl) = true;
   15141        42847 :           if (flags & WMB_Export)
   15142        41702 :             OVL_EXPORT_P (decl) = true;
   15143              :         }
   15144              : 
   15145       195934 :       entity_kind ek = EK_FOR_BINDING;
   15146       195934 :       if (internal_decl)
   15147              :         ek = EK_TU_LOCAL;
   15148       195841 :       else if (flags & WMB_Using)
   15149        42847 :         ek = EK_USING;
   15150              : 
   15151       195934 :       depset *dep = data->hash->make_dependency (decl, ek);
   15152       195934 :       if (flags & WMB_Hidden)
   15153           89 :         dep->set_hidden_binding ();
   15154       195934 :       data->binding->deps.safe_push (dep);
   15155              :       /* Binding and contents are mutually dependent.  */
   15156       195934 :       dep->deps.safe_push (data->binding);
   15157              : 
   15158       195934 :       return (flags & WMB_Using
   15159       195934 :               ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
   15160              :     }
   15161         8685 :   else if (!data->met_namespace)
   15162              :     {
   15163              :       /* Namespace, walk exactly once.  */
   15164         8676 :       data->met_namespace = true;
   15165         8676 :       if (data->hash->add_namespace_entities (decl, data->partitions))
   15166              :         {
   15167              :           /* It contains an exported thing, so it is exported.  */
   15168         1735 :           gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
   15169         1735 :           gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
   15170         1735 :           DECL_MODULE_EXPORT_P (decl) = true;
   15171              :         }
   15172              : 
   15173         8676 :       if (DECL_MODULE_PURVIEW_P (decl))
   15174              :         {
   15175         2104 :           data->hash->make_dependency (decl, depset::EK_NAMESPACE);
   15176              : 
   15177         2104 :           return DECL_MODULE_EXPORT_P (decl);
   15178              :         }
   15179              :     }
   15180              : 
   15181              :   return false;
   15182              : }
   15183              : 
   15184              : /* Recursively find all the namespace bindings of NS.  Add a depset
   15185              :    for every binding that contains an export or module-linkage entity.
   15186              :    Add a defining depset for every such decl that we need to write a
   15187              :    definition.  Such defining depsets depend on the binding depset.
   15188              :    Returns true if we contain something exported.  */
   15189              : 
   15190              : bool
   15191        11490 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   15192              : {
   15193        12704 :   dump () && dump ("Looking for writables in %N", ns);
   15194        11490 :   dump.indent ();
   15195              : 
   15196        11490 :   unsigned count = 0;
   15197        11490 :   add_binding_data data;
   15198        11490 :   data.ns = ns;
   15199        11490 :   data.partitions = partitions;
   15200        11490 :   data.hash = this;
   15201              : 
   15202      7714448 :   for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
   15203              :     {
   15204      7702958 :       data.binding = nullptr;
   15205      7702958 :       data.met_namespace = false;
   15206      7702958 :       if (walk_module_binding (binding, partitions, add_binding_entity, &data))
   15207       144656 :         count++;
   15208              :     }
   15209              : 
   15210              :   /* Seed any using-directives so that we emit the relevant namespaces.  */
   15211        12126 :   for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
   15212          218 :     if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
   15213              :       {
   15214              :         /* Unless it's a (TU-local) anonymous namespace.
   15215              : 
   15216              :            FIXME instead of checking here, they should be
   15217              :            is_tu_local_entity.  */
   15218          182 :         if (!TREE_PUBLIC (USING_DECL_DECLS (udir)))
   15219           73 :           continue;
   15220          109 :         make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
   15221          109 :         if (DECL_MODULE_EXPORT_P (udir))
   15222           80 :           count++;
   15223              :       }
   15224              : 
   15225        11490 :   if (count)
   15226         4064 :     dump () && dump ("Found %u entries", count);
   15227        11490 :   dump.outdent ();
   15228              : 
   15229        11490 :   return count != 0;
   15230              : }
   15231              : 
   15232              : void
   15233          216 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
   15234              : {
   15235        22475 :   for (unsigned ix = 0; ix != partial_classes->length (); ix++)
   15236              :     {
   15237        22259 :       tree inner = (*partial_classes)[ix];
   15238              : 
   15239        22259 :       depset *dep = make_dependency (inner, depset::EK_DECL);
   15240              : 
   15241        22259 :       if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15242              :         {
   15243        22259 :           dep = dep->deps[0];
   15244              :           /* We should have recorded the template as a partial
   15245              :              specialization.  */
   15246        22259 :           gcc_checking_assert (dep->get_entity_kind ()
   15247              :                                == depset::EK_PARTIAL);
   15248              : 
   15249              :           /* Only emit GM entities if reached.  */
   15250        22259 :           if (!DECL_LANG_SPECIFIC (inner)
   15251        34883 :               || !DECL_MODULE_PURVIEW_P (inner))
   15252        10534 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15253              :         }
   15254              :       else
   15255              :         {
   15256              :           /* It was an explicit specialization, not a partial one.
   15257              :              We should have already added this.  */
   15258            0 :           gcc_checking_assert (dep->get_entity_kind ()
   15259              :                                == depset::EK_SPECIALIZATION);
   15260            0 :           gcc_checking_assert (dep->is_special ());
   15261              :         }
   15262              :     }
   15263          216 : }
   15264              : 
   15265              : /* Add the members of imported classes that we defined in this TU.
   15266              :    This will also include lazily created implicit member function
   15267              :    declarations.  (All others will be definitions.)  */
   15268              : 
   15269              : void
   15270           12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
   15271              : {
   15272           24 :   for (unsigned ix = 0; ix != class_members->length (); ix++)
   15273              :     {
   15274           12 :       tree defn = (*class_members)[ix];
   15275           12 :       depset *dep = make_dependency (defn, EK_INNER_DECL);
   15276              : 
   15277           12 :       if (dep->get_entity_kind () == EK_REDIRECT)
   15278            0 :         dep = dep->deps[0];
   15279              : 
   15280              :       /* Only non-instantiations need marking as pendings.  */
   15281           24 :       if (dep->get_entity_kind () == EK_DECL)
   15282           12 :         dep->set_flag_bit <DB_IS_PENDING_BIT> ();
   15283              :     }
   15284           12 : }
   15285              : 
   15286              : /* Add any entities found via dependent ADL.  */
   15287              : 
   15288              : void
   15289      8448744 : depset::hash::add_dependent_adl_entities (tree expr)
   15290              : {
   15291      8448744 :   gcc_checking_assert (!is_key_order ());
   15292              : 
   15293              :   /* This is not needed for header units where everything is
   15294              :      visible to name lookup, nothing is discarded.  */
   15295      8448744 :   if (header_module_p ())
   15296      8406705 :     return;
   15297              : 
   15298      3843625 :   if (TREE_CODE (current->get_entity ()) != TEMPLATE_DECL)
   15299              :     return;
   15300              : 
   15301      2141720 :   dep_adl_info info;
   15302      2141720 :   auto_vec<tree, 3> args;
   15303      2141720 :   switch (TREE_CODE (expr))
   15304              :     {
   15305       228787 :     case CALL_EXPR:
   15306       228787 :       if (!KOENIG_LOOKUP_P (expr)
   15307       228787 :           || !type_dependent_expression_p_push (expr))
   15308              :         return;
   15309         7946 :       info.name = CALL_EXPR_FN (expr);
   15310         7946 :       if (!info.name)
   15311              :         return;
   15312         7946 :       if (TREE_CODE (info.name) == TEMPLATE_ID_EXPR)
   15313         1311 :         info.name = TREE_OPERAND (info.name, 0);
   15314         7946 :       if (TREE_CODE (info.name) == TU_LOCAL_ENTITY)
   15315              :         return;
   15316        13942 :       if (!identifier_p (info.name))
   15317         7788 :         info.name = OVL_NAME (info.name);
   15318        25470 :       for (int ix = 0; ix < call_expr_nargs (expr); ix++)
   15319        17524 :         args.safe_push (CALL_EXPR_ARG (expr, ix));
   15320              :       break;
   15321              : 
   15322        16794 :     case LE_EXPR:
   15323        16794 :     case GE_EXPR:
   15324        16794 :     case LT_EXPR:
   15325        16794 :     case GT_EXPR:
   15326        16794 :       info.rewrite = SPACESHIP_EXPR;
   15327        16794 :       goto overloadable_expr;
   15328              : 
   15329         9053 :     case NE_EXPR:
   15330         9053 :       info.rewrite = EQ_EXPR;
   15331         9053 :       goto overloadable_expr;
   15332              : 
   15333        18449 :     case EQ_EXPR:
   15334              :       /* Not strictly a rewrite candidate, but we need to ensure
   15335              :          that lookup of a matching NE_EXPR can succeed if that
   15336              :          would inhibit a rewrite with reversed parameters.  */
   15337        18449 :       info.rewrite = NE_EXPR;
   15338        18449 :       goto overloadable_expr;
   15339              : 
   15340       135606 :     case COMPOUND_EXPR:
   15341       135606 :     case MEMBER_REF:
   15342       135606 :     case MULT_EXPR:
   15343       135606 :     case TRUNC_DIV_EXPR:
   15344       135606 :     case TRUNC_MOD_EXPR:
   15345       135606 :     case PLUS_EXPR:
   15346       135606 :     case MINUS_EXPR:
   15347       135606 :     case LSHIFT_EXPR:
   15348       135606 :     case RSHIFT_EXPR:
   15349       135606 :     case SPACESHIP_EXPR:
   15350       135606 :     case BIT_AND_EXPR:
   15351       135606 :     case BIT_XOR_EXPR:
   15352       135606 :     case BIT_IOR_EXPR:
   15353       135606 :     case TRUTH_ANDIF_EXPR:
   15354       135606 :     case TRUTH_ORIF_EXPR:
   15355       135606 :     overloadable_expr:
   15356       135606 :       if (!type_dependent_expression_p_push (expr))
   15357              :         return;
   15358        98710 :       info.name = ovl_op_identifier (TREE_CODE (expr));
   15359        98710 :       gcc_checking_assert (tree_operand_length (expr) == 2);
   15360        98710 :       args.safe_push (TREE_OPERAND (expr, 0));
   15361        98710 :       args.safe_push (TREE_OPERAND (expr, 1));
   15362        98710 :       break;
   15363              : 
   15364              :     default:
   15365              :       return;
   15366              :     }
   15367              : 
   15368              :   /* If all arguments are type-dependent we don't need to do
   15369              :      anything further, we won't find new entities.  */
   15370       106656 :   bool all_type_dependent = true;
   15371       475995 :   for (tree arg : args)
   15372       198066 :     if (!type_dependent_expression_p_push (arg))
   15373              :       {
   15374              :         all_type_dependent = false;
   15375              :         break;
   15376              :       }
   15377       106656 :   if (all_type_dependent)
   15378              :     return;
   15379              : 
   15380        42039 :   gcc_checking_assert (!info.args);
   15381        42039 :   info.args = make_tree_vector ();
   15382       213777 :   for (tree arg : args)
   15383        87660 :     vec_safe_push (info.args, arg);
   15384              : 
   15385              :   /* We need to defer name lookup until after walking, otherwise
   15386              :      we get confused by stray TREE_VISITEDs.  */
   15387        42039 :   dep_adl_entity_list.safe_push (info);
   15388      2141720 : }
   15389              : 
   15390              : /* We add the partial & explicit specializations, and the explicit
   15391              :    instantiations.  */
   15392              : 
   15393              : static void
   15394      1116886 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
   15395              : {
   15396      1116886 :   vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
   15397              : 
   15398      1116886 :   if (!decl_p)
   15399              :     {
   15400              :       /* We exclusively use decls to locate things.  Make sure there's
   15401              :          no mismatch between the two specialization tables we keep.
   15402              :          pt.cc optimizes instantiation lookup using a complicated
   15403              :          heuristic.  We don't attempt to replicate that algorithm, but
   15404              :          observe its behaviour and reproduce it upon read back.  */
   15405              : 
   15406       329942 :        gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
   15407              :                            || DECL_CLASS_TEMPLATE_P (entry->tmpl));
   15408              : 
   15409       329942 :        gcc_checking_assert (!match_mergeable_specialization (true, entry));
   15410              :     }
   15411       786944 :   else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
   15412       385387 :     gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
   15413              : 
   15414      1116886 :   data->safe_push (entry);
   15415      1116886 : }
   15416              : 
   15417              : /* Arbitrary stable comparison.  */
   15418              : 
   15419              : static int
   15420     67963896 : specialization_cmp (const void *a_, const void *b_)
   15421              : {
   15422     67963896 :   const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
   15423     67963896 :   const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
   15424              : 
   15425     67963896 :   if (ea == eb)
   15426              :     return 0;
   15427              : 
   15428     67963896 :   tree a = ea->spec;
   15429     67963896 :   tree b = eb->spec;
   15430     67963896 :   if (TYPE_P (a))
   15431              :     {
   15432     19052757 :       a = TYPE_NAME (a);
   15433     19052757 :       b = TYPE_NAME (b);
   15434              :     }
   15435              : 
   15436     67963896 :   if (a == b)
   15437              :     /* This can happen with friend specializations.  Just order by
   15438              :        entry address.  See note in depset_cmp.  */
   15439            0 :     return ea < eb ? -1 : +1;
   15440              : 
   15441     67963896 :   return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
   15442              : }
   15443              : 
   15444              : /* We add all kinds of specialializations.  Implicit specializations
   15445              :    should only streamed and walked if they are reachable from
   15446              :    elsewhere.  Hence the UNREACHED flag.  This is making the
   15447              :    assumption that it is cheaper to reinstantiate them on demand
   15448              :    elsewhere, rather than stream them in when we instantiate their
   15449              :    general template.  Also, if we do stream them, we can only do that
   15450              :    if they are not internal (which they can become if they themselves
   15451              :    touch an internal entity?).  */
   15452              : 
   15453              : void
   15454         5628 : depset::hash::add_specializations (bool decl_p)
   15455              : {
   15456         5628 :   vec<spec_entry *> data;
   15457         5628 :   data.create (100);
   15458         5628 :   walk_specializations (decl_p, specialization_add, &data);
   15459         5628 :   data.qsort (specialization_cmp);
   15460      1122514 :   while (data.length ())
   15461              :     {
   15462      1116886 :       spec_entry *entry = data.pop ();
   15463      1116886 :       tree spec = entry->spec;
   15464      1116886 :       int use_tpl = 0;
   15465      1116886 :       bool is_friend = false;
   15466              : 
   15467      1116886 :       if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
   15468              :         /* A friend of a template.  This is keyed to the
   15469              :            instantiation.  */
   15470              :         is_friend = true;
   15471              : 
   15472      1116886 :       if (decl_p)
   15473              :         {
   15474       786944 :           if (tree ti = DECL_TEMPLATE_INFO (spec))
   15475              :             {
   15476       786944 :               tree tmpl = TI_TEMPLATE (ti);
   15477              : 
   15478       786944 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15479       786944 :               if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15480              :                 {
   15481         5973 :                   spec = tmpl;
   15482         5973 :                   gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
   15483              :                 }
   15484       780971 :               else if (is_friend)
   15485              :                 {
   15486         5519 :                   if (TI_TEMPLATE (ti) != entry->tmpl
   15487         5519 :                       || !template_args_equal (TI_ARGS (ti), entry->tmpl))
   15488         5519 :                     goto template_friend;
   15489              :                 }
   15490              :             }
   15491              :           else
   15492              :             {
   15493            0 :             template_friend:;
   15494         5519 :               gcc_checking_assert (is_friend);
   15495              :               /* This is a friend of a template class, but not the one
   15496              :                  that generated entry->spec itself (i.e. it's an
   15497              :                  equivalent clone).  We do not need to record
   15498              :                  this.  */
   15499         5519 :               continue;
   15500              :             }
   15501              :         }
   15502              :       else
   15503              :         {
   15504       329942 :           if (TREE_CODE (spec) == ENUMERAL_TYPE)
   15505              :             {
   15506         1528 :               tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
   15507              : 
   15508         1528 :               if (TYPE_P (ctx))
   15509         1522 :                 use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
   15510              :               else
   15511            6 :                 use_tpl = DECL_USE_TEMPLATE (ctx);
   15512              :             }
   15513              :           else
   15514       328414 :             use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
   15515              : 
   15516       329942 :           tree ti = TYPE_TEMPLATE_INFO (spec);
   15517       329942 :           tree tmpl = TI_TEMPLATE (ti);
   15518              : 
   15519       329942 :           spec = TYPE_NAME (spec);
   15520       329942 :           if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15521              :             {
   15522         1717 :               spec = tmpl;
   15523         1717 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15524              :             }
   15525              :         }
   15526              : 
   15527      1111367 :       bool needs_reaching = false;
   15528      1111367 :       if (use_tpl == 1)
   15529              :         /* Implicit instantiations only walked if we reach them.  */
   15530              :         needs_reaching = true;
   15531        87300 :       else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
   15532       156476 :                || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
   15533              :         /* Likewise, GMF explicit or partial specializations.  */
   15534              :         needs_reaching = true;
   15535              : 
   15536              : #if false && CHECKING_P
   15537              :       /* The instantiation isn't always on
   15538              :          DECL_TEMPLATE_INSTANTIATIONS, */
   15539              :       // FIXME: we probably need to remember this information?
   15540              :       /* Verify the specialization is on the
   15541              :          DECL_TEMPLATE_INSTANTIATIONS of the template.  */
   15542              :       for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
   15543              :            cons; cons = TREE_CHAIN (cons))
   15544              :         if (TREE_VALUE (cons) == entry->spec)
   15545              :           {
   15546              :             gcc_assert (entry->args == TREE_PURPOSE (cons));
   15547              :             goto have_spec;
   15548              :           }
   15549              :       gcc_unreachable ();
   15550              :     have_spec:;
   15551              : #endif
   15552              : 
   15553              :       /* Make sure nobody left a tree visited lying about.  */
   15554      1111367 :       gcc_checking_assert (!TREE_VISITED (spec));
   15555      1111367 :       depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
   15556      1111367 :       if (dep->is_special ())
   15557            0 :         gcc_unreachable ();
   15558              :       else
   15559              :         {
   15560      1111367 :           if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15561        25497 :             dep = dep->deps[0];
   15562      1085870 :           else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
   15563              :             {
   15564      1085870 :               dep->set_special ();
   15565      1085870 :               dep->deps.safe_push (reinterpret_cast<depset *> (entry));
   15566      1085870 :               if (!decl_p)
   15567       308624 :                 dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
   15568              :             }
   15569              : 
   15570      1111367 :           if (needs_reaching)
   15571      1065890 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15572      1111367 :           if (is_friend)
   15573            0 :             dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
   15574              :         }
   15575              :     }
   15576         5628 :   data.release ();
   15577         5628 : }
   15578              : 
   15579              : /* Add a depset into the mergeable hash.  */
   15580              : 
   15581              : void
   15582      1266178 : depset::hash::add_mergeable (depset *mergeable)
   15583              : {
   15584      1266178 :   gcc_checking_assert (is_key_order ());
   15585      1266178 :   entity_kind ek = mergeable->get_entity_kind ();
   15586      1266178 :   tree decl = mergeable->get_entity ();
   15587      1266178 :   gcc_checking_assert (ek < EK_DIRECT_HWM);
   15588              : 
   15589      1266178 :   depset **slot = entity_slot (decl, true);
   15590      1266178 :   gcc_checking_assert (!*slot);
   15591      1266178 :   depset *dep = make_entity (decl, ek);
   15592      1266178 :   *slot = dep;
   15593              : 
   15594      1266178 :   worklist.safe_push (dep);
   15595              : 
   15596              :   /* So we can locate the mergeable depset this depset refers to,
   15597              :      mark the first dep.  */
   15598      1266178 :   dep->set_special ();
   15599      1266178 :   dep->deps.safe_push (mergeable);
   15600      1266178 : }
   15601              : 
   15602              : /* Find the innermost-namespace scope of DECL, and that
   15603              :    namespace-scope decl.  */
   15604              : 
   15605              : tree
   15606     40806684 : find_pending_key (tree decl, tree *decl_p = nullptr)
   15607              : {
   15608     40806684 :   tree ns = decl;
   15609     49512751 :   do
   15610              :     {
   15611     49512751 :       decl = ns;
   15612     49512751 :       ns = CP_DECL_CONTEXT (ns);
   15613     49512751 :       if (TYPE_P (ns))
   15614      5375512 :         ns = TYPE_NAME (ns);
   15615              :     }
   15616     49512751 :   while (TREE_CODE (ns) != NAMESPACE_DECL);
   15617              : 
   15618     40806684 :   if (decl_p)
   15619     40217330 :     *decl_p = decl;
   15620              : 
   15621     40806684 :   return ns;
   15622              : }
   15623              : 
   15624              : /* Creates bindings and dependencies for all deduction guides of
   15625              :    the given class template DECL as needed.  */
   15626              : 
   15627              : void
   15628        59132 : depset::hash::add_deduction_guides (tree decl)
   15629              : {
   15630              :   /* Alias templates never have deduction guides.  */
   15631        59132 :   if (DECL_ALIAS_TEMPLATE_P (decl))
   15632        58074 :     return;
   15633              : 
   15634              :   /* We don't need to do anything for class-scope deduction guides,
   15635              :      as they will be added as members anyway.  */
   15636        59132 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   15637              :     return;
   15638              : 
   15639        45467 :   tree ns = CP_DECL_CONTEXT (decl);
   15640        45467 :   tree name = dguide_name (decl);
   15641              : 
   15642              :   /* We always add all deduction guides with a given name at once,
   15643              :      so if there's already a binding there's nothing to do.  */
   15644        45467 :   if (find_binding (ns, name))
   15645              :     return;
   15646              : 
   15647        42206 :   tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
   15648              :                                        /*complain=*/false);
   15649        42206 :   if (guides == error_mark_node)
   15650              :     return;
   15651              : 
   15652         1058 :   depset *binding = nullptr;
   15653         5124 :   for (tree t : lkp_range (guides))
   15654              :     {
   15655         3008 :       gcc_checking_assert (!TREE_VISITED (t));
   15656         3008 :       depset *dep = make_dependency (t, EK_FOR_BINDING);
   15657              : 
   15658              :       /* We don't want to create bindings for imported deduction guides, as
   15659              :          this would potentially cause name lookup to return duplicates.  */
   15660         3008 :       if (dep->is_import ())
   15661            6 :         continue;
   15662              : 
   15663         3002 :       if (!binding)
   15664              :         {
   15665              :           /* We have bindings to add.  */
   15666         1052 :           binding = make_binding (ns, name);
   15667         1052 :           add_namespace_context (binding, ns);
   15668              : 
   15669         1052 :           depset **slot = binding_slot (ns, name, /*insert=*/true);
   15670         1052 :           *slot = binding;
   15671              :         }
   15672              : 
   15673         3002 :       binding->deps.safe_push (dep);
   15674         3002 :       dep->deps.safe_push (binding);
   15675         3002 :       dump (dumper::DEPEND)
   15676            0 :         && dump ("Built binding for deduction guide %C:%N",
   15677            0 :                  TREE_CODE (decl), decl);
   15678              :     }
   15679              : }
   15680              : 
   15681              : /* Iteratively find dependencies.  During the walk we may find more
   15682              :    entries on the same binding that need walking.  */
   15683              : 
   15684              : void
   15685       330282 : depset::hash::find_dependencies (module_state *module)
   15686              : {
   15687       330282 :   trees_out walker (NULL, module, *this);
   15688       330282 :   vec<depset *> unreached;
   15689       660564 :   unreached.create (worklist.length ());
   15690              : 
   15691       332576 :   for (;;)
   15692              :     {
   15693       331429 :       reached_unreached = false;
   15694      5828591 :       while (worklist.length ())
   15695              :         {
   15696      5497162 :           depset *item = worklist.pop ();
   15697              : 
   15698      5497162 :           gcc_checking_assert (!item->is_binding ());
   15699      5497162 :           if (item->is_unreached ())
   15700      2915979 :             unreached.quick_push (item);
   15701              :           else
   15702              :             {
   15703      2581183 :               current = item;
   15704      2581183 :               tree decl = current->get_entity ();
   15705      2581183 :               dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
   15706      2582506 :                 && dump ("Dependencies of %s %C:%N",
   15707         1323 :                          is_key_order () ? "key-order"
   15708         1323 :                          : current->entity_kind_name (), TREE_CODE (decl), decl);
   15709      2581183 :               dump.indent ();
   15710      2581183 :               walker.begin ();
   15711      2581183 :               if (current->get_entity_kind () == EK_USING)
   15712        42847 :                 walker.tree_node (OVL_FUNCTION (decl));
   15713      2538336 :               else if (current->get_entity_kind () == EK_TU_LOCAL)
   15714              :                 /* We only stream its name and location.  */
   15715           93 :                 module->note_location (DECL_SOURCE_LOCATION (decl));
   15716      2538243 :               else if (TREE_VISITED (decl))
   15717              :                 /* A global tree.  */;
   15718      2535689 :               else if (current->get_entity_kind () == EK_NAMESPACE)
   15719              :                 {
   15720         2716 :                   module->note_location (DECL_SOURCE_LOCATION (decl));
   15721         2716 :                   add_namespace_context (current, CP_DECL_CONTEXT (decl));
   15722              :                 }
   15723              :               else
   15724              :                 {
   15725      2532973 :                   walker.mark_declaration (decl, current->has_defn ());
   15726              : 
   15727      2532973 :                   if (!is_key_order ()
   15728      2532973 :                       && item->is_pending_entity ())
   15729              :                     {
   15730       589354 :                       tree ns = find_pending_key (decl, nullptr);
   15731       589354 :                       add_namespace_context (item, ns);
   15732              :                     }
   15733              : 
   15734      2532973 :                   auto ovr = make_temp_override
   15735      2532973 :                     (ignore_exposure, item->is_ignored_exposure_context ());
   15736      2532973 :                   walker.decl_value (decl, current);
   15737      2532973 :                   if (current->has_defn ())
   15738       488539 :                     walker.write_definition (decl, current->refs_tu_local ());
   15739      2532973 :                 }
   15740      2581183 :               walker.end ();
   15741              : 
   15742              :               /* If we see either a class template or a deduction guide, make
   15743              :                  sure to add all visible deduction guides.  We need to check
   15744              :                  both in case they have been added in separate modules, or
   15745              :                  one is in the GMF and would have otherwise been discarded.  */
   15746      2581183 :               if (!is_key_order ()
   15747      2581183 :                   && DECL_CLASS_TEMPLATE_P (decl))
   15748        56124 :                 add_deduction_guides (decl);
   15749      2581183 :               if (!is_key_order ()
   15750      2581183 :                   && deduction_guide_p (decl))
   15751         3008 :                 add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
   15752              : 
   15753              :               /* Handle dependent ADL for [module.global.frag] p3.3.  */
   15754      2581183 :               if (!is_key_order () && !dep_adl_entity_list.is_empty ())
   15755              :                 {
   15756        22537 :                   processing_template_decl_sentinel ptds;
   15757        22537 :                   ++processing_template_decl;
   15758        64576 :                   for (auto &info : dep_adl_entity_list)
   15759              :                     {
   15760        42039 :                       tree lookup = lookup_arg_dependent (info.name, NULL_TREE,
   15761              :                                                           info.args, true);
   15762       127882 :                       for (tree fn : lkp_range (lookup))
   15763              :                         /* We don't need to add_dependency, just have
   15764              :                            make_dependency build an ADL binding.  */
   15765        43804 :                         make_dependency (fn, EK_DECL);
   15766              : 
   15767        42039 :                       if (info.rewrite)
   15768              :                         {
   15769        10000 :                           tree rewrite_name = ovl_op_identifier (info.rewrite);
   15770        10000 :                           lookup = lookup_arg_dependent (rewrite_name, NULL_TREE,
   15771              :                                                          info.args, true);
   15772        32455 :                           for (tree fn : lkp_range (lookup))
   15773        12455 :                             make_dependency (fn, EK_DECL);
   15774              :                         }
   15775        42039 :                       release_tree_vector (info.args);
   15776              :                     }
   15777        22537 :                   dep_adl_entity_list.truncate (0);
   15778        22537 :                 }
   15779              : 
   15780      2581183 :               if (!is_key_order ()
   15781      1315005 :                   && TREE_CODE (decl) == TEMPLATE_DECL
   15782      3039758 :                   && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   15783              :                 {
   15784              :                   /* Mark all the explicit & partial specializations as
   15785              :                      reachable.  We search both specialization lists as some
   15786              :                      constrained partial specializations for class types are
   15787              :                      only found in DECL_TEMPLATE_SPECIALIZATIONS.  */
   15788      1316380 :                   auto mark_reached = [this](tree spec)
   15789              :                     {
   15790       866888 :                       if (TYPE_P (spec))
   15791       249537 :                         spec = TYPE_NAME (spec);
   15792       866888 :                       int use_tpl;
   15793       866888 :                       node_template_info (spec, use_tpl);
   15794       866888 :                       if (use_tpl & 2)
   15795              :                         {
   15796        87051 :                           depset *spec_dep = find_dependency (spec);
   15797        87051 :                           if (spec_dep->get_entity_kind () == EK_REDIRECT)
   15798        19619 :                             spec_dep = spec_dep->deps[0];
   15799        87051 :                           if (spec_dep->is_unreached ())
   15800              :                             {
   15801        19741 :                               reached_unreached = true;
   15802        19741 :                               spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   15803        19741 :                               dump (dumper::DEPEND)
   15804            0 :                                 && dump ("Reaching unreached specialization"
   15805            0 :                                          " %C:%N", TREE_CODE (spec), spec);
   15806              :                             }
   15807              :                         }
   15808      1316380 :                     };
   15809              : 
   15810       449492 :                   for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
   15811      1296017 :                        cons; cons = TREE_CHAIN (cons))
   15812       846525 :                     mark_reached (TREE_VALUE (cons));
   15813       449492 :                   for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
   15814       469855 :                        cons; cons = TREE_CHAIN (cons))
   15815        20363 :                     mark_reached (TREE_VALUE (cons));
   15816              :                 }
   15817              : 
   15818      2581183 :               dump.outdent ();
   15819      2581183 :               current = NULL;
   15820              :             }
   15821              :         }
   15822              : 
   15823       331429 :       if (!reached_unreached)
   15824              :         break;
   15825              : 
   15826              :       /* It's possible the we reached the unreached before we
   15827              :          processed it in the above loop, so we'll be doing this an
   15828              :          extra time.  However, to avoid that we have to do some
   15829              :          bit shuffling that also involves a scan of the list.
   15830              :          Swings & roundabouts I guess.  */
   15831         1147 :       std::swap (worklist, unreached);
   15832         1147 :     }
   15833              : 
   15834       330282 :   unreached.release ();
   15835       330282 : }
   15836              : 
   15837              : /* Compare two entries of a single binding.  TYPE_DECL before
   15838              :    non-exported before exported.  */
   15839              : 
   15840              : static int
   15841      1022332 : binding_cmp (const void *a_, const void *b_)
   15842              : {
   15843      1022332 :   depset *a = *(depset *const *)a_;
   15844      1022332 :   depset *b = *(depset *const *)b_;
   15845              : 
   15846      1022332 :   tree a_ent = a->get_entity ();
   15847      1022332 :   tree b_ent = b->get_entity ();
   15848      1022332 :   gcc_checking_assert (a_ent != b_ent
   15849              :                        && !a->is_binding ()
   15850              :                        && !b->is_binding ());
   15851              : 
   15852              :   /* Implicit typedefs come first.  */
   15853      1022332 :   bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
   15854      1022332 :   bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
   15855      1022198 :   if (a_implicit || b_implicit)
   15856              :     {
   15857              :       /* A binding with two implicit type decls?  That's unpossible!  */
   15858          268 :       gcc_checking_assert (!(a_implicit && b_implicit));
   15859          268 :       return a_implicit ? -1 : +1;  /* Implicit first.  */
   15860              :     }
   15861              : 
   15862              :   /* TU-local before non-TU-local.  */
   15863      1022064 :   bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
   15864      1022064 :   bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
   15865      1022064 :   if (a_internal != b_internal)
   15866            0 :     return a_internal ? -1 : +1;  /* Internal first.  */
   15867              : 
   15868              :   /* Hidden before non-hidden.  */
   15869      1022064 :   bool a_hidden = a->is_hidden ();
   15870      1022064 :   bool b_hidden = b->is_hidden ();
   15871      1022064 :   if (a_hidden != b_hidden)
   15872            0 :     return a_hidden ? -1 : +1;
   15873              : 
   15874      1022064 :   bool a_using = a->get_entity_kind () == depset::EK_USING;
   15875      1022064 :   bool a_export;
   15876      1022064 :   if (a_using)
   15877              :     {
   15878       335661 :       a_export = OVL_EXPORT_P (a_ent);
   15879       335661 :       a_ent = OVL_FUNCTION (a_ent);
   15880              :     }
   15881       686403 :   else if (TREE_CODE (a_ent) == CONST_DECL
   15882            0 :            && DECL_LANG_SPECIFIC (a_ent)
   15883       686403 :            && DECL_MODULE_EXPORT_P (a_ent))
   15884              :     a_export = true;
   15885              :   else
   15886       686403 :     a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
   15887              :                                      ? TYPE_NAME (TREE_TYPE (a_ent))
   15888              :                                      : STRIP_TEMPLATE (a_ent));
   15889              : 
   15890      1022064 :   bool b_using = b->get_entity_kind () == depset::EK_USING;
   15891      1022064 :   bool b_export;
   15892      1022064 :   if (b_using)
   15893              :     {
   15894       353379 :       b_export = OVL_EXPORT_P (b_ent);
   15895       353379 :       b_ent = OVL_FUNCTION (b_ent);
   15896              :     }
   15897       668685 :   else if (TREE_CODE (b_ent) == CONST_DECL
   15898            0 :            && DECL_LANG_SPECIFIC (b_ent)
   15899       668685 :            && DECL_MODULE_EXPORT_P (b_ent))
   15900              :     b_export = true;
   15901              :   else
   15902       668685 :     b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
   15903              :                                      ? TYPE_NAME (TREE_TYPE (b_ent))
   15904              :                                      : STRIP_TEMPLATE (b_ent));
   15905              : 
   15906              :   /* Non-exports before exports.  */
   15907      1022064 :   if (a_export != b_export)
   15908       170106 :     return a_export ? +1 : -1;
   15909              : 
   15910              :   /* At this point we don't care, but want a stable sort.  */
   15911              : 
   15912       851958 :   if (a_using != b_using)
   15913              :     /* using first.  */
   15914        17887 :     return a_using? -1 : +1;
   15915              : 
   15916       834071 :   return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
   15917              : }
   15918              : 
   15919              : /* True iff TMPL has an explicit instantiation definition.
   15920              : 
   15921              :    This is local to module.cc because register_specialization skips adding most
   15922              :    instantiations unless module_maybe_has_cmi_p.  */
   15923              : 
   15924              : static bool
   15925           76 : template_has_explicit_inst (tree tmpl)
   15926              : {
   15927           88 :   for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
   15928              :     {
   15929           24 :       tree spec = TREE_VALUE (t);
   15930           24 :       if (DECL_EXPLICIT_INSTANTIATION (spec)
   15931           24 :           && !DECL_REALLY_EXTERN (spec))
   15932              :         return true;
   15933              :     }
   15934              :   return false;
   15935              : }
   15936              : 
   15937              : /* Complain about DEP that exposes 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          127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
   15944              : {
   15945          127 :   tree decl = dep->get_entity ();
   15946              : 
   15947              :   /* Don't need to walk if we're not going to be emitting
   15948              :      any diagnostics anyway.  */
   15949          148 :   if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15950           21 :                                      OPT_Wexpose_global_module_tu_local))
   15951            0 :     return false;
   15952              : 
   15953          523 :   for (depset *rdep : dep->deps)
   15954          135 :     if (!rdep->is_binding () && rdep->is_tu_local (strict)
   15955          369 :         && !is_exposure_of_member_type (dep, rdep))
   15956              :       {
   15957              :         // FIXME:QOI Better location information?  We're
   15958              :         // losing, so it doesn't matter about efficiency.
   15959          118 :         tree exposed = rdep->get_entity ();
   15960          118 :         auto_diagnostic_group d;
   15961          118 :         if (strict)
   15962              :           {
   15963              :             /* Allow suppressing the warning from the point of declaration
   15964              :                of the otherwise-exposed decl, for cases we know that
   15965              :                exposures will never be 'bad'.  */
   15966           27 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
   15967           27 :                                     OPT_Wexpose_global_module_tu_local)
   15968           45 :                 && pedwarn (DECL_SOURCE_LOCATION (decl),
   15969           18 :                             OPT_Wexpose_global_module_tu_local,
   15970              :                             "%qD exposes TU-local entity %qD", decl, exposed))
   15971              :               {
   15972           18 :                 bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15973           18 :                 gcc_checking_assert (informed);
   15974              :                 return true;
   15975              :               }
   15976              :           }
   15977              :         else
   15978              :           {
   15979           91 :             error_at (DECL_SOURCE_LOCATION (decl),
   15980              :                       "%qD exposes TU-local entity %qD", decl, exposed);
   15981           91 :             bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15982           91 :             gcc_checking_assert (informed);
   15983           91 :             if (dep->is_tu_local (/*strict=*/true))
   15984            3 :               inform (DECL_SOURCE_LOCATION (decl),
   15985              :                       "%qD is also TU-local but has been exposed elsewhere",
   15986              :                       decl);
   15987              :             return true;
   15988              :           }
   15989          118 :       }
   15990              : 
   15991              :   return false;
   15992              : }
   15993              : 
   15994              : /* Warn about a template DEP that references a TU-local entity.
   15995              : 
   15996              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15997              :    if we explained anything.  */
   15998              : 
   15999              : bool
   16000           94 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
   16001              : {
   16002           94 :   tree decl = dep->get_entity ();
   16003              : 
   16004              :   /* Don't bother walking if we know we won't be emitting anything.  */
   16005           94 :   if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   16006           94 :                            OPT_Wtemplate_names_tu_local)
   16007              :       /* Only warn strictly if users haven't silenced this warning here.  */
   16008          121 :       || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   16009           27 :                                          OPT_Wexpose_global_module_tu_local)))
   16010              :     return false;
   16011              : 
   16012              :   /* Friend decls in a class body are ignored, but this is harmless:
   16013              :      it should not impact any consumers.  */
   16014           94 :   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
   16015              :     return false;
   16016              : 
   16017              :   /* We should now only be warning about templates.  */
   16018           76 :   gcc_checking_assert
   16019              :     (TREE_CODE (decl) == TEMPLATE_DECL
   16020              :      && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
   16021              : 
   16022              :   /* Don't warn if we've seen any explicit instantiation definitions,
   16023              :      the intent might be for importers to only use those.  */
   16024           76 :   if (template_has_explicit_inst (decl))
   16025              :     return false;
   16026              : 
   16027          268 :   for (depset *rdep : dep->deps)
   16028          134 :     if (!rdep->is_binding () && rdep->is_tu_local (strict))
   16029              :       {
   16030           67 :         tree ref = rdep->get_entity ();
   16031           67 :         auto_diagnostic_group d;
   16032           67 :         if (strict)
   16033              :           {
   16034           15 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
   16035           15 :                                     OPT_Wexpose_global_module_tu_local)
   16036           21 :                 && warning_at (DECL_SOURCE_LOCATION (decl),
   16037            6 :                                OPT_Wtemplate_names_tu_local,
   16038              :                                "%qD refers to TU-local entity %qD, which may "
   16039              :                                "cause issues when instantiating in other TUs",
   16040              :                                decl, ref))
   16041              :               {
   16042            6 :                 is_tu_local_entity (ref, /*explain=*/true);
   16043            6 :                 return true;
   16044              :               }
   16045              :           }
   16046           52 :         else if (warning_at (DECL_SOURCE_LOCATION (decl),
   16047           52 :                              OPT_Wtemplate_names_tu_local,
   16048              :                              "%qD refers to TU-local entity %qD and cannot "
   16049              :                              "be instantiated in other TUs", decl, ref))
   16050              :           {
   16051           52 :             is_tu_local_entity (ref, /*explain=*/true);
   16052           52 :             return true;
   16053              :           }
   16054           67 :       }
   16055              : 
   16056              :   return false;
   16057              : }
   16058              : 
   16059              : /* Sort the bindings, issue errors about bad internal refs.  */
   16060              : 
   16061              : bool
   16062         2814 : depset::hash::finalize_dependencies ()
   16063              : {
   16064         2814 :   bool ok = true;
   16065      2073758 :   for (depset *dep : *this)
   16066              :     {
   16067      2070944 :       if (dep->is_binding ())
   16068              :         {
   16069              :           /* Keep the containing namespace dep first.  */
   16070       162519 :           gcc_checking_assert (dep->deps.length () > 1
   16071              :                                && (dep->deps[0]->get_entity_kind ()
   16072              :                                    == EK_NAMESPACE)
   16073              :                                && (dep->deps[0]->get_entity ()
   16074              :                                    == dep->get_entity ()));
   16075       162519 :           if (dep->deps.length () > 2)
   16076        16235 :             gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
   16077              :                        sizeof (dep->deps[1]), binding_cmp);
   16078              : 
   16079              :           /* Bindings shouldn't refer to imported entities.  */
   16080       162519 :           if (CHECKING_P)
   16081       877781 :             for (depset *entity : dep->deps)
   16082       390224 :               gcc_checking_assert (!entity->is_import ());
   16083       162519 :           continue;
   16084       162519 :         }
   16085              : 
   16086              :       /* Otherwise, we'll check for bad internal refs.
   16087              :          Don't complain about any references from TU-local entities.  */
   16088      1908425 :       if (dep->is_tu_local ())
   16089          264 :         continue;
   16090              : 
   16091              :       /* We already complained about usings of non-external entities in
   16092              :          check_can_export_using_decl, don't do it again here.  */
   16093      1908161 :       if (dep->get_entity_kind () == EK_USING)
   16094        42847 :         continue;
   16095              : 
   16096      1865314 :       if (dep->is_exposure ())
   16097              :         {
   16098          106 :           bool explained = diagnose_bad_internal_ref (dep);
   16099              : 
   16100              :           /* A TU-local variable will always be considered an exposure,
   16101              :              so we don't have to worry about strict-only handling.  */
   16102          106 :           tree decl = dep->get_entity ();
   16103          106 :           if (!explained
   16104           15 :               && VAR_P (decl)
   16105          121 :               && (DECL_DECLARED_CONSTEXPR_P (decl)
   16106            6 :                   || DECL_INLINE_VAR_P (decl)))
   16107              :             {
   16108           15 :               auto_diagnostic_group d;
   16109           15 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   16110            9 :                 error_at (DECL_SOURCE_LOCATION (decl),
   16111              :                           "%qD is declared %<constexpr%> and is initialized to "
   16112              :                           "a TU-local value", decl);
   16113              :               else
   16114              :                 {
   16115              :                   /* This can only occur with references.  */
   16116            6 :                   gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
   16117            6 :                   error_at (DECL_SOURCE_LOCATION (decl),
   16118              :                             "%qD is a reference declared %<inline%> and is "
   16119              :                             "constant-initialized to a TU-local value", decl);
   16120              :                 }
   16121           15 :               bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
   16122              :                                                  /*explain=*/true);
   16123           15 :               gcc_checking_assert (informed);
   16124           15 :               explained = true;
   16125           15 :             }
   16126              : 
   16127              :           /* We should have emitted an error above, unless the warning was
   16128              :              silenced.  */
   16129          106 :           gcc_checking_assert (explained);
   16130          106 :           ok = false;
   16131          106 :           continue;
   16132          106 :         }
   16133              : 
   16134              :       /* In all other cases, we're just warning (rather than erroring).
   16135              :          We don't want to do too much warning, so let's just bail after
   16136              :          the first warning we successfully emit.  */
   16137      1865226 :       if (warn_expose_global_module_tu_local
   16138      1865208 :           && !dep->is_tu_local (/*strict=*/true)
   16139      1865168 :           && dep->is_exposure (/*strict=*/true)
   16140      1865229 :           && diagnose_bad_internal_ref (dep, /*strict=*/true))
   16141           18 :         continue;
   16142              : 
   16143      1865242 :       if (warn_template_names_tu_local
   16144      1865205 :           && dep->refs_tu_local ()
   16145      1865257 :           && diagnose_template_names_tu_local (dep))
   16146           52 :         continue;
   16147              : 
   16148      1865138 :       if (warn_template_names_tu_local
   16149       273051 :           && warn_expose_global_module_tu_local
   16150       273051 :           && !dep->is_tu_local (/*strict=*/true)
   16151       273027 :           && dep->refs_tu_local (/*strict=*/true)
   16152           30 :           && !dep->is_exposure (/*strict=*/true)
   16153      1865165 :           && diagnose_template_names_tu_local (dep, /*strict=*/true))
   16154              :         continue;
   16155              :     }
   16156              : 
   16157         2814 :   return ok;
   16158              : }
   16159              : 
   16160              : /* Core of TARJAN's algorithm to find Strongly Connected Components
   16161              :    within a graph.  See https://en.wikipedia.org/wiki/
   16162              :    Tarjan%27s_strongly_connected_components_algorithm for details.
   16163              : 
   16164              :    We use depset::section as lowlink.  Completed nodes have
   16165              :    depset::cluster containing the cluster number, with the top
   16166              :    bit set.
   16167              : 
   16168              :    A useful property is that the output vector is a reverse
   16169              :    topological sort of the resulting DAG.  In our case that means
   16170              :    dependent SCCs are found before their dependers.  We make use of
   16171              :    that property.  */
   16172              : 
   16173              : void
   16174      2742685 : depset::tarjan::connect (depset *v)
   16175              : {
   16176      2742685 :   gcc_checking_assert (v->is_binding ()
   16177              :                        || !(v->is_tu_local ()
   16178              :                             || v->is_unreached ()
   16179              :                             || v->is_import ()));
   16180              : 
   16181      2742685 :   v->cluster = v->section = ++index;
   16182      2742685 :   stack.safe_push (v);
   16183              : 
   16184              :   /* Walk all our dependencies, ignore a first marked slot  */
   16185     24080005 :   for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
   16186              :     {
   16187      9302771 :       depset *dep = v->deps[ix];
   16188              : 
   16189      9302771 :       if (dep->is_binding ()
   16190     18378142 :           || !(dep->is_import () || dep->is_tu_local ()))
   16191              :         {
   16192      9290392 :           unsigned lwm = dep->cluster;
   16193              : 
   16194      9290392 :           if (!dep->cluster)
   16195              :             {
   16196              :               /* A new node.  Connect it.  */
   16197      1535479 :               connect (dep);
   16198      1535479 :               lwm = dep->section;
   16199              :             }
   16200              : 
   16201      9290392 :           if (dep->section && v->section > lwm)
   16202      1455092 :             v->section = lwm;
   16203              :         }
   16204              :     }
   16205              : 
   16206      2742685 :   if (v->section == v->cluster)
   16207              :     {
   16208              :       /* Root of a new SCC.  Push all the members onto the result list. */
   16209              :       unsigned num = v->cluster;
   16210      2742685 :       depset *p;
   16211      2742685 :       do
   16212              :         {
   16213      2742685 :           p = stack.pop ();
   16214      2742685 :           p->cluster = num;
   16215      2742685 :           p->section = 0;
   16216      2742685 :           result.quick_push (p);
   16217              :         }
   16218      2742685 :       while (p != v);
   16219              :     }
   16220      2742685 : }
   16221              : 
   16222              : /* Compare two depsets.  The specific ordering is unimportant, we're
   16223              :    just trying to get consistency.  */
   16224              : 
   16225              : static int
   16226    132101458 : depset_cmp (const void *a_, const void *b_)
   16227              : {
   16228    132101458 :   depset *a = *(depset *const *)a_;
   16229    132101458 :   depset *b = *(depset *const *)b_;
   16230              : 
   16231    132101458 :   depset::entity_kind a_kind = a->get_entity_kind ();
   16232    132101458 :   depset::entity_kind b_kind = b->get_entity_kind ();
   16233              : 
   16234    132101458 :   if  (a_kind != b_kind)
   16235              :     /* Different entity kinds, order by that.  */
   16236      4690509 :     return a_kind < b_kind ? -1 : +1;
   16237              : 
   16238    127410949 :   tree a_decl = a->get_entity ();
   16239    127410949 :   tree b_decl = b->get_entity ();
   16240    127410949 :   if (a_kind == depset::EK_USING)
   16241              :     {
   16242              :       /* If one is a using, the other must be too.  */
   16243      2472110 :       a_decl = OVL_FUNCTION (a_decl);
   16244      2472110 :       b_decl = OVL_FUNCTION (b_decl);
   16245              :     }
   16246              : 
   16247    127410949 :   if (a_decl != b_decl)
   16248              :     /* Different entities, order by their UID.  */
   16249    119415726 :     return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
   16250              : 
   16251      7995223 :   if (a_kind == depset::EK_BINDING)
   16252              :     {
   16253              :       /* Both are bindings.  Order by identifier hash.  */
   16254      7991937 :       gcc_checking_assert (a->get_name () != b->get_name ());
   16255      7991937 :       hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
   16256      7991937 :       hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
   16257      7991937 :       return (ah == bh ? 0 : ah < bh ? -1 : +1);
   16258              :     }
   16259              : 
   16260              :   /* They are the same decl.  This can happen with two using decls
   16261              :      pointing to the same target.  The best we can aim for is
   16262              :      consistently telling qsort how to order them.  Hopefully we'll
   16263              :      never have to debug a case that depends on this.  Oh, who am I
   16264              :      kidding?  Good luck.  */
   16265         3286 :   gcc_checking_assert (a_kind == depset::EK_USING);
   16266              : 
   16267              :   /* Order by depset address.  Not the best, but it is something.  */
   16268         3286 :   return a < b ? -1 : +1;
   16269              : }
   16270              : 
   16271              : /* Sort the clusters in SCC such that those that depend on one another
   16272              :    are placed later.   */
   16273              : 
   16274              : // FIXME: I am not convinced this is needed and, if needed,
   16275              : // sufficient.  We emit the decls in this order but that emission
   16276              : // could walk into later decls (from the body of the decl, or default
   16277              : // arg-like things).  Why doesn't that walk do the right thing?  And
   16278              : // if it DTRT why do we need to sort here -- won't things naturally
   16279              : // work?  I think part of the issue is that when we're going to refer
   16280              : // to an entity by name, and that entity is in the same cluster as us,
   16281              : // we need to actually walk that entity, if we've not already walked
   16282              : // it.
   16283              : static void
   16284       327468 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   16285              : {
   16286       327468 :   depset::hash table (size, original);
   16287              : 
   16288       327468 :   dump.indent ();
   16289              : 
   16290              :   /* Place bindings last, usings before that.  It's not strictly
   16291              :      necessary, but it does make things neater.  Says Mr OCD.  */
   16292       327468 :   unsigned bind_lwm = size;
   16293       327468 :   unsigned use_lwm = size;
   16294      1798752 :   for (unsigned ix = 0; ix != use_lwm;)
   16295              :     {
   16296      1471284 :       depset *dep = scc[ix];
   16297      1471284 :       switch (dep->get_entity_kind ())
   16298              :         {
   16299       162262 :         case depset::EK_BINDING:
   16300              :           /* Move to end.  No increment.  Notice this could be moving
   16301              :              a using decl, which we'll then move again.  */
   16302       162262 :           if (--bind_lwm != ix)
   16303              :             {
   16304        94218 :               scc[ix] = scc[bind_lwm];
   16305        94218 :               scc[bind_lwm] = dep;
   16306              :             }
   16307       162262 :           if (use_lwm > bind_lwm)
   16308              :             {
   16309       128151 :               use_lwm--;
   16310       128151 :               break;
   16311              :             }
   16312              :           /* We must have copied a using or TU-local, so move it too.  */
   16313        34111 :           dep = scc[ix];
   16314        34111 :           gcc_checking_assert
   16315              :             (dep->get_entity_kind () == depset::EK_USING
   16316              :              || dep->get_entity_kind () == depset::EK_TU_LOCAL);
   16317              :           /* FALLTHROUGH  */
   16318              : 
   16319        76955 :         case depset::EK_USING:
   16320        76955 :         case depset::EK_TU_LOCAL:
   16321        76955 :           if (--use_lwm != ix)
   16322              :             {
   16323        58714 :               scc[ix] = scc[use_lwm];
   16324        58714 :               scc[use_lwm] = dep;
   16325              :             }
   16326              :           break;
   16327              : 
   16328      1266178 :         case depset::EK_DECL:
   16329      1266178 :         case depset::EK_SPECIALIZATION:
   16330      1266178 :         case depset::EK_PARTIAL:
   16331      1266178 :           table.add_mergeable (dep);
   16332      1266178 :           ix++;
   16333      1266178 :           break;
   16334              : 
   16335            0 :         default:
   16336            0 :           gcc_unreachable ();
   16337              :         }
   16338              :     }
   16339              : 
   16340       327468 :   gcc_checking_assert (use_lwm <= bind_lwm);
   16341       327756 :   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
   16342              : 
   16343       327468 :   table.find_dependencies (nullptr);
   16344              : 
   16345       327468 :   auto_vec<depset *> order = table.connect ();
   16346       654936 :   gcc_checking_assert (order.length () == use_lwm);
   16347              : 
   16348              :   /* Now rewrite entries [0,lwm), in the dependency order we
   16349              :      discovered.  Usually each entity is in its own cluster.  Rarely,
   16350              :      we can get multi-entity clusters, in which case all but one must
   16351              :      only be reached from within the cluster.  This happens for
   16352              :      something like:
   16353              : 
   16354              :      template<typename T>
   16355              :      auto Foo (const T &arg) -> TPL<decltype (arg)>;
   16356              : 
   16357              :      The instantiation of TPL will be in the specialization table, and
   16358              :      refer to Foo via arg.  But we can only get to that specialization
   16359              :      from Foo's declaration, so we only need to treat Foo as mergeable
   16360              :      (We'll do structural comparison of TPL<decltype (arg)>).
   16361              : 
   16362              :      We approximate finding the single cluster entry dep by checking for
   16363              :      entities recursively depending on a dep first seen when streaming
   16364              :      its own merge key; the first dep we see in such a cluster should be
   16365              :      the first one streamed.  */
   16366              :   unsigned entry_pos = ~0u;
   16367              :   unsigned cluster = ~0u;
   16368      3187292 :   for (unsigned ix = 0; ix != order.length (); ix++)
   16369              :     {
   16370      1266178 :       gcc_checking_assert (order[ix]->is_special ());
   16371      1266178 :       bool tight = order[ix]->cluster == cluster;
   16372      1266178 :       depset *dep = order[ix]->deps[0];
   16373      1267171 :       dump (dumper::MERGE)
   16374         1983 :         && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
   16375          993 :                  tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
   16376      1266178 :       scc[ix] = dep;
   16377      1266178 :       if (tight)
   16378              :         {
   16379          131 :           gcc_checking_assert (dep->is_maybe_recursive ());
   16380          131 :           if (dep->is_entry ())
   16381              :             {
   16382              :               /* There should only be one entry dep in a cluster.  */
   16383            9 :               gcc_checking_assert (!scc[entry_pos]->is_entry ());
   16384            9 :               gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
   16385            9 :               scc[ix] = scc[entry_pos];
   16386            9 :               scc[entry_pos] = dep;
   16387              :             }
   16388              :         }
   16389              :       else
   16390              :         entry_pos = ix;
   16391      1266178 :       cluster = order[ix]->cluster;
   16392              :     }
   16393              : 
   16394       327756 :   dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
   16395       327468 :   dump.outdent ();
   16396       327468 : }
   16397              : 
   16398              : /* Reduce graph to SCCS clusters.  SCCS will be populated with the
   16399              :    depsets in dependency order.  Each depset's CLUSTER field contains
   16400              :    its cluster number.  Each SCC has a unique cluster number, and are
   16401              :    contiguous in SCCS. Cluster numbers are otherwise arbitrary.  */
   16402              : 
   16403              : vec<depset *>
   16404       330253 : depset::hash::connect ()
   16405              : {
   16406       330253 :   tarjan connector (size ());
   16407       330253 :   vec<depset *> deps;
   16408       330253 :   deps.create (size ());
   16409      3666452 :   for (depset *item : *this)
   16410              :     {
   16411      3336199 :       entity_kind kind = item->get_entity_kind ();
   16412      3173937 :       if (kind == EK_BINDING
   16413      3173937 :           || !(kind == EK_REDIRECT
   16414      3147499 :                || item->is_tu_local ()
   16415      3147368 :                || item->is_unreached ()
   16416      2589345 :                || item->is_import ()))
   16417      2742685 :         deps.quick_push (item);
   16418              :     }
   16419              : 
   16420              :   /* Iteration over the hash table is an unspecified ordering.  While
   16421              :      that has advantages, it causes 2 problems.  Firstly repeatable
   16422              :      builds are tricky.  Secondly creating testcases that check
   16423              :      dependencies are correct by making sure a bad ordering would
   16424              :      happen if that was wrong.  */
   16425       330253 :   deps.qsort (depset_cmp);
   16426              : 
   16427      3403191 :   while (deps.length ())
   16428              :     {
   16429      2742685 :       depset *v = deps.pop ();
   16430      2742685 :       dump (dumper::CLUSTER) &&
   16431         1800 :         (v->is_binding ()
   16432          210 :          ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
   16433         1590 :          : dump ("Connecting %s %s %C:%N",
   16434         1590 :                  is_key_order () ? "key-order"
   16435          870 :                  : !v->has_defn () ? "declaration" : "definition",
   16436         1590 :                  v->entity_kind_name (), TREE_CODE (v->get_entity ()),
   16437              :                  v->get_entity ()));
   16438      2742685 :       if (!v->cluster)
   16439      1207206 :         connector.connect (v);
   16440              :     }
   16441              : 
   16442       330253 :   deps.release ();
   16443       660506 :   return connector.result;
   16444       330253 : }
   16445              : 
   16446              : /* Initialize location spans.  */
   16447              : 
   16448              : void
   16449         4973 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
   16450              : {
   16451         4973 :   gcc_checking_assert (!init_p ());
   16452         4973 :   spans = new vec<span> ();
   16453         4973 :   spans->reserve (20);
   16454              : 
   16455         4973 :   span interval;
   16456         4973 :   interval.ordinary.first = 0;
   16457         4973 :   interval.macro.second = MAX_LOCATION_T + 1;
   16458         4973 :   interval.ordinary_delta = interval.macro_delta = 0;
   16459              : 
   16460              :   /* A span for reserved fixed locs.  */
   16461         4973 :   interval.ordinary.second
   16462         4973 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   16463         4973 :   interval.macro.first = interval.macro.second;
   16464         4973 :   dump (dumper::LOCATION)
   16465           42 :     && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16466              :              interval.ordinary.first, interval.ordinary.second,
   16467              :              interval.macro.first, interval.macro.second);
   16468         4973 :   spans->quick_push (interval);
   16469              : 
   16470              :   /* A span for command line & forced headers.  */
   16471         4973 :   interval.ordinary.first = interval.ordinary.second;
   16472         4973 :   interval.macro.second = interval.macro.first;
   16473         4973 :   if (map)
   16474              :     {
   16475         4967 :       interval.ordinary.second = map->start_location;
   16476         4967 :       interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
   16477              :     }
   16478         4973 :   dump (dumper::LOCATION)
   16479           21 :     && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16480              :              interval.ordinary.first, interval.ordinary.second,
   16481              :              interval.macro.first, interval.macro.second);
   16482         4973 :   spans->quick_push (interval);
   16483              : 
   16484              :   /* Start an interval for the main file.  */
   16485         4973 :   interval.ordinary.first = interval.ordinary.second;
   16486         4973 :   interval.macro.second = interval.macro.first;
   16487         4973 :   dump (dumper::LOCATION)
   16488           21 :     && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
   16489              :              interval.ordinary.first, interval.macro.second);
   16490         4973 :   spans->quick_push (interval);
   16491         4973 : }
   16492              : 
   16493              : /* Reopen the span, if we want the about-to-be-inserted set of maps to
   16494              :    be propagated in our own location table.  I.e. we are the primary
   16495              :    interface and we're importing a partition.  */
   16496              : 
   16497              : bool
   16498         3099 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
   16499              : {
   16500         3099 :   bool opened = (module_interface_p () && !module_partition_p ()
   16501         3569 :                  && import->is_partition ());
   16502          178 :   if (opened)
   16503          178 :     open (hwm);
   16504         3099 :   return opened;
   16505              : }
   16506              : 
   16507              : /* Open a new linemap interval.  The just-created ordinary map is the
   16508              :    first map of the interval.  */
   16509              : 
   16510              : void
   16511         1089 : loc_spans::open (location_t hwm)
   16512              : {
   16513         1089 :   span interval;
   16514         1089 :   interval.ordinary.first = interval.ordinary.second = hwm;
   16515         2178 :   interval.macro.first = interval.macro.second
   16516         1089 :     = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16517         1089 :   interval.ordinary_delta = interval.macro_delta = 0;
   16518         1089 :   dump (dumper::LOCATION)
   16519            0 :     && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
   16520            0 :              spans->length (), interval.ordinary.first,
   16521              :              interval.macro.second);
   16522         1089 :   if (spans->length ())
   16523              :     {
   16524              :       /* No overlapping!  */
   16525         1089 :       auto &last = spans->last ();
   16526         1089 :       gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
   16527         1089 :       gcc_checking_assert (interval.macro.second <= last.macro.first);
   16528              :     }
   16529         1089 :   spans->safe_push (interval);
   16530         1089 : }
   16531              : 
   16532              : /* Close out the current linemap interval.  The last maps are within
   16533              :    the interval.  */
   16534              : 
   16535              : void
   16536         6059 : loc_spans::close ()
   16537              : {
   16538         6059 :   span &interval = spans->last ();
   16539              : 
   16540         6059 :   interval.ordinary.second
   16541         6059 :     = ((line_table->highest_location
   16542         6059 :         + (loc_one << line_table->default_range_bits))
   16543         6059 :        & ~((loc_one << line_table->default_range_bits) - 1));
   16544         6059 :   interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16545         6059 :   dump (dumper::LOCATION)
   16546           21 :     && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
   16547           21 :              spans->length () - 1,
   16548              :              interval.ordinary.first,interval.ordinary.second,
   16549              :              interval.macro.first, interval.macro.second);
   16550         6059 : }
   16551              : 
   16552              : /* Given an ordinary location LOC, return the lmap_interval it resides
   16553              :    in.  NULL if it is not in an interval.  */
   16554              : 
   16555              : const loc_spans::span *
   16556     41930775 : loc_spans::ordinary (location_t loc)
   16557              : {
   16558     41930775 :   unsigned len = spans->length ();
   16559     41930775 :   unsigned pos = 0;
   16560    125675775 :   while (len)
   16561              :     {
   16562     83729617 :       unsigned half = len / 2;
   16563     83729617 :       const span &probe = (*spans)[pos + half];
   16564     83729617 :       if (loc < probe.ordinary.first)
   16565              :         len = half;
   16566     83719286 :       else if (loc < probe.ordinary.second)
   16567              :         return &probe;
   16568              :       else
   16569              :         {
   16570     41803894 :           pos += half + 1;
   16571     41803894 :           len = len - (half + 1);
   16572              :         }
   16573              :     }
   16574              :   return NULL;
   16575              : }
   16576              : 
   16577              : /* Likewise, given a macro location LOC, return the lmap interval it
   16578              :    resides in.   */
   16579              : 
   16580              : const loc_spans::span *
   16581      3205803 : loc_spans::macro (location_t loc)
   16582              : {
   16583      3205803 :   unsigned len = spans->length ();
   16584      3205803 :   unsigned pos = 0;
   16585      9614379 :   while (len)
   16586              :     {
   16587      6408546 :       unsigned half = len / 2;
   16588      6408546 :       const span &probe = (*spans)[pos + half];
   16589      6408546 :       if (loc >= probe.macro.second)
   16590              :         len = half;
   16591      6408528 :       else if (loc >= probe.macro.first)
   16592              :         return &probe;
   16593              :       else
   16594              :         {
   16595      3202755 :           pos += half + 1;
   16596      3202755 :           len = len - (half + 1);
   16597              :         }
   16598              :     }
   16599              :   return NULL;
   16600              : }
   16601              : 
   16602              : /* Return the ordinary location closest to FROM.  */
   16603              : 
   16604              : static location_t
   16605         6949 : ordinary_loc_of (line_maps *lmaps, location_t from)
   16606              : {
   16607        13901 :   while (!IS_ORDINARY_LOC (from))
   16608              :     {
   16609            3 :       if (IS_ADHOC_LOC (from))
   16610            3 :         from = get_location_from_adhoc_loc (lmaps, from);
   16611            3 :       if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
   16612              :         {
   16613              :           /* Find the ordinary location nearest FROM.  */
   16614            0 :           const line_map *map = linemap_lookup (lmaps, from);
   16615            0 :           const line_map_macro *mac_map = linemap_check_macro (map);
   16616            0 :           from = mac_map->get_expansion_point_location ();
   16617              :         }
   16618              :     }
   16619         6949 :   return from;
   16620              : }
   16621              : 
   16622              : static module_state **
   16623        12470 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
   16624              : {
   16625        12470 :   module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
   16626        12470 :   hashval_t hv = module_state_hash::hash (ct);
   16627              : 
   16628        12470 :   return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
   16629              : }
   16630              : 
   16631              : static module_state *
   16632       152662 : get_primary (module_state *parent)
   16633              : {
   16634       156728 :   while (parent->is_partition ())
   16635          670 :     parent = parent->parent;
   16636              : 
   16637       156058 :   if (!parent->name)
   16638              :     // Implementation unit has null name
   16639        96219 :     parent = parent->parent;
   16640              : 
   16641       151496 :   return parent;
   16642              : }
   16643              : 
   16644              : /* Find or create module NAME & PARENT in the hash table.  */
   16645              : 
   16646              : module_state *
   16647        12470 : get_module (tree name, module_state *parent, bool partition)
   16648              : {
   16649              :   /* We might be given an empty NAME if preprocessing fails to handle
   16650              :      a header-name token.  */
   16651        12470 :   if (name && TREE_CODE (name) == STRING_CST
   16652        15375 :       && TREE_STRING_LENGTH (name) == 0)
   16653              :     return nullptr;
   16654              : 
   16655        12470 :   if (partition)
   16656              :     {
   16657         1105 :       if (!parent)
   16658          235 :         parent = get_primary (this_module ());
   16659              : 
   16660         1105 :       if (!parent->is_partition () && !parent->flatname)
   16661          259 :         parent->set_flatname ();
   16662              :     }
   16663              : 
   16664        12470 :   module_state **slot = get_module_slot (name, parent, partition, true);
   16665        12470 :   module_state *state = *slot;
   16666        12470 :   if (!state)
   16667              :     {
   16668         6751 :       state = (new (ggc_alloc<module_state> ())
   16669         6751 :                module_state (name, parent, partition));
   16670         6751 :       *slot = state;
   16671              :     }
   16672              :   return state;
   16673              : }
   16674              : 
   16675              : /* Process string name PTR into a module_state.  */
   16676              : 
   16677              : static module_state *
   16678          457 : get_module (const char *ptr)
   16679              : {
   16680              :   /* On DOS based file systems, there is an ambiguity with A:B which can be
   16681              :      interpreted as a module Module:Partition or Drive:PATH.  Interpret strings
   16682              :      which clearly starts as pathnames as header-names and everything else is
   16683              :      treated as a (possibly malformed) named moduled.  */
   16684          457 :   if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
   16685              : #if HAVE_DOS_BASED_FILE_SYSTEM
   16686              :       || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
   16687              : #endif
   16688              :       || false)
   16689              :     /* A header name.  */
   16690          112 :     return get_module (build_string (strlen (ptr), ptr));
   16691              : 
   16692              :   bool partition = false;
   16693              :   module_state *mod = NULL;
   16694              : 
   16695         1357 :   for (const char *probe = ptr;; probe++)
   16696         1702 :     if (!*probe || *probe == '.' || *probe == ':')
   16697              :       {
   16698          435 :         if (probe == ptr)
   16699              :           return NULL;
   16700              : 
   16701          435 :         mod = get_module (get_identifier_with_length (ptr, probe - ptr),
   16702              :                           mod, partition);
   16703          435 :         ptr = probe;
   16704          435 :         if (*ptr == ':')
   16705              :           {
   16706           87 :             if (partition)
   16707              :               return NULL;
   16708              :             partition = true;
   16709              :           }
   16710              : 
   16711          435 :         if (!*ptr++)
   16712              :           break;
   16713              :       }
   16714         1267 :     else if (!(ISALPHA (*probe) || *probe == '_'
   16715           18 :                || (probe != ptr && ISDIGIT (*probe))))
   16716              :       return NULL;
   16717              : 
   16718              :   return mod;
   16719              : }
   16720              : 
   16721              : /* Create a new mapper connecting to OPTION.  */
   16722              : 
   16723              : module_client *
   16724         4973 : make_mapper (location_t loc, class mkdeps *deps)
   16725              : {
   16726         4973 :   timevar_start (TV_MODULE_MAPPER);
   16727         4973 :   const char *option = module_mapper_name;
   16728         4973 :   if (!option)
   16729         4925 :     option = getenv ("CXX_MODULE_MAPPER");
   16730              : 
   16731         9946 :   mapper = module_client::open_module_client
   16732         4973 :     (loc, option, deps, &set_cmi_repo,
   16733         4973 :      (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
   16734         4973 :      && save_decoded_options[0].arg != progname
   16735              :      ? save_decoded_options[0].arg : nullptr);
   16736              : 
   16737         4973 :   timevar_stop (TV_MODULE_MAPPER);
   16738              : 
   16739         4973 :   return mapper;
   16740              : }
   16741              : 
   16742              : static unsigned lazy_snum;
   16743              : 
   16744              : static bool
   16745        12247 : recursive_lazy (unsigned snum = ~0u)
   16746              : {
   16747        12247 :   if (lazy_snum)
   16748              :     {
   16749            0 :       error_at (input_location, "recursive lazy load");
   16750            0 :       return true;
   16751              :     }
   16752              : 
   16753        12247 :   lazy_snum = snum;
   16754        12247 :   return false;
   16755              : }
   16756              : 
   16757              : /* If THIS has an interface dependency on itself, report an error and
   16758              :    return false.  */
   16759              : 
   16760              : bool
   16761         2942 : module_state::check_circular_import (location_t from)
   16762              : {
   16763         2942 :   if (this == this_module ())
   16764              :     {
   16765              :       /* Cannot import the current module.  */
   16766            9 :       auto_diagnostic_group d;
   16767            9 :       error_at (from, "module %qs depends on itself", get_flatname ());
   16768            9 :       if (!header_module_p ())
   16769            6 :         inform (loc, "module %qs declared here", get_flatname ());
   16770            9 :       return false;
   16771            9 :     }
   16772              :   return true;
   16773              : }
   16774              : 
   16775              : /* Module name substitutions.  */
   16776              : static vec<module_state *,va_heap> substs;
   16777              : 
   16778              : void
   16779         9175 : module_state::mangle (bool include_partition)
   16780              : {
   16781         9175 :   if (subst)
   16782          425 :     mangle_module_substitution (subst);
   16783              :   else
   16784              :     {
   16785         8750 :       if (parent)
   16786          882 :         parent->mangle (include_partition);
   16787         8750 :       if (include_partition || !is_partition ())
   16788              :         {
   16789              :           // Partitions are significant for global initializer
   16790              :           // functions
   16791         8544 :           bool partition = is_partition () && !parent->is_partition ();
   16792         8544 :           subst = mangle_module_component (name, partition);
   16793         8544 :           substs.safe_push (this);
   16794              :         }
   16795              :     }
   16796         9175 : }
   16797              : 
   16798              : void
   16799         8293 : mangle_module (int mod, bool include_partition)
   16800              : {
   16801         8293 :   module_state *imp = (*modules)[mod];
   16802              : 
   16803         8293 :   gcc_checking_assert (!imp->is_header ());
   16804              : 
   16805         8293 :   if (!imp->name)
   16806              :     /* Set when importing the primary module interface.  */
   16807          223 :     imp = imp->parent;
   16808              : 
   16809              :   /* Ensure this is actually a module unit.  */
   16810          223 :   gcc_checking_assert (imp);
   16811              : 
   16812         8293 :   imp->mangle (include_partition);
   16813         8293 : }
   16814              : 
   16815              : /* Clean up substitutions.  */
   16816              : void
   16817         7822 : mangle_module_fini ()
   16818              : {
   16819        16366 :   while (substs.length ())
   16820         8544 :     substs.pop ()->subst = 0;
   16821         7822 : }
   16822              : 
   16823              : /* Announce WHAT about the module.  */
   16824              : 
   16825              : void
   16826        12809 : module_state::announce (const char *what) const
   16827              : {
   16828        12809 :   if (noisy_p ())
   16829              :     {
   16830            0 :       fprintf (stderr, " %s:%s", what, get_flatname ());
   16831            0 :       fflush (stderr);
   16832              :     }
   16833        12809 : }
   16834              : 
   16835              : /* A human-readable README section.  The contents of this section to
   16836              :    not contribute to the CRC, so the contents can change per
   16837              :    compilation.  That allows us to embed CWD, hostname, build time and
   16838              :    what not.  It is a STRTAB that may be extracted with:
   16839              :      readelf -pgnu.c++.README $(module).gcm */
   16840              : 
   16841              : void
   16842         2785 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
   16843              : {
   16844         2785 :   bytes_out readme (to);
   16845              : 
   16846         2785 :   readme.begin (false);
   16847              : 
   16848         2785 :   readme.printf ("GNU C++ %s",
   16849         2785 :                  is_header () ? "header unit"
   16850         1876 :                  : !is_partition () ? "primary interface"
   16851          205 :                  : is_interface () ? "interface partition"
   16852              :                  : "internal partition");
   16853              : 
   16854              :   /* Compiler's version.  */
   16855         2785 :   readme.printf ("compiler: %s", version_string);
   16856              : 
   16857              :   /* Module format version.  */
   16858         2785 :   verstr_t string;
   16859         2785 :   version2string (MODULE_VERSION, string);
   16860         2785 :   readme.printf ("version: %s", string);
   16861              : 
   16862              :   /* Module information.  */
   16863         2785 :   readme.printf ("module: %s", get_flatname ());
   16864         2785 :   readme.printf ("source: %s", main_input_filename);
   16865         2785 :   readme.printf ("dialect: %s", dialect);
   16866         2785 :   if (extensions)
   16867           30 :     readme.printf ("extensions: %s%s%s",
   16868              :                    extensions & SE_OPENMP ? "-fopenmp"
   16869            6 :                    : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
   16870              :                    (extensions & SE_OPENACC)
   16871            3 :                    && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
   16872              :                    ? " " : "",
   16873           12 :                    extensions & SE_OPENACC ? "-fopenacc" : "");
   16874              : 
   16875              :   /* The following fields could be expected to change between
   16876              :      otherwise identical compilations.  Consider a distributed build
   16877              :      system.  We should have a way of overriding that.  */
   16878         2785 :   if (char *cwd = getcwd (NULL, 0))
   16879              :     {
   16880         2785 :       readme.printf ("cwd: %s", cwd);
   16881         2785 :       free (cwd);
   16882              :     }
   16883         5570 :   readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
   16884              : #if NETWORKING
   16885              :   {
   16886              :     char hostname[64];
   16887              :     if (!gethostname (hostname, sizeof (hostname)))
   16888              :       readme.printf ("host: %s", hostname);
   16889              :   }
   16890              : #endif
   16891         2785 :   {
   16892              :     /* This of course will change!  */
   16893         2785 :     time_t stampy;
   16894         2785 :     auto kind = cpp_get_date (reader, &stampy);
   16895         2785 :     if (kind != CPP_time_kind::UNKNOWN)
   16896              :       {
   16897         2785 :         struct tm *time;
   16898              : 
   16899         2785 :         time = gmtime (&stampy);
   16900         2785 :         readme.print_time ("build", time, "UTC");
   16901              : 
   16902         2785 :         if (kind == CPP_time_kind::DYNAMIC)
   16903              :           {
   16904         2785 :             time = localtime (&stampy);
   16905         2785 :             readme.print_time ("local", time,
   16906              : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way?  */
   16907              :                                time->tm_zone
   16908              : #else
   16909              :                                ""
   16910              : #endif
   16911              :                                );
   16912              :           }
   16913              :       }
   16914              :   }
   16915              : 
   16916              :   /* Its direct imports.  */
   16917         3465 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16918              :     {
   16919          680 :       module_state *state = (*modules)[ix];
   16920              : 
   16921          680 :       if (state->is_direct ())
   16922          999 :         readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
   16923              :                        state->get_flatname (), state->filename);
   16924              :     }
   16925              : 
   16926         2785 :   readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
   16927         2785 : }
   16928              : 
   16929              : /* Sort environment var names in reverse order.  */
   16930              : 
   16931              : static int
   16932            0 : env_var_cmp (const void *a_, const void *b_)
   16933              : {
   16934            0 :   const unsigned char *a = *(const unsigned char *const *)a_;
   16935            0 :   const unsigned char *b = *(const unsigned char *const *)b_;
   16936              : 
   16937            0 :   for (unsigned ix = 0; ; ix++)
   16938              :     {
   16939            0 :       bool a_end = !a[ix] || a[ix] == '=';
   16940            0 :       if (a[ix] == b[ix])
   16941              :         {
   16942            0 :           if (a_end)
   16943              :             break;
   16944              :         }
   16945              :       else
   16946              :         {
   16947            0 :           bool b_end = !b[ix] || b[ix] == '=';
   16948              : 
   16949            0 :           if (!a_end && !b_end)
   16950            0 :             return a[ix] < b[ix] ? +1 : -1;
   16951            0 :           if (a_end && b_end)
   16952              :             break;
   16953            0 :           return a_end ? +1 : -1;
   16954              :         }
   16955            0 :     }
   16956              : 
   16957              :   return 0;
   16958              : }
   16959              : 
   16960              : /* Write the environment. It is a STRTAB that may be extracted with:
   16961              :      readelf -pgnu.c++.ENV $(module).gcm */
   16962              : 
   16963              : void
   16964            0 : module_state::write_env (elf_out *to)
   16965              : {
   16966            0 :   vec<const char *> vars;
   16967            0 :   vars.create (20);
   16968              : 
   16969            0 :   extern char **environ;
   16970            0 :   while (const char *var = environ[vars.length ()])
   16971            0 :     vars.safe_push (var);
   16972            0 :   vars.qsort (env_var_cmp);
   16973              : 
   16974            0 :   bytes_out env (to);
   16975            0 :   env.begin (false);
   16976            0 :   while (vars.length ())
   16977            0 :     env.printf ("%s", vars.pop ());
   16978            0 :   env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
   16979              : 
   16980            0 :   vars.release ();
   16981            0 : }
   16982              : 
   16983              : /* Write the direct or indirect imports.
   16984              :    u:N
   16985              :    {
   16986              :      u:index
   16987              :      s:name
   16988              :      u32:crc
   16989              :      s:filename (direct)
   16990              :      u:exported (direct)
   16991              :    } imports[N]
   16992              :  */
   16993              : 
   16994              : void
   16995          918 : module_state::write_imports (bytes_out &sec, bool direct)
   16996              : {
   16997          918 :   unsigned count = 0;
   16998              : 
   16999         1940 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   17000              :     {
   17001         1022 :       module_state *imp = (*modules)[ix];
   17002              : 
   17003         1022 :       if (imp->remap && imp->is_direct () == direct)
   17004          490 :         count++;
   17005              :     }
   17006              : 
   17007          918 :   gcc_assert (!direct || count);
   17008              : 
   17009          918 :   sec.u (count);
   17010         2858 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   17011              :     {
   17012         1022 :       module_state *imp = (*modules)[ix];
   17013              : 
   17014         1022 :       if (imp->remap && imp->is_direct () == direct)
   17015              :         {
   17016          652 :           dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
   17017              :                            !direct ? "indirect "
   17018           81 :                            : imp->exported_p ? "exported " : "",
   17019              :                            ix, imp->remap, imp, imp->crc);
   17020          490 :           sec.u (imp->remap);
   17021          490 :           sec.str (imp->get_flatname ());
   17022          490 :           sec.u32 (imp->crc);
   17023          490 :           if (direct)
   17024              :             {
   17025          481 :               write_location (sec, imp->imported_from ());
   17026          481 :               sec.str (imp->filename);
   17027          481 :               int exportedness = 0;
   17028          481 :               if (imp->exported_p)
   17029              :                 exportedness = +1;
   17030          287 :               else if (!imp->is_purview_direct ())
   17031           13 :                 exportedness = -1;
   17032          481 :               sec.i (exportedness);
   17033              :             }
   17034              :         }
   17035              :     }
   17036          918 : }
   17037              : 
   17038              : /* READER, LMAPS  != NULL == direct imports,
   17039              :    == NUL == indirect imports.  */
   17040              : 
   17041              : unsigned
   17042          780 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
   17043              : {
   17044          780 :   unsigned count = sec.u ();
   17045          780 :   unsigned loaded = 0;
   17046              : 
   17047         1972 :   while (count--)
   17048              :     {
   17049          412 :       unsigned ix = sec.u ();
   17050          412 :       if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
   17051              :         {
   17052            0 :           sec.set_overrun ();
   17053            0 :           break;
   17054              :         }
   17055              : 
   17056          412 :       const char *name = sec.str (NULL);
   17057          412 :       module_state *imp = get_module (name);
   17058          412 :       unsigned crc = sec.u32 ();
   17059          412 :       int exportedness = 0;
   17060              : 
   17061              :       /* If the import is a partition, it must be the same primary
   17062              :          module as this TU.  */
   17063          412 :       if (imp && imp->is_partition () &&
   17064              :           (!named_module_p ()
   17065          144 :            || (get_primary (this_module ()) != get_primary (imp))))
   17066              :         imp = NULL;
   17067              : 
   17068          412 :       if (!imp)
   17069            0 :         sec.set_overrun ();
   17070          412 :       if (sec.get_overrun ())
   17071              :         break;
   17072              : 
   17073          412 :       if (lmaps)
   17074              :         {
   17075              :           /* A direct import, maybe load it.  */
   17076          408 :           location_t floc = read_location (sec);
   17077          408 :           const char *fname = sec.str (NULL);
   17078          408 :           exportedness = sec.i ();
   17079              : 
   17080          408 :           if (sec.get_overrun ())
   17081              :             break;
   17082              : 
   17083          408 :           if (!imp->check_circular_import (floc))
   17084            3 :             continue;
   17085              : 
   17086          405 :           if (imp->loadedness == ML_NONE)
   17087              :             {
   17088          318 :               imp->loc = floc;
   17089          318 :               imp->crc = crc;
   17090          318 :               if (!imp->get_flatname ())
   17091          275 :                 imp->set_flatname ();
   17092              : 
   17093          318 :               unsigned n = dump.push (imp);
   17094              : 
   17095          318 :               if (!imp->filename && fname)
   17096          275 :                 imp->filename = xstrdup (fname);
   17097              : 
   17098          318 :               if (imp->is_partition ())
   17099           33 :                 dump () && dump ("Importing elided partition %M", imp);
   17100              : 
   17101          318 :               if (!imp->do_import (reader, false))
   17102            3 :                 imp = NULL;
   17103          318 :               dump.pop (n);
   17104          318 :               if (!imp)
   17105            3 :                 continue;
   17106              :             }
   17107              : 
   17108          402 :           if (is_partition ())
   17109              :             {
   17110           69 :               if (!imp->is_direct () && !imp->is_partition_direct ())
   17111              :                 {
   17112           30 :                   imp->directness = MD_PARTITION_DIRECT;
   17113           30 :                   linemap_module_reparent (line_table, imp->loc, floc);
   17114              :                 }
   17115           69 :               if (exportedness > 0)
   17116            6 :                 imp->exported_p = true;
   17117              :             }
   17118              :         }
   17119              :       else
   17120              :         {
   17121              :           /* An indirect import, find it, it should already be here.  */
   17122            4 :           if (imp->loadedness == ML_NONE)
   17123              :             {
   17124            0 :               error_at (loc, "indirect import %qs is not already loaded", name);
   17125            0 :               continue;
   17126              :             }
   17127              :         }
   17128              : 
   17129          406 :       if (imp->crc != crc)
   17130            0 :         error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
   17131              : 
   17132          406 :       (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
   17133              : 
   17134          406 :       if (lmaps && exportedness >= 0)
   17135          388 :         set_import (imp, bool (exportedness));
   17136          586 :       dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
   17137           90 :                        : exportedness > 0 ? "exported "
   17138           51 :                        : exportedness < 0 ? "gmf" : "", ix, imp,
   17139              :                        imp->mod);
   17140          406 :       loaded++;
   17141              :     }
   17142              : 
   17143          780 :   return loaded;
   17144              : }
   17145              : 
   17146              : /* Write the import table to MOD_SNAME_PFX.imp.  */
   17147              : 
   17148              : void
   17149          459 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
   17150              : {
   17151          537 :   dump () && dump ("Writing imports");
   17152          459 :   dump.indent ();
   17153              : 
   17154          459 :   bytes_out sec (to);
   17155          459 :   sec.begin ();
   17156              : 
   17157          459 :   write_imports (sec, true);
   17158          459 :   write_imports (sec, false);
   17159              : 
   17160          459 :   sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
   17161          459 :   dump.outdent ();
   17162          459 : }
   17163              : 
   17164              : bool
   17165          390 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
   17166              : {
   17167          390 :   bytes_in sec;
   17168              : 
   17169          390 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
   17170              :     return false;
   17171              : 
   17172          477 :   dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
   17173          390 :   dump.indent ();
   17174              : 
   17175              :   /* Read the imports.  */
   17176          390 :   unsigned direct = read_imports (sec, reader, lmaps);
   17177          390 :   unsigned indirect = read_imports (sec, NULL, NULL);
   17178          390 :   if (direct + indirect + 1 != slurp->remap->length ())
   17179            6 :     from ()->set_error (elf::E_BAD_IMPORT);
   17180              : 
   17181          390 :   dump.outdent ();
   17182          390 :   if (!sec.end (from ()))
   17183              :     return false;
   17184              :   return true;
   17185          390 : }
   17186              : 
   17187              : /* We're the primary module interface, but have partitions.  Document
   17188              :    them so that non-partition module implementation units know which
   17189              :    have already been loaded.  */
   17190              : 
   17191              : void
   17192          136 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
   17193              : {
   17194          160 :   dump () && dump ("Writing %u elided partitions", count);
   17195          136 :   dump.indent ();
   17196              : 
   17197          136 :   bytes_out sec (to);
   17198          136 :   sec.begin ();
   17199              : 
   17200          486 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   17201              :     {
   17202          214 :       module_state *imp = (*modules)[ix];
   17203          214 :       if (imp->is_partition ())
   17204              :         {
   17205          229 :           dump () && dump ("Writing elided partition %M (crc=%x)",
   17206              :                            imp, imp->crc);
   17207          190 :           sec.str (imp->get_flatname ());
   17208          190 :           sec.u32 (imp->crc);
   17209          371 :           write_location (sec, imp->is_direct ()
   17210          181 :                           ? imp->imported_from () : UNKNOWN_LOCATION);
   17211          190 :           sec.str (imp->filename);
   17212              :         }
   17213              :     }
   17214              : 
   17215          136 :   sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
   17216          136 :   dump.outdent ();
   17217          136 : }
   17218              : 
   17219              : bool
   17220           27 : module_state::read_partitions (unsigned count)
   17221              : {
   17222           27 :   bytes_in sec;
   17223           27 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
   17224              :     return false;
   17225              : 
   17226           33 :   dump () && dump ("Reading %u elided partitions", count);
   17227           27 :   dump.indent ();
   17228              : 
   17229           66 :   while (count--)
   17230              :     {
   17231           39 :       const char *name = sec.str (NULL);
   17232           39 :       unsigned crc = sec.u32 ();
   17233           39 :       location_t floc = read_location (sec);
   17234           39 :       const char *fname = sec.str (NULL);
   17235              : 
   17236           39 :       if (sec.get_overrun ())
   17237              :         break;
   17238              : 
   17239           48 :       dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
   17240              : 
   17241           39 :       module_state *imp = get_module (name);
   17242           39 :       if (!imp  /* Partition should be ...  */
   17243           39 :           || !imp->is_partition () /* a partition ...  */
   17244           39 :           || imp->loadedness != ML_NONE  /* that is not yet loaded ...  */
   17245           78 :           || get_primary (imp) != this) /* whose primary is this.  */
   17246              :         {
   17247            0 :           sec.set_overrun ();
   17248            0 :           break;
   17249              :         }
   17250              : 
   17251           39 :       if (!imp->has_location ())
   17252           30 :         imp->loc = floc;
   17253           39 :       imp->crc = crc;
   17254           39 :       if (!imp->filename && fname[0])
   17255           30 :         imp->filename = xstrdup (fname);
   17256              :     }
   17257              : 
   17258           27 :   dump.outdent ();
   17259           27 :   if (!sec.end (from ()))
   17260              :     return false;
   17261              :   return true;
   17262           27 : }
   17263              : 
   17264              : /* Data for config reading and writing.  */
   17265              : struct module_state_config {
   17266              :   const char *dialect_str = get_dialect ();
   17267              :   line_map_uint_t ordinary_locs = 0;
   17268              :   line_map_uint_t macro_locs = 0;
   17269              :   unsigned num_imports = 0;
   17270              :   unsigned num_partitions = 0;
   17271              :   unsigned num_entities = 0;
   17272              :   unsigned loc_range_bits = 0;
   17273              :   unsigned active_init = 0;
   17274              : 
   17275        99773 :   static void release ()
   17276              :   {
   17277        99773 :     XDELETEVEC (dialect);
   17278        99773 :     dialect = NULL;
   17279              :   }
   17280              : 
   17281              : private:
   17282              :   static const char *get_dialect ();
   17283              :   static char *dialect;
   17284              : };
   17285              : 
   17286              : char *module_state_config::dialect;
   17287              : 
   17288              : /* Generate a string of the significant compilation options.
   17289              :    Generally assume the user knows what they're doing, in the same way
   17290              :    that object files can be mixed.  */
   17291              : 
   17292              : const char *
   17293         6012 : module_state_config::get_dialect ()
   17294              : {
   17295         6012 :   if (!dialect)
   17296         9514 :     dialect = concat (get_cxx_dialect_name (cxx_dialect),
   17297              :                       /* C++ implies these, only show if disabled.  */
   17298         4757 :                       flag_exceptions ? "" : "/no-exceptions",
   17299         4757 :                       flag_rtti ? "" : "/no-rtti",
   17300         4757 :                       flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
   17301              :                       /* C++ 20 implies concepts and coroutines.  */
   17302         1485 :                       cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
   17303         4757 :                       (cxx_dialect < cxx20 && flag_coroutines
   17304              :                        ? "/coroutines" : ""),
   17305         4757 :                       flag_module_implicit_inline ? "/implicit-inline" : "",
   17306         4757 :                       flag_contracts ? "/contracts" : "",
   17307         4757 :                       flag_reflection ? "/reflection" : "",
   17308              :                       NULL);
   17309              : 
   17310         6012 :   return dialect;
   17311              : }
   17312              : 
   17313              : /* Contents of a cluster.  */
   17314              : enum cluster_tag {
   17315              :   ct_decl,      /* A decl.  */
   17316              :   ct_defn,      /* A definition.  */
   17317              :   ct_bind,      /* A binding.  */
   17318              :   ct_hwm
   17319              : };
   17320              : 
   17321              : /* Binding modifiers.  */
   17322              : enum ct_bind_flags
   17323              : {
   17324              :   cbf_export = 0x1,     /* An exported decl.  */
   17325              :   cbf_hidden = 0x2,     /* A hidden (friend) decl.  */
   17326              :   cbf_using = 0x4,      /* A using decl.  */
   17327              :   cbf_internal = 0x8,   /* A TU-local decl.  */
   17328              : };
   17329              : 
   17330              : /* DEP belongs to a different cluster, seed it to prevent
   17331              :    unfortunately timed duplicate import.  */
   17332              : // FIXME: QOI For inter-cluster references we could just only pick
   17333              : // one entity from an earlier cluster.  Even better track
   17334              : // dependencies between earlier clusters
   17335              : 
   17336              : void
   17337      9387812 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
   17338              : {
   17339      9387812 :   if (dep->is_tu_local ())
   17340              :     /* We only stream placeholders for TU-local entities anyway.  */;
   17341      9387603 :   else if (dep->is_import () || dep->cluster < index_hwm)
   17342              :     {
   17343      4107841 :       tree ent = dep->get_entity ();
   17344      4107841 :       if (!TREE_VISITED (ent))
   17345              :         {
   17346      1751013 :           sec.tree_node (ent);
   17347      1751463 :           dump (dumper::CLUSTER)
   17348          450 :             && dump ("Seeded %s %N",
   17349          450 :                      dep->is_import () ? "import" : "intercluster", ent);
   17350              :         }
   17351              :     }
   17352      9387812 : }
   17353              : 
   17354              : /* Write the cluster of depsets in SCC[0-SIZE).
   17355              :    dep->section -> section number
   17356              :    dep->cluster -> entity number
   17357              :  */
   17358              : 
   17359              : unsigned
   17360       327468 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
   17361              :                              depset::hash &table, unsigned *counts,
   17362              :                              unsigned *crc_ptr)
   17363              : {
   17364       329002 :   dump () && dump ("Writing section:%u %u depsets", table.section, size);
   17365       327468 :   dump.indent ();
   17366              : 
   17367       327468 :   trees_out sec (to, this, table, table.section);
   17368       327468 :   sec.begin ();
   17369       327468 :   unsigned index_lwm = counts[MSC_entities];
   17370              : 
   17371              :   /* Determine entity numbers, mark for writing.   */
   17372       327777 :   dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
   17373      1798752 :   for (unsigned ix = 0; ix != size; ix++)
   17374              :     {
   17375      1471284 :       depset *b = scc[ix];
   17376              : 
   17377      1471284 :       switch (b->get_entity_kind ())
   17378              :         {
   17379            0 :         default:
   17380            0 :           gcc_unreachable ();
   17381              : 
   17382       162262 :         case depset::EK_BINDING:
   17383       162262 :           {
   17384       162262 :             dump (dumper::CLUSTER)
   17385          210 :               && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
   17386              :                        b->get_entity (), b->get_name ());
   17387       162262 :             depset *ns_dep = b->deps[0];
   17388       162262 :             gcc_checking_assert (ns_dep->get_entity_kind ()
   17389              :                                  == depset::EK_NAMESPACE
   17390              :                                  && ns_dep->get_entity () == b->get_entity ());
   17391       389710 :             for (unsigned jx = b->deps.length (); --jx;)
   17392              :               {
   17393       227448 :                 depset *dep = b->deps[jx];
   17394              :                 // We could be declaring something that is also a
   17395              :                 // (merged) import
   17396       270340 :                 gcc_checking_assert (dep->is_import ()
   17397              :                                      || TREE_VISITED (dep->get_entity ())
   17398              :                                      || (dep->get_entity_kind ()
   17399              :                                          == depset::EK_USING)
   17400              :                                      || (dep->get_entity_kind ()
   17401              :                                          == depset::EK_TU_LOCAL));
   17402              :               }
   17403              :           }
   17404              :           break;
   17405              : 
   17406      1266178 :         case depset::EK_DECL:
   17407      1266178 :         case depset::EK_SPECIALIZATION:
   17408      1266178 :         case depset::EK_PARTIAL:
   17409      1266178 :           b->cluster = counts[MSC_entities]++;
   17410      1266178 :           sec.mark_declaration (b->get_entity (), b->has_defn ());
   17411              :           /* FALLTHROUGH  */
   17412              : 
   17413      1309022 :         case depset::EK_USING:
   17414      1309022 :         case depset::EK_TU_LOCAL:
   17415      2618044 :           gcc_checking_assert (!b->is_import ()
   17416              :                                && !b->is_unreached ());
   17417      1474458 :           dump (dumper::CLUSTER)
   17418         1161 :             && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
   17419          729 :                      b->has_defn () ? "definition" : "declaration",
   17420              :                      b->get_entity ());
   17421              :           break;
   17422              :         }
   17423              :     }
   17424       327777 :   dump (dumper::CLUSTER) && (dump.outdent (), true);
   17425              : 
   17426              :   /* Ensure every out-of-cluster decl is referenced before we start
   17427              :      streaming.  We must do both imports *and* earlier clusters,
   17428              :      because the latter could reach into the former and cause a
   17429              :      duplicate loop.   */
   17430       327468 :   sec.set_importing (+1);
   17431      1798752 :   for (unsigned ix = 0; ix != size; ix++)
   17432              :     {
   17433      1471284 :       depset *b = scc[ix];
   17434     18717018 :       for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
   17435              :         {
   17436      7891416 :           depset *dep = b->deps[jx];
   17437              : 
   17438      7891416 :           if (dep->is_binding ())
   17439              :             {
   17440      1951196 :               for (unsigned ix = dep->deps.length (); --ix;)
   17441              :                 {
   17442      1496396 :                   depset *bind = dep->deps[ix];
   17443      1496396 :                   if (bind->get_entity_kind () == depset::EK_USING)
   17444       542380 :                     bind = bind->deps[1];
   17445              : 
   17446      1496396 :                   intercluster_seed (sec, index_lwm, bind);
   17447              :                 }
   17448              :               /* Also check the namespace itself.  */
   17449       227400 :               dep = dep->deps[0];
   17450              :             }
   17451              : 
   17452      7891416 :           intercluster_seed (sec, index_lwm, dep);
   17453              :         }
   17454              :     }
   17455       327468 :   sec.tree_node (NULL_TREE);
   17456              :   /* We're done importing now.  */
   17457       327468 :   sec.set_importing (-1);
   17458              : 
   17459              :   /* Write non-definitions.  */
   17460      1798752 :   for (unsigned ix = 0; ix != size; ix++)
   17461              :     {
   17462      1471284 :       depset *b = scc[ix];
   17463      1471284 :       tree decl = b->get_entity ();
   17464      1471284 :       switch (b->get_entity_kind ())
   17465              :         {
   17466            0 :         default:
   17467            0 :           gcc_unreachable ();
   17468       162262 :           break;
   17469              : 
   17470       162262 :         case depset::EK_BINDING:
   17471       162262 :           {
   17472       162262 :             gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
   17473       163460 :             dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
   17474              :                              decl, b->get_name ());
   17475       162262 :             sec.u (ct_bind);
   17476       162262 :             sec.tree_node (decl);
   17477       162262 :             sec.tree_node (b->get_name ());
   17478              : 
   17479              :             /* Write in reverse order, so reading will see the exports
   17480              :                first, thus building the overload chain will be
   17481              :                optimized.  */
   17482       551972 :             for (unsigned jx = b->deps.length (); --jx;)
   17483              :               {
   17484       227448 :                 depset *dep = b->deps[jx];
   17485       227448 :                 tree bound = dep->get_entity ();
   17486       227448 :                 unsigned flags = 0;
   17487       227448 :                 if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
   17488              :                   flags |= cbf_internal;
   17489       227400 :                 else if (dep->get_entity_kind () == depset::EK_USING)
   17490              :                   {
   17491        42844 :                     tree ovl = bound;
   17492        42844 :                     bound = OVL_FUNCTION (bound);
   17493        42844 :                     if (!(TREE_CODE (bound) == CONST_DECL
   17494         5011 :                           && UNSCOPED_ENUM_P (TREE_TYPE (bound))
   17495         4169 :                           && decl == TYPE_NAME (TREE_TYPE (bound))))
   17496              :                       /* An unscoped enumerator in its enumeration's
   17497              :                          scope is not a using.  */
   17498              :                       flags |= cbf_using;
   17499        42844 :                     if (OVL_EXPORT_P (ovl))
   17500        41702 :                       flags |= cbf_export;
   17501              :                   }
   17502              :                 else
   17503              :                   {
   17504              :                     /* An implicit typedef must be at one.  */
   17505       184556 :                     gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
   17506       184556 :                     if (dep->is_hidden ())
   17507              :                       flags |= cbf_hidden;
   17508       184467 :                     else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
   17509       143392 :                       flags |= cbf_export;
   17510              :                   }
   17511              : 
   17512       227448 :                 gcc_checking_assert (DECL_P (bound));
   17513              : 
   17514       227448 :                 sec.i (flags);
   17515       227448 :                 if (flags & cbf_internal)
   17516              :                   {
   17517           48 :                     sec.tree_node (name_for_tu_local_decl (bound));
   17518           48 :                     write_location (sec, DECL_SOURCE_LOCATION (bound));
   17519              :                   }
   17520              :                 else
   17521       227400 :                   sec.tree_node (bound);
   17522              :               }
   17523              : 
   17524              :             /* Terminate the list.  */
   17525       162262 :             sec.i (-1);
   17526              :           }
   17527       162262 :           break;
   17528              : 
   17529        42844 :         case depset::EK_USING:
   17530        42844 :         case depset::EK_TU_LOCAL:
   17531        42871 :           dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
   17532           27 :                            TREE_CODE (decl), decl);
   17533              :           break;
   17534              : 
   17535      1266178 :         case depset::EK_SPECIALIZATION:
   17536      1266178 :         case depset::EK_PARTIAL:
   17537      1266178 :         case depset::EK_DECL:
   17538      1269325 :           dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
   17539              :                            b->entity_kind_name (), b->cluster,
   17540         3147 :                            TREE_CODE (decl), decl);
   17541              : 
   17542      1266178 :           sec.u (ct_decl);
   17543      1266178 :           sec.tree_node (decl);
   17544              : 
   17545      1474431 :           dump () && dump ("Wrote declaration entity:%u %C:%N",
   17546         3147 :                            b->cluster, TREE_CODE (decl), decl);
   17547              :           break;
   17548              :         }
   17549              :     }
   17550              : 
   17551              :   depset *namer = NULL;
   17552              : 
   17553              :   /* Write out definitions  */
   17554      1798752 :   for (unsigned ix = 0; ix != size; ix++)
   17555              :     {
   17556      1471284 :       depset *b = scc[ix];
   17557      1471284 :       tree decl = b->get_entity ();
   17558      1471284 :       switch (b->get_entity_kind ())
   17559              :         {
   17560              :         default:
   17561              :           break;
   17562              : 
   17563      1266178 :         case depset::EK_SPECIALIZATION:
   17564      1266178 :         case depset::EK_PARTIAL:
   17565      1266178 :         case depset::EK_DECL:
   17566      1266178 :           if (!namer)
   17567       311464 :             namer = b;
   17568              : 
   17569      1266178 :           if (b->has_defn ())
   17570              :             {
   17571       488358 :               sec.u (ct_defn);
   17572       488358 :               sec.tree_node (decl);
   17573       489301 :               dump () && dump ("Writing definition %N", decl);
   17574       488358 :               sec.write_definition (decl, b->refs_tu_local ());
   17575              : 
   17576       488358 :               if (!namer->has_defn ())
   17577      1471284 :                 namer = b;
   17578              :             }
   17579              :           break;
   17580              :         }
   17581              :     }
   17582              : 
   17583              :   /* We don't find the section by name.  Use depset's decl's name for
   17584              :      human friendliness.  */
   17585       327468 :   unsigned name = 0;
   17586       327468 :   tree naming_decl = NULL_TREE;
   17587       327468 :   if (namer)
   17588              :     {
   17589       311464 :       naming_decl = namer->get_entity ();
   17590       311464 :       if (namer->get_entity_kind () == depset::EK_USING)
   17591              :         /* This unfortunately names the section from the target of the
   17592              :            using decl.  But the name is only a guide, so Do Not Care.  */
   17593            0 :         naming_decl = OVL_FUNCTION (naming_decl);
   17594       311464 :       if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
   17595              :         /* Lose any anonymousness.  */
   17596       110170 :         naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
   17597       311464 :       name = to->qualified_name (naming_decl, namer->has_defn ());
   17598              :     }
   17599              : 
   17600       327468 :   unsigned bytes = sec.pos;
   17601       327468 :   unsigned snum = sec.end (to, name, crc_ptr);
   17602              : 
   17603      2126220 :   for (unsigned ix = size; ix--;)
   17604      1471284 :     gcc_checking_assert (scc[ix]->section == snum);
   17605              : 
   17606       327468 :   dump.outdent ();
   17607       329002 :   dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
   17608              : 
   17609       327468 :   return bytes;
   17610       327468 : }
   17611              : 
   17612              : /* Read a cluster from section SNUM.  */
   17613              : 
   17614              : bool
   17615       213248 : module_state::read_cluster (unsigned snum)
   17616              : {
   17617       213248 :   trees_in sec (this);
   17618              : 
   17619       213248 :   if (!sec.begin (loc, from (), snum))
   17620              :     return false;
   17621              : 
   17622       214566 :   dump () && dump ("Reading section:%u", snum);
   17623       213248 :   dump.indent ();
   17624              : 
   17625              :   /* We care about structural equality.  */
   17626       213248 :   comparing_dependent_aliases++;
   17627              : 
   17628              :   /* First seed the imports.  */
   17629      1317910 :   while (tree import = sec.tree_node ())
   17630      1106287 :     dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
   17631              : 
   17632      1568231 :   while (!sec.get_overrun () && sec.more_p ())
   17633              :     {
   17634      1354983 :       unsigned ct = sec.u ();
   17635      1354983 :       switch (ct)
   17636              :         {
   17637            0 :         default:
   17638            0 :           sec.set_overrun ();
   17639            0 :           break;
   17640              : 
   17641       110359 :         case ct_bind:
   17642              :           /* A set of namespace bindings.  */
   17643       110359 :           {
   17644       110359 :             tree ns = sec.tree_node ();
   17645       110359 :             tree name = sec.tree_node ();
   17646       110359 :             tree decls = NULL_TREE;
   17647       110359 :             tree visible = NULL_TREE;
   17648       110359 :             tree internal = NULL_TREE;
   17649       110359 :             tree type = NULL_TREE;
   17650       110359 :             bool dedup = false;
   17651       110359 :             bool global_p = is_header ();
   17652              : 
   17653              :             /* We rely on the bindings being in the reverse order of
   17654              :                the resulting overload set.  */
   17655       259054 :             for (;;)
   17656              :               {
   17657       259054 :                 int flags = sec.i ();
   17658       259054 :                 if (flags < 0)
   17659              :                   break;
   17660              : 
   17661       148695 :                 if ((flags & cbf_hidden)
   17662           65 :                     && (flags & (cbf_using | cbf_export)))
   17663            0 :                   sec.set_overrun ();
   17664       148695 :                 if ((flags & cbf_internal)
   17665           15 :                     && flags != cbf_internal)
   17666            0 :                   sec.set_overrun ();
   17667              : 
   17668            0 :                 if (flags & cbf_internal)
   17669              :                   {
   17670           15 :                     tree name = sec.tree_node ();
   17671           15 :                     location_t loc = read_location (sec);
   17672           15 :                     if (sec.get_overrun ())
   17673              :                       break;
   17674              : 
   17675           15 :                     tree decl = make_node (TU_LOCAL_ENTITY);
   17676           15 :                     TU_LOCAL_ENTITY_NAME (decl) = name;
   17677           15 :                     TU_LOCAL_ENTITY_LOCATION (decl) = loc;
   17678           15 :                     internal = tree_cons (NULL_TREE, decl, internal);
   17679           15 :                     continue;
   17680           15 :                   }
   17681              : 
   17682       148680 :                 tree decl = sec.tree_node ();
   17683       148680 :                 if (sec.get_overrun ())
   17684              :                   break;
   17685              : 
   17686       148680 :                 if (!global_p)
   17687              :                   {
   17688              :                     /* Check if the decl could require GM merging.  */
   17689         8942 :                     tree orig = get_originating_module_decl (decl);
   17690         8942 :                     tree inner = STRIP_TEMPLATE (orig);
   17691         8942 :                     if (!DECL_LANG_SPECIFIC (inner)
   17692        17884 :                         || !DECL_MODULE_ATTACH_P (inner))
   17693              :                       global_p = true;
   17694              :                   }
   17695              : 
   17696       148680 :                 if (decls && TREE_CODE (decl) == TYPE_DECL)
   17697              :                   {
   17698              :                     /* Stat hack.  */
   17699           54 :                     if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
   17700            0 :                       sec.set_overrun ();
   17701              : 
   17702           54 :                     if (flags & cbf_using)
   17703              :                       {
   17704            3 :                         type = build_lang_decl_loc (UNKNOWN_LOCATION,
   17705              :                                                     USING_DECL,
   17706            3 :                                                     DECL_NAME (decl),
   17707              :                                                     NULL_TREE);
   17708            3 :                         USING_DECL_DECLS (type) = decl;
   17709            3 :                         USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
   17710            3 :                         DECL_CONTEXT (type) = ns;
   17711              : 
   17712            3 :                         DECL_MODULE_PURVIEW_P (type) = true;
   17713            3 :                         if (flags & cbf_export)
   17714            3 :                           DECL_MODULE_EXPORT_P (type) = true;
   17715              :                       }
   17716              :                     else
   17717              :                       type = decl;
   17718              :                   }
   17719              :                 else
   17720              :                   {
   17721       148626 :                     if ((flags & cbf_using) &&
   17722        17245 :                         !DECL_DECLARES_FUNCTION_P (decl))
   17723              :                       {
   17724              :                         /* We should only see a single non-function using-decl
   17725              :                            for a binding; more than that would clash.  */
   17726         4808 :                         if (decls)
   17727            0 :                           sec.set_overrun ();
   17728              : 
   17729              :                         /* FIXME: Propagate the location of the using-decl
   17730              :                            for use in diagnostics.  */
   17731         4808 :                         decls = build_lang_decl_loc (UNKNOWN_LOCATION,
   17732              :                                                      USING_DECL,
   17733         4808 :                                                      DECL_NAME (decl),
   17734              :                                                      NULL_TREE);
   17735         4808 :                         USING_DECL_DECLS (decls) = decl;
   17736              :                         /* We don't currently record the actual scope of the
   17737              :                            using-declaration, but this approximation should
   17738              :                            generally be good enough.  */
   17739         4808 :                         USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
   17740         4808 :                         DECL_CONTEXT (decls) = ns;
   17741              : 
   17742         4808 :                         DECL_MODULE_PURVIEW_P (decls) = true;
   17743         4808 :                         if (flags & cbf_export)
   17744         4796 :                           DECL_MODULE_EXPORT_P (decls) = true;
   17745              :                       }
   17746       143818 :                     else if (decls
   17747       105536 :                              || (flags & (cbf_hidden | cbf_using))
   17748       243010 :                              || DECL_FUNCTION_TEMPLATE_P (decl))
   17749              :                       {
   17750        60764 :                         decls = ovl_make (decl, decls);
   17751        60764 :                         if (flags & cbf_using)
   17752              :                           {
   17753        12437 :                             dedup = true;
   17754        12437 :                             OVL_USING_P (decls) = true;
   17755        12437 :                             OVL_PURVIEW_P (decls) = true;
   17756        12437 :                             if (flags & cbf_export)
   17757        12422 :                               OVL_EXPORT_P (decls) = true;
   17758              :                           }
   17759              : 
   17760        60764 :                         if (flags & cbf_hidden)
   17761           65 :                           OVL_HIDDEN_P (decls) = true;
   17762        60699 :                         else if (dedup)
   17763        15527 :                           OVL_DEDUP_P (decls) = true;
   17764              :                       }
   17765              :                     else
   17766              :                       decls = decl;
   17767              : 
   17768       148614 :                     if (flags & cbf_export
   17769       148626 :                         || (!(flags & cbf_hidden)
   17770         8907 :                             && (is_module () || is_partition ())))
   17771       148695 :                       visible = decls;
   17772              :                   }
   17773              :               }
   17774              : 
   17775       110359 :             if (!decls && !internal)
   17776            0 :               sec.set_overrun ();
   17777              : 
   17778       110359 :             if (sec.get_overrun ())
   17779              :               break; /* Bail.  */
   17780              : 
   17781       111271 :             dump () && dump ("Binding of %P", ns, name);
   17782       110359 :             if (!set_module_binding (ns, name, mod, global_p,
   17783       110359 :                                      is_module () || is_partition (),
   17784              :                                      decls, type, visible, internal))
   17785            0 :               sec.set_overrun ();
   17786              :           }
   17787              :           break;
   17788              : 
   17789       898099 :         case ct_decl:
   17790              :           /* A decl.  */
   17791       898099 :           {
   17792       898099 :             tree decl = sec.tree_node ();
   17793      1358791 :             dump () && dump ("Read declaration of %N", decl);
   17794              :           }
   17795              :           break;
   17796              : 
   17797       346525 :         case ct_defn:
   17798       346525 :           {
   17799       346525 :             tree decl = sec.tree_node ();
   17800       347865 :             dump () && dump ("Reading definition of %N", decl);
   17801       346525 :             sec.read_definition (decl);
   17802              :           }
   17803       346525 :           break;
   17804              :         }
   17805              :     }
   17806              : 
   17807              :   /* When lazy loading is in effect, we can be in the middle of
   17808              :      parsing or instantiating a function.  Save it away.
   17809              :      push_function_context does too much work.   */
   17810       213248 :   tree old_cfd = current_function_decl;
   17811       213248 :   struct function *old_cfun = cfun;
   17812       403343 :   for (const post_process_data& pdata : sec.post_process ())
   17813              :     {
   17814       144809 :       tree decl = pdata.decl;
   17815              : 
   17816       144809 :       bool abstract = false;
   17817       144809 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   17818              :         {
   17819        90665 :           abstract = true;
   17820        90665 :           decl = DECL_TEMPLATE_RESULT (decl);
   17821              :         }
   17822              : 
   17823       144809 :       current_function_decl = decl;
   17824       144809 :       allocate_struct_function (decl, abstract);
   17825       144809 :       cfun->language = ggc_cleared_alloc<language_function> ();
   17826       144809 :       cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
   17827       144809 :       cfun->function_start_locus = pdata.start_locus;
   17828       144809 :       cfun->function_end_locus = pdata.end_locus;
   17829       144809 :       cfun->language->returns_value = pdata.returns_value;
   17830       144809 :       cfun->language->returns_null = pdata.returns_null;
   17831       144809 :       cfun->language->returns_abnormally = pdata.returns_abnormally;
   17832       144809 :       cfun->language->infinite_loop = pdata.infinite_loop;
   17833       144809 :       cfun->coroutine_component = DECL_COROUTINE_P (decl);
   17834              : 
   17835              :       /* Make sure we emit explicit instantiations.
   17836              :          FIXME do we want to do this in expand_or_defer_fn instead?  */
   17837       144809 :       if (DECL_EXPLICIT_INSTANTIATION (decl)
   17838       144809 :           && !DECL_EXTERNAL (decl))
   17839           27 :         setup_explicit_instantiation_definition_linkage (decl);
   17840              : 
   17841       144809 :       if (abstract)
   17842              :         ;
   17843        54144 :       else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
   17844         9116 :         vec_safe_push (post_load_decls, decl);
   17845              :       else
   17846              :         {
   17847        45028 :           bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
   17848              : #ifdef PCC_STATIC_STRUCT_RETURN
   17849              :           cfun->returns_pcc_struct = aggr;
   17850              : #endif
   17851        45028 :           cfun->returns_struct = aggr;
   17852        45028 :           expand_or_defer_fn (decl);
   17853              : 
   17854              :           /* If we first see this function after at_eof, it doesn't get
   17855              :              note_vague_linkage_fn from tentative_decl_linkage, so the loop in
   17856              :              c_parse_final_cleanups won't consider it.  But with DECL_COMDAT we
   17857              :              can just clear DECL_EXTERNAL and let cgraph decide.
   17858              :              FIXME handle this outside module.cc after GCC 15.  */
   17859         4910 :           if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   17860        47006 :               && DECL_NOT_REALLY_EXTERN (decl))
   17861         1935 :             DECL_EXTERNAL (decl) = false;
   17862              :         }
   17863              : 
   17864              :     }
   17865       213324 :   for (const tree& type : sec.post_process_type ())
   17866              :     {
   17867              :       /* Attempt to complete an array type now in case its element type
   17868              :          had a definition streamed later in the cluster.  */
   17869           36 :       gcc_checking_assert (TREE_CODE (type) == ARRAY_TYPE);
   17870           36 :       complete_type (type);
   17871              :     }
   17872       213248 :   set_cfun (old_cfun);
   17873       213248 :   current_function_decl = old_cfd;
   17874       213248 :   comparing_dependent_aliases--;
   17875              : 
   17876       213248 :   dump.outdent ();
   17877       214566 :   dump () && dump ("Read section:%u", snum);
   17878              : 
   17879       213248 :   loaded_clusters++;
   17880              : 
   17881       213248 :   if (!sec.end (from ()))
   17882              :     return false;
   17883              : 
   17884              :   return true;
   17885       213248 : }
   17886              : 
   17887              : void
   17888       165282 : module_state::write_namespace (bytes_out &sec, depset *dep)
   17889              : {
   17890       165282 :   unsigned ns_num = dep->cluster;
   17891       165282 :   unsigned ns_import = 0;
   17892              : 
   17893       165282 :   if (dep->is_import ())
   17894            0 :     ns_import = dep->section;
   17895       165282 :   else if (dep->get_entity () != global_namespace)
   17896       109513 :     ns_num++;
   17897              : 
   17898       165282 :   sec.u (ns_import);
   17899       165282 :   sec.u (ns_num);
   17900       165282 : }
   17901              : 
   17902              : tree
   17903       199290 : module_state::read_namespace (bytes_in &sec)
   17904              : {
   17905       199290 :   unsigned ns_import = sec.u ();
   17906       199290 :   unsigned ns_num = sec.u ();
   17907       199290 :   tree ns = NULL_TREE;
   17908              : 
   17909       199290 :   if (ns_import || ns_num)
   17910              :     {
   17911       127451 :       if (!ns_import)
   17912       127451 :         ns_num--;
   17913              : 
   17914       127451 :       if (unsigned origin = slurp->remap_module (ns_import))
   17915              :         {
   17916       127451 :           module_state *from = (*modules)[origin];
   17917       127451 :           if (ns_num < from->entity_num)
   17918              :             {
   17919       127451 :               binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
   17920              : 
   17921       127451 :               if (!slot.is_lazy ())
   17922       127451 :                 ns = slot;
   17923              :             }
   17924              :         }
   17925              :       else
   17926            0 :         sec.set_overrun ();
   17927              :     }
   17928              :   else
   17929        71839 :     ns = global_namespace;
   17930              : 
   17931       199290 :   return ns;
   17932              : }
   17933              : 
   17934              : /* SPACES is a sorted vector of namespaces.  Write out the namespaces
   17935              :    to MOD_SNAME_PFX.nms section.   */
   17936              : 
   17937              : void
   17938          594 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
   17939              :                                 unsigned num, unsigned *crc_p)
   17940              : {
   17941          652 :   dump () && dump ("Writing namespaces");
   17942          594 :   dump.indent ();
   17943              : 
   17944          594 :   bytes_out sec (to);
   17945          594 :   sec.begin ();
   17946              : 
   17947         3886 :   for (unsigned ix = 0; ix != num; ix++)
   17948              :     {
   17949         2698 :       depset *b = spaces[ix];
   17950         2698 :       tree ns = b->get_entity ();
   17951              : 
   17952              :       /* This could be an anonymous namespace even for a named module,
   17953              :          since we can still emit no-linkage decls.  */
   17954         2698 :       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
   17955              : 
   17956         2698 :       unsigned flags = 0;
   17957         2698 :       if (TREE_PUBLIC (ns))
   17958         2637 :         flags |= 1;
   17959         2698 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17960          515 :         flags |= 2;
   17961         2698 :       if (DECL_MODULE_PURVIEW_P (ns))
   17962         2089 :         flags |= 4;
   17963         2698 :       if (DECL_MODULE_EXPORT_P (ns))
   17964         1866 :         flags |= 8;
   17965         2698 :       if (TREE_DEPRECATED (ns))
   17966           18 :         flags |= 16;
   17967              : 
   17968         2838 :       dump () && dump ("Writing namespace:%u %N%s%s%s%s",
   17969              :                        b->cluster, ns,
   17970          140 :                        flags & 1 ? ", public" : "",
   17971          140 :                        flags & 2 ? ", inline" : "",
   17972          140 :                        flags & 4 ? ", purview" : "",
   17973          140 :                        flags & 8 ? ", export" : "",
   17974          140 :                        flags & 16 ? ", deprecated" : "");
   17975         2698 :       sec.u (b->cluster);
   17976         2698 :       sec.u (to->name (DECL_NAME (ns)));
   17977         2698 :       write_namespace (sec, b->deps[0]);
   17978              : 
   17979         2698 :       sec.u (flags);
   17980         2698 :       write_location (sec, DECL_SOURCE_LOCATION (ns));
   17981              : 
   17982         2698 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17983              :         {
   17984          515 :           if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
   17985              :             {
   17986          129 :               tree tags = TREE_VALUE (attr);
   17987          129 :               sec.u (list_length (tags));
   17988          390 :               for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
   17989          132 :                 sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
   17990              :             }
   17991              :           else
   17992          386 :             sec.u (0);
   17993              :         }
   17994              :     }
   17995              : 
   17996          594 :   sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
   17997          594 :   dump.outdent ();
   17998          594 : }
   17999              : 
   18000              : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace.  Fill in
   18001              :    SPACES from that data.  */
   18002              : 
   18003              : bool
   18004          612 : module_state::read_namespaces (unsigned num)
   18005              : {
   18006          612 :   bytes_in sec;
   18007              : 
   18008          612 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
   18009              :     return false;
   18010              : 
   18011          685 :   dump () && dump ("Reading namespaces");
   18012          612 :   dump.indent ();
   18013              : 
   18014         3520 :   for (unsigned ix = 0; ix != num; ix++)
   18015              :     {
   18016         2908 :       unsigned entity_index = sec.u ();
   18017         2908 :       unsigned name = sec.u ();
   18018              : 
   18019         2908 :       tree parent = read_namespace (sec);
   18020              : 
   18021              :       /* See comment in write_namespace about why not bits.  */
   18022         2908 :       unsigned flags = sec.u ();
   18023         2908 :       location_t src_loc = read_location (sec);
   18024         2908 :       unsigned tags_count = (flags & 2) ? sec.u () : 0;
   18025              : 
   18026         2908 :       if (entity_index >= entity_num
   18027         2908 :           || !parent
   18028         2908 :           || (flags & 0xc) == 0x8)
   18029            0 :         sec.set_overrun ();
   18030              : 
   18031         2908 :       tree tags = NULL_TREE;
   18032         3052 :       while (tags_count--)
   18033              :         {
   18034          144 :           size_t len;
   18035          144 :           const char *str = sec.str (&len);
   18036          144 :           tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
   18037          144 :           tags = nreverse (tags);
   18038              :         }
   18039              : 
   18040         2908 :       if (sec.get_overrun ())
   18041              :         break;
   18042              : 
   18043         5752 :       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
   18044              : 
   18045         3128 :       dump () && dump ("Read namespace:%u %P%s%s%s%s",
   18046              :                        entity_index, parent, id,
   18047          110 :                        flags & 1 ? ", public" : "",
   18048              :                        flags & 2 ? ", inline" : "",
   18049          110 :                        flags & 4 ? ", purview" : "",
   18050          110 :                        flags & 8 ? ", export" : "",
   18051          110 :                        flags & 16 ? ", deprecated" : "");
   18052         2908 :       bool visible_p = ((flags & 8)
   18053         2908 :                         || ((flags & 1)
   18054          543 :                             && (flags & 4)
   18055          101 :                             && (is_partition () || is_module ())));
   18056         2908 :       tree inner = add_imported_namespace (parent, id, src_loc, mod,
   18057              :                                            bool (flags & 2), visible_p);
   18058         2908 :       if (!inner)
   18059              :         {
   18060            0 :           sec.set_overrun ();
   18061            0 :           break;
   18062              :         }
   18063              : 
   18064         2908 :       if (is_partition ())
   18065              :         {
   18066           54 :           if (flags & 4)
   18067           48 :             DECL_MODULE_PURVIEW_P (inner) = true;
   18068           54 :           if (flags & 8)
   18069           27 :             DECL_MODULE_EXPORT_P (inner) = true;
   18070              :         }
   18071              : 
   18072         2908 :       if (flags & 16)
   18073           19 :         TREE_DEPRECATED (inner) = true;
   18074              : 
   18075         2908 :       if (tags)
   18076          141 :         DECL_ATTRIBUTES (inner)
   18077          282 :           = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
   18078              : 
   18079              :       /* Install the namespace.  */
   18080         2908 :       (*entity_ary)[entity_lwm + entity_index] = inner;
   18081         2908 :       if (DECL_MODULE_IMPORT_P (inner))
   18082              :         {
   18083            0 :           bool existed;
   18084            0 :           unsigned *slot = &entity_map->get_or_insert
   18085            0 :             (DECL_UID (inner), &existed);
   18086            0 :           if (existed)
   18087              :             /* If it existed, it should match.  */
   18088            0 :             gcc_checking_assert (inner == (*entity_ary)[*slot]);
   18089              :           else
   18090            0 :             *slot = entity_lwm + entity_index;
   18091              :         }
   18092              :     }
   18093              : 
   18094          612 :   dump.outdent ();
   18095          612 :   if (!sec.end (from ()))
   18096              :     return false;
   18097              :   return true;
   18098          612 : }
   18099              : 
   18100              : unsigned
   18101          594 : module_state::write_using_directives (elf_out *to, depset::hash &table,
   18102              :                                       vec<depset *> spaces, unsigned *crc_p)
   18103              : {
   18104          652 :   dump () && dump ("Writing using-directives");
   18105          594 :   dump.indent ();
   18106              : 
   18107          594 :   bytes_out sec (to);
   18108          594 :   sec.begin ();
   18109              : 
   18110          594 :   unsigned num = 0;
   18111         3886 :   auto emit_one_ns = [&](depset *parent_dep)
   18112              :     {
   18113         3292 :       tree parent = parent_dep->get_entity ();
   18114         3835 :       for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
   18115              :         {
   18116          185 :           if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
   18117           18 :             continue;
   18118          167 :           bool exported = DECL_MODULE_EXPORT_P (udir);
   18119          167 :           tree target = USING_DECL_DECLS (udir);
   18120          167 :           depset *target_dep = table.find_dependency (target);
   18121              : 
   18122              :           /* An using-directive imported from a different module might not
   18123              :              have been walked earlier (PR c++/122915).  But importers will
   18124              :              be able to just refer to the decl in that module unless it was
   18125              :              a partition anyway, so we don't have anything to do here.  */
   18126          167 :           if (!target_dep)
   18127              :             {
   18128            6 :               gcc_checking_assert (DECL_MODULE_IMPORT_P (udir));
   18129            6 :               continue;
   18130              :             }
   18131              : 
   18132          185 :           dump () && dump ("Writing using-directive in %N for %N",
   18133              :                            parent, target);
   18134          161 :           sec.u (exported);
   18135          161 :           write_namespace (sec, parent_dep);
   18136          161 :           write_namespace (sec, target_dep);
   18137          161 :           ++num;
   18138              :         }
   18139         3886 :     };
   18140              : 
   18141          594 :   emit_one_ns (table.find_dependency (global_namespace));
   18142         4480 :   for (depset *parent_dep : spaces)
   18143         2698 :     emit_one_ns (parent_dep);
   18144              : 
   18145          594 :   sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
   18146          594 :   dump.outdent ();
   18147              : 
   18148          594 :   return num;
   18149          594 : }
   18150              : 
   18151              : bool
   18152          165 : module_state::read_using_directives (unsigned num)
   18153              : {
   18154          165 :   if (!bitmap_bit_p (this_module ()->imports, mod))
   18155              :     {
   18156           13 :       dump () && dump ("Ignoring using-directives because module %M "
   18157              :                        "is not visible in this TU", this);
   18158              :       return true;
   18159              :     }
   18160              : 
   18161          155 :   bytes_in sec;
   18162              : 
   18163          155 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
   18164              :     return false;
   18165              : 
   18166          167 :   dump () && dump ("Reading using-directives");
   18167          155 :   dump.indent ();
   18168              : 
   18169          338 :   for (unsigned ix = 0; ix != num; ++ix)
   18170              :     {
   18171          183 :       bool exported = sec.u ();
   18172          183 :       tree parent = read_namespace (sec);
   18173          183 :       tree target = read_namespace (sec);
   18174          183 :       if (sec.get_overrun ())
   18175              :         break;
   18176              : 
   18177          195 :       dump () && dump ("Read using-directive in %N for %N", parent, target);
   18178          183 :       if (exported || is_module () || is_partition ())
   18179          156 :         add_imported_using_namespace (parent, target);
   18180              :     }
   18181              : 
   18182          155 :   dump.outdent ();
   18183          155 :   if (!sec.end (from ()))
   18184              :     return false;
   18185              :   return true;
   18186          155 : }
   18187              : 
   18188              : /* Write the binding TABLE to MOD_SNAME_PFX.bnd   */
   18189              : 
   18190              : unsigned
   18191         2785 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
   18192              : {
   18193         3085 :   dump () && dump ("Writing binding table");
   18194         2785 :   dump.indent ();
   18195              : 
   18196         2785 :   unsigned num = 0;
   18197         2785 :   bytes_out sec (to);
   18198         2785 :   sec.begin ();
   18199              : 
   18200      2961369 :   for (unsigned ix = 0; ix != sccs.length (); ix++)
   18201              :     {
   18202      1476507 :       depset *b = sccs[ix];
   18203      1476507 :       if (b->is_binding ())
   18204              :         {
   18205       162262 :           tree ns = b->get_entity ();
   18206       163460 :           dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
   18207              :                            b->section);
   18208       162262 :           sec.u (to->name (b->get_name ()));
   18209       162262 :           write_namespace (sec, b->deps[0]);
   18210       162262 :           sec.u (b->section);
   18211       162262 :           num++;
   18212              :         }
   18213              :     }
   18214              : 
   18215         2785 :   sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
   18216         2785 :   dump.outdent ();
   18217              : 
   18218         2785 :   return num;
   18219         2785 : }
   18220              : 
   18221              : /* Read the binding table from MOD_SNAME_PFX.bind.  */
   18222              : 
   18223              : bool
   18224         2970 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
   18225              : {
   18226         2970 :   bytes_in sec;
   18227              : 
   18228         2970 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
   18229              :     return false;
   18230              : 
   18231         3502 :   dump () && dump ("Reading binding table");
   18232         2970 :   dump.indent ();
   18233       198986 :   for (; !sec.get_overrun () && num--;)
   18234              :     {
   18235       196016 :       const char *name = from ()->name (sec.u ());
   18236       196016 :       tree ns = read_namespace (sec);
   18237       196016 :       unsigned snum = sec.u ();
   18238              : 
   18239       196016 :       if (!ns || !name || (snum - lwm) >= (hwm - lwm))
   18240            0 :         sec.set_overrun ();
   18241       196016 :       if (!sec.get_overrun ())
   18242              :         {
   18243       196016 :           tree id = get_identifier (name);
   18244       197566 :           dump () && dump ("Bindings %P section:%u", ns, id, snum);
   18245       196016 :           if (mod && !import_module_binding (ns, id, mod, snum))
   18246              :             break;
   18247              :         }
   18248              :     }
   18249              : 
   18250         2970 :   dump.outdent ();
   18251         2970 :   if (!sec.end (from ()))
   18252              :     return false;
   18253              :   return true;
   18254         2970 : }
   18255              : 
   18256              : /* Write the entity table to MOD_SNAME_PFX.ent
   18257              : 
   18258              :    Each entry is a section number.  */
   18259              : 
   18260              : void
   18261         2519 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
   18262              :                               unsigned count, unsigned *crc_p)
   18263              : {
   18264         2766 :   dump () && dump ("Writing entities");
   18265         2519 :   dump.indent ();
   18266              : 
   18267         2519 :   bytes_out sec (to);
   18268         2519 :   sec.begin ();
   18269              : 
   18270         2519 :   unsigned current = 0;
   18271      1481524 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18272              :     {
   18273      1476486 :       depset *d = depsets[ix];
   18274              : 
   18275      2747881 :       switch (d->get_entity_kind ())
   18276              :         {
   18277              :         default:
   18278              :           break;
   18279              : 
   18280         5217 :         case depset::EK_NAMESPACE:
   18281         5217 :           if (!d->is_import () && d->get_entity () != global_namespace)
   18282              :             {
   18283         2698 :               gcc_checking_assert (d->cluster == current);
   18284         2698 :               current++;
   18285         2698 :               sec.u (0);
   18286              :             }
   18287              :           break;
   18288              : 
   18289      1266178 :         case depset::EK_DECL:
   18290      1266178 :         case depset::EK_SPECIALIZATION:
   18291      1266178 :         case depset::EK_PARTIAL:
   18292      2532356 :           gcc_checking_assert (!d->is_unreached ()
   18293              :                                && !d->is_import ()
   18294              :                                && d->cluster == current
   18295              :                                && d->section);
   18296      1266178 :           current++;
   18297      1266178 :           sec.u (d->section);
   18298      1266178 :           break;
   18299              :         }
   18300              :     }
   18301         2519 :   gcc_assert (count == current);
   18302         2519 :   sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
   18303         2519 :   dump.outdent ();
   18304         2519 : }
   18305              : 
   18306              : bool
   18307         2741 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
   18308              : {
   18309         2741 :   trees_in sec (this);
   18310              : 
   18311         2741 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
   18312              :     return false;
   18313              : 
   18314         3219 :   dump () && dump ("Reading entities");
   18315         2741 :   dump.indent ();
   18316              : 
   18317      1343649 :   for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
   18318              :     {
   18319      1340908 :       unsigned snum = sec.u ();
   18320      1340908 :       if (snum && (snum - lwm) >= (hwm - lwm))
   18321            0 :         sec.set_overrun ();
   18322      1340908 :       if (sec.get_overrun ())
   18323              :         break;
   18324              : 
   18325      1340908 :       if (snum)
   18326      1338000 :         slot->set_lazy (snum << 2);
   18327              :     }
   18328              : 
   18329         2741 :   dump.outdent ();
   18330         2741 :   if (!sec.end (from ()))
   18331              :     return false;
   18332              :   return true;
   18333         2741 : }
   18334              : 
   18335              : /* Write the pending table to MOD_SNAME_PFX.pnd
   18336              : 
   18337              :    The pending table holds information about clusters that need to be
   18338              :    loaded because they contain information about something that is not
   18339              :    found by namespace-scope lookup.
   18340              : 
   18341              :    The three cases are:
   18342              : 
   18343              :    (a) Template (maybe-partial) specializations that we have
   18344              :    instantiated or defined.  When an importer needs to instantiate
   18345              :    that template, they /must have/ the partial, explicit & extern
   18346              :    specializations available.  If they have the other specializations
   18347              :    available, they'll have less work to do.  Thus, when we're about to
   18348              :    instantiate FOO, we have to be able to ask 'are there any
   18349              :    specialization of FOO in our imports?'.
   18350              : 
   18351              :    (b) (Maybe-implicit) member functions definitions.  A class could
   18352              :    be defined in one header, and an inline member defined in a
   18353              :    different header (this occurs in the STL).  Similarly, like the
   18354              :    specialization case, an implicit member function could have been
   18355              :    'instantiated' in one module, and it'd be nice to not have to
   18356              :    reinstantiate it in another.
   18357              : 
   18358              :    (c) Classes completed elsewhere.  A class could be declared in one
   18359              :    header and defined in another.  We need to know to load the class
   18360              :    definition before looking in it.  It does highlight an issue --
   18361              :    there could be an intermediate import between the outermost containing
   18362              :    namespace-scope class and the innermost being-defined class.  This is
   18363              :    actually possible with all of these cases, so be aware -- we're not
   18364              :    just talking of one level of import to get to the innermost namespace.
   18365              : 
   18366              :    This gets complicated fast, it took me multiple attempts to even
   18367              :    get something remotely working.  Partially because I focussed on
   18368              :    optimizing what I think turns out to be a smaller problem, given
   18369              :    the known need to do the more general case *anyway*.  I document
   18370              :    the smaller problem, because it does appear to be the natural way
   18371              :    to do it.  It's trap!
   18372              : 
   18373              :    **** THE TRAP
   18374              : 
   18375              :    Let's refer to the primary template or the containing class as the
   18376              :    KEY.  And the specialization or member as the PENDING-ENTITY.  (To
   18377              :    avoid having to say those mouthfuls all the time.)
   18378              : 
   18379              :    In either case, we have an entity and we need some way of mapping
   18380              :    that to a set of entities that need to be loaded before we can
   18381              :    proceed with whatever processing of the entity we were going to do.
   18382              : 
   18383              :    We need to link the key to the pending-entity in some way.  Given a
   18384              :    key, tell me the pending-entities I need to have loaded.  However
   18385              :    we tie the key to the pending-entity must not rely on the key being
   18386              :    loaded -- that'd defeat the lazy loading scheme.
   18387              : 
   18388              :    As the key will be an import in we know its entity number (either
   18389              :    because we imported it, or we're writing it out too).  Thus we can
   18390              :    generate a map of key-indices to pending-entities.  The
   18391              :    pending-entity indices will be into our span of the entity table,
   18392              :    and thus allow them to be lazily loaded.  The key index will be
   18393              :    into another slot of the entity table.  Notice that this checking
   18394              :    could be expensive, we don't want to iterate over a bunch of
   18395              :    pending-entity indices (across multiple imports), every time we're
   18396              :    about do to the thing with the key.  We need to quickly determine
   18397              :    'definitely nothing needed'.
   18398              : 
   18399              :    That's almost good enough, except that key indices are not unique
   18400              :    in a couple of cases :( Specifically the Global Module or a module
   18401              :    partition can result in multiple modules assigning an entity index
   18402              :    for the key.  The decl-merging on loading will detect that so we
   18403              :    only have one Key loaded, and in the entity hash it'll indicate the
   18404              :    entity index of first load.  Which might be different to how we
   18405              :    know it.  Notice this is restricted to GM entities or this-module
   18406              :    entities.  Foreign imports cannot have this.
   18407              : 
   18408              :    We can simply resolve this in the direction of how this module
   18409              :    referred to the key to how the importer knows it.  Look in the
   18410              :    entity table slot that we nominate, maybe lazy load it, and then
   18411              :    lookup the resultant entity in the entity hash to learn how the
   18412              :    importer knows it.
   18413              : 
   18414              :    But we need to go in the other direction :( Given the key, find all
   18415              :    the index-aliases of that key.  We can partially solve that by
   18416              :    adding an alias hash table.  Whenever we load a merged decl, add or
   18417              :    augment a mapping from the entity (or its entity-index) to the
   18418              :    newly-discovered index.  Then when we look for pending entities of
   18419              :    a key, we also iterate over this aliases this mapping provides.
   18420              : 
   18421              :    But that requires the alias to be loaded.  And that's not
   18422              :    necessarily true.
   18423              : 
   18424              :    *** THE SIMPLER WAY
   18425              : 
   18426              :    The remaining fixed thing we have is the innermost namespace
   18427              :    containing the ultimate namespace-scope container of the key and
   18428              :    the name of that container (which might be the key itself).  I.e. a
   18429              :    namespace-decl/identifier/module tuple.  Let's call this the
   18430              :    top-key.  We'll discover that the module is not important here,
   18431              :    because of cross-module possibilities mentioned in case #c above.
   18432              :    We can't markup namespace-binding slots.  The best we can do is
   18433              :    mark the binding vector with 'there's something here', and have
   18434              :    another map from namespace/identifier pairs to a vector of pending
   18435              :    entity indices.
   18436              : 
   18437              :    Maintain a pending-entity map.  This is keyed by top-key, and
   18438              :    maps to a vector of pending-entity indices.  On the binding vector
   18439              :    have flags saying whether the pending-name-entity map has contents.
   18440              :    (We might want to further extend the key to be GM-vs-Partition and
   18441              :    specialization-vs-member, but let's not get ahead of ourselves.)
   18442              : 
   18443              :    For every key-like entity, find the outermost namespace-scope
   18444              :    name.  Use that to lookup in the pending-entity map and then make
   18445              :    sure the specified entities are loaded.
   18446              : 
   18447              :    An optimization might be to have a flag in each key-entity saying
   18448              :    that its top key might be in the entity table.  It's not clear to
   18449              :    me how to set that flag cheaply -- cheaper than just looking.
   18450              : 
   18451              :    FIXME: It'd be nice to have a bit in decls to tell us whether to
   18452              :    even try this.  We can have a 'already done' flag, that we set when
   18453              :    we've done KLASS's lazy pendings.  When we import a module that
   18454              :    registers pendings on the same top-key as KLASS we need to clear
   18455              :    the flag.  A recursive walk of the top-key clearing the bit will
   18456              :    suffice.  Plus we only need to recurse on classes that have the bit
   18457              :    set.  (That means we need to set the bit on parents of KLASS here,
   18458              :    don't forget.)  However, first: correctness, second: efficiency.  */
   18459              : 
   18460              : unsigned
   18461         2785 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
   18462              :                               depset::hash &table, unsigned *crc_p)
   18463              : {
   18464         3085 :   dump () && dump ("Writing pending-entities");
   18465         2785 :   dump.indent ();
   18466              : 
   18467         2785 :   trees_out sec (to, this, table);
   18468         2785 :   sec.begin ();
   18469              : 
   18470         2785 :   unsigned count = 0;
   18471         2785 :   tree cache_ns = NULL_TREE;
   18472         2785 :   tree cache_id = NULL_TREE;
   18473         2785 :   unsigned cache_section = ~0;
   18474      1482077 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18475              :     {
   18476      1476507 :       depset *d = depsets[ix];
   18477              : 
   18478      1476507 :       if (d->is_binding ())
   18479       887223 :         continue;
   18480              : 
   18481      1314245 :       if (d->is_import ())
   18482            0 :         continue;
   18483              : 
   18484      1314245 :       if (!d->is_pending_entity ())
   18485       724961 :         continue;
   18486              : 
   18487       589284 :       tree key_decl = nullptr;
   18488       589284 :       tree key_ns = find_pending_key (d->get_entity (), &key_decl);
   18489       589284 :       tree key_name = DECL_NAME (key_decl);
   18490              : 
   18491       589284 :       if (IDENTIFIER_ANON_P (key_name))
   18492              :         {
   18493            6 :           gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
   18494           12 :           if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
   18495            6 :             key_name = DECL_NAME (attached);
   18496              :           else
   18497              :             {
   18498              :               /* There's nothing to attach it to.  Must
   18499              :                  always reinstantiate.  */
   18500            0 :               dump ()
   18501            0 :                 && dump ("Unattached lambda %N[%u] section:%u",
   18502            0 :                          d->get_entity_kind () == depset::EK_DECL
   18503              :                          ? "Member" : "Specialization", d->get_entity (),
   18504              :                          d->cluster, d->section);
   18505            0 :               continue;
   18506              :             }
   18507              :         }
   18508              : 
   18509       589284 :       char const *also = "";
   18510       589284 :       if (d->section == cache_section
   18511       393389 :           && key_ns == cache_ns
   18512       393389 :           && key_name == cache_id)
   18513              :         /* Same section & key as previous, no need to repeat ourselves.  */
   18514              :         also = "also ";
   18515              :       else
   18516              :         {
   18517       265448 :           cache_ns = key_ns;
   18518       265448 :           cache_id = key_name;
   18519       265448 :           cache_section = d->section;
   18520       265448 :           gcc_checking_assert (table.find_dependency (cache_ns));
   18521       265448 :           sec.tree_node (cache_ns);
   18522       265448 :           sec.tree_node (cache_id);
   18523       265448 :           sec.u (d->cluster);
   18524       265448 :           count++;
   18525              :         }
   18526       590740 :       dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
   18527          728 :                        d->get_entity_kind () == depset::EK_DECL
   18528              :                        ? "member" : "specialization", d->get_entity (),
   18529              :                        d->cluster, cache_section, also, cache_ns, cache_id);
   18530              :       }
   18531         2785 :   sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
   18532         2785 :   dump.outdent ();
   18533              : 
   18534         2785 :   return count;
   18535         2785 : }
   18536              : 
   18537              : bool
   18538         1583 : module_state::read_pendings (unsigned count)
   18539              : {
   18540         1583 :   trees_in sec (this);
   18541              : 
   18542         1583 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
   18543              :     return false;
   18544              : 
   18545         1905 :   dump () && dump ("Reading %u pendings", count);
   18546         1583 :   dump.indent ();
   18547              : 
   18548       280187 :   for (unsigned ix = 0; ix != count; ix++)
   18549              :     {
   18550       278604 :       pending_key key;
   18551       278604 :       unsigned index;
   18552              : 
   18553       278604 :       key.ns = sec.tree_node ();
   18554       278604 :       key.id = sec.tree_node ();
   18555       278604 :       index = sec.u ();
   18556              : 
   18557       278604 :       if (!key.ns || !key.id
   18558       278604 :           || !(TREE_CODE (key.ns) == NAMESPACE_DECL
   18559       278604 :                && !DECL_NAMESPACE_ALIAS (key.ns))
   18560       278604 :           || !identifier_p (key.id)
   18561       557208 :           || index >= entity_num)
   18562            0 :         sec.set_overrun ();
   18563              : 
   18564       278604 :       if (sec.get_overrun ())
   18565              :         break;
   18566              : 
   18567       279413 :       dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
   18568              : 
   18569       278604 :       index += entity_lwm;
   18570       278604 :       auto &vec = pending_table->get_or_insert (key);
   18571       278604 :       vec.safe_push (index);
   18572              :     }
   18573              : 
   18574         1583 :   dump.outdent ();
   18575         1583 :   if (!sec.end (from ()))
   18576              :     return false;
   18577              :   return true;
   18578         1583 : }
   18579              : 
   18580              : /* Read & write locations.  */
   18581              : enum loc_kind {
   18582              :   LK_ORDINARY,
   18583              :   LK_MACRO,
   18584              :   LK_IMPORT_ORDINARY,
   18585              :   LK_IMPORT_MACRO,
   18586              :   LK_ADHOC,
   18587              :   LK_RESERVED,
   18588              : };
   18589              : 
   18590              : static const module_state *
   18591         7689 : module_for_ordinary_loc (location_t loc)
   18592              : {
   18593         7689 :   unsigned pos = 0;
   18594         7689 :   unsigned len = ool->length () - pos;
   18595              : 
   18596        15381 :   while (len)
   18597              :     {
   18598         7692 :       unsigned half = len / 2;
   18599         7692 :       module_state *probe = (*ool)[pos + half];
   18600         7692 :       if (loc < probe->ordinary_locs.first)
   18601              :         len = half;
   18602         7689 :       else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
   18603              :         return probe;
   18604              :       else
   18605              :         {
   18606            0 :           pos += half + 1;
   18607            0 :           len = len - (half + 1);
   18608              :         }
   18609              :     }
   18610              : 
   18611              :   return nullptr;
   18612              : }
   18613              : 
   18614              : static const module_state *
   18615           15 : module_for_macro_loc (location_t loc)
   18616              : {
   18617           15 :   unsigned pos = 1;
   18618           15 :   unsigned len = modules->length () - pos;
   18619              : 
   18620           15 :   while (len)
   18621              :     {
   18622           15 :       unsigned half = len / 2;
   18623           15 :       module_state *probe = (*modules)[pos + half];
   18624           15 :       if (loc < probe->macro_locs.first)
   18625              :         {
   18626            0 :           pos += half + 1;
   18627            0 :           len = len - (half + 1);
   18628              :         }
   18629           15 :       else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
   18630              :         len = half;
   18631              :       else
   18632              :         return probe;
   18633              :     }
   18634              : 
   18635              :   return NULL;
   18636              : }
   18637              : 
   18638              : location_t
   18639         1331 : module_state::imported_from () const
   18640              : {
   18641         1331 :   location_t from = loc;
   18642         1331 :   line_map_ordinary const *fmap
   18643         1331 :     = linemap_check_ordinary (linemap_lookup (line_table, from));
   18644              : 
   18645         1331 :   if (MAP_MODULE_P (fmap))
   18646         1331 :     from = linemap_included_from (fmap);
   18647              : 
   18648         1331 :   return from;
   18649              : }
   18650              : 
   18651              : /* Note that LOC will need writing.  This allows us to prune locations
   18652              :    that are not needed.  */
   18653              : 
   18654              : bool
   18655     28991684 : module_state::note_location (location_t loc)
   18656              : {
   18657     28991684 :   bool added = false;
   18658     28991684 :   if (!macro_loc_table && !ord_loc_table)
   18659              :     ;
   18660     28991684 :   else if (loc < RESERVED_LOCATION_COUNT)
   18661              :     ;
   18662     26409922 :   else if (IS_ADHOC_LOC (loc))
   18663              :     {
   18664      3333213 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18665      3333213 :       note_location (locus);
   18666      3333213 :       source_range range = get_range_from_loc (line_table, loc);
   18667      3333213 :       if (range.m_start != locus)
   18668      3214486 :         note_location (range.m_start);
   18669      3333213 :       note_location (range.m_finish);
   18670              :     }
   18671     23076709 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18672              :     {
   18673      1607639 :       if (spans.macro (loc))
   18674              :         {
   18675      1607624 :           const line_map *map = linemap_lookup (line_table, loc);
   18676      1607624 :           const line_map_macro *mac_map = linemap_check_macro (map);
   18677      1607624 :           hashval_t hv = macro_loc_traits::hash (mac_map);
   18678      1607624 :           macro_loc_info *slot
   18679      1607624 :             = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
   18680      1607624 :           if (!slot->src)
   18681              :             {
   18682       168247 :               slot->src = mac_map;
   18683       168247 :               slot->remap = 0;
   18684              :               // Expansion locations could themselves be from a
   18685              :               // macro, we need to note them all.
   18686       168247 :               note_location (mac_map->m_expansion);
   18687       168247 :               gcc_checking_assert (mac_map->n_tokens);
   18688       168247 :               location_t tloc = UNKNOWN_LOCATION;
   18689      6338437 :               for (unsigned ix = mac_map->n_tokens * 2; ix--;)
   18690      6170190 :                 if (mac_map->macro_locations[ix] != tloc)
   18691              :                   {
   18692      3258170 :                     tloc = mac_map->macro_locations[ix];
   18693      3258170 :                     note_location (tloc);
   18694              :                   }
   18695              :               added = true;
   18696              :             }
   18697              :         }
   18698              :     }
   18699     21469070 :   else if (IS_ORDINARY_LOC (loc))
   18700              :     {
   18701     21469070 :       if (spans.ordinary (loc))
   18702              :         {
   18703     21461376 :           const line_map *map = linemap_lookup (line_table, loc);
   18704     21461376 :           const line_map_ordinary *ord_map = linemap_check_ordinary (map);
   18705     21461376 :           ord_loc_info lkup;
   18706     21461376 :           lkup.src = ord_map;
   18707     21461376 :           lkup.span = loc_one << ord_map->m_column_and_range_bits;
   18708     21461376 :           lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
   18709     21461376 :           lkup.remap = 0;
   18710     21461376 :           ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
   18711     21461376 :                                 (lkup, ord_loc_traits::hash (lkup), INSERT));
   18712     21461376 :           if (!slot->src)
   18713              :             {
   18714      2431298 :               *slot = lkup;
   18715      2431298 :               added = true;
   18716              :             }
   18717              :         }
   18718              :     }
   18719              :   else
   18720            0 :     gcc_unreachable ();
   18721     28991684 :   return added;
   18722              : }
   18723              : 
   18724              : /* If we're not streaming, record that we need location LOC.
   18725              :    Otherwise stream it.  */
   18726              : 
   18727              : void
   18728     43365823 : module_state::write_location (bytes_out &sec, location_t loc)
   18729              : {
   18730     43365823 :   if (!sec.streaming_p ())
   18731              :     {
   18732     15397059 :       note_location (loc);
   18733     15397059 :       return;
   18734              :     }
   18735              : 
   18736     27968764 :   if (loc < RESERVED_LOCATION_COUNT)
   18737              :     {
   18738      2654709 :       dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
   18739      2654691 :       sec.loc (LK_RESERVED + loc);
   18740              :     }
   18741     25314073 :   else if (IS_ADHOC_LOC (loc))
   18742              :     {
   18743      3254207 :       dump (dumper::LOCATION) && dump ("Adhoc location");
   18744      3254204 :       sec.u (LK_ADHOC);
   18745      3254204 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18746      3254204 :       write_location (sec, locus);
   18747      3254204 :       source_range range = get_range_from_loc (line_table, loc);
   18748      3254204 :       if (range.m_start == locus)
   18749              :         /* Compress.  */
   18750       111528 :         range.m_start = UNKNOWN_LOCATION;
   18751      3254204 :       write_location (sec, range.m_start);
   18752      3254204 :       write_location (sec, range.m_finish);
   18753      3254204 :       unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
   18754      3254204 :       sec.u (discriminator);
   18755              :     }
   18756     22059869 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18757              :     {
   18758      1598164 :       const macro_loc_info *info = nullptr;
   18759      1598164 :       line_map_uint_t offset = 0;
   18760      1598164 :       if (unsigned hwm = macro_loc_remap->length ())
   18761              :         {
   18762      1598158 :           info = macro_loc_remap->begin ();
   18763     23567394 :           while (hwm != 1)
   18764              :             {
   18765     20371078 :               unsigned mid = hwm / 2;
   18766     20371078 :               if (MAP_START_LOCATION (info[mid].src) <= loc)
   18767              :                 {
   18768     10307783 :                   info += mid;
   18769     10307783 :                   hwm -= mid;
   18770              :                 }
   18771              :               else
   18772              :                 hwm = mid;
   18773              :             }
   18774      1598158 :           offset = loc - MAP_START_LOCATION (info->src);
   18775      1598158 :           if (offset > info->src->n_tokens)
   18776            9 :             info = nullptr;
   18777              :         }
   18778              : 
   18779      1598164 :       gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
   18780              : 
   18781      1598164 :       if (info)
   18782              :         {
   18783      1598149 :           offset += info->remap;
   18784      1598149 :           sec.u (LK_MACRO);
   18785      1598149 :           sec.loc (offset);
   18786      1598149 :           dump (dumper::LOCATION)
   18787            9 :             && dump ("Macro location %K output %K", loc, offset);
   18788              :         }
   18789           15 :       else if (const module_state *import = module_for_macro_loc (loc))
   18790              :         {
   18791           15 :           auto off = loc - import->macro_locs.first;
   18792           15 :           sec.u (LK_IMPORT_MACRO);
   18793           15 :           sec.u (import->remap);
   18794           15 :           sec.loc (off);
   18795           15 :           dump (dumper::LOCATION)
   18796            0 :             && dump ("Imported macro location %K output %u:%K",
   18797            0 :                      loc, import->remap, off);
   18798              :         }
   18799              :       else
   18800            0 :         gcc_unreachable ();
   18801              :     }
   18802     20461705 :   else if (IS_ORDINARY_LOC (loc))
   18803              :     {
   18804              :       /* If we ran out of locations for imported decls, this location could
   18805              :          be a module unit's location.  In that case, remap the location
   18806              :          to be where we imported the module from.  */
   18807     20461705 :       if (spans.locations_exhausted_p () || CHECKING_P)
   18808              :         {
   18809     20461705 :           const line_map_ordinary *map
   18810     20461705 :             = linemap_check_ordinary (linemap_lookup (line_table, loc));
   18811     20461705 :           if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
   18812              :             {
   18813            0 :               gcc_checking_assert (spans.locations_exhausted_p ());
   18814            0 :               write_location (sec, linemap_included_from (map));
   18815            0 :               return;
   18816              :             }
   18817              :         }
   18818              : 
   18819     20461705 :       const ord_loc_info *info = nullptr;
   18820     20461705 :       line_map_uint_t offset = 0;
   18821     20461705 :       if (line_map_uint_t hwm = ord_loc_remap->length ())
   18822              :         {
   18823     20461705 :           info = ord_loc_remap->begin ();
   18824    304262567 :           while (hwm != 1)
   18825              :             {
   18826    263339157 :               auto mid = hwm / 2;
   18827    263339157 :               if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
   18828              :                 {
   18829    137606678 :                   info += mid;
   18830    137606678 :                   hwm -= mid;
   18831              :                 }
   18832              :               else
   18833              :                 hwm = mid;
   18834              :             }
   18835     20461705 :           offset = loc - MAP_START_LOCATION (info->src) - info->offset;
   18836     20461705 :           if (offset > info->span)
   18837         7689 :             info = nullptr;
   18838              :         }
   18839              : 
   18840     20461705 :       gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
   18841              : 
   18842     20461705 :       if (info)
   18843              :         {
   18844     20454016 :           offset += info->remap;
   18845     20454016 :           sec.u (LK_ORDINARY);
   18846     20454016 :           sec.loc (offset);
   18847              : 
   18848     20454016 :           dump (dumper::LOCATION)
   18849           78 :             && dump ("Ordinary location %K output %K", loc, offset);
   18850              :         }
   18851         7689 :       else if (const module_state *import = module_for_ordinary_loc (loc))
   18852              :         {
   18853         7689 :           auto off = loc - import->ordinary_locs.first;
   18854         7689 :           sec.u (LK_IMPORT_ORDINARY);
   18855         7689 :           sec.u (import->remap);
   18856         7689 :           sec.loc (off);
   18857         7689 :           dump (dumper::LOCATION)
   18858            0 :             && dump ("Imported ordinary location %K output %u:%K",
   18859            0 :                      loc, import->remap, off);
   18860              :         }
   18861              :       else
   18862            0 :         gcc_unreachable ();
   18863              :     }
   18864              :   else
   18865            0 :     gcc_unreachable ();
   18866              : }
   18867              : 
   18868              : location_t
   18869     22057290 : module_state::read_location (bytes_in &sec) const
   18870              : {
   18871     22057290 :   location_t locus = UNKNOWN_LOCATION;
   18872     22057290 :   unsigned kind = sec.u ();
   18873     22057290 :   switch (kind)
   18874              :      {
   18875      1985573 :     default:
   18876      1985573 :       {
   18877      1985573 :         if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
   18878      1985573 :           locus = location_t (kind - LK_RESERVED);
   18879              :         else
   18880            0 :           sec.set_overrun ();
   18881      1985573 :         dump (dumper::LOCATION)
   18882            0 :           && dump ("Reserved location %K", locus);
   18883              :       }
   18884              :       break;
   18885              : 
   18886      2391094 :      case LK_ADHOC:
   18887      2391094 :       {
   18888      2391094 :         dump (dumper::LOCATION) && dump ("Adhoc location");
   18889      2391094 :         locus = read_location (sec);
   18890      2391094 :         source_range range;
   18891      2391094 :         range.m_start = read_location (sec);
   18892      2391094 :         if (range.m_start == UNKNOWN_LOCATION)
   18893        73761 :           range.m_start = locus;
   18894      2391094 :         range.m_finish = read_location (sec);
   18895      2391094 :         unsigned discriminator = sec.u ();
   18896      2391094 :         if (locus != loc && range.m_start != loc && range.m_finish != loc)
   18897      2391094 :           locus = line_table->get_or_create_combined_loc (locus, range,
   18898              :                                                           nullptr, discriminator);
   18899              :       }
   18900              :       break;
   18901              : 
   18902      1617507 :     case LK_MACRO:
   18903      1617507 :       {
   18904      1617507 :         auto off = sec.loc ();
   18905              : 
   18906      1617507 :         if (macro_locs.second)
   18907              :           {
   18908      1617507 :             if (off < macro_locs.second)
   18909      1617507 :               locus = off + macro_locs.first;
   18910              :             else
   18911            0 :               sec.set_overrun ();
   18912              :           }
   18913              :         else
   18914            0 :           locus = loc;
   18915      1617507 :         dump (dumper::LOCATION)
   18916            0 :           && dump ("Macro %K becoming %K", off, locus);
   18917              :       }
   18918              :       break;
   18919              : 
   18920     16059248 :     case LK_ORDINARY:
   18921     16059248 :       {
   18922     16059248 :         auto off = sec.loc ();
   18923     16059248 :         if (ordinary_locs.second)
   18924              :           {
   18925     16059248 :             if (off < ordinary_locs.second)
   18926     16059248 :               locus = off + ordinary_locs.first;
   18927              :             else
   18928            0 :               sec.set_overrun ();
   18929              :           }
   18930              :         else
   18931            0 :           locus = loc;
   18932              : 
   18933     16059248 :         dump (dumper::LOCATION)
   18934            0 :           && dump ("Ordinary location %K becoming %K", off, locus);
   18935              :       }
   18936              :       break;
   18937              : 
   18938         3868 :      case LK_IMPORT_MACRO:
   18939         3868 :      case LK_IMPORT_ORDINARY:
   18940         3868 :        {
   18941         3868 :          unsigned mod = sec.u ();
   18942         3868 :          location_t off = sec.loc ();
   18943         3868 :          const module_state *import = NULL;
   18944              : 
   18945         3868 :          if (!mod && !slurp->remap)
   18946              :            /* This is an early read of a partition location during the
   18947              :               read of our ordinary location map.  */
   18948              :            import = this;
   18949              :          else
   18950              :            {
   18951         3868 :              mod = slurp->remap_module (mod);
   18952         3868 :              if (!mod)
   18953            0 :                sec.set_overrun ();
   18954              :              else
   18955         3868 :                import = (*modules)[mod];
   18956              :            }
   18957              : 
   18958         3868 :          if (import)
   18959              :            {
   18960         3868 :              if (kind == LK_IMPORT_MACRO)
   18961              :                {
   18962           22 :                  if (!import->macro_locs.second)
   18963            0 :                    locus = import->loc;
   18964           22 :                  else if (off < import->macro_locs.second)
   18965           22 :                    locus = off + import->macro_locs.first;
   18966              :                  else
   18967            0 :                    sec.set_overrun ();
   18968              :                }
   18969              :              else
   18970              :                {
   18971         3846 :                  if (!import->ordinary_locs.second)
   18972            0 :                    locus = import->loc;
   18973         3846 :                  else if (off < import->ordinary_locs.second)
   18974         3846 :                    locus = import->ordinary_locs.first + off;
   18975              :                  else
   18976            0 :                    sec.set_overrun ();
   18977              :                }
   18978              :            }
   18979              :        }
   18980              :        break;
   18981              :     }
   18982              : 
   18983     22057290 :   return locus;
   18984              : }
   18985              : 
   18986              : /* Allocate hash tables to record needed locations.  */
   18987              : 
   18988              : void
   18989         2814 : module_state::write_init_maps ()
   18990              : {
   18991         2814 :   macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
   18992         2814 :   ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
   18993         2814 : }
   18994              : 
   18995              : /* Prepare the span adjustments.  We prune unneeded locations -- at
   18996              :    this point every needed location must have been seen by
   18997              :    note_location.  */
   18998              : 
   18999              : range_t
   19000         2785 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
   19001              : {
   19002         3085 :   dump () && dump ("Preparing locations");
   19003         2785 :   dump.indent ();
   19004              : 
   19005         3085 :   dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
   19006          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.first,
   19007          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.second,
   19008          300 :                    spans[loc_spans::SPAN_RESERVED].macro.first,
   19009          300 :                    spans[loc_spans::SPAN_RESERVED].macro.second);
   19010              : 
   19011         2785 :   range_t info {0, 0};
   19012              : 
   19013              :   // Sort the noted lines.
   19014         2785 :   vec_alloc (ord_loc_remap, ord_loc_table->size ());
   19015         2785 :   for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   19016      2419498 :        iter != end; ++iter)
   19017      2416713 :     ord_loc_remap->quick_push (*iter);
   19018         2785 :   ord_loc_remap->qsort (&ord_loc_info::compare);
   19019              : 
   19020              :   // Note included-from maps.
   19021         2785 :   bool added = false;
   19022         2785 :   const line_map_ordinary *current = nullptr;
   19023      2425068 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19024      2419498 :        iter != end; ++iter)
   19025      2416713 :     if (iter->src != current)
   19026              :       {
   19027        35125 :         current = iter->src;
   19028        14103 :         for (auto probe = current;
   19029        35125 :              auto from = linemap_included_from (probe);
   19030        14103 :              probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
   19031              :           {
   19032        31969 :             if (has_partitions)
   19033              :               {
   19034              :                 // Partition locations need to elide their module map
   19035              :                 // entry.
   19036          223 :                 probe
   19037          223 :                   = linemap_check_ordinary (linemap_lookup (line_table, from));
   19038          223 :                 if (MAP_MODULE_P (probe))
   19039          190 :                   from = linemap_included_from (probe);
   19040              :               }
   19041              : 
   19042        31969 :             if (!note_location (from))
   19043              :               break;
   19044        14103 :             added = true;
   19045        14103 :           }
   19046              :       }
   19047         2785 :   if (added)
   19048              :     {
   19049              :       // Reconstruct the line array as we added items to the hash table.
   19050          505 :       vec_free (ord_loc_remap);
   19051          505 :       vec_alloc (ord_loc_remap, ord_loc_table->size ());
   19052          505 :       for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   19053      2419275 :            iter != end; ++iter)
   19054      2418770 :         ord_loc_remap->quick_push (*iter);
   19055          505 :       ord_loc_remap->qsort (&ord_loc_info::compare);
   19056              :     }
   19057         2785 :   delete ord_loc_table;
   19058         2785 :   ord_loc_table = nullptr;
   19059              : 
   19060              :   // Merge (sufficiently) adjacent spans, and calculate remapping.
   19061         2785 :   constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
   19062         5570 :   auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19063         2785 :   auto dst = begin;
   19064         2785 :   line_map_uint_t offset = 0;
   19065         2785 :   unsigned range_bits = 0;
   19066         2785 :   ord_loc_info *base = nullptr;
   19067      2433601 :   for (auto iter = begin; iter != end; ++iter)
   19068              :     {
   19069      2430816 :       if (base && iter->src == base->src)
   19070              :         {
   19071      4558467 :           if (base->offset + base->span +
   19072      2400531 :               ((adjacency << base->src->m_column_and_range_bits)
   19073              :                // If there are few c&r bits, allow further separation.
   19074      2400531 :                | (adjacency << 4))
   19075      2400531 :               >= iter->offset)
   19076              :             {
   19077              :               // Merge.
   19078      2157936 :               offset -= base->span;
   19079      2157936 :               base->span = iter->offset + iter->span - base->offset;
   19080      2157936 :               offset += base->span;
   19081      2157936 :               continue;
   19082              :             }
   19083              :         }
   19084        30285 :       else if (range_bits < iter->src->m_range_bits)
   19085         2686 :         range_bits = iter->src->m_range_bits;
   19086              : 
   19087       272880 :       offset += ((loc_one << iter->src->m_range_bits) - 1);
   19088       272880 :       offset &= ~((loc_one << iter->src->m_range_bits) - 1);
   19089       272880 :       iter->remap = offset;
   19090       272880 :       offset += iter->span;
   19091       272880 :       base = dst;
   19092       272880 :       *dst++ = *iter;
   19093              :     }
   19094         2785 :   ord_loc_remap->truncate (dst - begin);
   19095              : 
   19096         2785 :   info.first = ord_loc_remap->length ();
   19097         2785 :   cfg->ordinary_locs = offset;
   19098         2785 :   cfg->loc_range_bits = range_bits;
   19099         3085 :   dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
   19100              :                    info.first,
   19101              :                    cfg->ordinary_locs,
   19102              :                    cfg->loc_range_bits);
   19103              : 
   19104              :   // Remap the macro locations.
   19105         2785 :   vec_alloc (macro_loc_remap, macro_loc_table->size ());
   19106         2785 :   for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
   19107       171032 :        iter != end; ++iter)
   19108       168247 :     macro_loc_remap->quick_push (*iter);
   19109         2785 :   delete macro_loc_table;
   19110         2785 :   macro_loc_table = nullptr;
   19111              : 
   19112         2785 :   macro_loc_remap->qsort (&macro_loc_info::compare);
   19113         2785 :   offset = 0;
   19114         8355 :   for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
   19115       171032 :        iter != end; ++iter)
   19116              :     {
   19117       168247 :       auto mac = iter->src;
   19118       168247 :       iter->remap = offset;
   19119       168247 :       offset += mac->n_tokens;
   19120              :     }
   19121         2785 :   info.second = macro_loc_remap->length ();
   19122         2785 :   cfg->macro_locs = offset;
   19123              : 
   19124         3085 :   dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
   19125              : 
   19126         2785 :   dump.outdent ();
   19127              : 
   19128              :   // If we have no ordinary locs, we must also have no macro locs.
   19129         2785 :   gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
   19130              : 
   19131         2785 :   return info;
   19132              : }
   19133              : 
   19134              : bool
   19135         3021 : module_state::read_prepare_maps (const module_state_config *cfg)
   19136              : {
   19137         3021 :   location_t ordinary = line_table->highest_location + 1;
   19138         3021 :   ordinary += cfg->ordinary_locs;
   19139              : 
   19140         3021 :   location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19141         3021 :   macro -= cfg->macro_locs;
   19142              : 
   19143         3021 :   if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
   19144         3021 :       && macro >= LINE_MAP_MAX_LOCATION)
   19145              :     /* OK, we have enough locations.  */
   19146              :     return true;
   19147              : 
   19148            0 :   ordinary_locs.first = ordinary_locs.second = 0;
   19149            0 :   macro_locs.first = macro_locs.second = 0;
   19150              : 
   19151            0 :   spans.report_location_exhaustion (loc);
   19152              : 
   19153              :   return false;
   19154              : }
   19155              : 
   19156              : /* Write & read the location maps. Not called if there are no
   19157              :    locations.   */
   19158              : 
   19159              : void
   19160         2686 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
   19161              :                                    bool has_partitions, unsigned *crc_p)
   19162              : {
   19163         2964 :   dump () && dump ("Writing ordinary location maps");
   19164         2686 :   dump.indent ();
   19165              : 
   19166         2686 :   vec<const char *> filenames;
   19167         2686 :   filenames.create (20);
   19168              : 
   19169              :   /* Determine the unique filenames.  */
   19170         2686 :   const line_map_ordinary *current = nullptr;
   19171       280938 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19172       275566 :        iter != end; ++iter)
   19173       272880 :     if (iter->src != current)
   19174              :       {
   19175        30285 :         current = iter->src;
   19176        30285 :         const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19177              : 
   19178              :         /* We should never find a module linemap in an interval.  */
   19179        30285 :         gcc_checking_assert (!MAP_MODULE_P (iter->src));
   19180              : 
   19181              :         /* We expect very few filenames, so just an array.
   19182              :            (Not true when headers are still in play :()  */
   19183      2263910 :         for (unsigned jx = filenames.length (); jx--;)
   19184              :           {
   19185      2217714 :             const char *name = filenames[jx];
   19186      2217714 :             if (0 == strcmp (name, fname))
   19187              :               {
   19188              :                 /* Reset the linemap's name, because for things like
   19189              :                    preprocessed input we could have multiple instances
   19190              :                    of the same name, and we'd rather not percolate
   19191              :                    that.  */
   19192        14374 :                 const_cast<line_map_ordinary *> (iter->src)->to_file = name;
   19193        14374 :                 fname = NULL;
   19194        14374 :                 break;
   19195              :               }
   19196              :           }
   19197        30285 :         if (fname)
   19198        15911 :           filenames.safe_push (fname);
   19199              :       }
   19200              : 
   19201         2686 :   bytes_out sec (to);
   19202         2686 :   sec.begin ();
   19203              : 
   19204              :   /* Write the filenames.  */
   19205         2686 :   unsigned len = filenames.length ();
   19206         2686 :   sec.u (len);
   19207         2964 :   dump () && dump ("%u source file names", len);
   19208        18597 :   for (unsigned ix = 0; ix != len; ix++)
   19209              :     {
   19210        15911 :       const char *fname = filenames[ix];
   19211        15926 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19212        15911 :       sec.str (fname);
   19213              :     }
   19214              : 
   19215         2686 :   sec.loc (info.first); /* Num maps.  */
   19216         2686 :   const ord_loc_info *base = nullptr;
   19217       280938 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19218       275566 :        iter != end; ++iter)
   19219              :     {
   19220       272880 :       dump (dumper::LOCATION)
   19221           36 :         && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
   19222           36 :                  (location_t) (iter - ord_loc_remap->begin ()),
   19223           18 :                  MAP_START_LOCATION (iter->src),
   19224              :                  iter->offset, iter->span, iter->remap,
   19225              :                  iter->span);
   19226              : 
   19227       272880 :       if (!base || iter->src != base->src)
   19228        30285 :         base = iter;
   19229       272880 :       sec.loc (iter->offset - base->offset);
   19230       272880 :       if (base == iter)
   19231              :         {
   19232        30285 :           sec.u (iter->src->sysp);
   19233        30285 :           sec.u (iter->src->m_range_bits);
   19234        30285 :           sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
   19235              : 
   19236        30285 :           const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19237      6859236 :           for (unsigned ix = 0; ix != filenames.length (); ix++)
   19238      3429618 :             if (filenames[ix] == fname)
   19239              :               {
   19240        30285 :                 sec.u (ix);
   19241        30285 :                 break;
   19242              :               }
   19243        30285 :           unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
   19244        30285 :           line += iter->offset >> iter->src->m_column_and_range_bits;
   19245        30285 :           sec.u (line);
   19246              :         }
   19247       272880 :       sec.loc (iter->remap);
   19248       272880 :       if (base == iter)
   19249              :         {
   19250              :           /* Write the included from location, which means reading it
   19251              :              while reading in the ordinary maps.  So we'd better not
   19252              :              be getting ahead of ourselves.  */
   19253        30285 :           location_t from = linemap_included_from (iter->src);
   19254        30285 :           gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
   19255        30285 :           if (from != UNKNOWN_LOCATION && has_partitions)
   19256              :             {
   19257              :               /* A partition's span will have a from pointing at a
   19258              :                  MODULE_INC.  Find that map's from.  */
   19259          217 :               line_map_ordinary const *fmap
   19260          217 :                 = linemap_check_ordinary (linemap_lookup (line_table, from));
   19261          217 :               if (MAP_MODULE_P (fmap))
   19262          184 :                 from = linemap_included_from (fmap);
   19263              :             }
   19264        30285 :           write_location (sec, from);
   19265              :         }
   19266              :     }
   19267              : 
   19268         2686 :   filenames.release ();
   19269              : 
   19270         2686 :   sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
   19271         2686 :   dump.outdent ();
   19272         2686 : }
   19273              : 
   19274              : /* Return the prefix to use for dumping a #pragma diagnostic change to DK.  */
   19275              : 
   19276              : static const char *
   19277         1036 : dk_string (enum diagnostics::kind dk)
   19278              : {
   19279         1036 :   gcc_assert (dk > diagnostics::kind::unspecified
   19280              :               && dk < diagnostics::kind::last_diagnostic_kind);
   19281         1036 :   if (dk == diagnostics::kind::ignored)
   19282              :     /* diagnostics/kinds.def has an empty string for ignored.  */
   19283              :     return "ignored: ";
   19284              :   else
   19285            0 :     return diagnostics::get_text_for_kind (dk);
   19286              : }
   19287              : 
   19288              : /* Dump one #pragma GCC diagnostic entry.  */
   19289              : 
   19290              : static bool
   19291         2116 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
   19292              : {
   19293         2116 :   if (dk == diagnostics::kind::pop)
   19294         1080 :     return dump (" Index %u: pop from %d", index, opt);
   19295              :   else
   19296         1036 :     return dump (" Index %u: %s%s", index, dk_string (dk),
   19297         2072 :                  cl_options[opt].opt_text);
   19298              : }
   19299              : 
   19300              : /* Write out any #pragma GCC diagnostic info to the .dgc section.  */
   19301              : 
   19302              : void
   19303         5471 : module_state::write_diagnostic_classification (elf_out *to,
   19304              :                                                diagnostics::context *dc,
   19305              :                                                unsigned *crc_p)
   19306              : {
   19307         5471 :   auto &changes = dc->get_classification_history ();
   19308              : 
   19309         5471 :   bytes_out sec (to);
   19310         5471 :   if (sec.streaming_p ())
   19311              :     {
   19312         2686 :       sec.begin ();
   19313         2964 :       dump () && dump ("Writing diagnostic change locations");
   19314         2686 :       dump.indent ();
   19315              :     }
   19316              : 
   19317         5471 :   unsigned len = changes.length ();
   19318              : 
   19319              :   /* We don't want to write out any entries that came from one of our imports.
   19320              :      But then we need to adjust the total, and change diagnostics::kind::pop
   19321              :      targets to match the index in our actual output.  So remember how many
   19322              :      lines we had skipped at each step, where -1 means this line itself
   19323              :      is skipped.  */
   19324         5471 :   int skips = 0;
   19325         5471 :   auto_vec<int> skips_at (len);
   19326         5471 :   skips_at.safe_grow (len);
   19327              : 
   19328        78922 :   for (unsigned i = 0; i < len; ++i)
   19329              :     {
   19330        67980 :       const auto &c = changes[i];
   19331        67980 :       skips_at[i] = skips;
   19332        67980 :       if (linemap_location_from_module_p (line_table, c.location))
   19333              :         {
   19334        14850 :           ++skips;
   19335        14850 :           skips_at[i] = -1;
   19336        14850 :           continue;
   19337              :         }
   19338              :     }
   19339              : 
   19340         5471 :   if (sec.streaming_p ())
   19341              :     {
   19342         2686 :       sec.u (len - skips);
   19343         2964 :       dump () && dump ("Diagnostic changes: %u", len - skips);
   19344              :     }
   19345              : 
   19346        73451 :   for (unsigned i = 0; i < len; ++i)
   19347              :     {
   19348        67980 :       if (skips_at[i] == -1)
   19349        14850 :         continue;
   19350              : 
   19351        53130 :       const auto &c = changes[i];
   19352        53130 :       write_location (sec, c.location);
   19353        53130 :       if (sec.streaming_p ())
   19354              :         {
   19355        26565 :           unsigned opt = c.option;
   19356        26565 :           if (c.kind == diagnostics::kind::pop)
   19357        13660 :             opt -= skips_at[opt];
   19358        26565 :           sec.u (opt);
   19359        26565 :           sec.u (static_cast<unsigned> (c.kind));
   19360        70029 :           dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
   19361              :         }
   19362              :     }
   19363              : 
   19364         5471 :   if (sec.streaming_p ())
   19365              :     {
   19366         2686 :       sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
   19367         2686 :       dump.outdent ();
   19368              :     }
   19369         5471 : }
   19370              : 
   19371              : /* Read any #pragma GCC diagnostic info from the .dgc section.  */
   19372              : 
   19373              : bool
   19374         2963 : module_state::read_diagnostic_classification (diagnostics::context *dc)
   19375              : {
   19376         2963 :   bytes_in sec;
   19377              : 
   19378         2963 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
   19379              :     return false;
   19380              : 
   19381         3483 :   dump () && dump ("Reading diagnostic change locations");
   19382         2963 :   dump.indent ();
   19383              : 
   19384         2963 :   unsigned len = sec.u ();
   19385         3483 :   dump () && dump ("Diagnostic changes: %u", len);
   19386              : 
   19387         2963 :   auto &changes = dc->get_classification_history ();
   19388         2963 :   int offset = changes.length ();
   19389         2963 :   changes.reserve (len + 1);
   19390        31560 :   for (unsigned i = 0; i < len; ++i)
   19391              :     {
   19392        25634 :       location_t loc = read_location (sec);
   19393        25634 :       int opt = sec.u ();
   19394        25634 :       enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
   19395        25634 :       if (kind == diagnostics::kind::pop)
   19396              :         /* For a pop, opt is the 'changes' index to return to.  */
   19397        13188 :         opt += offset;
   19398        25634 :       changes.quick_push ({ loc, opt, kind });
   19399        25701 :       dump () && dump_dc_change (changes.length () - 1, opt, kind);
   19400              :     }
   19401              : 
   19402              :   /* Did the import pop all its diagnostic changes?  */
   19403         2963 :   bool last_was_reset = (len == 0);
   19404         2963 :   if (len)
   19405          238 :     for (int i = changes.length () - 1; ; --i)
   19406              :       {
   19407        11419 :         gcc_checking_assert (i >= offset);
   19408              : 
   19409        11419 :         const auto &c = changes[i];
   19410        11419 :         if (c.kind != diagnostics::kind::pop)
   19411              :           break;
   19412        11410 :         else if (c.option == offset)
   19413              :           {
   19414              :             last_was_reset = true;
   19415              :             break;
   19416              :           }
   19417              :         else
   19418              :           /* As in update_effective_level_from_pragmas, the loop will decrement
   19419              :              i so we actually jump to c.option - 1.  */
   19420        11300 :           i = c.option;
   19421        11300 :       }
   19422         2963 :   if (!last_was_reset)
   19423              :     {
   19424              :       /* It didn't, so add a pop at its last location to avoid affecting later
   19425              :          imports.  */
   19426            9 :       location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
   19427            9 :       changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
   19428           15 :       dump () && dump (" Adding final pop from index %d", offset);
   19429              :     }
   19430              : 
   19431         2963 :   dump.outdent ();
   19432         2963 :   if (!sec.end (from ()))
   19433              :     return false;
   19434              : 
   19435              :   return true;
   19436         2963 : }
   19437              : 
   19438              : void
   19439          128 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
   19440              : {
   19441          140 :   dump () && dump ("Writing macro location maps");
   19442          128 :   dump.indent ();
   19443              : 
   19444          128 :   bytes_out sec (to);
   19445          128 :   sec.begin ();
   19446              : 
   19447          140 :   dump () && dump ("Macro maps:%K", info.second);
   19448          128 :   sec.loc (info.second);
   19449              : 
   19450          128 :   line_map_uint_t macro_num = 0;
   19451          256 :   for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
   19452       168375 :        iter-- != begin;)
   19453              :     {
   19454       168247 :       auto mac = iter->src;
   19455       168247 :       sec.loc (iter->remap);
   19456       168247 :       sec.u (mac->n_tokens);
   19457       168247 :       sec.cpp_node (mac->macro);
   19458       168247 :       write_location (sec, mac->m_expansion);
   19459       168247 :       const location_t *locs = mac->macro_locations;
   19460              :       /* There are lots of identical runs.  */
   19461       168247 :       location_t prev = UNKNOWN_LOCATION;
   19462       168247 :       unsigned count = 0;
   19463       168247 :       unsigned runs = 0;
   19464      6338437 :       for (unsigned jx = mac->n_tokens * 2; jx--;)
   19465              :         {
   19466      6170190 :           location_t tok_loc = locs[jx];
   19467      6170190 :           if (tok_loc == prev)
   19468              :             {
   19469      2912020 :               count++;
   19470      2912020 :               continue;
   19471              :             }
   19472      3258170 :           runs++;
   19473      3258170 :           sec.u (count);
   19474      3258170 :           count = 1;
   19475      3258170 :           prev = tok_loc;
   19476      3258170 :           write_location (sec, tok_loc);
   19477              :         }
   19478       168247 :       sec.u (count);
   19479       168247 :       dump (dumper::LOCATION)
   19480            9 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
   19481            9 :                  macro_num, identifier (mac->macro),
   19482              :                  runs, mac->n_tokens,
   19483              :                  MAP_START_LOCATION (mac),
   19484            9 :                  MAP_START_LOCATION (mac) + mac->n_tokens,
   19485              :                  iter->remap);
   19486       168247 :       macro_num++;
   19487              :     }
   19488          128 :   gcc_assert (macro_num == info.second);
   19489              : 
   19490          128 :   sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
   19491          128 :   dump.outdent ();
   19492          128 : }
   19493              : 
   19494              : bool
   19495         2963 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
   19496              :                                   unsigned range_bits)
   19497              : {
   19498         2963 :   bytes_in sec;
   19499              : 
   19500         2963 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
   19501              :     return false;
   19502         3483 :   dump () && dump ("Reading ordinary location maps");
   19503         2963 :   dump.indent ();
   19504              : 
   19505              :   /* Read the filename table.  */
   19506         2963 :   unsigned len = sec.u ();
   19507         3483 :   dump () && dump ("%u source file names", len);
   19508         2963 :   vec<const char *> filenames;
   19509         2963 :   filenames.create (len);
   19510        23058 :   for (unsigned ix = 0; ix != len; ix++)
   19511              :     {
   19512        17132 :       size_t l;
   19513        17132 :       const char *buf = sec.str (&l);
   19514        17132 :       char *fname = XNEWVEC (char, l + 1);
   19515        17132 :       memcpy (fname, buf, l + 1);
   19516        17132 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19517              :       /* We leak these names into the line-map table.  But it
   19518              :          doesn't own them.  */
   19519        17132 :       filenames.quick_push (fname);
   19520              :     }
   19521              : 
   19522         2963 :   line_map_uint_t num_ordinary = sec.loc ();
   19523         3483 :   dump () && dump ("Ordinary maps:%K, range_bits:%u",
   19524              :                    num_ordinary, range_bits);
   19525              : 
   19526         2963 :   location_t offset = line_table->highest_location + 1;
   19527         2963 :   offset += ((loc_one << range_bits) - 1);
   19528         2963 :   offset &= ~((loc_one << range_bits) - 1);
   19529         2963 :   ordinary_locs.first = offset;
   19530              : 
   19531         2963 :   bool propagated = spans.maybe_propagate (this, offset);
   19532         2963 :   line_map_ordinary *maps = static_cast<line_map_ordinary *>
   19533         2963 :     (line_map_new_raw (line_table, false, num_ordinary));
   19534              : 
   19535         2963 :   const line_map_ordinary *base = nullptr;
   19536       311002 :   for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
   19537              :     {
   19538       305076 :       line_map_ordinary *map = &maps[ix];
   19539              : 
   19540       305076 :       location_t offset = sec.loc ();
   19541       305076 :       if (!offset)
   19542              :         {
   19543        33565 :           map->reason = LC_RENAME;
   19544        33565 :           map->sysp = sec.u ();
   19545        33565 :           map->m_range_bits = sec.u ();
   19546        33565 :           map->m_column_and_range_bits = sec.u () + map->m_range_bits;
   19547        33565 :           unsigned fnum = sec.u ();
   19548        67130 :           map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
   19549        33565 :           map->to_line = sec.u ();
   19550        33565 :           base = map;
   19551              :         }
   19552              :       else
   19553              :         {
   19554       271511 :           *map = *base;
   19555       271511 :           map->to_line += offset >> map->m_column_and_range_bits;
   19556              :         }
   19557       305076 :       location_t remap = sec.loc ();
   19558       305076 :       map->start_location = remap + ordinary_locs.first;
   19559       305076 :       if (base == map)
   19560              :         {
   19561              :           /* Root the outermost map at our location.  */
   19562        33565 :           ordinary_locs.second = remap;
   19563        33565 :           location_t from = read_location (sec);
   19564        33565 :           map->included_from = from != UNKNOWN_LOCATION ? from : loc;
   19565              :         }
   19566              :     }
   19567              : 
   19568         2963 :   ordinary_locs.second = num_ord_locs;
   19569              :   /* highest_location is the one handed out, not the next one to
   19570              :      hand out.  */
   19571         2963 :   line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
   19572              : 
   19573         2963 :   if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
   19574              :     /* We shouldn't run out of locations, as we checked before
   19575              :        starting.  */
   19576            0 :     sec.set_overrun ();
   19577         3483 :   dump () && dump ("Ordinary location [%K,+%K)",
   19578              :                    ordinary_locs.first, ordinary_locs.second);
   19579              : 
   19580         2963 :   if (propagated)
   19581          175 :     spans.close ();
   19582              : 
   19583         2963 :   filenames.release ();
   19584              : 
   19585         2963 :   dump.outdent ();
   19586         2963 :   if (!sec.end (from ()))
   19587              :     return false;
   19588              : 
   19589              :   return true;
   19590         2963 : }
   19591              : 
   19592              : bool
   19593          136 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
   19594              : {
   19595          136 :   bytes_in sec;
   19596              : 
   19597          136 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
   19598              :     return false;
   19599          145 :   dump () && dump ("Reading macro location maps");
   19600          136 :   dump.indent ();
   19601              : 
   19602          136 :   line_map_uint_t num_macros = sec.loc ();
   19603          145 :   dump () && dump ("Macro maps:%K locs:%K",
   19604              :                    num_macros, num_macro_locs);
   19605              : 
   19606          272 :   bool propagated = spans.maybe_propagate (this,
   19607          136 :                                            line_table->highest_location + 1);
   19608              : 
   19609          136 :   location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19610          136 :   macro_locs.second = num_macro_locs;
   19611          136 :   macro_locs.first = offset - num_macro_locs;
   19612              : 
   19613          145 :   dump () && dump ("Macro loc delta %K", offset);
   19614          145 :   dump () && dump ("Macro locations [%K,%K)",
   19615              :                    macro_locs.first, macro_locs.second);
   19616              : 
   19617       206535 :   for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
   19618              :     {
   19619       206399 :       location_t offset = sec.loc ();
   19620       206399 :       unsigned n_tokens = sec.u ();
   19621       206399 :       cpp_hashnode *node = sec.cpp_node ();
   19622       206399 :       location_t exp_loc = read_location (sec);
   19623              : 
   19624       206399 :       const line_map_macro *macro
   19625       206399 :         = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
   19626       206399 :       if (!macro)
   19627              :         /* We shouldn't run out of locations, as we checked that we
   19628              :            had enough before starting.  */
   19629              :         break;
   19630       206399 :       gcc_checking_assert (MAP_START_LOCATION (macro)
   19631              :                            == offset + macro_locs.first);
   19632              : 
   19633       206399 :       location_t *locs = macro->macro_locations;
   19634       206399 :       location_t tok_loc = UNKNOWN_LOCATION;
   19635       206399 :       unsigned count = sec.u ();
   19636       206399 :       unsigned runs = 0;
   19637      7590533 :       for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
   19638              :         {
   19639     11296212 :           while (!count-- && !sec.get_overrun ())
   19640              :             {
   19641      3912078 :               runs++;
   19642      3912078 :               tok_loc = read_location (sec);
   19643      3912078 :               count = sec.u ();
   19644              :             }
   19645      7384134 :           locs[jx] = tok_loc;
   19646              :         }
   19647       206399 :       if (count)
   19648            0 :         sec.set_overrun ();
   19649       206429 :       dump (dumper::LOCATION)
   19650            0 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
   19651              :                  ix, identifier (node), runs, n_tokens,
   19652              :                  MAP_START_LOCATION (macro),
   19653            0 :                  MAP_START_LOCATION (macro) + n_tokens);
   19654              :     }
   19655              : 
   19656          145 :   dump () && dump ("Macro location lwm:%K", macro_locs.first);
   19657          136 :   if (propagated)
   19658            3 :     spans.close ();
   19659              : 
   19660          136 :   dump.outdent ();
   19661          136 :   if (!sec.end (from ()))
   19662              :     return false;
   19663              : 
   19664              :   return true;
   19665          136 : }
   19666              : 
   19667              : /* Serialize the definition of MACRO.  */
   19668              : 
   19669              : void
   19670        78790 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
   19671              : {
   19672        78790 :   sec.u (macro->count);
   19673              : 
   19674        78790 :   bytes_out::bits_out bits = sec.stream_bits ();
   19675        78790 :   bits.b (macro->fun_like);
   19676        78790 :   bits.b (macro->variadic);
   19677        78790 :   bits.b (macro->syshdr);
   19678        78790 :   bits.bflush ();
   19679              : 
   19680        78790 :   write_location (sec, macro->line);
   19681        78790 :   if (macro->fun_like)
   19682              :     {
   19683         9800 :       sec.u (macro->paramc);
   19684         9800 :       const cpp_hashnode *const *parms = macro->parm.params;
   19685        24668 :       for (unsigned ix = 0; ix != macro->paramc; ix++)
   19686        14868 :         sec.cpp_node (parms[ix]);
   19687              :     }
   19688              : 
   19689        78790 :   unsigned len = 0;
   19690       251421 :   for (unsigned ix = 0; ix != macro->count; ix++)
   19691              :     {
   19692       172631 :       const cpp_token *token = &macro->exp.tokens[ix];
   19693       172631 :       write_location (sec, token->src_loc);
   19694       172631 :       sec.u (token->type);
   19695       172631 :       sec.u (token->flags);
   19696       172631 :       switch (cpp_token_val_index (token))
   19697              :         {
   19698            0 :         default:
   19699            0 :           gcc_unreachable ();
   19700              : 
   19701        13067 :         case CPP_TOKEN_FLD_ARG_NO:
   19702              :           /* An argument reference.  */
   19703        13067 :           sec.u (token->val.macro_arg.arg_no);
   19704        13067 :           sec.cpp_node (token->val.macro_arg.spelling);
   19705        13067 :           break;
   19706              : 
   19707        33593 :         case CPP_TOKEN_FLD_NODE:
   19708              :           /* An identifier.  */
   19709        33593 :           sec.cpp_node (token->val.node.node);
   19710        33593 :           if (token->val.node.spelling == token->val.node.node)
   19711              :             /* The spelling will usually be the same.  so optimize
   19712              :                that.  */
   19713        33593 :             sec.str (NULL, 0);
   19714              :           else
   19715            0 :             sec.cpp_node (token->val.node.spelling);
   19716              :           break;
   19717              : 
   19718              :         case CPP_TOKEN_FLD_NONE:
   19719              :           break;
   19720              : 
   19721        56367 :         case CPP_TOKEN_FLD_STR:
   19722              :           /* A string, number or comment.  Not always NUL terminated,
   19723              :              we stream out in a single concatenation with embedded
   19724              :              NULs as that's a safe default.  */
   19725        56367 :           len += token->val.str.len + 1;
   19726        56367 :           sec.u (token->val.str.len);
   19727        56367 :           break;
   19728              : 
   19729            0 :         case CPP_TOKEN_FLD_SOURCE:
   19730            0 :         case CPP_TOKEN_FLD_TOKEN_NO:
   19731            0 :         case CPP_TOKEN_FLD_PRAGMA:
   19732              :           /* These do not occur inside a macro itself.  */
   19733            0 :           gcc_unreachable ();
   19734              :         }
   19735              :     }
   19736              : 
   19737        78790 :   if (len)
   19738              :     {
   19739        52743 :       char *ptr = reinterpret_cast<char *> (sec.buf (len));
   19740        52743 :       len = 0;
   19741       209150 :       for (unsigned ix = 0; ix != macro->count; ix++)
   19742              :         {
   19743       103664 :           const cpp_token *token = &macro->exp.tokens[ix];
   19744       103664 :           if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19745              :             {
   19746        56367 :               memcpy (ptr + len, token->val.str.text,
   19747        56367 :                       token->val.str.len);
   19748        56367 :               len += token->val.str.len;
   19749        56367 :               ptr[len++] = 0;
   19750              :             }
   19751              :         }
   19752              :     }
   19753        78790 : }
   19754              : 
   19755              : /* Read a macro definition.  */
   19756              : 
   19757              : cpp_macro *
   19758          807 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
   19759              : {
   19760          807 :   unsigned count = sec.u ();
   19761              :   /* We rely on knowing cpp_reader's hash table is ident_hash, and
   19762              :      its subobject allocator is stringpool_ggc_alloc and that is just
   19763              :      a wrapper for ggc_alloc_atomic.  */
   19764          807 :   cpp_macro *macro
   19765         1614 :     = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
   19766          807 :                                      + sizeof (cpp_token) * (count - !!count));
   19767          807 :   memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
   19768              : 
   19769          807 :   macro->count = count;
   19770          807 :   macro->kind = cmk_macro;
   19771          807 :   macro->imported_p = true;
   19772              : 
   19773          807 :   bytes_in::bits_in bits = sec.stream_bits ();
   19774          807 :   macro->fun_like = bits.b ();
   19775          807 :   macro->variadic = bits.b ();
   19776          807 :   macro->syshdr = bits.b ();
   19777          807 :   bits.bflush ();
   19778              : 
   19779          807 :   macro->line = read_location (sec);
   19780              : 
   19781          807 :   if (macro->fun_like)
   19782              :     {
   19783           83 :       unsigned paramc = sec.u ();
   19784           83 :       cpp_hashnode **params
   19785           83 :         = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
   19786           83 :       macro->paramc = paramc;
   19787           83 :       macro->parm.params = params;
   19788          177 :       for (unsigned ix = 0; ix != paramc; ix++)
   19789           94 :         params[ix] = sec.cpp_node ();
   19790              :     }
   19791              : 
   19792          807 :   unsigned len = 0;
   19793         2232 :   for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19794              :     {
   19795         1425 :       cpp_token *token = &macro->exp.tokens[ix];
   19796         1425 :       token->src_loc = read_location (sec);
   19797         1425 :       token->type = cpp_ttype (sec.u ());
   19798         1425 :       token->flags = sec.u ();
   19799         1425 :       switch (cpp_token_val_index (token))
   19800              :         {
   19801            0 :         default:
   19802            0 :           sec.set_overrun ();
   19803            0 :           break;
   19804              : 
   19805           77 :         case CPP_TOKEN_FLD_ARG_NO:
   19806              :           /* An argument reference.  */
   19807           77 :           {
   19808           77 :             unsigned arg_no = sec.u ();
   19809           77 :             if (arg_no - 1 >= macro->paramc)
   19810            0 :               sec.set_overrun ();
   19811           77 :             token->val.macro_arg.arg_no = arg_no;
   19812           77 :             token->val.macro_arg.spelling = sec.cpp_node ();
   19813              :           }
   19814           77 :           break;
   19815              : 
   19816          273 :         case CPP_TOKEN_FLD_NODE:
   19817              :           /* An identifier.  */
   19818          273 :           token->val.node.node = sec.cpp_node ();
   19819          273 :           token->val.node.spelling = sec.cpp_node ();
   19820          273 :           if (!token->val.node.spelling)
   19821          273 :             token->val.node.spelling = token->val.node.node;
   19822              :           break;
   19823              : 
   19824              :         case CPP_TOKEN_FLD_NONE:
   19825              :           break;
   19826              : 
   19827          629 :         case CPP_TOKEN_FLD_STR:
   19828              :           /* A string, number or comment.  */
   19829          629 :           token->val.str.len = sec.u ();
   19830          629 :           len += token->val.str.len + 1;
   19831          629 :           break;
   19832              :         }
   19833              :     }
   19834              : 
   19835          807 :   if (len)
   19836          626 :     if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
   19837              :       {
   19838              :         /* There should be a final NUL.  */
   19839          626 :         if (ptr[len-1])
   19840            0 :           sec.set_overrun ();
   19841              :         /* cpp_alloc_token_string will add a final NUL.  */
   19842          626 :         const unsigned char *buf
   19843          626 :           = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
   19844          626 :         len = 0;
   19845         2201 :         for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19846              :           {
   19847          949 :             cpp_token *token = &macro->exp.tokens[ix];
   19848          949 :             if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19849              :               {
   19850          629 :                 token->val.str.text = buf + len;
   19851          629 :                 len += token->val.str.len;
   19852          629 :                 if (buf[len++])
   19853            0 :                   sec.set_overrun ();
   19854              :               }
   19855              :           }
   19856              :       }
   19857              : 
   19858          807 :   if (sec.get_overrun ())
   19859            0 :     return NULL;
   19860              :   return macro;
   19861          807 : }
   19862              : 
   19863              : /* Exported macro data.  */
   19864              : struct GTY(()) macro_export {
   19865              :   cpp_macro *def;
   19866              :   location_t undef_loc;
   19867              : 
   19868       110955 :   macro_export ()
   19869       110955 :     :def (NULL), undef_loc (UNKNOWN_LOCATION)
   19870              :   {
   19871              :   }
   19872              : };
   19873              : 
   19874              : /* Imported macro data.  */
   19875              : class macro_import {
   19876              : public:
   19877              :   struct slot {
   19878              : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
   19879              :     int offset;
   19880              : #endif
   19881              :     /* We need to ensure we don't use the LSB for representation, as
   19882              :        that's the union discriminator below.  */
   19883              :     unsigned bits;
   19884              : 
   19885              : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
   19886              :     int offset;
   19887              : #endif
   19888              : 
   19889              :   public:
   19890              :     enum Layout {
   19891              :       L_DEF = 1,
   19892              :       L_UNDEF = 2,
   19893              :       L_BOTH = 3,
   19894              :       L_MODULE_SHIFT = 2
   19895              :     };
   19896              : 
   19897              :   public:
   19898              :     /* Not a regular ctor, because we put it in a union, and that's
   19899              :        not allowed in C++ 98.  */
   19900       198046 :     static slot ctor (unsigned module, unsigned defness)
   19901              :     {
   19902       198046 :       gcc_checking_assert (defness);
   19903       198046 :       slot s;
   19904       198046 :       s.bits = defness | (module << L_MODULE_SHIFT);
   19905       198046 :       s.offset = -1;
   19906       198046 :       return s;
   19907              :     }
   19908              : 
   19909              :   public:
   19910       160596 :     unsigned get_defness () const
   19911              :     {
   19912       160596 :       return bits & L_BOTH;
   19913              :     }
   19914       113783 :     unsigned get_module () const
   19915              :     {
   19916       113783 :       return bits >> L_MODULE_SHIFT;
   19917              :     }
   19918           12 :     void become_undef ()
   19919              :     {
   19920           12 :       bits &= ~unsigned (L_DEF);
   19921           12 :       bits |= unsigned (L_UNDEF);
   19922              :     }
   19923              :   };
   19924              : 
   19925              : private:
   19926              :   typedef vec<slot, va_heap, vl_embed> ary_t;
   19927              :   union either {
   19928              :     /* Discriminated by bits 0|1 != 0.  The expected case is that
   19929              :        there will be exactly one slot per macro, hence the effort of
   19930              :        packing that.  */
   19931              :     ary_t *ary;
   19932              :     slot single;
   19933              :   } u;
   19934              : 
   19935              : public:
   19936       161630 :   macro_import ()
   19937              :   {
   19938       161630 :     u.ary = NULL;
   19939              :   }
   19940              : 
   19941              : private:
   19942      8701382 :   bool single_p () const
   19943              :   {
   19944      8701382 :     return u.single.bits & slot::L_BOTH;
   19945              :   }
   19946      8863145 :   bool occupied_p () const
   19947              :   {
   19948      8863145 :     return u.ary != NULL;
   19949              :   }
   19950              : 
   19951              : public:
   19952         2379 :   unsigned length () const
   19953              :   {
   19954         2379 :     gcc_checking_assert (occupied_p ());
   19955         2379 :     return single_p () ? 1 : u.ary->length ();
   19956              :   }
   19957      8552445 :   slot &operator[] (unsigned ix)
   19958              :   {
   19959      8552445 :     gcc_checking_assert (occupied_p ());
   19960      8552445 :     if (single_p ())
   19961              :       {
   19962      8445680 :         gcc_checking_assert (!ix);
   19963      8445680 :         return u.single;
   19964              :       }
   19965              :     else
   19966       106765 :       return (*u.ary)[ix];
   19967              :   }
   19968              : 
   19969              : public:
   19970              :   slot &exported ();
   19971              :   slot &append (unsigned module, unsigned defness);
   19972              : };
   19973              : 
   19974              : /* O is a new import to append to the list for.  If we're an empty
   19975              :    set, initialize us.  */
   19976              : 
   19977              : macro_import::slot &
   19978       198046 : macro_import::append (unsigned module, unsigned defness)
   19979              : {
   19980       198046 :   if (!occupied_p ())
   19981              :     {
   19982       161630 :       u.single = slot::ctor (module, defness);
   19983       161630 :       return u.single;
   19984              :     }
   19985              :   else
   19986              :     {
   19987        36416 :       bool single = single_p ();
   19988        36416 :       ary_t *m = single ? NULL : u.ary;
   19989        36416 :       vec_safe_reserve (m, 1 + single);
   19990        36416 :       if (single)
   19991        36413 :         m->quick_push (u.single);
   19992        36416 :       u.ary = m;
   19993        36416 :       return *u.ary->quick_push (slot::ctor (module, defness));
   19994              :     }
   19995              : }
   19996              : 
   19997              : /* We're going to export something.  Make sure the first import slot
   19998              :    is us.  */
   19999              : 
   20000              : macro_import::slot &
   20001       110275 : macro_import::exported ()
   20002              : {
   20003       110275 :   if (occupied_p () && !(*this)[0].get_module ())
   20004              :     {
   20005          133 :       slot &res = (*this)[0];
   20006          133 :       res.bits |= slot::L_DEF;
   20007          133 :       return res;
   20008              :     }
   20009              : 
   20010       110142 :   slot *a = &append (0, slot::L_DEF);
   20011       110142 :   if (!single_p ())
   20012              :     {
   20013        32060 :       slot &f = (*this)[0];
   20014        32060 :       std::swap (f, *a);
   20015        32060 :       a = &f;
   20016              :     }
   20017              :   return *a;
   20018              : }
   20019              : 
   20020              : /* The import (&exported) macros.  cpp_hasnode's deferred field
   20021              :    indexes this array (offset by 1, so zero means 'not present'.  */
   20022              : 
   20023              : static vec<macro_import, va_heap, vl_embed> *macro_imports;
   20024              : 
   20025              : /* The exported macros.  A macro_import slot's zeroth element's offset
   20026              :    indexes this array.  If the zeroth slot is not for module zero,
   20027              :    there is no export.  */
   20028              : 
   20029              : static GTY(()) vec<macro_export, va_gc> *macro_exports;
   20030              : 
   20031              : /* The reachable set of header imports from this TU.  */
   20032              : 
   20033              : static GTY(()) bitmap headers;
   20034              : 
   20035              : /* Get the (possibly empty) macro imports for NODE.  */
   20036              : 
   20037              : static macro_import &
   20038       166119 : get_macro_imports (cpp_hashnode *node)
   20039              : {
   20040       166119 :   if (node->deferred)
   20041         4489 :     return (*macro_imports)[node->deferred - 1];
   20042              : 
   20043       161630 :   vec_safe_reserve (macro_imports, 1);
   20044       161630 :   node->deferred = macro_imports->length () + 1;
   20045       161630 :   return *vec_safe_push (macro_imports, macro_import ());
   20046              : }
   20047              : 
   20048              : /* Get the macro export for export EXP of NODE.  */
   20049              : 
   20050              : static macro_export &
   20051       110275 : get_macro_export (macro_import::slot &slot)
   20052              : {
   20053       110275 :   if (slot.offset >= 0)
   20054          133 :     return (*macro_exports)[slot.offset];
   20055              : 
   20056       110142 :   vec_safe_reserve (macro_exports, 1);
   20057       110142 :   slot.offset = macro_exports->length ();
   20058       110142 :   return *macro_exports->quick_push (macro_export ());
   20059              : }
   20060              : 
   20061              : /* If NODE is an exportable macro, add it to the export set.  */
   20062              : 
   20063              : static int
   20064      4001021 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
   20065              : {
   20066      4001021 :   bool exporting = false;
   20067              : 
   20068      4001021 :   if (cpp_user_macro_p (node))
   20069       514397 :     if (cpp_macro *macro = node->value.macro)
   20070              :       /* Ignore imported, builtins, command line and forced header macros.  */
   20071       513940 :       if (!macro->imported_p
   20072       513940 :           && !macro->lazy && macro->line >= spans.main_start ())
   20073              :         {
   20074        78215 :           gcc_checking_assert (macro->kind == cmk_macro);
   20075              :           /* I don't want to deal with this corner case, that I suspect is
   20076              :              a devil's advocate reading of the standard.  */
   20077        78215 :           gcc_checking_assert (!macro->extra_tokens);
   20078              : 
   20079        78215 :           macro_import::slot &slot = get_macro_imports (node).exported ();
   20080        78215 :           macro_export &exp = get_macro_export (slot);
   20081        78215 :           exp.def = macro;
   20082        78215 :           exporting = true;
   20083              :         }
   20084              : 
   20085      3922806 :   if (!exporting && node->deferred)
   20086              :     {
   20087          613 :       macro_import &imports = (*macro_imports)[node->deferred - 1];
   20088          613 :       macro_import::slot &slot = imports[0];
   20089          613 :       if (!slot.get_module ())
   20090              :         {
   20091          582 :           gcc_checking_assert (slot.get_defness ());
   20092              :           exporting = true;
   20093              :         }
   20094              :     }
   20095              : 
   20096        78215 :   if (exporting)
   20097        78797 :     static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
   20098              : 
   20099      4001021 :   return 1; /* Don't stop.  */
   20100              : }
   20101              : 
   20102              : /* Order cpp_hashnodes A_ and B_ by their exported macro locations.  */
   20103              : 
   20104              : static int
   20105      4124113 : macro_loc_cmp (const void *a_, const void *b_)
   20106              : {
   20107      4124113 :   const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
   20108      4124113 :   macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
   20109      4124113 :   const macro_export &export_a = (*macro_exports)[import_a[0].offset];
   20110      4124113 :   location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
   20111              : 
   20112      4124113 :   const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
   20113      4124113 :   macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
   20114      4124113 :   const macro_export &export_b = (*macro_exports)[import_b[0].offset];
   20115      4124113 :   location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
   20116              : 
   20117      4124113 :   if (loc_a < loc_b)
   20118              :     return +1;
   20119      2119075 :   else if (loc_a > loc_b)
   20120              :     return -1;
   20121              :   else
   20122            0 :     return 0;
   20123              : }
   20124              : 
   20125              : /* Gather the macro definitions and undefinitions that we will need to
   20126              :    write out.   */
   20127              : 
   20128              : vec<cpp_hashnode *> *
   20129          909 : module_state::prepare_macros (cpp_reader *reader)
   20130              : {
   20131          909 :   vec<cpp_hashnode *> *macros;
   20132          909 :   vec_alloc (macros, 100);
   20133              : 
   20134          909 :   cpp_forall_identifiers (reader, maybe_add_macro, macros);
   20135              : 
   20136          933 :   dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
   20137              : 
   20138          909 :   macros->qsort (macro_loc_cmp);
   20139              : 
   20140              :   // Note the locations.
   20141        80615 :   for (unsigned ix = macros->length (); ix--;)
   20142              :     {
   20143        78797 :       cpp_hashnode *node = (*macros)[ix];
   20144        78797 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20145        78797 :       macro_export &mac = (*macro_exports)[slot.offset];
   20146              : 
   20147        78797 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20148            1 :         continue;
   20149              : 
   20150        78796 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20151           12 :         note_location (mac.undef_loc);
   20152        78796 :       if (mac.def)
   20153              :         {
   20154        78790 :           note_location (mac.def->line);
   20155       330211 :           for (unsigned ix = 0; ix != mac.def->count; ix++)
   20156       172631 :             note_location (mac.def->exp.tokens[ix].src_loc);
   20157              :         }
   20158              :     }
   20159              : 
   20160          909 :   return macros;
   20161              : }
   20162              : 
   20163              : /* Write out the exported defines.  This is two sections, one
   20164              :    containing the definitions, the other a table of node names.  */
   20165              : 
   20166              : unsigned
   20167          909 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
   20168              :                             unsigned *crc_p)
   20169              : {
   20170          976 :   dump () && dump ("Writing macros");
   20171          909 :   dump.indent ();
   20172              : 
   20173              :   /* Write the defs */
   20174          909 :   bytes_out sec (to);
   20175          909 :   sec.begin ();
   20176              : 
   20177          909 :   unsigned count = 0;
   20178        80615 :   for (unsigned ix = macros->length (); ix--;)
   20179              :     {
   20180        78797 :       cpp_hashnode *node = (*macros)[ix];
   20181        78797 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20182        78797 :       gcc_assert (!slot.get_module () && slot.get_defness ());
   20183              : 
   20184        78797 :       macro_export &mac = (*macro_exports)[slot.offset];
   20185        78797 :       gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
   20186              :                   == (mac.undef_loc != UNKNOWN_LOCATION)
   20187              :                   && !!(slot.get_defness () & macro_import::slot::L_DEF)
   20188              :                   == (mac.def != NULL));
   20189              : 
   20190        78797 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20191              :         {
   20192            1 :           warning_at (mac.def->line, 0,
   20193              :                       "not exporting %<#define %E%> as it is a keyword",
   20194              :                       identifier (node));
   20195            1 :           slot.offset = 0;
   20196            1 :           continue;
   20197              :         }
   20198              : 
   20199        78796 :       count++;
   20200        78796 :       slot.offset = sec.pos;
   20201        78796 :       dump (dumper::MACRO)
   20202           24 :         && dump ("Writing macro %s%s%s %I at %u",
   20203           24 :                  slot.get_defness () & macro_import::slot::L_UNDEF
   20204              :                  ? "#undef" : "",
   20205           24 :                  slot.get_defness () == macro_import::slot::L_BOTH
   20206              :                  ? " & " : "",
   20207           24 :                  slot.get_defness () & macro_import::slot::L_DEF
   20208              :                  ? "#define" : "",
   20209              :                  identifier (node), slot.offset);
   20210        78796 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20211           12 :         write_location (sec, mac.undef_loc);
   20212        78796 :       if (mac.def)
   20213        78790 :         write_define (sec, mac.def);
   20214              :     }
   20215          909 :   if (count)
   20216              :     // We may have ended on a tokenless macro with a very short
   20217              :     // location, that will cause problems reading its bit flags.
   20218          145 :     sec.u (0);
   20219          909 :   sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
   20220              : 
   20221          909 :   if (count)
   20222              :     {
   20223              :       /* Write the table.  */
   20224          145 :       bytes_out sec (to);
   20225          145 :       sec.begin ();
   20226          145 :       sec.u (count);
   20227              : 
   20228        79086 :       for (unsigned ix = macros->length (); ix--;)
   20229              :         {
   20230        78796 :           const cpp_hashnode *node = (*macros)[ix];
   20231        78796 :           macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20232              : 
   20233        78796 :           if (slot.offset)
   20234              :             {
   20235        78796 :               sec.cpp_node (node);
   20236        78796 :               sec.u (slot.get_defness ());
   20237        78796 :               sec.u (slot.offset);
   20238              :             }
   20239              :         }
   20240          145 :       sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
   20241          145 :     }
   20242              : 
   20243          909 :   dump.outdent ();
   20244          909 :   return count;
   20245          909 : }
   20246              : 
   20247              : bool
   20248          941 : module_state::read_macros ()
   20249              : {
   20250              :   /* Get the def section.  */
   20251          941 :   if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
   20252              :     return false;
   20253              : 
   20254              :   /* Get the tbl section, if there are defs. */
   20255          941 :   if (slurp->macro_defs.more_p ()
   20256          941 :       && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
   20257              :     return false;
   20258              : 
   20259              :   return true;
   20260              : }
   20261              : 
   20262              : /* Install the macro name table.  */
   20263              : 
   20264              : void
   20265          947 : module_state::install_macros ()
   20266              : {
   20267          947 :   bytes_in &sec = slurp->macro_tbl;
   20268          947 :   if (!sec.size)
   20269              :     return;
   20270              : 
   20271          204 :   dump () && dump ("Reading macro table %M", this);
   20272          182 :   dump.indent ();
   20273              : 
   20274          182 :   unsigned count = sec.u ();
   20275          204 :   dump () && dump ("%u macros", count);
   20276        88086 :   while (count--)
   20277              :     {
   20278        87904 :       cpp_hashnode *node = sec.cpp_node ();
   20279        87904 :       macro_import &imp = get_macro_imports (node);
   20280        87904 :       unsigned flags = sec.u () & macro_import::slot::L_BOTH;
   20281        87904 :       if (!flags)
   20282            0 :         sec.set_overrun ();
   20283              : 
   20284        87904 :       if (sec.get_overrun ())
   20285              :         break;
   20286              : 
   20287        87904 :       macro_import::slot &slot = imp.append (mod, flags);
   20288        87904 :       slot.offset = sec.u ();
   20289              : 
   20290        87904 :       dump (dumper::MACRO)
   20291           84 :         && dump ("Read %s macro %s%s%s %I at %u",
   20292           30 :                  imp.length () > 1 ? "add" : "new",
   20293           27 :                  flags & macro_import::slot::L_UNDEF ? "#undef" : "",
   20294              :                  flags == macro_import::slot::L_BOTH ? " & " : "",
   20295           30 :                  flags & macro_import::slot::L_DEF ? "#define" : "",
   20296              :                  identifier (node), slot.offset);
   20297              : 
   20298              :       /* We'll leak an imported definition's TOKEN_FLD_STR's data
   20299              :          here.  But that only happens when we've had to resolve the
   20300              :          deferred macro before this import -- why are you doing
   20301              :          that?  */
   20302        87904 :       if (cpp_macro *cur = cpp_set_deferred_macro (node))
   20303        32048 :         if (!cur->imported_p)
   20304              :           {
   20305        32048 :             macro_import::slot &slot = imp.exported ();
   20306        32048 :             macro_export &exp = get_macro_export (slot);
   20307        32048 :             exp.def = cur;
   20308        87904 :             dump (dumper::MACRO)
   20309            0 :               && dump ("Saving current #define %I", identifier (node));
   20310              :           }
   20311              :     }
   20312              : 
   20313              :   /* We're now done with the table.  */
   20314          182 :   elf_in::release (slurp->from, sec);
   20315              : 
   20316          182 :   dump.outdent ();
   20317              : }
   20318              : 
   20319              : /* Import the transitive macros.  */
   20320              : 
   20321              : void
   20322          905 : module_state::import_macros ()
   20323              : {
   20324          905 :   bitmap_ior_into (headers, slurp->headers);
   20325              : 
   20326          905 :   bitmap_iterator bititer;
   20327          905 :   unsigned bitnum;
   20328         1852 :   EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
   20329          947 :     (*modules)[bitnum]->install_macros ();
   20330          905 : }
   20331              : 
   20332              : /* NODE is being undefined at LOC.  Record it in the export table, if
   20333              :    necessary.  */
   20334              : 
   20335              : void
   20336       335308 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
   20337              : {
   20338       335308 :   if (!node->deferred)
   20339              :     /* The macro is not imported, so our undef is irrelevant.  */
   20340              :     return;
   20341              : 
   20342           12 :   unsigned n = dump.push (NULL);
   20343              : 
   20344           12 :   macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
   20345           12 :   macro_export &exp = get_macro_export (slot);
   20346              : 
   20347           12 :   exp.undef_loc = loc;
   20348           12 :   slot.become_undef ();
   20349           12 :   exp.def = NULL;
   20350              : 
   20351           18 :   dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
   20352              : 
   20353           12 :   dump.pop (n);
   20354              : }
   20355              : 
   20356              : /* NODE is a deferred macro node.  Determine the definition and return
   20357              :    it, with NULL if undefined.  May issue diagnostics.
   20358              : 
   20359              :    This can leak memory, when merging declarations -- the string
   20360              :    contents (TOKEN_FLD_STR) of each definition are allocated in
   20361              :    unreclaimable cpp objstack.  Only one will win.  However, I do not
   20362              :    expect this to be common -- mostly macros have a single point of
   20363              :    definition.  Perhaps we could restore the objstack to its position
   20364              :    after the first imported definition (if that wins)?  The macros
   20365              :    themselves are GC'd.  */
   20366              : 
   20367              : cpp_macro *
   20368          783 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
   20369              :                               cpp_hashnode *node)
   20370              : {
   20371          783 :   macro_import &imports = (*macro_imports)[node->deferred - 1];
   20372              : 
   20373          783 :   unsigned n = dump.push (NULL);
   20374          789 :   dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
   20375              : 
   20376          783 :   bitmap visible (BITMAP_GGC_ALLOC ());
   20377              : 
   20378          783 :   if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
   20379            0 :         && !imports[0].get_module ()))
   20380              :     {
   20381              :       /* Calculate the set of visible header imports.  */
   20382          783 :       bitmap_copy (visible, headers);
   20383         1873 :       for (unsigned ix = imports.length (); ix--;)
   20384              :         {
   20385         1090 :           const macro_import::slot &slot = imports[ix];
   20386         1090 :           unsigned mod = slot.get_module ();
   20387         1090 :           if ((slot.get_defness () & macro_import::slot::L_UNDEF)
   20388         1090 :               && bitmap_bit_p (visible, mod))
   20389              :             {
   20390           12 :               bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
   20391           12 :               bitmap_and_compl_into (visible, arg);
   20392           12 :               bitmap_set_bit (visible, mod);
   20393              :             }
   20394              :         }
   20395              :     }
   20396          783 :   bitmap_set_bit (visible, 0);
   20397              : 
   20398              :   /* Now find the macros that are still visible.  */
   20399          783 :   bool failed = false;
   20400          783 :   cpp_macro *def = NULL;
   20401          783 :   vec<macro_export> defs;
   20402          783 :   defs.create (imports.length ());
   20403         1873 :   for (unsigned ix = imports.length (); ix--;)
   20404              :     {
   20405         1090 :       const macro_import::slot &slot = imports[ix];
   20406         1090 :       unsigned mod = slot.get_module ();
   20407         1090 :       if (bitmap_bit_p (visible, mod))
   20408              :         {
   20409         1078 :           macro_export *pushed = NULL;
   20410         1078 :           if (mod)
   20411              :             {
   20412          813 :               const module_state *imp = (*modules)[mod];
   20413          813 :               bytes_in &sec = imp->slurp->macro_defs;
   20414          813 :               if (!sec.get_overrun ())
   20415              :                 {
   20416          813 :                   dump (dumper::MACRO)
   20417            6 :                     && dump ("Reading macro %s%s%s %I module %M at %u",
   20418            6 :                              slot.get_defness () & macro_import::slot::L_UNDEF
   20419              :                              ? "#undef" : "",
   20420            6 :                              slot.get_defness () == macro_import::slot::L_BOTH
   20421              :                              ? " & " : "",
   20422            6 :                              slot.get_defness () & macro_import::slot::L_DEF
   20423              :                              ? "#define" : "",
   20424            6 :                              identifier (node), imp, slot.offset);
   20425          813 :                   sec.random_access (slot.offset);
   20426              : 
   20427          813 :                   macro_export exp;
   20428          813 :                   if (slot.get_defness () & macro_import::slot::L_UNDEF)
   20429           12 :                     exp.undef_loc = imp->read_location (sec);
   20430          813 :                   if (slot.get_defness () & macro_import::slot::L_DEF)
   20431          807 :                     exp.def = imp->read_define (sec, reader);
   20432          813 :                   if (sec.get_overrun ())
   20433            0 :                     error_at (loc, "macro definitions of %qE corrupted",
   20434            0 :                               imp->name);
   20435              :                   else
   20436          813 :                     pushed = defs.quick_push (exp);
   20437              :                 }
   20438              :             }
   20439              :           else
   20440          265 :             pushed = defs.quick_push ((*macro_exports)[slot.offset]);
   20441         1078 :           if (pushed && pushed->def)
   20442              :             {
   20443         1072 :               if (!def)
   20444              :                 def = pushed->def;
   20445          292 :               else if (cpp_compare_macros (def, pushed->def))
   20446         1090 :                 failed = true;
   20447              :             }
   20448              :         }
   20449              :     }
   20450              : 
   20451          783 :   if (failed)
   20452              :     {
   20453              :       /* If LOC is the first loc, this is the end of file check, which
   20454              :          is a warning.  */
   20455           15 :       auto_diagnostic_group d;
   20456           15 :       if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
   20457            9 :         warning_at (loc, OPT_Winvalid_imported_macros,
   20458              :                     "inconsistent imported macro definition %qE",
   20459              :                     identifier (node));
   20460              :       else
   20461            6 :         error_at (loc, "inconsistent imported macro definition %qE",
   20462              :                   identifier (node));
   20463           60 :       for (unsigned ix = defs.length (); ix--;)
   20464              :         {
   20465           30 :           macro_export &exp = defs[ix];
   20466           30 :           if (exp.undef_loc)
   20467            0 :             inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
   20468           30 :           if (exp.def)
   20469           30 :             inform (exp.def->line, "%<#define %s%>",
   20470              :                     cpp_macro_definition (reader, node, exp.def));
   20471              :         }
   20472           15 :       def = NULL;
   20473           15 :     }
   20474              : 
   20475          783 :   defs.release ();
   20476              : 
   20477          783 :   dump.pop (n);
   20478              : 
   20479          783 :   return def;
   20480              : }
   20481              : 
   20482              : /* Stream the static aggregates.  Sadly some headers (ahem:
   20483              :    iostream) contain static vars, and rely on them to run global
   20484              :    ctors.  */
   20485              : unsigned
   20486          909 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
   20487              : {
   20488          909 :   if (!static_aggregates && !tls_aggregates)
   20489              :     return 0;
   20490              : 
   20491           45 :   dump () && dump ("Writing initializers");
   20492           45 :   dump.indent ();
   20493              : 
   20494           45 :   static_aggregates = nreverse (static_aggregates);
   20495           45 :   tls_aggregates = nreverse (tls_aggregates);
   20496              : 
   20497           45 :   unsigned count = 0;
   20498           45 :   trees_out sec (to, this, table, ~0u);
   20499           45 :   sec.begin ();
   20500              : 
   20501           45 :   tree list = static_aggregates;
   20502          135 :   for (int passes = 0; passes != 2; passes++)
   20503              :     {
   20504          258 :       for (tree init = list; init; init = TREE_CHAIN (init))
   20505          168 :         if (TREE_LANG_FLAG_0 (init))
   20506              :           {
   20507          144 :             if (STATIC_INIT_DECOMP_BASE_P (init))
   20508              :               {
   20509              :                 /* Ensure that in the returned result chain if the
   20510              :                    STATIC_INIT_DECOMP_*BASE_P flags are set, there is
   20511              :                    always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
   20512              :                    followed by one or more STATIC_INIT_DECOMP_NONBASE_P.  */
   20513           21 :                 int phase = 0;
   20514           21 :                 tree last = NULL_TREE;
   20515           21 :                 for (tree init2 = TREE_CHAIN (init);
   20516          102 :                      init2; init2 = TREE_CHAIN (init2))
   20517              :                   {
   20518          123 :                     if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
   20519              :                       ;
   20520          102 :                     else if (phase == 0
   20521          123 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20522              :                       {
   20523          102 :                         phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
   20524              :                         last = init2;
   20525              :                       }
   20526           81 :                     else if (IN_RANGE (phase, 1, 2)
   20527          162 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20528              :                       {
   20529           60 :                         if (TREE_LANG_FLAG_0 (init2))
   20530           81 :                           phase = 2;
   20531              :                         last = init2;
   20532              :                       }
   20533              :                     else
   20534              :                       break;
   20535              :                   }
   20536           21 :                 if (phase == 2)
   20537              :                   {
   20538              :                     /* In that case, add markers about it so that the
   20539              :                        STATIC_INIT_DECOMP_BASE_P and
   20540              :                        STATIC_INIT_DECOMP_NONBASE_P flags can be restored.  */
   20541           21 :                     sec.tree_node (build_int_cst (integer_type_node,
   20542           21 :                                                   2 * passes + 1));
   20543           21 :                     phase = 1;
   20544          123 :                     for (tree init2 = init; init2 != TREE_CHAIN (last);
   20545          102 :                          init2 = TREE_CHAIN (init2))
   20546          102 :                       if (TREE_LANG_FLAG_0 (init2))
   20547              :                         {
   20548          102 :                           tree decl = TREE_VALUE (init2);
   20549          102 :                           if (phase == 1
   20550          102 :                               && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20551              :                             {
   20552           21 :                               sec.tree_node (build_int_cst (integer_type_node,
   20553           21 :                                                             2 * passes + 2));
   20554           21 :                               phase = 2;
   20555              :                             }
   20556          102 :                           dump ("Initializer:%u for %N", count, decl);
   20557          102 :                           sec.tree_node (decl);
   20558          102 :                           ++count;
   20559              :                         }
   20560           21 :                     sec.tree_node (integer_zero_node);
   20561           21 :                     init = last;
   20562           21 :                     continue;
   20563           21 :                   }
   20564              :               }
   20565              : 
   20566          123 :             tree decl = TREE_VALUE (init);
   20567              : 
   20568          123 :             dump ("Initializer:%u for %N", count, decl);
   20569          123 :             sec.tree_node (decl);
   20570          123 :             ++count;
   20571              :           }
   20572              : 
   20573           90 :       list = tls_aggregates;
   20574              :     }
   20575              : 
   20576           45 :   sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
   20577           45 :   dump.outdent ();
   20578              : 
   20579           45 :   return count;
   20580           45 : }
   20581              : 
   20582              : /* We have to defer some post-load processing until we've completed
   20583              :    reading, because they can cause more reading.  */
   20584              : 
   20585              : static void
   20586        12843 : post_load_processing ()
   20587              : {
   20588              :   /* We mustn't cause a GC, our caller should have arranged for that
   20589              :      not to happen.  */
   20590        12843 :   gcc_checking_assert (function_depth);
   20591              : 
   20592        12843 :   if (!post_load_decls)
   20593              :     return;
   20594              : 
   20595         8209 :   tree old_cfd = current_function_decl;
   20596         8209 :   struct function *old_cfun = cfun;
   20597        17334 :   while (post_load_decls->length ())
   20598              :     {
   20599         9125 :       tree decl = post_load_decls->pop ();
   20600              : 
   20601         9180 :       dump () && dump ("Post-load processing of %N", decl);
   20602              : 
   20603         9125 :       if (VAR_P (decl) && DECL_NTTP_OBJECT_P (decl))
   20604              :         {
   20605            9 :           if (!DECL_SIZE (decl))
   20606              :             {
   20607            3 :               push_to_top_level ();
   20608            3 :               cp_finish_decl (decl, DECL_INITIAL (decl), false, NULL_TREE, 0);
   20609            3 :               pop_from_top_level ();
   20610              :             }
   20611            9 :           continue;
   20612              :         }
   20613              : 
   20614         9116 :       gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
   20615         9116 :       expand_or_defer_fn (decl);
   20616              :       /* As in module_state::read_cluster.  */
   20617          992 :       if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   20618         9449 :           && DECL_NOT_REALLY_EXTERN (decl))
   20619          292 :         DECL_EXTERNAL (decl) = false;
   20620              :     }
   20621              : 
   20622         8209 :   set_cfun (old_cfun);
   20623         8209 :   current_function_decl = old_cfd;
   20624              : }
   20625              : 
   20626              : bool
   20627           45 : module_state::read_inits (unsigned count)
   20628              : {
   20629           45 :   trees_in sec (this);
   20630           45 :   if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
   20631              :     return false;
   20632           57 :   dump () && dump ("Reading %u initializers", count);
   20633           45 :   dump.indent ();
   20634              : 
   20635           45 :   lazy_snum = ~0u;
   20636           45 :   int decomp_phase = 0;
   20637           45 :   tree *aggrp = NULL;
   20638          270 :   for (unsigned ix = 0; ix != count; ix++)
   20639              :     {
   20640          225 :       tree last = NULL_TREE;
   20641          225 :       if (decomp_phase)
   20642          102 :         last = *aggrp;
   20643              :       /* Merely referencing the decl causes its initializer to be read
   20644              :          and added to the correct list.  */
   20645          225 :       tree decl = sec.tree_node ();
   20646              :       /* module_state::write_inits can add special INTEGER_CST markers in
   20647              :          between the decls.  1 means STATIC_INIT_DECOMP_BASE_P entries
   20648              :          follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
   20649              :          entries follow in static_aggregates, 3 means
   20650              :          STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
   20651              :          4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
   20652              :          0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence.  */
   20653          225 :       if (tree_fits_shwi_p (decl))
   20654              :         {
   20655           63 :           if (sec.get_overrun ())
   20656              :             break;
   20657           63 :           decomp_phase = tree_to_shwi (decl);
   20658           63 :           if (decomp_phase)
   20659              :             {
   20660           42 :               aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
   20661              :               last = *aggrp;
   20662              :             }
   20663           63 :           decl = sec.tree_node ();
   20664              :         }
   20665              : 
   20666          225 :       if (sec.get_overrun ())
   20667              :         break;
   20668          225 :       if (decl)
   20669          225 :         dump ("Initializer:%u for %N", ix, decl);
   20670          225 :       if (decomp_phase)
   20671              :         {
   20672          102 :           tree init = *aggrp;
   20673          102 :           gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
   20674          102 :           if ((decomp_phase & 1) != 0)
   20675           21 :             STATIC_INIT_DECOMP_BASE_P (init) = 1;
   20676              :           else
   20677           81 :             STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
   20678              :         }
   20679              :     }
   20680           45 :   if (decomp_phase && !sec.get_overrun ())
   20681              :     {
   20682            0 :       tree decl = sec.tree_node ();
   20683            0 :       gcc_assert (integer_zerop (decl));
   20684              :     }
   20685           45 :   lazy_snum = 0;
   20686           45 :   post_load_processing ();
   20687           45 :   dump.outdent ();
   20688           45 :   if (!sec.end (from ()))
   20689              :     return false;
   20690              :   return true;
   20691           45 : }
   20692              : 
   20693              : void
   20694         2785 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
   20695              :                             unsigned *crc_ptr)
   20696              : {
   20697         2785 :   bytes_out cfg (to);
   20698              : 
   20699         2785 :   cfg.begin ();
   20700              : 
   20701        30635 :   for (unsigned ix = MSC_HWM; ix--;)
   20702        25065 :     cfg.u (counts[ix]);
   20703              : 
   20704         2785 :   if (dump ())
   20705              :     {
   20706          300 :       dump ("Cluster sections are [%u,%u)",
   20707              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20708          300 :       dump ("Bindings %u", counts[MSC_bindings]);
   20709          300 :       dump ("Pendings %u", counts[MSC_pendings]);
   20710          300 :       dump ("Entities %u", counts[MSC_entities]);
   20711          300 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20712          300 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20713          300 :       dump ("Macros %u", counts[MSC_macros]);
   20714          300 :       dump ("Initializers %u", counts[MSC_inits]);
   20715              :     }
   20716              : 
   20717         2785 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
   20718         2785 : }
   20719              : 
   20720              : bool
   20721         2970 : module_state::read_counts (unsigned counts[MSC_HWM])
   20722              : {
   20723         2970 :   bytes_in cfg;
   20724              : 
   20725         2970 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
   20726              :     return false;
   20727              : 
   20728        29700 :   for (unsigned ix = MSC_HWM; ix--;)
   20729        26730 :     counts[ix] = cfg.u ();
   20730              : 
   20731         2970 :   if (dump ())
   20732              :     {
   20733          532 :       dump ("Declaration sections are [%u,%u)",
   20734              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20735          532 :       dump ("Bindings %u", counts[MSC_bindings]);
   20736          532 :       dump ("Pendings %u", counts[MSC_pendings]);
   20737          532 :       dump ("Entities %u", counts[MSC_entities]);
   20738          532 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20739          532 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20740          532 :       dump ("Macros %u", counts[MSC_macros]);
   20741          532 :       dump ("Initializers %u", counts[MSC_inits]);
   20742              :     }
   20743              : 
   20744         2970 :   return cfg.end (from ());
   20745         2970 : }
   20746              : 
   20747              : /* Tool configuration:  MOD_SNAME_PFX .config
   20748              : 
   20749              :    This is data that confirms current state (or fails).  */
   20750              : 
   20751              : void
   20752         2785 : module_state::write_config (elf_out *to, module_state_config &config,
   20753              :                             unsigned inner_crc)
   20754              : {
   20755         2785 :   bytes_out cfg (to);
   20756              : 
   20757         2785 :   cfg.begin ();
   20758              : 
   20759              :   /* Write version and inner crc as u32 values, for easier
   20760              :      debug inspection.  */
   20761         3085 :   dump () && dump ("Writing version=%V, inner_crc=%x",
   20762              :                    MODULE_VERSION, inner_crc);
   20763         2785 :   cfg.u32 (unsigned (MODULE_VERSION));
   20764         2785 :   cfg.u32 (inner_crc);
   20765              : 
   20766         2785 :   cfg.u (to->name (is_header () ? "" : get_flatname ()));
   20767              : 
   20768              :   /* Configuration. */
   20769         3085 :   dump () && dump ("Writing target='%s', host='%s'",
   20770              :                    TARGET_MACHINE, HOST_MACHINE);
   20771         2785 :   unsigned target = to->name (TARGET_MACHINE);
   20772         2785 :   unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
   20773              :                    ? target : to->name (HOST_MACHINE));
   20774         2785 :   cfg.u (target);
   20775         2785 :   cfg.u (host);
   20776              : 
   20777         2785 :   cfg.str (config.dialect_str);
   20778         2785 :   cfg.u (extensions);
   20779              : 
   20780              :   /* Global tree information.  We write the globals crc separately,
   20781              :      rather than mix it directly into the overall crc, as it is used
   20782              :      to ensure data match between instances of the compiler, not
   20783              :      integrity of the file.  */
   20784         3085 :   dump () && dump ("Writing globals=%u, crc=%x",
   20785              :                    fixed_trees->length (), global_crc);
   20786         2785 :   cfg.u (fixed_trees->length ());
   20787         2785 :   cfg.u32 (global_crc);
   20788              : 
   20789         2785 :   if (is_partition ())
   20790          205 :     cfg.u (is_interface ());
   20791              : 
   20792         2785 :   cfg.u (config.num_imports);
   20793         2785 :   cfg.u (config.num_partitions);
   20794         2785 :   cfg.u (config.num_entities);
   20795              : 
   20796         2785 :   cfg.loc (config.ordinary_locs);
   20797         2785 :   cfg.loc (config.macro_locs);
   20798         2785 :   cfg.u (config.loc_range_bits);
   20799              : 
   20800         2785 :   cfg.u (config.active_init);
   20801              : 
   20802              :   /* Now generate CRC, we'll have incorporated the inner CRC because
   20803              :      of its serialization above.  */
   20804         2785 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
   20805         3085 :   dump () && dump ("Writing CRC=%x", crc);
   20806         2785 : }
   20807              : 
   20808              : void
   20809           40 : module_state::note_cmi_name ()
   20810              : {
   20811           40 :   if (!cmi_noted_p && filename)
   20812              :     {
   20813           40 :       cmi_noted_p = true;
   20814           40 :       inform (loc, "compiled module file is %qs",
   20815              :               maybe_add_cmi_prefix (filename));
   20816              :     }
   20817           40 : }
   20818              : 
   20819              : bool
   20820         3080 : module_state::read_config (module_state_config &config, bool complain)
   20821              : {
   20822         3080 :   bytes_in cfg;
   20823              : 
   20824         3080 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
   20825              :     return false;
   20826              : 
   20827              :   /* Check version.  */
   20828         3080 :   unsigned my_ver = MODULE_VERSION;
   20829         3080 :   unsigned their_ver = cfg.u32 ();
   20830         3615 :   dump () && dump  (my_ver == their_ver ? "Version %V"
   20831              :                     : "Expecting %V found %V", my_ver, their_ver);
   20832         3080 :   if (their_ver != my_ver)
   20833              :     {
   20834              :       /* The compiler versions differ.  Close enough? */
   20835            0 :       verstr_t my_string, their_string;
   20836              : 
   20837            0 :       version2string (my_ver, my_string);
   20838            0 :       version2string (their_ver, their_string);
   20839              : 
   20840              :       /* Reject when either is non-experimental or when experimental
   20841              :          major versions differ.  */
   20842            0 :       auto_diagnostic_group d;
   20843            0 :       bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
   20844              :                         || !IS_EXPERIMENTAL (their_ver)
   20845            0 :                         || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
   20846              :                        /* The 'I know what I'm doing' switch.  */
   20847            0 :                        && !flag_module_version_ignore);
   20848            0 :       bool inform_p = true;
   20849            0 :       if (!complain)
   20850              :         inform_p = false;
   20851            0 :       else if (reject_p)
   20852              :         {
   20853            0 :           cfg.set_overrun ();
   20854            0 :           error_at (loc, "compiled module is %sversion %s",
   20855              :                     IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20856              :                     their_string);
   20857              :         }
   20858              :       else
   20859            0 :         inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
   20860              :                              IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20861              :                              their_string);
   20862              : 
   20863            0 :       if (inform_p)
   20864              :         {
   20865            0 :           inform (loc, "compiler is %sversion %s%s%s",
   20866              :                   IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
   20867              :                   my_string,
   20868            0 :                   reject_p ? "" : flag_module_version_ignore
   20869            0 :                   ? ", be it on your own head!" : ", close enough?",
   20870              :                   reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
   20871            0 :           note_cmi_name ();
   20872              :         }
   20873              : 
   20874            0 :       if (reject_p)
   20875            0 :         goto done;
   20876            0 :     }
   20877              : 
   20878              :   /*  We wrote the inner crc merely to merge it, so simply read it
   20879              :       back and forget it.  */
   20880         3080 :   cfg.u32 ();
   20881              : 
   20882              :   /* Check module name.  */
   20883         3080 :   {
   20884         3080 :     const char *their_name = from ()->name (cfg.u ());
   20885         3080 :     const char *our_name = "";
   20886              : 
   20887         3080 :     if (!is_header ())
   20888         2036 :       our_name = get_flatname ();
   20889              : 
   20890              :     /* Header units can be aliased, so name checking is
   20891              :        inappropriate.  */
   20892         3080 :     if (0 != strcmp (their_name, our_name))
   20893              :       {
   20894            0 :         error_at (loc,
   20895            0 :                   their_name[0] && our_name[0] ? G_("module %qs found")
   20896              :                   : their_name[0]
   20897              :                   ? G_("header module expected, module %qs found")
   20898              :                   : G_("module %qs expected, header module found"),
   20899            0 :                   their_name[0] ? their_name : our_name);
   20900            0 :         cfg.set_overrun ();
   20901            0 :         goto done;
   20902              :       }
   20903              :   }
   20904              : 
   20905              :   /* Check the CRC after the above sanity checks, so that the user is
   20906              :      clued in.  */
   20907         3080 :   {
   20908         3080 :     unsigned e_crc = crc;
   20909         3080 :     crc = cfg.get_crc ();
   20910         3615 :     dump () && dump ("Reading CRC=%x", crc);
   20911              :     /* When not complaining we haven't set directness yet, so ignore the
   20912              :        mismatch. */
   20913         3080 :     if (complain && !is_direct () && crc != e_crc)
   20914              :       {
   20915            3 :         error_at (loc, "module %qs CRC mismatch", get_flatname ());
   20916            3 :         cfg.set_overrun ();
   20917            3 :         goto done;
   20918              :       }
   20919              :   }
   20920              : 
   20921              :   /* Check target & host.  */
   20922         3077 :   {
   20923         3077 :     const char *their_target = from ()->name (cfg.u ());
   20924         3077 :     const char *their_host = from ()->name (cfg.u ());
   20925         3612 :     dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
   20926         3077 :     if (strcmp (their_target, TARGET_MACHINE)
   20927         3077 :         || strcmp (their_host, HOST_MACHINE))
   20928              :       {
   20929            0 :         error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
   20930              :                   their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
   20931            0 :         cfg.set_overrun ();
   20932            0 :         goto done;
   20933              :       }
   20934              :   }
   20935              : 
   20936              :   /* Check compilation dialect.  This must match.  */
   20937         3077 :   {
   20938         3077 :     const char *their_dialect = cfg.str ();
   20939         3077 :     if (strcmp (their_dialect, config.dialect_str))
   20940              :       {
   20941            1 :         if (complain)
   20942            1 :           error_at (loc, "language dialect differs %qs, expected %qs",
   20943              :                     their_dialect, config.dialect_str);
   20944            1 :         cfg.set_overrun ();
   20945            1 :         goto done;
   20946              :       }
   20947              :   }
   20948              : 
   20949              :   /* Check for extensions.  If they set any, we must have them set
   20950              :      too.  */
   20951         3076 :   {
   20952         3076 :     unsigned ext = cfg.u ();
   20953         3076 :     unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
   20954         3076 :     if (flag_openmp_simd)
   20955            3 :       allowed |= SE_OPENMP_SIMD;
   20956         3076 :     if (flag_openacc)
   20957            3 :       allowed |= SE_OPENACC;
   20958              : 
   20959         3076 :     if (unsigned bad = ext & ~allowed)
   20960              :       {
   20961            9 :         if (bad & SE_OPENMP)
   20962            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
   20963            6 :         else if (bad & SE_OPENMP_SIMD)
   20964            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
   20965              :                          "%<-fopenmp-simd%> to enable");
   20966            9 :         if (bad & SE_OPENACC)
   20967            3 :           error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
   20968              :                          "enable");
   20969            9 :         cfg.set_overrun ();
   20970            9 :         goto done;
   20971              :       }
   20972         3067 :     extensions = ext;
   20973              :   }
   20974              : 
   20975              :   /* Check global trees.  */
   20976         3067 :   {
   20977         3067 :     unsigned their_fixed_length = cfg.u ();
   20978         3067 :     unsigned their_fixed_crc = cfg.u32 ();
   20979         3602 :     dump () && dump ("Read globals=%u, crc=%x",
   20980              :                      their_fixed_length, their_fixed_crc);
   20981         3067 :     if (!flag_preprocess_only
   20982         3067 :         && (their_fixed_length != fixed_trees->length ()
   20983         3013 :             || their_fixed_crc != global_crc))
   20984              :       {
   20985            0 :         error_at (loc, "fixed tree mismatch");
   20986            0 :         cfg.set_overrun ();
   20987            0 :         goto done;
   20988              :       }
   20989              :   }
   20990              : 
   20991              :   /* All non-partitions are interfaces.  */
   20992         3067 :   interface_p = !is_partition () || cfg.u ();
   20993              : 
   20994         3067 :   config.num_imports = cfg.u ();
   20995         3067 :   config.num_partitions = cfg.u ();
   20996         3067 :   config.num_entities = cfg.u ();
   20997              : 
   20998         3067 :   config.ordinary_locs = cfg.loc ();
   20999         3067 :   config.macro_locs = cfg.loc ();
   21000         3067 :   config.loc_range_bits = cfg.u ();
   21001              : 
   21002         3067 :   config.active_init = cfg.u ();
   21003              : 
   21004         3080 :  done:
   21005         3080 :   return cfg.end (from ());
   21006         3080 : }
   21007              : 
   21008              : /* Comparator for ordering the Ordered Ordinary Location array.  */
   21009              : 
   21010              : static int
   21011          124 : ool_cmp (const void *a_, const void *b_)
   21012              : {
   21013          124 :   auto *a = *static_cast<const module_state *const *> (a_);
   21014          124 :   auto *b = *static_cast<const module_state *const *> (b_);
   21015          124 :   if (a == b)
   21016              :     return 0;
   21017          124 :   else if (a->ordinary_locs.first < b->ordinary_locs.first)
   21018              :     return -1;
   21019              :   else
   21020           52 :     return +1;
   21021              : }
   21022              : 
   21023              : /* Use ELROND format to record the following sections:
   21024              :      qualified-names        : binding value(s)
   21025              :      MOD_SNAME_PFX.README   : human readable, strings
   21026              :      MOD_SNAME_PFX.ENV      : environment strings, strings
   21027              :      MOD_SNAME_PFX.nms      : namespace hierarchy
   21028              :      MOD_SNAME_PFX.udi      : namespace using-directives
   21029              :      MOD_SNAME_PFX.bnd      : binding table
   21030              :      MOD_SNAME_PFX.spc      : specialization table
   21031              :      MOD_SNAME_PFX.imp      : import table
   21032              :      MOD_SNAME_PFX.ent      : entity table
   21033              :      MOD_SNAME_PFX.prt      : partitions table
   21034              :      MOD_SNAME_PFX.olm      : ordinary line maps
   21035              :      MOD_SNAME_PFX.mlm      : macro line maps
   21036              :      MOD_SNAME_PFX.def      : macro definitions
   21037              :      MOD_SNAME_PFX.mac      : macro index
   21038              :      MOD_SNAME_PFX.ini      : inits
   21039              :      MOD_SNAME_PFX.cnt      : counts
   21040              :      MOD_SNAME_PFX.cfg      : config data
   21041              : */
   21042              : 
   21043              : bool
   21044         2814 : module_state::write_begin (elf_out *to, cpp_reader *reader,
   21045              :                            module_state_config &config, unsigned &crc)
   21046              : {
   21047              :   /* Figure out remapped module numbers, which might elide
   21048              :      partitions.  */
   21049         2814 :   bitmap partitions = NULL;
   21050         2814 :   if (!is_header () && !is_partition ())
   21051         1700 :     partitions = BITMAP_GGC_ALLOC ();
   21052         2814 :   write_init_maps ();
   21053              : 
   21054         2814 :   unsigned mod_hwm = 1;
   21055         6315 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   21056              :     {
   21057          687 :       module_state *imp = (*modules)[ix];
   21058              : 
   21059              :       /* Promote any non-partition direct import from a partition, unless
   21060              :          we're a partition.  */
   21061          627 :       if (!is_partition () && !imp->is_partition ()
   21062         1124 :           && imp->is_partition_direct ())
   21063           12 :         imp->directness = MD_PURVIEW_DIRECT;
   21064              : 
   21065              :       /* Write any import that is not a partition, unless we're a
   21066              :          partition.  */
   21067          687 :       if (!partitions || !imp->is_partition ())
   21068          497 :         imp->remap = mod_hwm++;
   21069              :       else
   21070              :         {
   21071          229 :           dump () && dump ("Partition %M %u", imp, ix);
   21072          190 :           bitmap_set_bit (partitions, ix);
   21073          190 :           imp->remap = 0;
   21074              :           /* All interface partitions must be exported.  */
   21075          190 :           if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
   21076              :             {
   21077            3 :               error_at (imp->loc, "interface partition is not exported");
   21078            3 :               bitmap_set_bit (exports, imp->mod);
   21079              :             }
   21080              : 
   21081              :           /* All the partition entities should have been loaded when
   21082              :              loading the partition.  */
   21083          190 :           if (CHECKING_P)
   21084         1245 :             for (unsigned jx = 0; jx != imp->entity_num; jx++)
   21085              :               {
   21086         1055 :                 binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
   21087         1055 :                 gcc_checking_assert (!slot->is_lazy ());
   21088              :               }
   21089              :         }
   21090              : 
   21091          687 :       if (imp->is_direct () && (imp->remap || imp->is_partition ()))
   21092          669 :         note_location (imp->imported_from ());
   21093              :     }
   21094              : 
   21095         2814 :   if (partitions && bitmap_empty_p (partitions))
   21096              :     /* No partitions present.  */
   21097              :     partitions = nullptr;
   21098              : 
   21099              :   /* Find the set of decls we must write out.  */
   21100         2814 :   depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
   21101              :   /* Add the specializations before the writables, so that we can
   21102              :      detect injected friend specializations.  */
   21103         2814 :   table.add_specializations (true);
   21104         2814 :   table.add_specializations (false);
   21105         2814 :   if (partial_specializations)
   21106              :     {
   21107          216 :       table.add_partial_entities (partial_specializations);
   21108          216 :       partial_specializations = NULL;
   21109              :     }
   21110         2814 :   table.add_namespace_entities (global_namespace, partitions);
   21111         2814 :   if (class_members)
   21112              :     {
   21113           12 :       table.add_class_entities (class_members);
   21114           12 :       class_members = NULL;
   21115              :     }
   21116              : 
   21117              :   /* Now join everything up.  */
   21118         2814 :   table.find_dependencies (this);
   21119              : 
   21120         2814 :   if (!table.finalize_dependencies ())
   21121              :     return false;
   21122              : 
   21123              : #if CHECKING_P
   21124              :   /* We're done verifying at-most once reading, reset to verify
   21125              :      at-most once writing.  */
   21126         2785 :   note_defs = note_defs_table_t::create_ggc (1000);
   21127              : #endif
   21128              : 
   21129              :   /* Determine Strongly Connected Components.  This will also strip any
   21130              :      unnecessary dependencies on imported or TU-local entities.  */
   21131         2785 :   vec<depset *> sccs = table.connect ();
   21132              : 
   21133         2785 :   vec_alloc (ool, modules->length ());
   21134         3465 :   for (unsigned ix = modules->length (); --ix;)
   21135              :     {
   21136          680 :       auto *import = (*modules)[ix];
   21137          680 :       if (import->loadedness > ML_NONE
   21138          680 :           && !(partitions && bitmap_bit_p (partitions, import->mod)))
   21139          490 :         ool->quick_push (import);
   21140              :     }
   21141         2785 :   ool->qsort (ool_cmp);
   21142              : 
   21143         2785 :   write_diagnostic_classification (nullptr, global_dc, nullptr);
   21144              : 
   21145         2785 :   vec<cpp_hashnode *> *macros = nullptr;
   21146         2785 :   if (is_header ())
   21147          909 :     macros = prepare_macros (reader);
   21148              : 
   21149         2785 :   config.num_imports = mod_hwm;
   21150         2785 :   config.num_partitions = modules->length () - mod_hwm;
   21151         2785 :   auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
   21152         2785 :   unsigned counts[MSC_HWM];
   21153         2785 :   memset (counts, 0, sizeof (counts));
   21154              : 
   21155              :   /* depset::cluster is the cluster number,
   21156              :      depset::section is unspecified scratch value.
   21157              : 
   21158              :      The following loops make use of the tarjan property that
   21159              :      dependencies will be earlier in the SCCS array.  */
   21160              : 
   21161              :   /* This first loop determines the number of depsets in each SCC, and
   21162              :      also the number of namespaces we're dealing with.  During the
   21163              :      loop, the meaning of a couple of depset fields now change:
   21164              : 
   21165              :      depset::cluster -> size_of cluster, if first of cluster & !namespace
   21166              :      depset::section -> section number of cluster (if !namespace). */
   21167              : 
   21168         2785 :   unsigned n_spaces = 0;
   21169         2785 :   counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
   21170       335476 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21171              :     {
   21172       332691 :       depset **base = &sccs[ix];
   21173              : 
   21174       629760 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21175              :         {
   21176         5223 :           n_spaces++;
   21177         5223 :           size = 1;
   21178              :         }
   21179              :       else
   21180              :         {
   21181              :           /* Count the members in this cluster.  */
   21182      1471284 :           for (size = 1; ix + size < sccs.length (); size++)
   21183      1468839 :             if (base[size]->cluster != base[0]->cluster)
   21184              :               break;
   21185              : 
   21186      1798752 :           for (unsigned jx = 0; jx != size; jx++)
   21187              :             {
   21188              :               /* Set the section number.  */
   21189      1471284 :               base[jx]->cluster = ~(~0u >> 1); /* A bad value.  */
   21190      1471284 :               base[jx]->section = counts[MSC_sec_hwm];
   21191              :             }
   21192              : 
   21193              :           /* Save the size in the first member's cluster slot.  */
   21194       327468 :           base[0]->cluster = size;
   21195              : 
   21196       327468 :           counts[MSC_sec_hwm]++;
   21197              :         }
   21198              :     }
   21199              : 
   21200              :   /* Write the clusters.  Namespace decls are put in the spaces array.
   21201              :      The meaning of depset::cluster changes to provide the
   21202              :      unnamed-decl count of the depset's decl (and remains zero for
   21203              :      non-decls and non-unnamed).  */
   21204         2785 :   unsigned bytes = 0;
   21205         2785 :   vec<depset *> spaces;
   21206         2785 :   spaces.create (n_spaces);
   21207              : 
   21208       338261 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21209              :     {
   21210       332691 :       depset **base = &sccs[ix];
   21211              : 
   21212       332691 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21213              :         {
   21214         5223 :           tree decl = base[0]->get_entity ();
   21215         5223 :           if (decl == global_namespace)
   21216         2525 :             base[0]->cluster = 0;
   21217         2698 :           else if (!base[0]->is_import ())
   21218              :             {
   21219         2698 :               base[0]->cluster = counts[MSC_entities]++;
   21220         2698 :               spaces.quick_push (base[0]);
   21221         2698 :               counts[MSC_namespaces]++;
   21222         2698 :               if (CHECKING_P)
   21223              :                 {
   21224              :                   /* Add it to the entity map, such that we can tell it is
   21225              :                      part of us.  */
   21226         2698 :                   bool existed;
   21227         2698 :                   unsigned *slot = &entity_map->get_or_insert
   21228         2698 :                     (DECL_UID (decl), &existed);
   21229         2698 :                   if (existed)
   21230              :                     /* It must have come from a partition.  */
   21231            0 :                     gcc_checking_assert
   21232              :                       (import_entity_module (*slot)->is_partition ());
   21233         2698 :                   *slot = ~base[0]->cluster;
   21234              :                 }
   21235       332831 :               dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
   21236              :             }
   21237              :           size = 1;
   21238              :         }
   21239              :       else
   21240              :         {
   21241       327468 :           size = base[0]->cluster;
   21242              : 
   21243              :           /* Cluster is now used to number entities.  */
   21244       327468 :           base[0]->cluster = ~(~0u >> 1); /* A bad value.  */
   21245              : 
   21246       327468 :           sort_cluster (&table, base, size);
   21247              : 
   21248              :           /* Record the section for consistency checking during stream
   21249              :              out -- we don't want to start writing decls in different
   21250              :              sections.  */
   21251       327468 :           table.section = base[0]->section;
   21252       327468 :           bytes += write_cluster (to, base, size, table, counts, &crc);
   21253       327468 :           table.section = 0;
   21254              :         }
   21255              :     }
   21256              : 
   21257              :   /* depset::cluster - entity number (on entities)
   21258              :      depset::section - cluster number  */
   21259              :   /* We'd better have written as many sections and found as many
   21260              :      namespaces as we predicted.  */
   21261         5570 :   gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
   21262              :               && spaces.length () == counts[MSC_namespaces]);
   21263              : 
   21264              :   /* Write the entities.  None happens if we contain namespaces or
   21265              :      nothing. */
   21266         2785 :   config.num_entities = counts[MSC_entities];
   21267         2785 :   if (counts[MSC_entities])
   21268         2519 :     write_entities (to, sccs, counts[MSC_entities], &crc);
   21269              : 
   21270              :   /* Write the namespaces.  */
   21271         2785 :   if (counts[MSC_namespaces])
   21272          594 :     write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
   21273              : 
   21274              :   /* Write any using-directives.  */
   21275         2785 :   if (counts[MSC_namespaces])
   21276          594 :     counts[MSC_using_directives]
   21277          594 :       = write_using_directives (to, table, spaces, &crc);
   21278              : 
   21279              :   /* Write the bindings themselves.  */
   21280         2785 :   counts[MSC_bindings] = write_bindings (to, sccs, &crc);
   21281              : 
   21282              :   /* Write the unnamed.  */
   21283         2785 :   counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
   21284              : 
   21285              :   /* Write the import table.  */
   21286         2785 :   if (config.num_imports > 1)
   21287          459 :     write_imports (to, &crc);
   21288              : 
   21289              :   /* Write elided partition table.  */
   21290         2785 :   if (config.num_partitions)
   21291          136 :     write_partitions (to, config.num_partitions, &crc);
   21292              : 
   21293              :   /* Write the line maps.  */
   21294         2785 :   if (config.ordinary_locs)
   21295              :     {
   21296         2686 :       write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
   21297         2686 :       write_diagnostic_classification (to, global_dc, &crc);
   21298              :     }
   21299         2785 :   if (config.macro_locs)
   21300          128 :     write_macro_maps (to, map_info, &crc);
   21301              : 
   21302         2785 :   if (is_header ())
   21303              :     {
   21304          909 :       counts[MSC_macros] = write_macros (to, macros, &crc);
   21305          909 :       counts[MSC_inits] = write_inits (to, table, &crc);
   21306          909 :       vec_free (macros);
   21307              :     }
   21308              : 
   21309         2785 :   unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21310         2785 :   dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
   21311          300 :                    clusters, (bytes + clusters / 2) / (clusters + !clusters));
   21312         2785 :   trees_out::instrument ();
   21313              : 
   21314         2785 :   write_counts (to, counts, &crc);
   21315              : 
   21316         2785 :   spaces.release ();
   21317         2785 :   sccs.release ();
   21318              : 
   21319         2785 :   vec_free (macro_loc_remap);
   21320         2785 :   vec_free (ord_loc_remap);
   21321         2785 :   vec_free (ool);
   21322              : 
   21323              :   // FIXME:QOI:  Have a command line switch to control more detailed
   21324              :   // information (which might leak data you do not want to leak).
   21325              :   // Perhaps (some of) the write_readme contents should also be
   21326              :   // so-controlled.
   21327         2785 :   if (false)
   21328              :     write_env (to);
   21329              : 
   21330         2785 :   return true;
   21331         2814 : }
   21332              : 
   21333              : // Finish module writing after we've emitted all dynamic initializers.
   21334              : 
   21335              : void
   21336         2785 : module_state::write_end (elf_out *to, cpp_reader *reader,
   21337              :                          module_state_config &config, unsigned &crc)
   21338              : {
   21339              :   /* And finish up.  */
   21340         2785 :   write_config (to, config, crc);
   21341              : 
   21342              :   /* Human-readable info.  */
   21343         2785 :   write_readme (to, reader, config.dialect_str);
   21344              : 
   21345         3085 :   dump () && dump ("Wrote %u sections", to->get_section_limit ());
   21346         2785 : }
   21347              : 
   21348              : /* Initial read of a CMI.  Checks config, loads up imports and line
   21349              :    maps.  */
   21350              : 
   21351              : bool
   21352         3034 : module_state::read_initial (cpp_reader *reader)
   21353              : {
   21354         3034 :   module_state_config config;
   21355         3034 :   bool ok = true;
   21356              : 
   21357         3034 :   if (ok && !read_config (config))
   21358              :     ok = false;
   21359              : 
   21360         3021 :   bool have_locs = ok && read_prepare_maps (&config);
   21361              : 
   21362              :   /* Ordinary maps before the imports.  */
   21363         3021 :   if (!(have_locs && config.ordinary_locs))
   21364           71 :     ordinary_locs.first = line_table->highest_location + 1;
   21365         2963 :   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
   21366              :     ok = false;
   21367              : 
   21368              :   /* Allocate the REMAP vector.  */
   21369         3034 :   slurp->alloc_remap (config.num_imports);
   21370              : 
   21371         3034 :   if (ok)
   21372              :     {
   21373              :       /* Read the import table.  Decrement current to stop this CMI
   21374              :          from being evicted during the import. */
   21375         3021 :       slurp->current--;
   21376         3021 :       if (config.num_imports > 1 && !read_imports (reader, line_table))
   21377              :         ok = false;
   21378         3021 :       slurp->current++;
   21379              :     }
   21380              : 
   21381              :   /* Read the elided partition table, if we're the primary partition.  */
   21382         3021 :   if (ok && config.num_partitions && is_module ()
   21383         3048 :       && !read_partitions (config.num_partitions))
   21384              :     ok = false;
   21385              : 
   21386              :   /* Determine the module's number.  */
   21387         3034 :   gcc_checking_assert (mod == MODULE_UNKNOWN);
   21388         3034 :   gcc_checking_assert (this != this_module ());
   21389              : 
   21390         3034 :   {
   21391              :     /* Allocate space in the entities array now -- that array must be
   21392              :        monotonically in step with the modules array.  */
   21393         3034 :     entity_lwm = vec_safe_length (entity_ary);
   21394         3034 :     entity_num = config.num_entities;
   21395         3034 :     gcc_checking_assert (modules->length () == 1
   21396              :                          || modules->last ()->entity_lwm <= entity_lwm);
   21397         3034 :     vec_safe_reserve (entity_ary, config.num_entities);
   21398              : 
   21399         3034 :     binding_slot slot;
   21400         3034 :     slot.u.binding = NULL_TREE;
   21401      1343963 :     for (unsigned count = config.num_entities; count--;)
   21402      1340929 :       entity_ary->quick_push (slot);
   21403              :   }
   21404              : 
   21405              :   /* We'll run out of other resources before we run out of module
   21406              :      indices.  */
   21407         3034 :   mod = modules->length ();
   21408         3034 :   vec_safe_push (modules, this);
   21409              : 
   21410              :   /* We always import and export ourselves. */
   21411         3034 :   bitmap_set_bit (imports, mod);
   21412         3034 :   bitmap_set_bit (exports, mod);
   21413              : 
   21414         3034 :   if (ok)
   21415         3021 :     (*slurp->remap)[0] = mod << 1;
   21416         3566 :   dump () && dump ("Assigning %M module number %u", this, mod);
   21417              : 
   21418              :   /* We should not have been frozen during the importing done by
   21419              :      read_config.  */
   21420         3034 :   gcc_assert (!from ()->is_frozen ());
   21421              : 
   21422              :   /* Macro maps after the imports.  */
   21423         3034 :   if (!(ok && have_locs && config.macro_locs))
   21424         2898 :     macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   21425          136 :   else if (!read_macro_maps (config.macro_locs))
   21426              :     ok = false;
   21427              : 
   21428              :   /* Diagnostic classification streaming needs to come after reading
   21429              :      macro maps to handle _Pragmas in macros.  */
   21430         3021 :   if (ok && have_locs && config.ordinary_locs
   21431         5997 :       && !read_diagnostic_classification (global_dc))
   21432              :     ok = false;
   21433              : 
   21434              :   /* Note whether there's an active initializer.  */
   21435         3034 :   active_init_p = !is_header () && bool (config.active_init);
   21436              : 
   21437         3034 :   gcc_assert (slurp->current == ~0u);
   21438         3034 :   return ok;
   21439              : }
   21440              : 
   21441              : /* Read a preprocessor state.  */
   21442              : 
   21443              : bool
   21444          947 : module_state::read_preprocessor (bool outermost)
   21445              : {
   21446          947 :   gcc_checking_assert (is_header () && slurp
   21447              :                        && slurp->remap_module (0) == mod);
   21448              : 
   21449          947 :   if (loadedness == ML_PREPROCESSOR)
   21450            6 :     return !(from () && from ()->get_error ());
   21451              : 
   21452          941 :   bool ok = true;
   21453              : 
   21454              :   /* Read direct header imports.  */
   21455          941 :   unsigned len = slurp->remap->length ();
   21456          983 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21457              :     {
   21458           42 :       unsigned map = (*slurp->remap)[ix];
   21459           42 :       if (map & 1)
   21460              :         {
   21461           42 :           module_state *import = (*modules)[map >> 1];
   21462           42 :           if (import->is_header ())
   21463              :             {
   21464           42 :               ok = import->read_preprocessor (false);
   21465           42 :               bitmap_ior_into (slurp->headers, import->slurp->headers);
   21466              :             }
   21467              :         }
   21468              :     }
   21469              : 
   21470              :   /* Record as a direct header.  */
   21471          941 :   if (ok)
   21472          941 :     bitmap_set_bit (slurp->headers, mod);
   21473              : 
   21474          941 :   if (ok && !read_macros ())
   21475              :     ok = false;
   21476              : 
   21477          941 :   loadedness = ML_PREPROCESSOR;
   21478          941 :   announce ("macros");
   21479              : 
   21480          941 :   if (flag_preprocess_only)
   21481              :     /* We're done with the string table.  */
   21482           39 :     from ()->release ();
   21483              : 
   21484          941 :   return check_read (outermost, ok);
   21485              : }
   21486              : 
   21487              : /* Read language state.  */
   21488              : 
   21489              : bool
   21490         3057 : module_state::read_language (bool outermost)
   21491              : {
   21492         3057 :   gcc_checking_assert (!lazy_snum);
   21493              : 
   21494         3057 :   if (loadedness == ML_LANGUAGE)
   21495           87 :     return !(slurp && from () && from ()->get_error ());
   21496              : 
   21497         2970 :   gcc_checking_assert (slurp && slurp->current == ~0u
   21498              :                        && slurp->remap_module (0) == mod);
   21499              : 
   21500         2970 :   bool ok = true;
   21501              : 
   21502              :   /* Read direct imports.  */
   21503         2970 :   unsigned len = slurp->remap->length ();
   21504         3376 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21505              :     {
   21506          406 :       unsigned map = (*slurp->remap)[ix];
   21507          406 :       if (map & 1)
   21508              :         {
   21509          402 :           module_state *import = (*modules)[map >> 1];
   21510          402 :           if (!import->read_language (false))
   21511          406 :             ok = false;
   21512              :         }
   21513              :     }
   21514              : 
   21515         2970 :   unsigned counts[MSC_HWM];
   21516              : 
   21517         2970 :   if (ok && !read_counts (counts))
   21518              :     ok = false;
   21519              : 
   21520         2970 :   function_depth++; /* Prevent unexpected GCs.  */
   21521              : 
   21522         2970 :   if (ok && counts[MSC_entities] != entity_num)
   21523              :     ok = false;
   21524         2970 :   if (ok && counts[MSC_entities]
   21525         2741 :       && !read_entities (counts[MSC_entities],
   21526              :                          counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21527              :     ok = false;
   21528              : 
   21529              :   /* Read the namespace hierarchy.  */
   21530         2970 :   if (ok && counts[MSC_namespaces]
   21531         3582 :       && !read_namespaces (counts[MSC_namespaces]))
   21532              :     ok = false;
   21533              : 
   21534              :   /* Read any using-directives.  */
   21535         2970 :   if (ok && counts[MSC_using_directives]
   21536         3135 :       && !read_using_directives (counts[MSC_using_directives]))
   21537              :     ok = false;
   21538              : 
   21539         2970 :   if (ok && !read_bindings (counts[MSC_bindings],
   21540              :                             counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21541              :     ok = false;
   21542              : 
   21543              :   /* And unnamed.  */
   21544         2970 :   if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
   21545              :     ok = false;
   21546              : 
   21547         2970 :   if (ok)
   21548              :     {
   21549         2970 :       slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21550         2970 :       available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21551              :     }
   21552              : 
   21553         2970 :   if (!flag_module_lazy
   21554         2970 :       || (is_partition ()
   21555          244 :           && module_interface_p ()
   21556          217 :           && !module_partition_p ()))
   21557              :     {
   21558              :       /* Read the sections in forward order, so that dependencies are read
   21559              :          first.  See note about tarjan_connect.  */
   21560          551 :       ggc_collect ();
   21561              : 
   21562          551 :       lazy_snum = ~0u;
   21563              : 
   21564          551 :       unsigned hwm = counts[MSC_sec_hwm];
   21565       145202 :       for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
   21566       144651 :         if (!load_section (ix, NULL))
   21567              :           {
   21568              :             ok = false;
   21569              :             break;
   21570              :           }
   21571          551 :       lazy_snum = 0;
   21572          551 :       post_load_processing ();
   21573              : 
   21574          551 :       ggc_collect ();
   21575              : 
   21576          551 :       if (ok && CHECKING_P)
   21577       520630 :         for (unsigned ix = 0; ix != entity_num; ix++)
   21578       520079 :           gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
   21579              :     }
   21580              : 
   21581              :   // If the import is a header-unit, we need to register initializers
   21582              :   // of any static objects it contains (looking at you _Ioinit).
   21583              :   // Notice, the ordering of these initializers will be that of a
   21584              :   // dynamic initializer at this point in the current TU.  (Other
   21585              :   // instances of these objects in other TUs will be initialized as
   21586              :   // part of that TU's global initializers.)
   21587         2970 :   if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
   21588              :     ok = false;
   21589              : 
   21590         2970 :   function_depth--;
   21591              : 
   21592         3331 :   announce (flag_module_lazy ? "lazy" : "imported");
   21593         2970 :   loadedness = ML_LANGUAGE;
   21594              : 
   21595         2970 :   gcc_assert (slurp->current == ~0u);
   21596              : 
   21597              :   /* We're done with the string table.  */
   21598         2970 :   from ()->release ();
   21599              : 
   21600         2970 :   return check_read (outermost, ok);
   21601              : }
   21602              : 
   21603              : bool
   21604       213248 : module_state::maybe_defrost ()
   21605              : {
   21606       213248 :   bool ok = true;
   21607       213248 :   if (from ()->is_frozen ())
   21608              :     {
   21609            9 :       if (lazy_open >= lazy_limit)
   21610            3 :         freeze_an_elf ();
   21611           18 :       dump () && dump ("Defrosting '%s'", filename);
   21612            9 :       ok = from ()->defrost (maybe_add_cmi_prefix (filename));
   21613            9 :       lazy_open++;
   21614              :     }
   21615              : 
   21616       213248 :   return ok;
   21617              : }
   21618              : 
   21619              : /* Load section SNUM, dealing with laziness.  It doesn't matter if we
   21620              :    have multiple concurrent loads, because we do not use TREE_VISITED
   21621              :    when reading back in.  */
   21622              : 
   21623              : bool
   21624       213248 : module_state::load_section (unsigned snum, binding_slot *mslot)
   21625              : {
   21626       213248 :   if (from ()->get_error ())
   21627              :     return false;
   21628              : 
   21629       213248 :   if (snum >= slurp->current)
   21630            0 :     from ()->set_error (elf::E_BAD_LAZY);
   21631       213248 :   else if (maybe_defrost ())
   21632              :     {
   21633       213248 :       unsigned old_current = slurp->current;
   21634       213248 :       slurp->current = snum;
   21635       213248 :       slurp->lru = 0;  /* Do not swap out.  */
   21636       213248 :       slurp->remaining--;
   21637       213248 :       read_cluster (snum);
   21638       213248 :       slurp->lru = ++lazy_lru;
   21639       213248 :       slurp->current = old_current;
   21640              :     }
   21641              : 
   21642       213248 :   if (mslot && mslot->is_lazy ())
   21643              :     {
   21644              :       /* Oops, the section didn't set this slot.  */
   21645            0 :       from ()->set_error (elf::E_BAD_DATA);
   21646            0 :       *mslot = NULL_TREE;
   21647              :     }
   21648              : 
   21649       213248 :   bool ok = !from ()->get_error ();
   21650       213248 :   if (!ok)
   21651              :     {
   21652            0 :       error_at (loc, "failed to read compiled module cluster %u: %s",
   21653            0 :                 snum, from ()->get_error (filename));
   21654            0 :       note_cmi_name ();
   21655              :     }
   21656              : 
   21657       213248 :   maybe_completed_reading ();
   21658              : 
   21659       213248 :   return ok;
   21660              : }
   21661              : 
   21662              : void
   21663       220177 : module_state::maybe_completed_reading ()
   21664              : {
   21665       220177 :   if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
   21666              :     {
   21667         2451 :       lazy_open--;
   21668              :       /* We no longer need the macros, all tokenizing has been done.  */
   21669         2451 :       slurp->release_macros ();
   21670              : 
   21671         2451 :       from ()->end ();
   21672         2451 :       slurp->close ();
   21673         2451 :       slurped ();
   21674              :     }
   21675       220177 : }
   21676              : 
   21677              : /* After a reading operation, make sure things are still ok.  If not,
   21678              :    emit an error and clean up.  */
   21679              : 
   21680              : bool
   21681         6966 : module_state::check_read (bool outermost, bool ok)
   21682              : {
   21683         6966 :   gcc_checking_assert (!outermost || slurp->current == ~0u);
   21684              : 
   21685         6966 :   if (!ok)
   21686           34 :     from ()->set_error ();
   21687              : 
   21688         6966 :   if (int e = from ()->get_error ())
   21689              :     {
   21690           40 :       auto_diagnostic_group d;
   21691           40 :       error_at (loc, "failed to read compiled module: %s",
   21692           40 :                 from ()->get_error (filename));
   21693           40 :       note_cmi_name ();
   21694              : 
   21695           40 :       if (e == EMFILE
   21696           40 :           || e == ENFILE
   21697              : #if MAPPED_READING
   21698           40 :           || e == ENOMEM
   21699              : #endif
   21700              :           || false)
   21701            0 :         inform (loc, "consider using %<-fno-module-lazy%>,"
   21702              :                 " increasing %<-param-lazy-modules=%u%> value,"
   21703              :                 " or increasing the per-process file descriptor limit",
   21704              :                 param_lazy_modules);
   21705           40 :       else if (e == ENOENT)
   21706           21 :         inform (loc, "imports must be built before being imported");
   21707              : 
   21708           40 :       if (outermost)
   21709           37 :         fatal_error (loc, "returning to the gate for a mechanical issue");
   21710              : 
   21711            3 :       ok = false;
   21712            3 :     }
   21713              : 
   21714         6929 :   maybe_completed_reading ();
   21715              : 
   21716         6929 :   return ok;
   21717              : }
   21718              : 
   21719              : /* Return the IDENTIFIER_NODE naming module IX.  This is the name
   21720              :    including dots.  */
   21721              : 
   21722              : char const *
   21723          408 : module_name (unsigned ix, bool header_ok)
   21724              : {
   21725          408 :   if (modules)
   21726              :     {
   21727          408 :       module_state *imp = (*modules)[ix];
   21728              : 
   21729          408 :       if (ix && !imp->name)
   21730            0 :         imp = imp->parent;
   21731              : 
   21732          408 :       if (header_ok || !imp->is_header ())
   21733          408 :         return imp->get_flatname ();
   21734              :     }
   21735              : 
   21736              :   return NULL;
   21737              : }
   21738              : 
   21739              : /* Return the bitmap describing what modules are imported.  Remember,
   21740              :    we always import ourselves.  */
   21741              : 
   21742              : bitmap
   21743       134689 : get_import_bitmap ()
   21744              : {
   21745       134689 :   return this_module ()->imports;
   21746              : }
   21747              : 
   21748              : /* Get the original decl for an instantiation at TINST, or NULL_TREE
   21749              :    if we're not an instantiation.  */
   21750              : 
   21751              : static tree
   21752       222568 : orig_decl_for_instantiation (tinst_level *tinst)
   21753              : {
   21754       222568 :   if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
   21755              :     return NULL_TREE;
   21756              : 
   21757       112220 :   tree decl = tinst->tldcl;
   21758       112220 :   if (TREE_CODE (decl) == TREE_LIST)
   21759            0 :     decl = TREE_PURPOSE (decl);
   21760       112220 :   if (TYPE_P (decl))
   21761        18858 :     decl = TYPE_NAME (decl);
   21762              :   return decl;
   21763              : }
   21764              : 
   21765              : /* Return the visible imports and path of instantiation for an
   21766              :    instantiation at TINST.  If TINST is nullptr, we're not in an
   21767              :    instantiation, and thus will return the visible imports of the
   21768              :    current TU (and NULL *PATH_MAP_P).   We cache the information on
   21769              :    the tinst level itself.  */
   21770              : 
   21771              : static bitmap
   21772       165513 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
   21773              : {
   21774       165513 :   gcc_checking_assert (modules_p ());
   21775              : 
   21776       165513 :   tree decl = orig_decl_for_instantiation (tinst);
   21777       165513 :   if (!decl)
   21778              :     {
   21779        71585 :       gcc_assert (!tinst || !tinst->next);
   21780              :       /* Not inside an instantiation, just the regular case.  */
   21781        71585 :       *path_map_p = nullptr;
   21782        71585 :       return get_import_bitmap ();
   21783              :     }
   21784              : 
   21785        93928 :   if (!tinst->path)
   21786              :     {
   21787              :       /* Calculate.  */
   21788        27798 :       bitmap visible = path_of_instantiation (tinst->next, path_map_p);
   21789        27798 :       bitmap path_map = *path_map_p;
   21790              : 
   21791        27798 :       if (!path_map)
   21792              :         {
   21793         3551 :           path_map = BITMAP_GGC_ALLOC ();
   21794         3551 :           bitmap_set_bit (path_map, 0);
   21795              :         }
   21796              : 
   21797        27798 :       if (unsigned mod = get_originating_module (decl))
   21798         3933 :         if (!bitmap_bit_p (path_map, mod))
   21799              :           {
   21800              :             /* This is brand new information!  */
   21801          172 :             bitmap new_path = BITMAP_GGC_ALLOC ();
   21802          172 :             bitmap_copy (new_path, path_map);
   21803          172 :             bitmap_set_bit (new_path, mod);
   21804          172 :             path_map = new_path;
   21805              : 
   21806          172 :             bitmap imports = (*modules)[mod]->imports;
   21807          172 :             if (bitmap_intersect_compl_p (imports, visible))
   21808              :               {
   21809              :                 /* IMPORTS contains additional modules to VISIBLE.  */
   21810           33 :                 bitmap new_visible = BITMAP_GGC_ALLOC ();
   21811              : 
   21812           33 :                 bitmap_ior (new_visible, visible, imports);
   21813           33 :                 visible = new_visible;
   21814              :               }
   21815              :           }
   21816              : 
   21817        27798 :       tinst->path = path_map;
   21818        27798 :       tinst->visible = visible;
   21819              :     }
   21820              : 
   21821        93928 :   *path_map_p = tinst->path;
   21822        93928 :   return tinst->visible;
   21823              : }
   21824              : 
   21825              : /* Return the bitmap describing what modules are visible along the
   21826              :    path of instantiation.  If we're not an instantiation, this will be
   21827              :    the visible imports of the TU.  *PATH_MAP_P is filled in with the
   21828              :    modules owning the instantiation path -- we see the module-linkage
   21829              :    entities of those modules.  */
   21830              : 
   21831              : bitmap
   21832     34434575 : visible_instantiation_path (bitmap *path_map_p)
   21833              : {
   21834     34434575 :   if (!modules_p ())
   21835              :     return NULL;
   21836              : 
   21837       137715 :   return path_of_instantiation (current_instantiation (), path_map_p);
   21838              : }
   21839              : 
   21840              : /* Returns the bitmap describing what modules were visible from the
   21841              :    module that the current instantiation originated from.  If we're
   21842              :    not an instantiation, returns NULL.  *MODULE_P is filled in with
   21843              :    the originating module of the definition for this instantiation.  */
   21844              : 
   21845              : bitmap
   21846        57055 : visible_from_instantiation_origination (unsigned *module_p)
   21847              : {
   21848        57055 :   if (!modules_p ())
   21849              :     return NULL;
   21850              : 
   21851        57055 :   tree decl = orig_decl_for_instantiation (current_instantiation ());
   21852        57055 :   if (!decl)
   21853              :     return NULL;
   21854              : 
   21855        18292 :   *module_p = get_originating_module (decl);
   21856        18292 :   return (*modules)[*module_p]->imports;
   21857              : }
   21858              : 
   21859              : /* We've just directly imported IMPORT.  Update our import/export
   21860              :    bitmaps.  IS_EXPORT is true if we're reexporting the OTHER.  */
   21861              : 
   21862              : void
   21863         3124 : module_state::set_import (module_state const *import, bool is_export)
   21864              : {
   21865         3124 :   gcc_checking_assert (this != import);
   21866              : 
   21867              :   /* We see IMPORT's exports (which includes IMPORT).  If IMPORT is
   21868              :      the primary interface or a partition we'll see its imports.  */
   21869         3124 :   bitmap_ior_into (imports, import->is_module () || import->is_partition ()
   21870              :                    ? import->imports : import->exports);
   21871              : 
   21872         3124 :   if (is_export)
   21873              :     /* We'll export OTHER's exports.  */
   21874          486 :     bitmap_ior_into (exports, import->exports);
   21875         3124 : }
   21876              : 
   21877              : /* Return the declaring entity of DECL.  That is the decl determining
   21878              :    how to decorate DECL with module information.  Returns NULL_TREE if
   21879              :    it's the global module.  */
   21880              : 
   21881              : tree
   21882    114618871 : get_originating_module_decl (tree decl)
   21883              : {
   21884              :   /* An enumeration constant.  */
   21885    114618871 :   if (TREE_CODE (decl) == CONST_DECL
   21886         9564 :       && DECL_CONTEXT (decl)
   21887    114628435 :       && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
   21888         8529 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   21889    114610342 :   else if (TREE_CODE (decl) == FIELD_DECL
   21890    114572416 :            || TREE_CODE (decl) == USING_DECL
   21891    229159429 :            || CONST_DECL_USING_P (decl))
   21892              :     {
   21893        62290 :       decl = DECL_CONTEXT (decl);
   21894        62290 :       if (TREE_CODE (decl) != FUNCTION_DECL)
   21895        62290 :         decl = TYPE_NAME (decl);
   21896              :     }
   21897              : 
   21898    114618871 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
   21899              :                        || TREE_CODE (decl) == FUNCTION_DECL
   21900              :                        || TREE_CODE (decl) == TYPE_DECL
   21901              :                        || TREE_CODE (decl) == VAR_DECL
   21902              :                        || TREE_CODE (decl) == CONCEPT_DECL
   21903              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
   21904              : 
   21905    117462959 :   for (;;)
   21906              :     {
   21907              :       /* Uninstantiated template friends are owned by the befriending
   21908              :          class -- not their context.  */
   21909    116040915 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   21910    116040915 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   21911        13024 :         decl = TYPE_NAME (DECL_CHAIN (decl));
   21912              : 
   21913              :       /* An imported temploid friend is attached to the same module the
   21914              :          befriending class was.  */
   21915    116040915 :       if (imported_temploid_friends)
   21916      4092084 :         if (tree *slot = imported_temploid_friends->get (decl))
   21917          716 :           decl = *slot;
   21918              : 
   21919    116040915 :       int use;
   21920    116040915 :       if (tree ti = node_template_info (decl, use))
   21921              :         {
   21922      1065385 :           decl = TI_TEMPLATE (ti);
   21923      1065385 :           if (TREE_CODE (decl) != TEMPLATE_DECL)
   21924              :             {
   21925              :               /* A friend template specialization.  */
   21926           54 :               gcc_checking_assert (OVL_P (decl) || identifier_p (decl));
   21927           54 :               return global_namespace;
   21928              :             }
   21929              :         }
   21930              :       else
   21931              :         {
   21932    114975530 :           tree ctx = CP_DECL_CONTEXT (decl);
   21933    114975530 :           if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21934              :             break;
   21935              : 
   21936       356713 :           if (TYPE_P (ctx))
   21937              :             {
   21938       319861 :               ctx = TYPE_NAME (ctx);
   21939       319861 :               if (!ctx)
   21940              :                 {
   21941              :                   /* Some kind of internal type.  */
   21942            0 :                   gcc_checking_assert (DECL_ARTIFICIAL (decl));
   21943            0 :                   return global_namespace;
   21944              :                 }
   21945              :             }
   21946       356713 :           decl = ctx;
   21947              :         }
   21948      1422044 :     }
   21949              : 
   21950    114618817 :   return decl;
   21951              : }
   21952              : 
   21953              : /* If DECL is imported, return which module imported it, or 0 for the current
   21954              :    module.  Except that if GLOBAL_M1, return -1 for decls attached to the
   21955              :    global module.  */
   21956              : 
   21957              : int
   21958      1667481 : get_originating_module (tree decl, bool global_m1)
   21959              : {
   21960      1667481 :   tree owner = get_originating_module_decl (decl);
   21961      1667481 :   tree not_tmpl = STRIP_TEMPLATE (owner);
   21962              : 
   21963      1667481 :   if (!DECL_LANG_SPECIFIC (not_tmpl))
   21964       470873 :     return global_m1 ? -1 : 0;
   21965              : 
   21966      2279903 :   if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
   21967              :     return -1;
   21968              : 
   21969       121678 :   int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
   21970       121678 :   gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
   21971              :   return mod;
   21972              : }
   21973              : 
   21974              : /* DECL is imported, return which module imported it.
   21975              :    If FLEXIBLE, return -1 if not found, otherwise checking ICE.  */
   21976              : 
   21977              : unsigned
   21978        16816 : get_importing_module (tree decl, bool flexible)
   21979              : {
   21980        16816 :   unsigned index = import_entity_index (decl, flexible);
   21981        16816 :   if (index == ~(~0u >> 1))
   21982              :     return -1;
   21983        16816 :   module_state *module = import_entity_module (index);
   21984              : 
   21985        16816 :   return module->mod;
   21986              : }
   21987              : 
   21988              : /* Is it permissible to redeclare OLDDECL with NEWDECL.
   21989              : 
   21990              :    If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
   21991              :    the current scope's module and attachment.  */
   21992              : 
   21993              : bool
   21994       213267 : module_may_redeclare (tree olddecl, tree newdecl)
   21995              : {
   21996       213267 :   tree decl = olddecl;
   21997       289153 :   for (;;)
   21998              :     {
   21999       251210 :       tree ctx = CP_DECL_CONTEXT (decl);
   22000       251210 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   22001              :         // Found the namespace-scope decl.
   22002              :         break;
   22003        39437 :       if (!CLASS_TYPE_P (ctx))
   22004              :         // We've met a non-class scope.  Such a thing is not
   22005              :         // reopenable, so we must be ok.
   22006              :         return true;
   22007        37943 :       decl = TYPE_NAME (ctx);
   22008        37943 :     }
   22009              : 
   22010       211773 :   int use_tpl = 0;
   22011       211773 :   if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
   22012              :     // Specializations of any kind can be redeclared anywhere.
   22013              :     // FIXME: Should we be checking this in more places on the scope chain?
   22014              :     return true;
   22015              : 
   22016       151307 :   module_state *old_mod = get_primary (this_module ());
   22017       151307 :   module_state *new_mod = old_mod;
   22018              : 
   22019       151307 :   tree old_origin = get_originating_module_decl (decl);
   22020       151307 :   tree old_inner = STRIP_TEMPLATE (old_origin);
   22021       151307 :   bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
   22022       229832 :                              && DECL_MODULE_ATTACH_P (old_inner));
   22023       229832 :   if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22024              :     {
   22025          849 :       unsigned index = import_entity_index (old_origin);
   22026          849 :       old_mod = get_primary (import_entity_module (index));
   22027              :     }
   22028              : 
   22029       151307 :   bool newdecl_attached_p = module_attach_p ();
   22030       151307 :   if (newdecl)
   22031              :     {
   22032        38197 :       tree new_origin = get_originating_module_decl (newdecl);
   22033        38197 :       tree new_inner = STRIP_TEMPLATE (new_origin);
   22034        38197 :       newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
   22035        73778 :                             && DECL_MODULE_ATTACH_P (new_inner));
   22036        73778 :       if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
   22037              :         {
   22038          175 :           unsigned index = import_entity_index (new_origin);
   22039          175 :           new_mod = get_primary (import_entity_module (index));
   22040              :         }
   22041              :     }
   22042              : 
   22043              :   /* Module attachment needs to match.  */
   22044       151307 :   if (olddecl_attached_p == newdecl_attached_p)
   22045              :     {
   22046       151163 :       if (!olddecl_attached_p)
   22047              :         /* Both are GM entities, OK.  */
   22048              :         return true;
   22049              : 
   22050         1440 :       if (new_mod == old_mod)
   22051              :         /* Both attached to same named module, OK.  */
   22052              :         return true;
   22053              :     }
   22054              : 
   22055              :   /* Attached to different modules, error.  */
   22056          171 :   decl = newdecl ? newdecl : olddecl;
   22057          171 :   location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
   22058          171 :   if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
   22059              :     {
   22060            3 :       if (newdecl_attached_p)
   22061            3 :         error_at (loc, "declaring %qD in module %qs conflicts with builtin "
   22062              :                   "in global module", decl, new_mod->get_flatname ());
   22063              :       else
   22064            0 :         error_at (loc, "declaration %qD conflicts with builtin", decl);
   22065              :     }
   22066          333 :   else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22067              :     {
   22068          153 :       auto_diagnostic_group d;
   22069          153 :       if (newdecl_attached_p)
   22070           75 :         error_at (loc, "redeclaring %qD in module %qs conflicts with import",
   22071              :                   decl, new_mod->get_flatname ());
   22072              :       else
   22073           78 :         error_at (loc, "redeclaring %qD in global module conflicts with import",
   22074              :                   decl);
   22075              : 
   22076          153 :       if (olddecl_attached_p)
   22077          105 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22078              :                 "import declared attached to module %qs",
   22079              :                 old_mod->get_flatname ());
   22080              :       else
   22081           48 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22082              :                 "import declared in global module");
   22083          153 :     }
   22084              :   else
   22085              :     {
   22086           15 :       auto_diagnostic_group d;
   22087           15 :       if (newdecl_attached_p)
   22088           15 :         error_at (loc, "conflicting declaration of %qD in module %qs",
   22089              :                   decl, new_mod->get_flatname ());
   22090              :       else
   22091            0 :         error_at (loc, "conflicting declaration of %qD in global module",
   22092              :                   decl);
   22093              : 
   22094           15 :       if (olddecl_attached_p)
   22095            0 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22096              :                 "previously declared in module %qs",
   22097              :                 old_mod->get_flatname ());
   22098              :       else
   22099           15 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22100              :                 "previously declared in global module");
   22101           15 :     }
   22102              :   return false;
   22103              : }
   22104              : 
   22105              : /* DECL is being created by this TU.  Record it came from here.  We
   22106              :    record module purview, so we can see if partial or explicit
   22107              :    specialization needs to be written out, even though its purviewness
   22108              :    comes from the most general template.  */
   22109              : 
   22110              : void
   22111    994326105 : set_instantiating_module (tree decl)
   22112              : {
   22113    994326105 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
   22114              :               || VAR_P (decl)
   22115              :               || TREE_CODE (decl) == TYPE_DECL
   22116              :               || TREE_CODE (decl) == CONCEPT_DECL
   22117              :               || TREE_CODE (decl) == TEMPLATE_DECL
   22118              :               || TREE_CODE (decl) == CONST_DECL
   22119              :               || (TREE_CODE (decl) == NAMESPACE_DECL
   22120              :                   && DECL_NAMESPACE_ALIAS (decl)));
   22121              : 
   22122    994326105 :   if (!modules_p ())
   22123              :     return;
   22124              : 
   22125      3957688 :   decl = STRIP_TEMPLATE (decl);
   22126              : 
   22127      3957688 :   if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
   22128       375885 :     retrofit_lang_decl (decl);
   22129              : 
   22130      3957688 :   if (DECL_LANG_SPECIFIC (decl))
   22131              :     {
   22132      3014774 :       DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
   22133              :       /* If this was imported, we'll still be in the entity_hash.  */
   22134      3014774 :       DECL_MODULE_IMPORT_P (decl) = false;
   22135              :     }
   22136              : }
   22137              : 
   22138              : /* If DECL is a class member, whose class is not defined in this TU
   22139              :    (it was imported), remember this decl.  */
   22140              : 
   22141              : void
   22142       156560 : set_defining_module (tree decl)
   22143              : {
   22144       156560 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   22145              :                        || !DECL_MODULE_IMPORT_P (decl));
   22146              : 
   22147       156560 :   if (module_maybe_has_cmi_p ())
   22148              :     {
   22149              :       /* We need to track all declarations within a module, not just those
   22150              :          in the module purview, because we don't necessarily know yet if
   22151              :          this module will require a CMI while in the global fragment.  */
   22152       106539 :       tree ctx = DECL_CONTEXT (decl);
   22153       106539 :       if (ctx
   22154       106539 :           && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
   22155        22683 :           && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
   22156       118103 :           && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
   22157              :         {
   22158              :           /* This entity's context is from an import.  We may need to
   22159              :              record this entity to make sure we emit it in the CMI.
   22160              :              Template specializations are in the template hash tables,
   22161              :              so we don't need to record them here as well.  */
   22162           39 :           int use_tpl = -1;
   22163           39 :           tree ti = node_template_info (decl, use_tpl);
   22164           39 :           if (use_tpl <= 0)
   22165              :             {
   22166           39 :               if (ti)
   22167              :                 {
   22168           21 :                   gcc_checking_assert (!use_tpl);
   22169              :                   /* Get to the TEMPLATE_DECL.  */
   22170           21 :                   decl = TI_TEMPLATE (ti);
   22171              :                 }
   22172              : 
   22173              :               /* Record it on the class_members list.  */
   22174           39 :               vec_safe_push (class_members, decl);
   22175              :             }
   22176              :         }
   22177              :     }
   22178       156560 : }
   22179              : 
   22180              : /* Also remember DECL if it's a newly declared class template partial
   22181              :    specialization, because these are not necessarily added to the
   22182              :    instantiation tables.  */
   22183              : 
   22184              : void
   22185      8540428 : set_defining_module_for_partial_spec (tree decl)
   22186              : {
   22187      8540428 :   if (module_maybe_has_cmi_p ()
   22188        26441 :       && DECL_IMPLICIT_TYPEDEF_P (decl)
   22189      8562690 :       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   22190        22262 :     vec_safe_push (partial_specializations, decl);
   22191      8540428 : }
   22192              : 
   22193              : /* Record that DECL is declared in this TU, and note attachment and
   22194              :    exporting for namespace-scope entities.  FRIEND_P is true if
   22195              :    this is a friend declaration.  */
   22196              : 
   22197              : void
   22198    320252733 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
   22199              : {
   22200    320252733 :   set_instantiating_module (decl);
   22201              : 
   22202              :   /* DECL_CONTEXT may not be set yet when we're called for
   22203              :      non-namespace-scope entities.  */
   22204    320252733 :   if (!DECL_CONTEXT (decl) || !DECL_NAMESPACE_SCOPE_P (decl))
   22205              :     return;
   22206              : 
   22207    119266333 :   gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
   22208              : 
   22209    119266333 :   if (module_attach_p ())
   22210              :     {
   22211         4309 :       retrofit_lang_decl (decl);
   22212         4309 :       DECL_MODULE_ATTACH_P (decl) = true;
   22213              :     }
   22214              : 
   22215              :   /* It is ill-formed to export a declaration with internal linkage.  However,
   22216              :      at the point this function is called we don't yet always know whether this
   22217              :      declaration has internal linkage; instead we defer this check for callers
   22218              :      to do once visibility has been determined.  */
   22219    119266333 :   if (module_exporting_p ())
   22220       159007 :     DECL_MODULE_EXPORT_P (decl) = true;
   22221              : }
   22222              : 
   22223              : /* Checks whether DECL within a module unit has valid linkage for its kind.
   22224              :    Must be called after visibility for DECL has been finalised.  */
   22225              : 
   22226              : void
   22227    402381370 : check_module_decl_linkage (tree decl)
   22228              : {
   22229    402381370 :   if (!module_has_cmi_p ())
   22230              :     return;
   22231              : 
   22232              :   /* A header unit shall not contain a definition of a non-inline function
   22233              :      or variable (not template) whose name has external linkage.  */
   22234       535917 :   if (header_module_p ()
   22235       483774 :       && !processing_template_decl
   22236       159980 :       && ((TREE_CODE (decl) == FUNCTION_DECL
   22237        94537 :            && !DECL_DECLARED_INLINE_P (decl))
   22238       123953 :           || (TREE_CODE (decl) == VAR_DECL
   22239        37546 :               && !DECL_INLINE_VAR_P (decl)))
   22240        45283 :       && decl_defined_p (decl)
   22241         4476 :       && !(DECL_LANG_SPECIFIC (decl)
   22242         4476 :            && DECL_TEMPLATE_INSTANTIATION (decl))
   22243       540393 :       && DECL_EXTERNAL_LINKAGE_P (decl))
   22244           60 :     error_at (DECL_SOURCE_LOCATION (decl),
   22245              :               "external linkage definition of %qD in header module must "
   22246              :               "be declared %<inline%>", decl);
   22247              : 
   22248              :   /* An internal-linkage declaration cannot be generally be exported.
   22249              :      But it's OK to export any declaration from a header unit, including
   22250              :      internal linkage declarations.  */
   22251       535917 :   if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
   22252              :     {
   22253              :       /* Let's additionally treat any exported declaration within an
   22254              :          internal namespace as exporting a declaration with internal
   22255              :          linkage, as this would also implicitly export the internal
   22256              :          linkage namespace.  */
   22257         1999 :       if (decl_anon_ns_mem_p (decl))
   22258              :         {
   22259           50 :           error_at (DECL_SOURCE_LOCATION (decl),
   22260              :                     "exporting declaration %qD declared in unnamed namespace",
   22261              :                     decl);
   22262           50 :           DECL_MODULE_EXPORT_P (decl) = false;
   22263              :         }
   22264         1949 :       else if (decl_linkage (decl) == lk_internal)
   22265              :         {
   22266           17 :           error_at (DECL_SOURCE_LOCATION (decl),
   22267              :                     "exporting declaration %qD with internal linkage", decl);
   22268           17 :           DECL_MODULE_EXPORT_P (decl) = false;
   22269              :         }
   22270              :     }
   22271              : }
   22272              : 
   22273              : /* Given a scope CTX, find the scope we want to attach the key to,
   22274              :    or NULL if no key scope is required.  */
   22275              : 
   22276              : static tree
   22277         2998 : adjust_key_scope (tree ctx)
   22278              : {
   22279              :   /* For members, key it to the containing type to handle deduplication
   22280              :      correctly.  For fields, this is necessary as FIELD_DECLs have no
   22281              :      dep and so would only be streamed after the lambda type, defeating
   22282              :      our ability to merge them.
   22283              : 
   22284              :      Other class-scope key decls might depend on the type of the lambda
   22285              :      but be within the same cluster; we need to ensure that we never
   22286              :      first see the key decl while streaming the lambda type as merging
   22287              :      would then fail when comparing the partially-streamed lambda type
   22288              :      of the key decl with the existing (PR c++/122310).
   22289              : 
   22290              :      Perhaps sort_cluster can be adjusted to handle this better, but
   22291              :      this is a simple workaround (and might down on the number of
   22292              :      entries in keyed_table as a bonus).  */
   22293         5019 :   while (!DECL_NAMESPACE_SCOPE_P (ctx))
   22294         2021 :     if (DECL_CLASS_SCOPE_P (ctx))
   22295         1907 :       ctx = TYPE_NAME (DECL_CONTEXT (ctx));
   22296              :     else
   22297          114 :       ctx = DECL_CONTEXT (ctx);
   22298              : 
   22299         2998 :   return ctx;
   22300              : }
   22301              : 
   22302              : /* DECL is keyed to CTX for odr purposes.  */
   22303              : 
   22304              : void
   22305      1943013 : maybe_key_decl (tree ctx, tree decl)
   22306              : {
   22307      1943013 :   if (!modules_p ())
   22308              :     return;
   22309              : 
   22310              :   /* We only need to deal here with decls attached to var, field,
   22311              :      parm, type, function, or concept decls.  */
   22312        12939 :   if (TREE_CODE (ctx) != VAR_DECL
   22313        12939 :       && TREE_CODE (ctx) != FIELD_DECL
   22314              :       && TREE_CODE (ctx) != PARM_DECL
   22315              :       && TREE_CODE (ctx) != TYPE_DECL
   22316              :       && TREE_CODE (ctx) != FUNCTION_DECL
   22317              :       && TREE_CODE (ctx) != CONCEPT_DECL)
   22318              :     return;
   22319              : 
   22320        25875 :   gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22321              :                        || TREE_CODE (ctx) == FUNCTION_DECL);
   22322              : 
   22323              :   /* We don't need to use the keyed map for functions with definitions,
   22324              :      as we can instead use the MK_local_type handling for streaming.  */
   22325        12939 :   if (TREE_CODE (ctx) == FUNCTION_DECL
   22326        12939 :       && (has_definition (ctx)
   22327              :           /* If we won't be streaming this definition there's also no
   22328              :              need to record the key, as it will not be useful for merging
   22329              :              (this function is non-inline and so a matching declaration
   22330              :              will always be an ODR violation anyway).  */
   22331          120 :           || !module_maybe_has_cmi_p ()))
   22332              :     return;
   22333              : 
   22334          686 :   ctx = adjust_key_scope (ctx);
   22335              : 
   22336          686 :   if (!keyed_table)
   22337          165 :     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22338              : 
   22339          686 :   auto &vec = keyed_table->get_or_insert (ctx);
   22340          686 :   if (!vec.length ())
   22341              :     {
   22342          633 :       retrofit_lang_decl (ctx);
   22343          633 :       DECL_MODULE_KEYED_DECLS_P (ctx) = true;
   22344              :     }
   22345          686 :   if (CHECKING_P)
   22346          851 :     for (tree t : vec)
   22347           59 :       gcc_checking_assert (t != decl);
   22348              : 
   22349          686 :   vec.safe_push (decl);
   22350              : }
   22351              : 
   22352              : /* Find the scope that the local type or lambda DECL is keyed to, if any.  */
   22353              : 
   22354              : static tree
   22355         2348 : get_keyed_decl_scope (tree decl)
   22356              : {
   22357         2348 :   gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   22358              : 
   22359         7044 :   tree scope = (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22360         4468 :                 ? LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))
   22361         2348 :                 : CP_DECL_CONTEXT (decl));
   22362         2348 :   if (!scope)
   22363              :     return NULL_TREE;
   22364              : 
   22365         2312 :   gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
   22366              :                        || TREE_CODE (scope) == FIELD_DECL
   22367              :                        || TREE_CODE (scope) == PARM_DECL
   22368              :                        || TREE_CODE (scope) == TYPE_DECL
   22369              :                        || (TREE_CODE (scope) == FUNCTION_DECL
   22370              :                            && !has_definition (scope))
   22371              :                        || TREE_CODE (scope) == CONCEPT_DECL);
   22372              : 
   22373         2312 :   scope = adjust_key_scope (scope);
   22374              : 
   22375         4624 :   gcc_checking_assert (scope
   22376              :                        && DECL_LANG_SPECIFIC (scope)
   22377              :                        && DECL_MODULE_KEYED_DECLS_P (scope));
   22378              :   return scope;
   22379              : }
   22380              : 
   22381              : /* DECL is an instantiated friend that should be attached to the same
   22382              :    module that ORIG is.  */
   22383              : 
   22384              : void
   22385      2763049 : propagate_defining_module (tree decl, tree orig)
   22386              : {
   22387      2763049 :   if (!modules_p ())
   22388              :     return;
   22389              : 
   22390         7966 :   tree not_tmpl = STRIP_TEMPLATE (orig);
   22391        15875 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
   22392              :     {
   22393          126 :       tree inner = STRIP_TEMPLATE (decl);
   22394          126 :       retrofit_lang_decl (inner);
   22395          126 :       DECL_MODULE_ATTACH_P (inner) = true;
   22396              :     }
   22397              : 
   22398        15875 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
   22399              :     {
   22400          387 :       bool exists = imported_temploid_friends->put (decl, orig);
   22401              : 
   22402              :       /* We should only be called if lookup for an existing decl
   22403              :          failed, in which case there shouldn't already be an entry
   22404              :          in the map.  */
   22405          387 :       gcc_assert (!exists);
   22406              :     }
   22407              : }
   22408              : 
   22409              : /* NEWDECL matched with OLDDECL, transfer defining module information
   22410              :    onto OLDDECL.  We've already validated attachment matches.  */
   22411              : 
   22412              : void
   22413     20635068 : transfer_defining_module (tree olddecl, tree newdecl)
   22414              : {
   22415     20635068 :   if (!modules_p ())
   22416              :     return;
   22417              : 
   22418        63985 :   tree old_inner = STRIP_TEMPLATE (olddecl);
   22419        63985 :   tree new_inner = STRIP_TEMPLATE (newdecl);
   22420              : 
   22421        63985 :   if (DECL_LANG_SPECIFIC (new_inner))
   22422              :     {
   22423        63833 :       retrofit_lang_decl (old_inner);
   22424        63833 :       if (DECL_MODULE_PURVIEW_P (new_inner))
   22425        22934 :         DECL_MODULE_PURVIEW_P (old_inner) = true;
   22426        63833 :       if (!DECL_MODULE_IMPORT_P (new_inner))
   22427        63833 :         DECL_MODULE_IMPORT_P (old_inner) = false;
   22428              :     }
   22429              : 
   22430        63985 :   if (tree *p = imported_temploid_friends->get (newdecl))
   22431              :     {
   22432           70 :       tree orig = *p;
   22433           70 :       tree &slot = imported_temploid_friends->get_or_insert (olddecl);
   22434           70 :       if (!slot)
   22435           47 :         slot = orig;
   22436           23 :       else if (slot != orig)
   22437              :         /* This can happen when multiple classes declare the same
   22438              :            friend function (e.g. g++.dg/modules/tpl-friend-4);
   22439              :            make sure we at least attach to the same module.  */
   22440            3 :         gcc_checking_assert (get_originating_module (slot)
   22441              :                              == get_originating_module (orig));
   22442              :     }
   22443              : }
   22444              : 
   22445              : /* DECL is being freed, clear data we don't need anymore.  */
   22446              : 
   22447              : void
   22448        45640 : remove_defining_module (tree decl)
   22449              : {
   22450        45640 :   if (!modules_p ())
   22451              :     return;
   22452              : 
   22453        45640 :   if (imported_temploid_friends)
   22454        45640 :     imported_temploid_friends->remove (decl);
   22455              : }
   22456              : 
   22457              : /* Create the flat name string.  It is simplest to have it handy.  */
   22458              : 
   22459              : void
   22460         6441 : module_state::set_flatname ()
   22461              : {
   22462         6441 :   gcc_checking_assert (!flatname);
   22463         6441 :   if (parent)
   22464              :     {
   22465          695 :       auto_vec<tree,5> ids;
   22466          695 :       size_t len = 0;
   22467          695 :       char const *primary = NULL;
   22468          695 :       size_t pfx_len = 0;
   22469              : 
   22470          695 :       for (module_state *probe = this;
   22471         1693 :            probe;
   22472          998 :            probe = probe->parent)
   22473         1525 :         if (is_partition () && !probe->is_partition ())
   22474              :           {
   22475          527 :             primary = probe->get_flatname ();
   22476          527 :             pfx_len = strlen (primary);
   22477          527 :             break;
   22478              :           }
   22479              :         else
   22480              :           {
   22481          998 :             ids.safe_push (probe->name);
   22482          998 :             len += IDENTIFIER_LENGTH (probe->name) + 1;
   22483              :           }
   22484              : 
   22485          695 :       char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
   22486          695 :       flatname = flat;
   22487              : 
   22488          695 :       if (primary)
   22489              :         {
   22490          527 :           memcpy (flat, primary, pfx_len);
   22491          527 :           flat += pfx_len;
   22492          527 :           *flat++ = ':';
   22493              :         }
   22494              : 
   22495         1693 :       for (unsigned len = 0; ids.length ();)
   22496              :         {
   22497          998 :           if (len)
   22498          303 :             flat[len++] = '.';
   22499          998 :           tree elt = ids.pop ();
   22500          998 :           unsigned l = IDENTIFIER_LENGTH (elt);
   22501          998 :           memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
   22502          998 :           len += l;
   22503              :         }
   22504          695 :     }
   22505         5746 :   else if (is_header ())
   22506         1934 :     flatname = TREE_STRING_POINTER (name);
   22507              :   else
   22508         3812 :     flatname = IDENTIFIER_POINTER (name);
   22509         6441 : }
   22510              : 
   22511              : /* Open the GCM file and prepare to read.  Return whether that was
   22512              :    successful.  */
   22513              : 
   22514              : bool
   22515         3104 : module_state::open_slurp (cpp_reader *reader)
   22516              : {
   22517         3104 :   if (slurp)
   22518              :     return true;
   22519              : 
   22520         3058 :   if (lazy_open >= lazy_limit)
   22521            9 :     freeze_an_elf ();
   22522              : 
   22523         3058 :   int fd = -1;
   22524         3058 :   int e = ENOENT;
   22525         3058 :   if (filename)
   22526              :     {
   22527         3058 :       const char *file = maybe_add_cmi_prefix (filename);
   22528         3590 :       dump () && dump ("CMI is %s", file);
   22529         3058 :       if (note_module_cmi_yes || inform_cmi_p)
   22530           12 :         inform (loc, "reading CMI %qs", file);
   22531              :       /* Add the CMI file to the dependency tracking. */
   22532         3058 :       if (cpp_get_deps (reader))
   22533           15 :         deps_add_dep (cpp_get_deps (reader), file);
   22534         3058 :       fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
   22535         3058 :       e = errno;
   22536              :     }
   22537              : 
   22538         3058 :   gcc_checking_assert (!slurp);
   22539         6092 :   slurp = new slurping (new elf_in (fd, e));
   22540              : 
   22541         3058 :   bool ok = from ()->begin (loc);
   22542         3058 :   if (ok)
   22543              :     {
   22544         3034 :       lazy_open++;
   22545         3034 :       slurp->lru = ++lazy_lru;
   22546              :     }
   22547              :   return ok;
   22548              : }
   22549              : 
   22550              : /* Return whether importing this GCM would work without an error in
   22551              :    read_config.  */
   22552              : 
   22553              : bool
   22554           52 : module_state::check_importable (cpp_reader *reader)
   22555              : {
   22556           52 :   if (loadedness > ML_CONFIG)
   22557              :     return true;
   22558           49 :   if (!open_slurp (reader))
   22559              :     return false;
   22560           46 :   module_state_config config;
   22561           46 :   return read_config (config, /*complain*/false);
   22562              : }
   22563              : 
   22564              : /* Read the CMI file for a module.  */
   22565              : 
   22566              : bool
   22567         3055 : module_state::do_import (cpp_reader *reader, bool outermost)
   22568              : {
   22569         3055 :   gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
   22570              : 
   22571              :   /* If this TU is a partition of the module we're importing,
   22572              :      that module is the primary module interface.  */
   22573         3055 :   if (this_module ()->is_partition ()
   22574         3109 :       && this == get_primary (this_module ()))
   22575            9 :     module_p = true;
   22576              : 
   22577         3055 :   loc = linemap_module_loc (line_table, loc, get_flatname ());
   22578              : 
   22579         3055 :   bool ok = open_slurp (reader);
   22580         3055 :   if (!from ()->get_error ())
   22581              :     {
   22582         3034 :       announce ("importing");
   22583         3034 :       loadedness = ML_CONFIG;
   22584         3034 :       ok = read_initial (reader);
   22585              :     }
   22586              : 
   22587         3055 :   gcc_assert (slurp->current == ~0u);
   22588              : 
   22589         3055 :   return check_read (outermost, ok);
   22590              : }
   22591              : 
   22592              : /* Attempt to increase the file descriptor limit.  */
   22593              : 
   22594              : static bool
   22595         4979 : try_increase_lazy (unsigned want)
   22596              : {
   22597         4979 :   gcc_checking_assert (lazy_open >= lazy_limit);
   22598              : 
   22599              :   /* If we're increasing, saturate at hard limit.  */
   22600         4979 :   if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
   22601         4979 :     want = lazy_hard_limit;
   22602              : 
   22603              : #if HAVE_SETRLIMIT
   22604         4979 :   if ((!lazy_limit || !param_lazy_modules)
   22605         4967 :       && lazy_hard_limit
   22606         4967 :       && want <= lazy_hard_limit)
   22607              :     {
   22608         4967 :       struct rlimit rlimit;
   22609         4967 :       rlimit.rlim_cur = want + LAZY_HEADROOM;
   22610         4967 :       rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
   22611         4967 :       if (!setrlimit (RLIMIT_NOFILE, &rlimit))
   22612         4967 :         lazy_limit = want;
   22613              :     }
   22614              : #endif
   22615              : 
   22616         4979 :   return lazy_open < lazy_limit;
   22617              : }
   22618              : 
   22619              : /* Pick a victim module to freeze its reader.  */
   22620              : 
   22621              : void
   22622           12 : module_state::freeze_an_elf ()
   22623              : {
   22624           12 :   if (try_increase_lazy (lazy_open * 2))
   22625              :     return;
   22626              : 
   22627           12 :   module_state *victim = NULL;
   22628           42 :   for (unsigned ix = modules->length (); ix--;)
   22629              :     {
   22630           30 :       module_state *candidate = (*modules)[ix];
   22631           30 :       if (candidate && candidate->slurp && candidate->slurp->lru
   22632           18 :           && candidate->from ()->is_freezable ()
   22633           39 :           && (!victim || victim->slurp->lru > candidate->slurp->lru))
   22634           30 :         victim = candidate;
   22635              :     }
   22636              : 
   22637           12 :   if (victim)
   22638              :     {
   22639           18 :       dump () && dump ("Freezing '%s'", victim->filename);
   22640            9 :       if (victim->slurp->macro_defs.size)
   22641              :         /* Save the macro definitions to a buffer.  */
   22642            0 :         victim->from ()->preserve (victim->slurp->macro_defs);
   22643            9 :       if (victim->slurp->macro_tbl.size)
   22644              :         /* Save the macro definitions to a buffer.  */
   22645            0 :         victim->from ()->preserve (victim->slurp->macro_tbl);
   22646            9 :       victim->from ()->freeze ();
   22647            9 :       lazy_open--;
   22648              :     }
   22649              :   else
   22650            3 :     dump () && dump ("No module available for freezing");
   22651              : }
   22652              : 
   22653              : /* Load the lazy slot *MSLOT, INDEX'th slot of the module.  */
   22654              : 
   22655              : bool
   22656        62665 : module_state::lazy_load (unsigned index, binding_slot *mslot)
   22657              : {
   22658        62665 :   unsigned n = dump.push (this);
   22659              : 
   22660        62665 :   gcc_checking_assert (function_depth);
   22661              : 
   22662        62665 :   unsigned cookie = mslot->get_lazy ();
   22663        62665 :   unsigned snum = cookie >> 2;
   22664        63073 :   dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
   22665              : 
   22666        62665 :   bool ok = load_section (snum, mslot);
   22667              : 
   22668        62665 :   dump.pop (n);
   22669              : 
   22670        62665 :   return ok;
   22671              : }
   22672              : 
   22673              : /* Load MOD's binding for NS::ID into *MSLOT.  *MSLOT contains the
   22674              :    lazy cookie.  OUTER is true if this is the outermost lazy, (used
   22675              :    for diagnostics).  */
   22676              : 
   22677              : void
   22678         5932 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
   22679              : {
   22680         5932 :   int count = errorcount + warningcount;
   22681              : 
   22682         5932 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22683              : 
   22684              :   /* Make sure lazy loading from a template context behaves as if
   22685              :      from a non-template context.  */
   22686         5932 :   processing_template_decl_sentinel ptds;
   22687              : 
   22688              :   /* Stop GC happening, even in outermost loads (because our caller
   22689              :      could well be building up a lookup set).  */
   22690         5932 :   function_depth++;
   22691              : 
   22692         5932 :   gcc_checking_assert (mod);
   22693         5932 :   module_state *module = (*modules)[mod];
   22694         5932 :   unsigned n = dump.push (module);
   22695              : 
   22696         5932 :   unsigned snum = mslot->get_lazy ();
   22697         6287 :   dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
   22698              :                    module->name, snum);
   22699              : 
   22700         5932 :   bool ok = !recursive_lazy (snum);
   22701         5932 :   if (ok)
   22702              :     {
   22703         5932 :       ok = module->load_section (snum, mslot);
   22704         5932 :       lazy_snum = 0;
   22705         5932 :       post_load_processing ();
   22706              :     }
   22707              : 
   22708         5932 :   dump.pop (n);
   22709              : 
   22710         5932 :   function_depth--;
   22711              : 
   22712         5932 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22713              : 
   22714         5932 :   if (!ok)
   22715            0 :     fatal_error (input_location,
   22716            0 :                  module->is_header ()
   22717              :                  ? G_("failed to load binding %<%E%s%E%>")
   22718              :                  : G_("failed to load binding %<%E%s%E@%s%>"),
   22719            0 :                  ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22720              :                  module->get_flatname ());
   22721              : 
   22722         5932 :   if (count != errorcount + warningcount)
   22723           27 :     inform (input_location,
   22724           27 :             module->is_header ()
   22725              :             ? G_("during load of binding %<%E%s%E%>")
   22726              :             : G_("during load of binding %<%E%s%E@%s%>"),
   22727           27 :             ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22728              :             module->get_flatname ());
   22729         5932 : }
   22730              : 
   22731              : /* Load any pending entities keyed to NS and NAME.
   22732              :    Used to find pending types if we don't yet have a decl built.  */
   22733              : 
   22734              : void
   22735     39723527 : lazy_load_pendings (tree ns, tree name)
   22736              : {
   22737              :   /* Make sure lazy loading from a template context behaves as if
   22738              :      from a non-template context.  */
   22739     39723527 :   processing_template_decl_sentinel ptds;
   22740              : 
   22741     39723527 :   pending_key key;
   22742     39723527 :   key.ns = ns;
   22743     39723527 :   key.id = name;
   22744              : 
   22745     39723527 :   auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
   22746     39718556 :   if (!pending_vec)
   22747     39717212 :     return;
   22748              : 
   22749         6315 :   int count = errorcount + warningcount;
   22750              : 
   22751         6315 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22752         6315 :   bool ok = !recursive_lazy ();
   22753         6315 :   if (ok)
   22754              :     {
   22755         6315 :       function_depth++; /* Prevent GC */
   22756         6315 :       unsigned n = dump.push (NULL);
   22757         6819 :       dump () && dump ("Reading %u pending entities keyed to %P",
   22758              :                        pending_vec->length (), key.ns, key.id);
   22759        84491 :       for (unsigned ix = pending_vec->length (); ix--;)
   22760              :         {
   22761        71861 :           unsigned index = (*pending_vec)[ix];
   22762        71861 :           binding_slot *slot = &(*entity_ary)[index];
   22763              : 
   22764        71861 :           if (slot->is_lazy ())
   22765              :             {
   22766         6816 :               module_state *import = import_entity_module (index);
   22767         6816 :               if (!import->lazy_load (index - import->entity_lwm, slot))
   22768        71861 :                 ok = false;
   22769              :             }
   22770        65045 :           else if (dump ())
   22771              :             {
   22772          363 :               module_state *import = import_entity_module (index);
   22773        71861 :               dump () && dump ("Entity %M[%u] already loaded",
   22774          363 :                                import, index - import->entity_lwm);
   22775              :             }
   22776              :         }
   22777              : 
   22778         6315 :       pending_table->remove (key);
   22779         6315 :       dump.pop (n);
   22780         6315 :       lazy_snum = 0;
   22781         6315 :       post_load_processing ();
   22782         6315 :       function_depth--;
   22783              :     }
   22784              : 
   22785         6315 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22786              : 
   22787         6315 :   if (!ok)
   22788            0 :     fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
   22789            0 :                  key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22790              : 
   22791         6315 :   if (count != errorcount + warningcount)
   22792            0 :     inform (input_location, "during load of pendings for %<%E%s%E%>",
   22793            0 :             key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22794     39723527 : }
   22795              : 
   22796              : /* Load any pending entities keyed to the top-key of DECL.  */
   22797              : 
   22798              : void
   22799     39628046 : lazy_load_pendings (tree decl)
   22800              : {
   22801     39628046 :   tree key_decl;
   22802     39628046 :   tree ns = find_pending_key (decl, &key_decl);
   22803     39628046 :   return lazy_load_pendings (ns, DECL_NAME (key_decl));
   22804              : }
   22805              : 
   22806              : static void
   22807         2773 : direct_import (module_state *import, cpp_reader *reader)
   22808              : {
   22809         2773 :   timevar_start (TV_MODULE_IMPORT);
   22810         2773 :   unsigned n = dump.push (import);
   22811              : 
   22812         2773 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22813         2773 :   if (import->loadedness == ML_NONE)
   22814         1822 :     if (!import->do_import (reader, true))
   22815            0 :       gcc_unreachable ();
   22816              : 
   22817         2736 :   this_module ()->set_import (import, import->exported_p);
   22818              : 
   22819         2736 :   if (import->loadedness < ML_LANGUAGE)
   22820              :     {
   22821         2655 :       if (!keyed_table)
   22822         2331 :         keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22823         2655 :       import->read_language (true);
   22824              :     }
   22825              : 
   22826         2736 :   dump.pop (n);
   22827         2736 :   timevar_stop (TV_MODULE_IMPORT);
   22828         2736 : }
   22829              : 
   22830              : /* Import module IMPORT.  */
   22831              : 
   22832              : void
   22833         2537 : import_module (module_state *import, location_t from_loc, bool exporting_p,
   22834              :                tree, cpp_reader *reader)
   22835              : {
   22836              :   /* A non-partition implementation unit has no name.  */
   22837         2537 :   if (!this_module ()->name && this_module ()->parent == import)
   22838              :     {
   22839            3 :       auto_diagnostic_group d;
   22840            3 :       error_at (from_loc, "import of %qs within its own implementation unit",
   22841              :                 import->get_flatname());
   22842            3 :       inform (import->loc, "module declared here");
   22843            3 :       return;
   22844            3 :     }
   22845              : 
   22846         2534 :   if (!import->check_circular_import (from_loc))
   22847              :     return;
   22848              : 
   22849         2528 :   if (!import->is_header () && current_lang_depth ())
   22850              :     /* Only header units should appear inside language
   22851              :        specifications.  The std doesn't specify this, but I think
   22852              :        that's an error in resolving US 033, because language linkage
   22853              :        is also our escape clause to getting things into the global
   22854              :        module, so we don't want to confuse things by having to think
   22855              :        about whether 'extern "C++" { import foo; }' puts foo's
   22856              :        contents into the global module all of a sudden.  */
   22857            6 :     warning (0, "import of named module %qs inside language-linkage block",
   22858              :              import->get_flatname ());
   22859              : 
   22860         2528 :   if (exporting_p || module_exporting_p ())
   22861          331 :     import->exported_p = true;
   22862              : 
   22863         2528 :   if (import->loadedness != ML_NONE)
   22864              :     {
   22865          948 :       from_loc = ordinary_loc_of (line_table, from_loc);
   22866          948 :       linemap_module_reparent (line_table, import->loc, from_loc);
   22867              :     }
   22868              : 
   22869         2528 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22870              : 
   22871         2528 :   direct_import (import, reader);
   22872              : }
   22873              : 
   22874              : /* Declare the name of the current module to be NAME.  EXPORTING_p is
   22875              :    true if this TU is the exporting module unit.  */
   22876              : 
   22877              : void
   22878         3189 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
   22879              :                 tree, cpp_reader *reader)
   22880              : {
   22881         3189 :   gcc_assert (global_namespace == current_scope ());
   22882              : 
   22883         3189 :   module_state *current = this_module ();
   22884         3189 :   if (module_purview_p () || module->loadedness > ML_CONFIG)
   22885              :     {
   22886            6 :       auto_diagnostic_group d;
   22887           12 :       error_at (from_loc, module_purview_p ()
   22888              :                 ? G_("module already declared")
   22889              :                 : G_("module already imported"));
   22890            6 :       if (module_purview_p ())
   22891            0 :         module = current;
   22892           12 :       inform (module->loc, module_purview_p ()
   22893              :               ? G_("module %qs declared here")
   22894              :               : G_("module %qs imported here"),
   22895              :               module->get_flatname ());
   22896            6 :       return;
   22897            6 :     }
   22898              : 
   22899         3183 :   gcc_checking_assert (module->is_module ());
   22900         3183 :   gcc_checking_assert (module->is_direct () && module->has_location ());
   22901              : 
   22902              :   /* Yer a module, 'arry.  */
   22903         3183 :   module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
   22904              : 
   22905              :   // Even in header units, we consider the decls to be purview
   22906         3183 :   module_kind |= MK_PURVIEW;
   22907              : 
   22908         3183 :   if (module->is_partition ())
   22909          214 :     module_kind |= MK_PARTITION;
   22910         3183 :   if (exporting_p)
   22911              :     {
   22912         2870 :       module->interface_p = true;
   22913         2870 :       module_kind |= MK_INTERFACE;
   22914              :     }
   22915              : 
   22916         3183 :   if (module_has_cmi_p ())
   22917              :     {
   22918              :       /* Copy the importing information we may have already done.  We
   22919              :          do not need to separate out the imports that only happen in
   22920              :          the GMF, in spite of what the literal wording of the std
   22921              :          might imply.  See p2191, the core list had a discussion
   22922              :          where the module implementors agreed that the GMF of a named
   22923              :          module is invisible to importers.  */
   22924         2938 :       module->imports = current->imports;
   22925              : 
   22926         2938 :       module->mod = 0;
   22927         2938 :       (*modules)[0] = module;
   22928              :     }
   22929              :   else
   22930              :     {
   22931          245 :       module->interface_p = true;
   22932          245 :       current->parent = module; /* So mangler knows module identity. */
   22933          245 :       direct_import (module, reader);
   22934              :     }
   22935              : }
   22936              : 
   22937              : /* Return true IFF we must emit a module global initializer function
   22938              :    (which will be called by importers' init code).  */
   22939              : 
   22940              : bool
   22941       107372 : module_global_init_needed ()
   22942              : {
   22943       107372 :   return module_has_cmi_p () && !header_module_p ();
   22944              : }
   22945              : 
   22946              : /* Calculate which, if any, import initializers need calling.  */
   22947              : 
   22948              : bool
   22949        99773 : module_determine_import_inits ()
   22950              : {
   22951        99773 :   if (!modules || header_module_p ())
   22952              :     return false;
   22953              : 
   22954              :   /* Prune active_init_p.  We need the same bitmap allocation
   22955              :      scheme as for the imports member.  */
   22956         3868 :   function_depth++; /* Disable GC.  */
   22957         3868 :   bitmap covered_imports (BITMAP_GGC_ALLOC ());
   22958              : 
   22959         3868 :   bool any = false;
   22960              : 
   22961              :   /* Because indirect imports are before their direct import, and
   22962              :      we're scanning the array backwards, we only need one pass!  */
   22963         6739 :   for (unsigned ix = modules->length (); --ix;)
   22964              :     {
   22965         2871 :       module_state *import = (*modules)[ix];
   22966              : 
   22967         2871 :       if (!import->active_init_p)
   22968              :         ;
   22969           64 :       else if (bitmap_bit_p (covered_imports, ix))
   22970            9 :         import->active_init_p = false;
   22971              :       else
   22972              :         {
   22973              :           /* Everything this imports is therefore handled by its
   22974              :              initializer, so doesn't need initializing by us.  */
   22975           55 :           bitmap_ior_into (covered_imports, import->imports);
   22976           55 :           any = true;
   22977              :         }
   22978              :     }
   22979         3868 :   function_depth--;
   22980              : 
   22981         3868 :   return any;
   22982              : }
   22983              : 
   22984              : /* Emit calls to each direct import's global initializer.  Including
   22985              :    direct imports of directly imported header units.  The initializers
   22986              :    of (static) entities in header units will be called by their
   22987              :    importing modules (for the instance contained within that), or by
   22988              :    the current TU (for the instances we've brought in).  Of course
   22989              :    such header unit behaviour is evil, but iostream went through that
   22990              :    door some time ago.  */
   22991              : 
   22992              : void
   22993           55 : module_add_import_initializers ()
   22994              : {
   22995           55 :   if (!modules || header_module_p ())
   22996            0 :     return;
   22997              : 
   22998           55 :   tree fntype = build_function_type (void_type_node, void_list_node);
   22999           55 :   releasing_vec args;  // There are no args
   23000              : 
   23001          125 :   for (unsigned ix = modules->length (); --ix;)
   23002              :     {
   23003           70 :       module_state *import = (*modules)[ix];
   23004           70 :       if (import->active_init_p)
   23005              :         {
   23006           55 :           tree name = mangle_module_global_init (ix);
   23007           55 :           tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
   23008              : 
   23009           55 :           DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
   23010           55 :           SET_DECL_ASSEMBLER_NAME (fndecl, name);
   23011           55 :           TREE_PUBLIC (fndecl) = true;
   23012           55 :           determine_visibility (fndecl);
   23013              : 
   23014           55 :           tree call = cp_build_function_call_vec (fndecl, &args,
   23015              :                                                   tf_warning_or_error);
   23016           55 :           finish_expr_stmt (call);
   23017              :         }
   23018              :     }
   23019           55 : }
   23020              : 
   23021              : /* NAME & LEN are a preprocessed header name, possibly including the
   23022              :    surrounding "" or <> characters.  Return the raw string name of the
   23023              :    module to which it refers.  This will be an absolute path, or begin
   23024              :    with ./, so it is immediately distinguishable from a (non-header
   23025              :    unit) module name.  If READER is non-null, ask the preprocessor to
   23026              :    locate the header to which it refers using the appropriate include
   23027              :    path.  Note that we do never do \ processing of the string, as that
   23028              :    matches the preprocessor's behaviour.  */
   23029              : 
   23030              : static const char *
   23031        26485 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
   23032              :                           const char *str, size_t &len_r)
   23033              : {
   23034        26485 :   size_t len = len_r;
   23035        26485 :   static char *buf = 0;
   23036        26485 :   static size_t alloc = 0;
   23037              : 
   23038        26485 :   if (!unquoted)
   23039              :     {
   23040            4 :       gcc_checking_assert (len >= 2
   23041              :                            && ((reader && str[0] == '<' && str[len-1] == '>')
   23042              :                                || (str[0] == '"' && str[len-1] == '"')));
   23043            4 :       str += 1;
   23044            4 :       len -= 2;
   23045              :     }
   23046              : 
   23047        26485 :   if (reader)
   23048              :     {
   23049            4 :       gcc_assert (!unquoted);
   23050              : 
   23051            4 :       if (len >= alloc)
   23052              :         {
   23053            4 :           alloc = len + 1;
   23054            4 :           buf = XRESIZEVEC (char, buf, alloc);
   23055              :         }
   23056            4 :       memcpy (buf, str, len);
   23057            4 :       buf[len] = 0;
   23058              : 
   23059            8 :       if (const char *hdr
   23060            4 :           = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
   23061              :         {
   23062            4 :           len = strlen (hdr);
   23063            4 :           str = hdr;
   23064              :         }
   23065              :       else
   23066            0 :         str = buf;
   23067              :     }
   23068              : 
   23069        26485 :   if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
   23070              :     {
   23071              :       /* Prepend './'  */
   23072            9 :       if (len + 3 > alloc)
   23073              :         {
   23074            9 :           alloc = len + 3;
   23075            9 :           buf = XRESIZEVEC (char, buf, alloc);
   23076              :         }
   23077              : 
   23078            9 :       buf[0] = '.';
   23079            9 :       buf[1] = DIR_SEPARATOR;
   23080            9 :       memmove (buf + 2, str, len);
   23081            9 :       len += 2;
   23082            9 :       buf[len] = 0;
   23083            9 :       str = buf;
   23084              :     }
   23085              : 
   23086        26485 :   len_r = len;
   23087        26485 :   return str;
   23088              : }
   23089              : 
   23090              : /* Set the CMI name from a cody packet.  Issue an error if
   23091              :    ill-formed.  */
   23092              : 
   23093         5844 : void module_state::set_filename (const Cody::Packet &packet)
   23094              : {
   23095         5844 :   if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23096              :     {
   23097              :       /* If we've seen this import before we better have the same CMI.  */
   23098         5841 :       const std::string &path = packet.GetString ();
   23099         5841 :       if (!filename)
   23100         5838 :         filename = xstrdup (packet.GetString ().c_str ());
   23101            3 :       else if (filename != path)
   23102            0 :         error_at (loc, "mismatching compiled module interface: "
   23103              :                   "had %qs, got %qs", filename, path.c_str ());
   23104              :     }
   23105              :   else
   23106              :     {
   23107            3 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23108            3 :       fatal_error (loc, "unknown compiled module interface: %s",
   23109            3 :                    packet.GetString ().c_str ());
   23110              :     }
   23111         5841 : }
   23112              : 
   23113              : /* The list of importable headers from C++ Table 24.  */
   23114              : 
   23115              : static const char *
   23116              : importable_headers[] =
   23117              :   {
   23118              :     "algorithm", "any", "array", "atomic",
   23119              :     "barrier", "bit", "bitset",
   23120              :     "charconv", "chrono", "compare", "complex", "concepts",
   23121              :     "condition_variable", "contracts", "coroutine",
   23122              :     "debugging", "deque",
   23123              :     "exception", "execution", "expected",
   23124              :     "filesystem", "flat_map", "flat_set", "format", "forward_list",
   23125              :     "fstream", "functional", "future",
   23126              :     "generator",
   23127              :     "hazard_pointer", "hive",
   23128              :     "initializer_list", "inplace_vector", "iomanip", "ios", "iosfwd",
   23129              :     "iostream", "istream", "iterator",
   23130              :     "latch", "limits", "linalg", "list", "locale",
   23131              :     "map", "mdspan", "memory", "memory_resource", "meta", "mutex",
   23132              :     "new", "numbers", "numeric",
   23133              :     "optional", "ostream",
   23134              :     "print",
   23135              :     "queue",
   23136              :     "random", "ranges", "ratio", "rcu", "regex",
   23137              :     "scoped_allocator", "semaphore", "set", "shared_mutex", "simd",
   23138              :     "source_location", "span", "spanstream", "sstream", "stack", "stacktrace",
   23139              :     "stdexcept", "stdfloat", "stop_token", "streambuf", "string",
   23140              :     "string_view", "syncstream", "system_error",
   23141              :     "text_encoding", "thread", "tuple", "type_traits", "typeindex", "typeinfo",
   23142              :     "unordered_map", "unordered_set",
   23143              :     "utility",
   23144              :     "valarray", "variant", "vector", "version"
   23145              :   };
   23146              : 
   23147              : /* True iff <name> is listed as an importable standard header.  */
   23148              : 
   23149              : static bool
   23150        24187 : is_importable_header (const char *name)
   23151              : {
   23152        24187 :   unsigned lo = 0;
   23153        24187 :   unsigned hi = ARRAY_SIZE (importable_headers);
   23154       183415 :   while (hi > lo)
   23155              :     {
   23156       162112 :       unsigned mid = (lo + hi)/2;
   23157       162112 :       int cmp = strcmp (name, importable_headers[mid]);
   23158       162112 :       if (cmp > 0)
   23159        37453 :         lo = mid + 1;
   23160       124659 :       else if (cmp < 0)
   23161              :         hi = mid;
   23162              :       else
   23163              :         return true;
   23164              :     }
   23165              :   return false;
   23166              : }
   23167              : 
   23168              : /* Figure out whether to treat HEADER as an include or an import.  */
   23169              : 
   23170              : static char *
   23171        25548 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
   23172              :                          _cpp_file *file, bool angle, const char **alternate)
   23173              : {
   23174        25548 :   if (!modules_p ())
   23175              :     {
   23176              :       /* Turn off.  */
   23177            0 :       cpp_get_callbacks (reader)->translate_include = NULL;
   23178            0 :       return nullptr;
   23179              :     }
   23180              : 
   23181        25548 :   const char *path = _cpp_get_file_path (file);
   23182              : 
   23183        25548 :   dump.push (NULL);
   23184              : 
   23185        27073 :   dump () && dump ("Checking include translation '%s'", path);
   23186        25548 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23187              : 
   23188        25548 :   size_t len = strlen (path);
   23189        25548 :   path = canonicalize_header_name (NULL, loc, true, path, len);
   23190        25548 :   auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
   23191              : 
   23192        25548 :   enum class xlate_kind {
   23193              :     unknown, text, import, invalid
   23194        25548 :   } translate = xlate_kind::unknown;
   23195              : 
   23196        25548 :   if (packet.GetCode () == Cody::Client::PC_BOOL)
   23197        25499 :     translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
   23198           49 :   else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23199              :     {
   23200              :       /* Record the CMI name for when we do the import.
   23201              :          We may already know about this import, but libcpp doesn't yet.  */
   23202           49 :       module_state *import = get_module (build_string (len, path));
   23203           49 :       import->set_filename (packet);
   23204           49 :       if (import->check_importable (reader))
   23205              :         translate = xlate_kind::import;
   23206              :       else
   23207            0 :         translate = xlate_kind::invalid;
   23208              :     }
   23209              :   else
   23210              :     {
   23211            0 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23212            0 :       error_at (loc, "cannot determine %<#include%> translation of %s: %s",
   23213            0 :                 path, packet.GetString ().c_str ());
   23214              :     }
   23215              : 
   23216        25548 :   bool note = (translate == xlate_kind::invalid);
   23217        25548 :   if (note_include_translate_yes && translate == xlate_kind::import)
   23218              :     note = true;
   23219        25543 :   else if (note_include_translate_no && translate == xlate_kind::unknown)
   23220              :     note = true;
   23221        25540 :   else if (note_includes)
   23222              :     /* We do not expect the note_includes vector to be large, so O(N)
   23223              :        iteration.  */
   23224          434 :     for (unsigned ix = note_includes->length (); !note && ix--;)
   23225          217 :       if (!strcmp ((*note_includes)[ix], path))
   23226            1 :         note = true;
   23227              : 
   23228              :   /* Maybe try importing a different header instead.  */
   23229        25548 :   if (alternate && translate == xlate_kind::unknown)
   23230              :     {
   23231        25098 :       const char *fname = _cpp_get_file_name (file);
   23232              :       /* Redirect importable <name> to <bits/stdc++.h>.  */
   23233              :       /* ??? Generalize to use a .json.  */
   23234        25098 :       expanded_location eloc = expand_location (loc);
   23235        27967 :       auto indir = [](const char *f, const char *d)
   23236              :       {
   23237         2869 :         if (!filename_ncmp (f, d, strlen (d))) return true;
   23238              :         /* Also check canonical paths (c++/123879).  */
   23239          938 :         auto cf = lrealpath (f); auto cd = lrealpath (d);
   23240          938 :         bool r = cf && cd && !filename_ncmp (cf, cd, strlen (cd));
   23241          938 :         free (cf); free (cd);
   23242          938 :         return r;
   23243              :       };
   23244        24187 :       if (angle && is_importable_header (fname)
   23245              :           /* Exclude <version> which often goes with import std.  */
   23246         2884 :           && strcmp (fname, "version") != 0
   23247              :           /* Don't redirect #includes between headers under the same include
   23248              :              path directory (i.e. between library headers); if the import
   23249              :              brings in the current file we then get redefinition errors.  */
   23250         2869 :           && !indir (eloc.file, _cpp_get_file_dir (file)->name)
   23251              :           /* ??? These are needed when running a toolchain from the build
   23252              :              directory, because libsupc++ headers aren't linked into
   23253              :              libstdc++-v3/include with the other headers.  */
   23254          859 :           && !strstr (eloc.file, "libstdc++-v3/include")
   23255        25586 :           && !strstr (eloc.file, "libsupc++"))
   23256          404 :         *alternate = "bits/stdc++.h";
   23257              :     }
   23258              : 
   23259        25548 :   if (note)
   23260           12 :     inform (loc, translate == xlate_kind::import
   23261              :             ? G_("include %qs translated to import")
   23262              :             : translate == xlate_kind::invalid
   23263            3 :             ? G_("import of %qs failed, falling back to include")
   23264              :             : G_("include %qs processed textually"), path);
   23265              : 
   23266        28595 :   dump () && dump (translate == xlate_kind::import
   23267              :                    ? "Translating include to import"
   23268              :                    : "Keeping include as include");
   23269        25548 :   dump.pop (0);
   23270              : 
   23271        25548 :   if (translate != xlate_kind::import)
   23272              :     return nullptr;
   23273              : 
   23274              :   /* Create the translation text.  */
   23275           49 :   loc = ordinary_loc_of (lmaps, loc);
   23276           49 :   const line_map_ordinary *map
   23277           49 :     = linemap_check_ordinary (linemap_lookup (lmaps, loc));
   23278           49 :   unsigned col = SOURCE_COLUMN (map, loc);
   23279           49 :   col -= (col != 0); /* Columns are 1-based.  */
   23280              : 
   23281           49 :   unsigned alloc = len + col + 60;
   23282           49 :   char *res = XNEWVEC (char, alloc);
   23283              : 
   23284           49 :   strcpy (res, "__import");
   23285           49 :   unsigned actual = 8;
   23286           49 :   if (col > actual)
   23287              :     {
   23288              :       /* Pad out so the filename appears at the same position.  */
   23289           46 :       memset (res + actual, ' ', col - actual);
   23290           46 :       actual = col;
   23291              :     }
   23292              :   /* No need to encode characters, that's not how header names are
   23293              :      handled.  */
   23294           49 :   actual += snprintf (res + actual, alloc - actual,
   23295              :                       "\"%s\" [[__translated]];\n", path);
   23296           49 :   gcc_checking_assert (actual < alloc);
   23297              : 
   23298              :   /* cpplib will delete the buffer.  */
   23299              :   return res;
   23300        25548 : }
   23301              : 
   23302              : static void
   23303          933 : begin_header_unit (cpp_reader *reader)
   23304              : {
   23305              :   /* Set the module header name from the main_input_filename.  */
   23306          933 :   const char *main = main_input_filename;
   23307          933 :   size_t len = strlen (main);
   23308          933 :   main = canonicalize_header_name (NULL, 0, true, main, len);
   23309          933 :   module_state *module = get_module (build_string (len, main));
   23310              : 
   23311          933 :   preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
   23312          933 : }
   23313              : 
   23314              : /* We've just properly entered the main source file.  I.e. after the
   23315              :    command line, builtins and forced headers.  Record the line map and
   23316              :    location of this map.  Note we may be called more than once.  The
   23317              :    first call sticks.  */
   23318              : 
   23319              : void
   23320       101788 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
   23321              :                        const line_map_ordinary *map)
   23322              : {
   23323       101788 :   gcc_checking_assert (lmaps == line_table);
   23324       101788 :   if (modules_p () && !spans.init_p ())
   23325              :     {
   23326         4967 :       unsigned n = dump.push (NULL);
   23327         4967 :       spans.init (lmaps, map);
   23328         4967 :       dump.pop (n);
   23329         4967 :       if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
   23330              :         {
   23331              :           /* Tell the preprocessor this is an include file.  */
   23332          924 :           cpp_retrofit_as_include (reader);
   23333          924 :           begin_header_unit (reader);
   23334              :         }
   23335              :     }
   23336       101788 : }
   23337              : 
   23338              : /* Process the pending_import queue, making sure we know the
   23339              :    filenames.   */
   23340              : 
   23341              : static void
   23342         5884 : name_pending_imports (cpp_reader *reader)
   23343              : {
   23344         5884 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23345              : 
   23346         5884 :   if (!vec_safe_length (pending_imports))
   23347              :     /* Not doing anything.  */
   23348              :     return;
   23349              : 
   23350         5030 :   timevar_start (TV_MODULE_MAPPER);
   23351              : 
   23352         5030 :   auto n = dump.push (NULL);
   23353         5652 :   dump () && dump ("Resolving direct import names");
   23354         5030 :   bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
   23355         5030 :                     || cpp_get_deps (reader));
   23356         5030 :   bool any = false;
   23357              : 
   23358        11009 :   for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23359              :     {
   23360         5979 :       module_state *module = (*pending_imports)[ix];
   23361         5979 :       gcc_checking_assert (module->is_direct ());
   23362         5979 :       if (!module->filename && !module->visited_p)
   23363              :         {
   23364         5867 :           bool export_p = (module->is_module ()
   23365         5867 :                            && (module->is_partition ()
   23366         3047 :                                || module->is_exported ()));
   23367              : 
   23368         5867 :           Cody::Flags flags = Cody::Flags::None;
   23369         5867 :           if (flag_preprocess_only
   23370         5867 :               && !(module->is_header () && !export_p))
   23371              :             {
   23372          141 :               if (!want_deps)
   23373           72 :                 continue;
   23374              :               flags = Cody::Flags::NameOnly;
   23375              :             }
   23376              : 
   23377         5795 :           if (!any)
   23378              :             {
   23379         4956 :               any = true;
   23380         4956 :               mapper->Cork ();
   23381              :             }
   23382         5795 :           if (export_p)
   23383         2992 :             mapper->ModuleExport (module->get_flatname (), flags);
   23384              :           else
   23385         2803 :             mapper->ModuleImport (module->get_flatname (), flags);
   23386         5795 :           module->visited_p = true;
   23387              :         }
   23388              :     }
   23389              : 
   23390         5030 :   if (any)
   23391              :     {
   23392         4956 :       auto response = mapper->Uncork ();
   23393         4956 :       auto r_iter = response.begin ();
   23394        10843 :       for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23395              :         {
   23396         5890 :           module_state *module = (*pending_imports)[ix];
   23397         5890 :           if (module->visited_p)
   23398              :             {
   23399         5795 :               module->visited_p = false;
   23400         5795 :               gcc_checking_assert (!module->filename);
   23401              : 
   23402         5795 :               module->set_filename (*r_iter);
   23403         5792 :               ++r_iter;
   23404              :             }
   23405              :         }
   23406         4953 :     }
   23407              : 
   23408         5027 :   dump.pop (n);
   23409              : 
   23410         5027 :   timevar_stop (TV_MODULE_MAPPER);
   23411              : }
   23412              : 
   23413              : /* We've just lexed a module-specific control line for MODULE.  Mark
   23414              :    the module as a direct import, and possibly load up its macro
   23415              :    state.  Returns the primary module, if this is a module
   23416              :    declaration.  */
   23417              : /* Perhaps we should offer a preprocessing mode where we read the
   23418              :    directives from the header unit, rather than require the header's
   23419              :    CMI.  */
   23420              : 
   23421              : module_state *
   23422         5999 : preprocess_module (module_state *module, location_t from_loc,
   23423              :                    bool in_purview, bool is_import, bool is_export,
   23424              :                    cpp_reader *reader)
   23425              : {
   23426         5999 :   if (!is_import)
   23427              :     {
   23428         3345 :       if (in_purview || module->loc)
   23429              :         {
   23430              :           /* We've already seen a module declaration.  If only preprocessing
   23431              :              then we won't complain in declare_module, so complain here.  */
   23432           42 :           if (flag_preprocess_only)
   23433            6 :             error_at (from_loc,
   23434              :                       in_purview
   23435              :                       ? G_("module already declared")
   23436              :                       : G_("module already imported"));
   23437              :           /* Always pretend this was an import to aid error recovery.  */
   23438              :           is_import = true;
   23439              :         }
   23440              :       else
   23441              :         {
   23442              :           /* Record it is the module.  */
   23443         3303 :           module->module_p = true;
   23444         3303 :           if (is_export)
   23445              :             {
   23446         2972 :               module->exported_p = true;
   23447         2972 :               module->interface_p = true;
   23448              :             }
   23449              :         }
   23450              :     }
   23451              : 
   23452         5999 :   if (module->directness < MD_DIRECT + in_purview)
   23453              :     {
   23454              :       /* Mark as a direct import.  */
   23455         5952 :       module->directness = module_directness (MD_DIRECT + in_purview);
   23456              : 
   23457              :       /* Set the location to be most informative for users.  */
   23458         5952 :       from_loc = ordinary_loc_of (line_table, from_loc);
   23459         5952 :       if (module->loadedness != ML_NONE)
   23460            6 :         linemap_module_reparent (line_table, module->loc, from_loc);
   23461              :       else
   23462              :         {
   23463              :           /* Don't overwrite the location if we're importing ourselves
   23464              :              after already having seen a module-declaration.  */
   23465         5946 :           if (!(is_import && module->is_module ()))
   23466         5916 :             module->loc = from_loc;
   23467         5946 :           if (!module->flatname)
   23468         5907 :             module->set_flatname ();
   23469              :         }
   23470              :     }
   23471              : 
   23472         5999 :   auto desired = ML_CONFIG;
   23473         5999 :   if (is_import
   23474         2696 :       && module->is_header ()
   23475         6930 :       && (!cpp_get_options (reader)->preprocessed
   23476            3 :           || cpp_get_options (reader)->directives_only))
   23477              :     /* We need preprocessor state now.  */
   23478              :     desired = ML_PREPROCESSOR;
   23479              : 
   23480         5999 :   if (!is_import || module->loadedness < desired)
   23481              :     {
   23482         5979 :       vec_safe_push (pending_imports, module);
   23483              : 
   23484         5979 :       if (desired == ML_PREPROCESSOR)
   23485              :         {
   23486          911 :           unsigned n = dump.push (NULL);
   23487              : 
   23488         1110 :           dump () && dump ("Reading %M preprocessor state", module);
   23489          911 :           name_pending_imports (reader);
   23490              : 
   23491              :           /* Preserve the state of the line-map.  */
   23492          911 :           auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
   23493              : 
   23494              :           /* We only need to close the span, if we're going to emit a
   23495              :              CMI.  But that's a little tricky -- our token scanner
   23496              :              needs to be smarter -- and this isn't much state.
   23497              :              Remember, we've not parsed anything at this point, so
   23498              :              our module state flags are inadequate.  */
   23499          911 :           spans.maybe_init ();
   23500          911 :           spans.close ();
   23501              : 
   23502          911 :           timevar_start (TV_MODULE_IMPORT);
   23503              : 
   23504              :           /* Load the config of each pending import -- we must assign
   23505              :              module numbers monotonically.  */
   23506         2922 :           for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23507              :             {
   23508         1100 :               auto *import = (*pending_imports)[ix];
   23509         1278 :               if (!(import->is_module ()
   23510          178 :                     && (import->is_partition () || import->is_exported ()))
   23511          928 :                   && import->loadedness == ML_NONE
   23512         2027 :                   && (!flag_preprocess_only
   23513           51 :                       || (import->is_header ()
   23514              :                           /* Allow a missing/unimportable GCM with -MG.
   23515              :                              FIXME We should also try falling back to #include
   23516              :                              before giving up entirely.  */
   23517           42 :                           && (!cpp_get_options (reader)->deps.missing_files
   23518            3 :                               || import->check_importable (reader)))))
   23519              :                 {
   23520          915 :                   unsigned n = dump.push (import);
   23521          915 :                   import->do_import (reader, true);
   23522          915 :                   dump.pop (n);
   23523              :                 }
   23524              :             }
   23525          911 :           vec_free (pending_imports);
   23526              : 
   23527              :           /* Restore the line-map state.  */
   23528          911 :           spans.open (linemap_module_restore (line_table, pre_hwm));
   23529              : 
   23530              :           /* Now read the preprocessor state of this particular
   23531              :              import.  */
   23532          911 :           if (module->loadedness == ML_CONFIG
   23533          911 :               && module->read_preprocessor (true))
   23534          905 :             module->import_macros ();
   23535              : 
   23536          911 :           timevar_stop (TV_MODULE_IMPORT);
   23537              : 
   23538          911 :           dump.pop (n);
   23539              :         }
   23540              :     }
   23541              : 
   23542         5999 :   return is_import ? NULL : get_primary (module);
   23543              : }
   23544              : 
   23545              : /* We've completed phase-4 translation.  Emit any dependency
   23546              :    information for the not-yet-loaded direct imports, and fill in
   23547              :    their file names.  We'll have already loaded up the direct header
   23548              :    unit wavefront.  */
   23549              : 
   23550              : void
   23551         4973 : preprocessed_module (cpp_reader *reader)
   23552              : {
   23553         4973 :   unsigned n = dump.push (NULL);
   23554              : 
   23555         5559 :   dump () && dump ("Completed phase-4 (tokenization) processing");
   23556              : 
   23557         4973 :   name_pending_imports (reader);
   23558         4970 :   vec_free (pending_imports);
   23559              : 
   23560         4970 :   spans.maybe_init ();
   23561         4970 :   spans.close ();
   23562              : 
   23563         4970 :   using iterator = hash_table<module_state_hash>::iterator;
   23564         4970 :   if (mkdeps *deps = cpp_get_deps (reader))
   23565              :     {
   23566              :       /* Walk the module hash, informing the dependency machinery.  */
   23567           57 :       iterator end = modules_hash->end ();
   23568          228 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23569              :         {
   23570          114 :           module_state *module = *iter;
   23571              : 
   23572          114 :           if (module->is_direct ())
   23573              :             {
   23574           90 :               if (module->is_module ()
   23575           90 :                   && (module->is_interface () || module->is_partition ()))
   23576           36 :                 deps_add_module_target (deps, module->get_flatname (),
   23577           36 :                                         maybe_add_cmi_prefix (module->filename),
   23578              :                                         module->is_header (),
   23579              :                                         module->is_exported ());
   23580              :               else
   23581           54 :                 deps_add_module_dep (deps, module->get_flatname ());
   23582              :             }
   23583              :         }
   23584              :     }
   23585              : 
   23586         4970 :   if (flag_header_unit && !flag_preprocess_only)
   23587              :     {
   23588              :       /* Find the main module -- remember, it's not yet in the module
   23589              :          array.  */
   23590          921 :       iterator end = modules_hash->end ();
   23591         1896 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23592              :         {
   23593          975 :           module_state *module = *iter;
   23594          975 :           if (module->is_module ())
   23595              :             {
   23596          921 :               declare_module (module, cpp_main_loc (reader), true, NULL, reader);
   23597          921 :               module_kind |= MK_EXPORTING;
   23598          921 :               break;
   23599              :             }
   23600              :         }
   23601              :     }
   23602              : 
   23603         4970 :   dump.pop (n);
   23604         4970 : }
   23605              : 
   23606              : /* VAL is a global tree, add it to the global vec if it is
   23607              :    interesting.  Add some of its targets, if they too are
   23608              :    interesting.  We do not add identifiers, as they can be re-found
   23609              :    via the identifier hash table.  There is a cost to the number of
   23610              :    global trees.  */
   23611              : 
   23612              : static int
   23613      3081701 : maybe_add_global (tree val, unsigned &crc)
   23614              : {
   23615      3081701 :   int v = 0;
   23616              : 
   23617      3081701 :   if (val && !(identifier_p (val) || TREE_VISITED (val)))
   23618              :     {
   23619       948286 :       TREE_VISITED (val) = true;
   23620       948286 :       crc = crc32_unsigned (crc, fixed_trees->length ());
   23621       948286 :       vec_safe_push (fixed_trees, val);
   23622       948286 :       v++;
   23623              : 
   23624       948286 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
   23625       948286 :         v += maybe_add_global (TREE_TYPE (val), crc);
   23626       948286 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
   23627       621623 :         v += maybe_add_global (TYPE_NAME (val), crc);
   23628              :     }
   23629              : 
   23630      3081701 :   return v;
   23631              : }
   23632              : 
   23633              : /* Initialize module state.  Create the hash table, determine the
   23634              :    global trees.  Create the module for current TU.  */
   23635              : 
   23636              : void
   23637         4973 : init_modules (cpp_reader *reader)
   23638              : {
   23639              :   /* PCH should not be reachable because of lang-specs, but the
   23640              :      user could have overridden that.  */
   23641         4973 :   if (pch_file)
   23642            0 :     fatal_error (input_location,
   23643              :                  "C++ modules are incompatible with precompiled headers");
   23644              : 
   23645         4973 :   if (cpp_get_options (reader)->traditional)
   23646            0 :     fatal_error (input_location,
   23647              :                  "C++ modules are incompatible with traditional preprocessing");
   23648              : 
   23649              :   /* :: is always exported.  */
   23650         4973 :   DECL_MODULE_EXPORT_P (global_namespace) = true;
   23651              : 
   23652         4973 :   modules_hash = hash_table<module_state_hash>::create_ggc (31);
   23653         4973 :   vec_safe_reserve (modules, 20);
   23654              : 
   23655              :   /* Create module for current TU.  */
   23656         4973 :   module_state *current
   23657         4973 :     = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
   23658         4973 :   current->mod = 0;
   23659         4973 :   bitmap_set_bit (current->imports, 0);
   23660         4973 :   modules->quick_push (current);
   23661              : 
   23662         4973 :   gcc_checking_assert (!fixed_trees);
   23663              : 
   23664         4973 :   headers = BITMAP_GGC_ALLOC ();
   23665              : 
   23666         4973 :   if (note_includes)
   23667              :     /* Canonicalize header names.  */
   23668            2 :     for (unsigned ix = 0; ix != note_includes->length (); ix++)
   23669              :       {
   23670            1 :         const char *hdr = (*note_includes)[ix];
   23671            1 :         size_t len = strlen (hdr);
   23672              : 
   23673            1 :         bool system = hdr[0] == '<';
   23674            1 :         bool user = hdr[0] == '"';
   23675            1 :         bool delimed = system || user;
   23676              : 
   23677            1 :         if (len <= (delimed ? 2 : 0)
   23678            1 :             || (delimed && hdr[len-1] != (system ? '>' : '"')))
   23679            0 :           error ("invalid header name %qs", hdr);
   23680              : 
   23681            1 :         hdr = canonicalize_header_name (delimed ? reader : NULL,
   23682              :                                         0, !delimed, hdr, len);
   23683            1 :         char *path = XNEWVEC (char, len + 1);
   23684            1 :         memcpy (path, hdr, len);
   23685            1 :         path[len] = 0;
   23686              : 
   23687            1 :         (*note_includes)[ix] = path;
   23688              :       }
   23689              : 
   23690         4973 :   if (note_cmis)
   23691              :     /* Canonicalize & mark module names.  */
   23692           12 :     for (unsigned ix = 0; ix != note_cmis->length (); ix++)
   23693              :       {
   23694            6 :         const char *name = (*note_cmis)[ix];
   23695            6 :         size_t len = strlen (name);
   23696              : 
   23697            6 :         bool is_system = name[0] == '<';
   23698            6 :         bool is_user = name[0] == '"';
   23699            6 :         bool is_pathname = false;
   23700            6 :         if (!(is_system || is_user))
   23701           12 :           for (unsigned ix = len; !is_pathname && ix--;)
   23702            9 :             is_pathname = IS_DIR_SEPARATOR (name[ix]);
   23703            6 :         if (is_system || is_user || is_pathname)
   23704              :           {
   23705            3 :             if (len <= (is_pathname ? 0 : 2)
   23706            3 :                 || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
   23707              :               {
   23708            0 :                 error ("invalid header name %qs", name);
   23709            0 :                 continue;
   23710              :               }
   23711              :             else
   23712            3 :               name = canonicalize_header_name (is_pathname ? nullptr : reader,
   23713              :                                                0, is_pathname, name, len);
   23714              :           }
   23715            6 :         if (auto module = get_module (name))
   23716            6 :           module->inform_cmi_p = 1;
   23717              :         else
   23718            0 :           error ("invalid module name %qs", name);
   23719              :       }
   23720              : 
   23721         4973 :   dump.push (NULL);
   23722              : 
   23723              :   /* Determine lazy handle bound.  */
   23724         4973 :   {
   23725         4973 :     unsigned limit = 1000;
   23726              : #if HAVE_GETRLIMIT
   23727         4973 :     struct rlimit rlimit;
   23728         4973 :     if (!getrlimit (RLIMIT_NOFILE, &rlimit))
   23729              :       {
   23730         4973 :         lazy_hard_limit = (rlimit.rlim_max < 1000000
   23731         4973 :                            ? unsigned (rlimit.rlim_max) : 1000000);
   23732         4973 :         lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
   23733         4973 :                            ? lazy_hard_limit - LAZY_HEADROOM : 0);
   23734         4973 :         if (rlimit.rlim_cur < limit)
   23735            0 :           limit = unsigned (rlimit.rlim_cur);
   23736              :       }
   23737              : #endif
   23738         4973 :     limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
   23739              : 
   23740         4973 :     if (unsigned parm = param_lazy_modules)
   23741              :       {
   23742         4973 :         if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
   23743            6 :           lazy_limit = parm;
   23744              :       }
   23745              :     else
   23746            0 :       lazy_limit = limit;
   23747              :   }
   23748              : 
   23749         4973 :   if (dump ())
   23750              :     {
   23751          586 :       verstr_t ver;
   23752          586 :       version2string (MODULE_VERSION, ver);
   23753          586 :       dump ("Source: %s", main_input_filename);
   23754          586 :       dump ("Compiler: %s", version_string);
   23755          586 :       dump ("Modules: %s", ver);
   23756          586 :       dump ("Checking: %s",
   23757              : #if CHECKING_P
   23758              :             "checking"
   23759              : #elif ENABLE_ASSERT_CHECKING
   23760              :             "asserting"
   23761              : #else
   23762              :             "release"
   23763              : #endif
   23764              :             );
   23765          586 :       dump ("Compiled by: "
   23766              : #ifdef __GNUC__
   23767              :             "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
   23768              : #ifdef __OPTIMIZE__
   23769              :             "optimizing"
   23770              : #else
   23771              :             "not optimizing"
   23772              : #endif
   23773              : #else
   23774              :             "not GCC"
   23775              : #endif
   23776              :             );
   23777          586 :       dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
   23778          586 :       dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
   23779          586 :       dump ("Lazy limit: %u", lazy_limit);
   23780          586 :       dump ("Lazy hard limit: %u", lazy_hard_limit);
   23781          586 :       dump ("");
   23782              :     }
   23783              : 
   23784              :   /* Construct the global tree array.  This is an array of unique
   23785              :      global trees (& types).  Do this now, rather than lazily, as
   23786              :      some global trees are lazily created and we don't want that to
   23787              :      mess with our syndrome of fixed trees.  */
   23788         4973 :   unsigned crc = 0;
   23789         4973 :   vec_alloc (fixed_trees, 250);
   23790              : 
   23791         5559 :   dump () && dump ("+Creating globals");
   23792              :   /* Insert the TRANSLATION_UNIT_DECL.  */
   23793         4973 :   TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
   23794         4973 :   fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
   23795        34811 :   for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
   23796              :     {
   23797        24865 :       const tree *ptr = global_tree_arys[jx].first;
   23798        24865 :       unsigned limit = global_tree_arys[jx].second;
   23799              : 
   23800      1521738 :       for (unsigned ix = 0; ix != limit; ix++, ptr++)
   23801              :         {
   23802      1496873 :           !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
   23803      1496873 :           unsigned v = maybe_add_global (*ptr, crc);
   23804      1673259 :           dump () && dump ("+%u", v);
   23805              :         }
   23806              :     }
   23807              :   /* OS- and machine-specific types are dynamically registered at
   23808              :      runtime, so cannot be part of global_tree_arys.  */
   23809         4973 :   registered_builtin_types && dump ("") && dump ("+\tB:");
   23810        19892 :   for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
   23811              :     {
   23812        14919 :       unsigned v = maybe_add_global (TREE_VALUE (t), crc);
   23813        16677 :       dump () && dump ("+%u", v);
   23814              :     }
   23815         4973 :   global_crc = crc32_unsigned (crc, fixed_trees->length ());
   23816         4973 :   dump ("") && dump ("Created %u unique globals, crc=%x",
   23817              :                      fixed_trees->length (), global_crc);
   23818       958232 :   for (unsigned ix = fixed_trees->length (); ix--;)
   23819       953259 :     TREE_VISITED ((*fixed_trees)[ix]) = false;
   23820              : 
   23821         4973 :   dump.pop (0);
   23822              : 
   23823         4973 :   if (!flag_module_lazy)
   23824              :     /* Get the mapper now, if we're not being lazy.  */
   23825          313 :     get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23826              : 
   23827         4973 :   if (!flag_preprocess_only)
   23828              :     {
   23829         4829 :       pending_table = new pending_map_t (EXPERIMENT (1, 400));
   23830         4829 :       entity_map = new entity_map_t (EXPERIMENT (1, 400));
   23831         4829 :       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
   23832         4829 :       imported_temploid_friends
   23833         4829 :         = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
   23834              :     }
   23835              : 
   23836              : #if CHECKING_P
   23837         4973 :   note_defs = note_defs_table_t::create_ggc (1000);
   23838              : #endif
   23839              : 
   23840         4973 :   if (flag_header_unit && cpp_get_options (reader)->preprocessed)
   23841            9 :     begin_header_unit (reader);
   23842              : 
   23843              :   /* Collect here to make sure things are tagged correctly (when
   23844              :      aggressively GC'd).  */
   23845         4973 :   ggc_collect ();
   23846         4973 : }
   23847              : 
   23848              : /* If NODE is a deferred macro, load it.  */
   23849              : 
   23850              : static int
   23851        83254 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
   23852              : {
   23853        83254 :   location_t main_loc
   23854        83254 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   23855              : 
   23856        83254 :   if (cpp_user_macro_p (node)
   23857        83254 :       && !node->value.macro)
   23858              :     {
   23859           72 :       cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
   23860           72 :       dump () && dump ("Loaded macro #%s %I",
   23861              :                        macro ? "define" : "undef", identifier (node));
   23862              :     }
   23863              : 
   23864        83254 :   return 1;
   23865              : }
   23866              : 
   23867              : /* At the end of tokenizing, we no longer need the macro tables of
   23868              :    imports.  But the user might have requested some checking.  */
   23869              : 
   23870              : void
   23871        99984 : maybe_check_all_macros (cpp_reader *reader)
   23872              : {
   23873        99984 :   if (!warn_imported_macros)
   23874              :     return;
   23875              : 
   23876              :   /* Force loading of any remaining deferred macros.  This will
   23877              :      produce diagnostics if they are ill-formed.  */
   23878           21 :   unsigned n = dump.push (NULL);
   23879           21 :   cpp_forall_identifiers (reader, load_macros, NULL);
   23880           21 :   dump.pop (n);
   23881              : }
   23882              : 
   23883              : // State propagated from finish_module_processing to fini_modules
   23884              : 
   23885              : struct module_processing_cookie
   23886              : {
   23887              :   elf_out out;
   23888              :   module_state_config config;
   23889              :   char *cmi_name;
   23890              :   char *tmp_name;
   23891              :   unsigned crc;
   23892              :   bool began;
   23893              : 
   23894         2932 :   module_processing_cookie (char *cmi, char *tmp, int fd, int e)
   23895         2932 :     : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
   23896              :   {
   23897              :   }
   23898         2932 :   ~module_processing_cookie ()
   23899              :   {
   23900         2932 :     XDELETEVEC (tmp_name);
   23901         2932 :     XDELETEVEC (cmi_name);
   23902         2932 :   }
   23903              : };
   23904              : 
   23905              : /* Write the CMI, if we're a module interface.  */
   23906              : 
   23907              : void *
   23908        99773 : finish_module_processing (cpp_reader *reader)
   23909              : {
   23910        99773 :   module_processing_cookie *cookie = nullptr;
   23911              : 
   23912        99773 :   if (header_module_p ())
   23913          921 :     module_kind &= ~MK_EXPORTING;
   23914              : 
   23915        99773 :   if (!modules || !this_module ()->name)
   23916              :     {
   23917        96838 :       if (flag_module_only)
   23918            6 :         warning (0, "%<-fmodule-only%> used for non-interface");
   23919              :     }
   23920         2935 :   else if (!flag_syntax_only)
   23921              :     {
   23922         2932 :       int fd = -1;
   23923         2932 :       int e = -1;
   23924              : 
   23925         2932 :       timevar_start (TV_MODULE_EXPORT);
   23926              : 
   23927              :       /* Force a valid but empty line map at the end.  This simplifies
   23928              :          the line table preparation and writing logic.  */
   23929         2932 :       linemap_add (line_table, LC_ENTER, false, "", 0);
   23930              : 
   23931              :       /* We write to a tmpname, and then atomically rename.  */
   23932         2932 :       char *cmi_name = NULL;
   23933         2932 :       char *tmp_name = NULL;
   23934         2932 :       module_state *state = this_module ();
   23935              : 
   23936         2932 :       unsigned n = dump.push (state);
   23937         2932 :       state->announce ("creating");
   23938         2932 :       if (state->filename)
   23939              :         {
   23940         2932 :           size_t len = 0;
   23941         2932 :           cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
   23942         2932 :           tmp_name = XNEWVEC (char, len + 3);
   23943         2932 :           memcpy (tmp_name, cmi_name, len);
   23944         2932 :           strcpy (&tmp_name[len], "~");
   23945              : 
   23946         2932 :           if (!errorcount)
   23947           40 :             for (unsigned again = 2; ; again--)
   23948              :               {
   23949         2860 :                 fd = open (tmp_name,
   23950              :                            O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
   23951              :                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   23952         2860 :                 e = errno;
   23953         2860 :                 if (fd >= 0 || !again || e != ENOENT)
   23954              :                   break;
   23955           40 :                 create_dirs (tmp_name);
   23956              :               }
   23957         2932 :           if (note_module_cmi_yes || state->inform_cmi_p)
   23958            3 :             inform (state->loc, "writing CMI %qs", cmi_name);
   23959         3232 :           dump () && dump ("CMI is %s", cmi_name);
   23960              :         }
   23961              : 
   23962         2932 :       cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
   23963              : 
   23964         2932 :       if (errorcount)
   23965              :         /* Don't write the module if we have reported errors.  */;
   23966         2820 :       else if (erroneous_templates
   23967         2820 :                && !erroneous_templates->is_empty ())
   23968              :         {
   23969              :           /* Don't write the module if it contains an erroneous template.
   23970              :              Also emit notes about where errors occurred in case
   23971              :              -Wno-template-body was passed.  */
   23972            6 :           auto_diagnostic_group d;
   23973            6 :           error_at (state->loc, "not writing module %qs due to errors "
   23974              :                     "in template bodies", state->get_flatname ());
   23975            6 :           if (!warn_template_body)
   23976            3 :             inform (state->loc, "enable %<-Wtemplate-body%> for more details");
   23977           12 :           for (auto e : *erroneous_templates)
   23978            6 :             inform (e.second, "first error in %qD appeared here", e.first);
   23979            6 :         }
   23980         2814 :       else if (cookie->out.begin ())
   23981              :         {
   23982              :           /* So crashes finger-point the module decl.  */
   23983         2814 :           iloc_sentinel ils = state->loc;
   23984         2814 :           if (state->write_begin (&cookie->out, reader, cookie->config,
   23985         2814 :                                   cookie->crc))
   23986         2785 :             cookie->began = true;
   23987         2814 :         }
   23988              : 
   23989         2932 :       dump.pop (n);
   23990         2932 :       timevar_stop (TV_MODULE_EXPORT);
   23991              : 
   23992         2932 :       ggc_collect ();
   23993              :     }
   23994              : 
   23995        99773 :   if (modules)
   23996              :     {
   23997         4789 :       unsigned n = dump.push (NULL);
   23998         5375 :       dump () && dump ("Imported %u modules", modules->length () - 1);
   23999         5375 :       dump () && dump ("Containing %u clusters", available_clusters);
   24000         4789 :       dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
   24001          586 :                        (loaded_clusters * 100 + available_clusters / 2) /
   24002          586 :                        (available_clusters + !available_clusters));
   24003         4789 :       dump.pop (n);
   24004              :     }
   24005              : 
   24006        99773 :   return cookie;
   24007              : }
   24008              : 
   24009              : // Do the final emission of a module.  At this point we know whether
   24010              : // the module static initializer is a NOP or not.
   24011              : 
   24012              : static void
   24013         2932 : late_finish_module (cpp_reader *reader,  module_processing_cookie *cookie,
   24014              :                     bool init_fn_non_empty)
   24015              : {
   24016         2932 :   timevar_start (TV_MODULE_EXPORT);
   24017              : 
   24018         2932 :   module_state *state = this_module ();
   24019         2932 :   unsigned n = dump.push (state);
   24020         2932 :   state->announce ("finishing");
   24021              : 
   24022         2932 :   cookie->config.active_init = init_fn_non_empty;
   24023         2932 :   if (cookie->began)
   24024         2785 :     state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
   24025              : 
   24026         2932 :   if (cookie->out.end () && cookie->cmi_name)
   24027              :     {
   24028              :       /* Some OS's do not replace NEWNAME if it already exists.
   24029              :          This'll have a race condition in erroneous concurrent
   24030              :          builds.  */
   24031         2820 :       unlink (cookie->cmi_name);
   24032         2820 :       if (rename (cookie->tmp_name, cookie->cmi_name))
   24033              :         {
   24034            0 :           dump () && dump ("Rename ('%s','%s') errno=%u",
   24035            0 :                            cookie->tmp_name, cookie->cmi_name, errno);
   24036            0 :           cookie->out.set_error (errno);
   24037              :         }
   24038              :     }
   24039              : 
   24040         2932 :   if (cookie->out.get_error () && cookie->began)
   24041              :     {
   24042            0 :       error_at (state->loc, "failed to write compiled module: %s",
   24043            0 :                 cookie->out.get_error (state->filename));
   24044            0 :       state->note_cmi_name ();
   24045              :     }
   24046              : 
   24047         2932 :   if (!errorcount)
   24048              :     {
   24049         2779 :       auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   24050         2779 :       mapper->ModuleCompiled (state->get_flatname ());
   24051              :     }
   24052          153 :   else if (cookie->cmi_name)
   24053              :     {
   24054              :       /* We failed, attempt to erase all evidence we even tried.  */
   24055          153 :       unlink (cookie->tmp_name);
   24056          153 :       unlink (cookie->cmi_name);
   24057              :     }
   24058              : 
   24059         2932 :   delete cookie;
   24060         2932 :   dump.pop (n);
   24061         2932 :   timevar_stop (TV_MODULE_EXPORT);
   24062         2932 : }
   24063              : 
   24064              : void
   24065        99773 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
   24066              : {
   24067        99773 :   if (cookie)
   24068         2932 :     late_finish_module (reader,
   24069              :                         static_cast<module_processing_cookie *> (cookie),
   24070              :                         has_inits);
   24071              : 
   24072              :   /* We're done with the macro tables now.  */
   24073        99773 :   vec_free (macro_exports);
   24074        99773 :   vec_free (macro_imports);
   24075        99773 :   headers = NULL;
   24076              : 
   24077              :   /* We're now done with everything but the module names.  */
   24078        99773 :   set_cmi_repo (NULL);
   24079        99773 :   if (mapper)
   24080              :     {
   24081         4789 :       timevar_start (TV_MODULE_MAPPER);
   24082         4789 :       module_client::close_module_client (0, mapper);
   24083         4789 :       mapper = nullptr;
   24084         4789 :       timevar_stop (TV_MODULE_MAPPER);
   24085              :     }
   24086        99773 :   module_state_config::release ();
   24087              : 
   24088              : #if CHECKING_P
   24089        99773 :   note_defs = NULL;
   24090              : #endif
   24091              : 
   24092        99773 :   if (modules)
   24093         7765 :     for (unsigned ix = modules->length (); --ix;)
   24094         2976 :       if (module_state *state = (*modules)[ix])
   24095         2976 :         state->release ();
   24096              : 
   24097              :   /* No need to lookup modules anymore.  */
   24098        99773 :   modules_hash = NULL;
   24099              : 
   24100              :   /* Or entity array.  We still need the entity map to find import numbers.  */
   24101        99773 :   vec_free (entity_ary);
   24102        99773 :   entity_ary = NULL;
   24103              : 
   24104              :   /* Or remember any pending entities.  */
   24105       104562 :   delete pending_table;
   24106        99773 :   pending_table = NULL;
   24107              : 
   24108              :   /* Or any keys -- Let it go!  */
   24109       102269 :   delete keyed_table;
   24110        99773 :   keyed_table = NULL;
   24111              : 
   24112              :   /* Allow a GC, we've possibly made much data unreachable.  */
   24113        99773 :   ggc_collect ();
   24114        99773 : }
   24115              : 
   24116              : /* If CODE is a module option, handle it & return true.  Otherwise
   24117              :    return false.  For unknown reasons I cannot get the option
   24118              :    generation machinery to set fmodule-mapper or -fmodule-header to
   24119              :    make a string type option variable.  */
   24120              : 
   24121              : bool
   24122      1978857 : handle_module_option (unsigned code, const char *str, int)
   24123              : {
   24124      1978857 :   auto hdr = CMS_header;
   24125              : 
   24126      1978857 :   switch (opt_code (code))
   24127              :     {
   24128           48 :     case OPT_fmodule_mapper_:
   24129           48 :       module_mapper_name = str;
   24130           48 :       return true;
   24131              : 
   24132           12 :     case OPT_fmodule_header_:
   24133           12 :       {
   24134           12 :         if (!strcmp (str, "user"))
   24135              :           hdr = CMS_user;
   24136           12 :         else if (!strcmp (str, "system"))
   24137              :           hdr = CMS_system;
   24138              :         else
   24139            0 :           error ("unknown header kind %qs", str);
   24140              :       }
   24141              :       /* Fallthrough.  */
   24142              : 
   24143          936 :     case OPT_fmodule_header:
   24144          936 :       flag_header_unit = hdr;
   24145          936 :       flag_modules = 1;
   24146          936 :       return true;
   24147              : 
   24148            1 :     case OPT_flang_info_include_translate_:
   24149            1 :       vec_safe_push (note_includes, str);
   24150            1 :       return true;
   24151              : 
   24152            6 :     case OPT_flang_info_module_cmi_:
   24153            6 :       vec_safe_push (note_cmis, str);
   24154            6 :       return true;
   24155              : 
   24156              :     default:
   24157              :       return false;
   24158              :     }
   24159              : }
   24160              : 
   24161              : /* Set preprocessor callbacks and options for modules.  */
   24162              : 
   24163              : void
   24164       101591 : module_preprocess_options (cpp_reader *reader)
   24165              : {
   24166       101591 :   gcc_checking_assert (!lang_hooks.preprocess_undef);
   24167       101591 :   if (modules_p ())
   24168              :     {
   24169         4973 :       auto *cb = cpp_get_callbacks (reader);
   24170              : 
   24171         4973 :       cb->translate_include = maybe_translate_include;
   24172         4973 :       cb->user_deferred_macro = module_state::deferred_macro;
   24173         4973 :       if (flag_header_unit)
   24174              :         {
   24175              :           /* If the preprocessor hook is already in use, that
   24176              :              implementation will call the undef langhook.  */
   24177          933 :           if (cb->undef)
   24178            0 :             lang_hooks.preprocess_undef = module_state::undef_macro;
   24179              :           else
   24180          933 :             cb->undef = module_state::undef_macro;
   24181              :         }
   24182         4973 :       auto *opt = cpp_get_options (reader);
   24183         4973 :       opt->module_directives = true;
   24184         4973 :       if (flag_no_output)
   24185           18 :         opt->directives_only = true;
   24186         4973 :       if (opt->main_search == CMS_none)
   24187         4967 :         opt->main_search = cpp_main_search (flag_header_unit);
   24188              :     }
   24189       101591 : }
   24190              : 
   24191              : #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.