LCOV - code coverage report
Current view: top level - gcc/cp - module.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.3 % 11155 10521
Test Date: 2026-08-01 15:33:25 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       292450 : static inline cpp_hashnode *cpp_node (tree id)
     285              : {
     286       292450 :   return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
     287              : }
     288              : 
     289       156946 : 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       156946 : #pragma GCC diagnostic push
     296       156946 : #pragma GCC diagnostic ignored "-Warray-bounds"
     297       156946 :   return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
     298       156946 : #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         4193 : version2string (unsigned version, verstr_t &out)
     315              : {
     316         4193 :   unsigned major = MODULE_MAJOR (version);
     317         4193 :   unsigned minor = MODULE_MINOR (version);
     318              : 
     319         4193 :   if (IS_EXPERIMENTAL (version))
     320         4193 :     sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
     321         4193 :              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         4193 : }
     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         2919 :   class allocator {
     364              :   public:
     365              :     /* Tools tend to moan if the dtor's not virtual.  */
     366       105153 :     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       904275 :   data ()
     388       904275 :     :buffer (NULL), size (0), pos (0)
     389              :   {
     390              :   }
     391       918850 :   ~data ()
     392              :   {
     393              :     /* Make sure the derived and/or using class know what they're
     394              :        doing.  */
     395       918850 :     gcc_checking_assert (!buffer);
     396       918850 :   }
     397              : 
     398              : protected:
     399    665646879 :   char *use (unsigned count)
     400              :   {
     401    665646879 :     if (size < pos + count)
     402              :       return NULL;
     403    665646879 :     char *res = &buffer[pos];
     404    665646879 :     pos += count;
     405    371669568 :     return res;
     406              :   }
     407              : 
     408              :   unsigned calc_crc (unsigned) const;
     409              : 
     410              : public:
     411     51498366 :   void unuse (unsigned count)
     412              :   {
     413     51498366 :     pos -= count;
     414        28394 :   }
     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       827251 : data::allocator::grow (data &obj, unsigned needed, bool exact)
     428              : {
     429       827251 :   gcc_checking_assert (needed ? needed > obj.size : !obj.size);
     430       827251 :   if (!needed)
     431              :     /* Pick a default size.  */
     432       343594 :     needed = EXPERIMENT (100, 1000);
     433              : 
     434       827251 :   if (!exact)
     435       818848 :     needed *= 2;
     436       827251 :   obj.buffer = grow (obj.buffer, needed);
     437       827251 :   if (obj.buffer)
     438       827251 :     obj.size = needed;
     439              :   else
     440            0 :     obj.pos = obj.size = 0;
     441       827251 : }
     442              : 
     443              : /* Free a buffer.  */
     444              : 
     445              : void
     446       358189 : data::allocator::shrink (data &obj)
     447              : {
     448            0 :   shrink (obj.buffer);
     449       358189 :   obj.buffer = NULL;
     450       358189 :   obj.size = 0;
     451            0 : }
     452              : 
     453              : char *
     454        11790 : data::allocator::grow (char *ptr, unsigned needed)
     455              : {
     456        11790 :   return XRESIZEVAR (char, ptr, needed);
     457              : }
     458              : 
     459              : void
     460        14595 : data::allocator::shrink (char *ptr)
     461              : {
     462        14595 :   XDELETEVEC (ptr);
     463        14595 : }
     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       569149 : data::calc_crc (unsigned l) const
     470              : {
     471       569149 :   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       235861 :   bytes_in ()
     487       235861 :     : parent (), overrun (false)
     488              :   {
     489              :   }
     490       238746 :   ~bytes_in ()
     491              :   {
     492        16060 :   }
     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      1793263 :   bool more_p () const
     504              :   {
     505      1793263 :     return pos != size;
     506              :   }
     507              : 
     508              : public:
     509              :   /* Start reading at OFFSET.  */
     510          807 :   void random_access (unsigned offset)
     511              :   {
     512          807 :     if (offset > size)
     513            0 :       set_overrun ();
     514          807 :     pos = offset;
     515              :   }
     516              : 
     517              : public:
     518      1633378 :   void align (unsigned boundary)
     519              :   {
     520      1633378 :     if (unsigned pad = pos & (boundary - 1))
     521      3163394 :       read (boundary - pad);
     522              :   }
     523              : 
     524              : public:
     525    293977311 :   const char *read (unsigned count)
     526              :   {
     527      1530016 :     char *ptr = use (count);
     528    293977311 :     if (!ptr)
     529            0 :       set_overrun ();
     530    238829920 :     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       237000 :   unsigned get_crc () const
     537              :   {
     538       237000 :     return *(const unsigned *)&buffer[0];
     539              :   }
     540              : 
     541              : public:
     542              :   /* Manipulate the overrun flag.  */
     543    171276542 :   bool get_overrun () const
     544              :   {
     545    171276542 :     return overrun;
     546              :   }
     547           26 :   void set_overrun ()
     548              :   {
     549           26 :     overrun = true;
     550            0 :   }
     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       233929 : bytes_in::check_crc () const
     576              : {
     577       233929 :   if (size < 4)
     578              :     return false;
     579              : 
     580       233929 :   unsigned c_crc = calc_crc (size);
     581       233929 :   if (c_crc != get_crc ())
     582              :     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       662446 :   bytes_out (allocator *memory)
     600       662446 :     : parent (), memory (memory)
     601              :   {
     602              :   }
     603       662446 :   ~bytes_out ()
     604              :   {
     605       692716 :   }
     606              : 
     607              : public:
     608    945444284 :   bool streaming_p () const
     609              :   {
     610    945444284 :     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      2240169 :   void align (unsigned boundary)
     624              :   {
     625      2240169 :     if (unsigned pad = pos & (boundary - 1))
     626      2096079 :       write (boundary - pad);
     627      2240169 :   }
     628              : 
     629              : public:
     630    371669568 :   char *write (unsigned count, bool exact = false)
     631              :   {
     632    371669568 :     if (size < pos + count)
     633       469066 :       memory->grow (*this, pos + count, exact);
     634    371669568 :     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        19738 :   void str (const char *ptr)
     649              :   {
     650        19738 :     str (ptr, strlen (ptr));
     651        19738 :   }
     652       301014 :   void cpp_node (const cpp_hashnode *node)
     653              :   {
     654       301014 :     str ((const char *)NODE_NAME (node), NODE_LEN (node));
     655        13064 :   }
     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     51442111 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
     683              : {
     684     51442111 :   gcc_assert (bit_pos);
     685     51442111 :   unsigned bytes = (bit_pos + 7) / 8;
     686     51442111 :   bits.unuse (4 - bytes);
     687     51442111 :   bit_pos = 0;
     688     51442111 :   bit_val = 0;
     689     51442111 :   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     16675597 :   bits_in (bytes_in& in)
     712     16675597 :     : in (in)
     713              :   { }
     714              : 
     715     16675597 :   ~bits_in ()
     716              :   {
     717     15661767 :     bflush ();
     718     16675597 :   }
     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     34585445 :   void bflush ()
     726              :   {
     727     34585445 :     if (bit_pos)
     728     18923678 :       bit_flush (in, bit_val, bit_pos);
     729     34585445 :   }
     730              : 
     731              :   /* Read one bit.  */
     732    571710114 :   bool b ()
     733              :   {
     734    571710114 :     if (!bit_pos)
     735     25018927 :       bit_val = in.u32 ();
     736    571710114 :     bool x = (bit_val >> bit_pos) & 1;
     737    571710114 :     bit_pos = (bit_pos + 1) % 32;
     738    571710114 :     return x;
     739              :   }
     740              : };
     741              : 
     742              : /* Factory function for bits_in.  */
     743              : 
     744              : bytes_in::bits_in
     745     16675597 : bytes_in::stream_bits ()
     746              : {
     747     16675597 :   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     21587161 :   bits_out (bytes_out& out)
     759     21587161 :     : out (out)
     760              :   { }
     761              : 
     762     21587161 :   ~bits_out ()
     763              :   {
     764        78430 :     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     44692671 :   void bflush ()
     773              :   {
     774     44692671 :     if (bit_pos)
     775              :       {
     776     24461981 :         out.u32 (bit_val);
     777     24461981 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     778              :       }
     779     44692671 :     out.spans[2]++;
     780     44692671 :     is_set = -1;
     781     44692671 :   }
     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    739604819 :   void b (bool x)
     788              :   {
     789    739604819 :     if (is_set != x)
     790              :       {
     791     77215280 :         is_set = x;
     792     77215280 :         out.spans[x]++;
     793              :       }
     794    739604819 :     out.lengths[x]++;
     795    739604819 :     bit_val |= unsigned (x) << bit_pos++;
     796    739604819 :     if (bit_pos == 32)
     797              :       {
     798      8056452 :         out.u32 (bit_val);
     799      8056452 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     800              :       }
     801    739604819 :   }
     802              : };
     803              : 
     804              : /* Factory function for bits_out.  */
     805              : 
     806              : bytes_out::bits_out
     807     21587161 : bytes_out::stream_bits ()
     808              : {
     809     21587161 :   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       337992 : bytes_out::set_crc (unsigned *crc_ptr)
     821              : {
     822       337992 :   if (crc_ptr)
     823              :     {
     824       335220 :       gcc_checking_assert (pos >= 4);
     825              : 
     826       335220 :       unsigned crc = calc_crc (pos);
     827       335220 :       unsigned accum = *crc_ptr;
     828              :       /* Only mix the existing *CRC_PTR if it is non-zero.  */
     829       335220 :       accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
     830       335220 :       *crc_ptr = accum;
     831              : 
     832              :       /* Buffer will be sufficiently aligned.  */
     833       335220 :       *(unsigned *)buffer = crc;
     834              :     }
     835       337992 : }
     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     32527429 : bytes_out::u32 (unsigned val)
     844              : {
     845     32527429 :   if (char *ptr = write (4))
     846              :     {
     847     32527429 :       ptr[0] = val;
     848     32527429 :       ptr[1] = val >> 8;
     849     32527429 :       ptr[2] = val >> 16;
     850     32527429 :       ptr[3] = val >> 24;
     851              :     }
     852     32527429 : }
     853              : 
     854              : unsigned
     855     25028578 : bytes_in::u32 ()
     856              : {
     857     25028578 :   unsigned val = 0;
     858     25028578 :   if (const char *ptr = read (4))
     859              :     {
     860     25028578 :       val |= (unsigned char)ptr[0];
     861     25028578 :       val |= (unsigned char)ptr[1] << 8;
     862     25028578 :       val |= (unsigned char)ptr[2] << 16;
     863     25028578 :       val |= (unsigned char)ptr[3] << 24;
     864              :     }
     865              : 
     866     25028578 :   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    131119590 : bytes_out::i (int v)
     892              : {
     893    131119590 :   if (char *ptr = write (1))
     894              :     {
     895    131119590 :       if (v <= 0x3f && v >= -0x40)
     896    102364207 :         *ptr = v & 0x7f;
     897              :       else
     898              :         {
     899     28755383 :           unsigned bytes = 0;
     900     28755383 :           int probe;
     901     28755383 :           if (v >= 0)
     902            0 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     903            0 :               bytes++;
     904              :           else
     905     43478326 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     906     14722943 :               bytes++;
     907     28755383 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     908     28755383 :           if ((ptr = write (++bytes)))
     909     72233709 :             for (; bytes--; v >>= 8)
     910     43478326 :               ptr[bytes] = v & 0xff;
     911              :         }
     912              :     }
     913    131119590 : }
     914              : 
     915              : int
     916    102296761 : bytes_in::i ()
     917              : {
     918    102296761 :   int v = 0;
     919    102296761 :   if (const char *ptr = read (1))
     920              :     {
     921    102296761 :       v = *ptr & 0xff;
     922    102296761 :       if (v & 0x80)
     923              :         {
     924     23346635 :           unsigned bytes = (v >> 4) & 0x7;
     925     23346635 :           v &= 0xf;
     926     23346635 :           if (v & 0x8)
     927     23346635 :             v |= -1 ^ 0x7;
     928              :           /* unsigned necessary due to left shifts of -ve values.  */
     929     23346635 :           unsigned uv = unsigned (v);
     930     23346635 :           if ((ptr = read (++bytes)))
     931     60339429 :             while (bytes--)
     932     36992794 :               uv = (uv << 8) | (*ptr++ & 0xff);
     933     23346635 :           v = int (uv);
     934              :         }
     935     78950126 :       else if (v & 0x40)
     936      9983688 :         v |= -1 ^ 0x3f;
     937              :     }
     938              : 
     939    102296761 :   return v;
     940              : }
     941              : 
     942              : void
     943    112030508 : bytes_out::u (unsigned v)
     944              : {
     945    112030508 :   if (char *ptr = write (1))
     946              :     {
     947    112030508 :       if (v <= 0x7f)
     948     97382311 :         *ptr = v;
     949              :       else
     950              :         {
     951     14648197 :           unsigned bytes = 0;
     952     14648197 :           unsigned probe;
     953     17444458 :           for (probe = v >> 8; probe > 0xf; probe >>= 8)
     954      2796261 :             bytes++;
     955     14648197 :           *ptr = 0x80 | bytes << 4 | probe;
     956     14648197 :           if ((ptr = write (++bytes)))
     957     32092655 :             for (; bytes--; v >>= 8)
     958     17444458 :               ptr[bytes] = v & 0xff;
     959              :         }
     960              :     }
     961    112030508 : }
     962              : 
     963              : unsigned
     964     89426820 : bytes_in::u ()
     965              : {
     966     89426820 :   unsigned v = 0;
     967              : 
     968     89426820 :   if (const char *ptr = read (1))
     969              :     {
     970     89426820 :       v = *ptr & 0xff;
     971     89426820 :       if (v & 0x80)
     972              :         {
     973     11789334 :           unsigned bytes = (v >> 4) & 0x7;
     974     11789334 :           v &= 0xf;
     975     11789334 :           if ((ptr = read (++bytes)))
     976     25895998 :             while (bytes--)
     977     14106664 :               v = (v << 8) | (*ptr++ & 0xff);
     978              :         }
     979              :     }
     980              : 
     981     89426820 :   return v;
     982              : }
     983              : 
     984              : void
     985     26723832 : bytes_out::wi (HOST_WIDE_INT v)
     986              : {
     987     26723832 :   if (char *ptr = write (1))
     988              :     {
     989     26723832 :       if (v <= 0x3f && v >= -0x40)
     990      5251706 :         *ptr = v & 0x7f;
     991              :       else
     992              :         {
     993     21472126 :           unsigned bytes = 0;
     994     21472126 :           HOST_WIDE_INT probe;
     995     21472126 :           if (v >= 0)
     996     78495188 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     997     57026274 :               bytes++;
     998              :           else
     999        10234 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
    1000         7022 :               bytes++;
    1001     21472126 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
    1002     21472126 :           if ((ptr = write (++bytes)))
    1003     99977548 :             for (; bytes--; v >>= 8)
    1004     78505422 :               ptr[bytes] = v & 0xff;
    1005              :         }
    1006              :     }
    1007     26723832 : }
    1008              : 
    1009              : HOST_WIDE_INT
    1010     20444383 : bytes_in::wi ()
    1011              : {
    1012     20444383 :   HOST_WIDE_INT v = 0;
    1013     20444383 :   if (const char *ptr = read (1))
    1014              :     {
    1015     20444383 :       v = *ptr & 0xff;
    1016     20444383 :       if (v & 0x80)
    1017              :         {
    1018     18481406 :           unsigned bytes = (v >> 4) & 0x7;
    1019     18481406 :           v &= 0xf;
    1020     18481406 :           if (v & 0x8)
    1021         2122 :             v |= -1 ^ 0x7;
    1022              :           /* unsigned necessary due to left shifts of -ve values.  */
    1023     18481406 :           unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
    1024     18481406 :           if ((ptr = read (++bytes)))
    1025     85417198 :             while (bytes--)
    1026     66935792 :               uv = (uv << 8) | (*ptr++ & 0xff);
    1027     18481406 :           v = (HOST_WIDE_INT) uv;
    1028              :         }
    1029      1962977 :       else if (v & 0x40)
    1030         8988 :         v |= -1 ^ 0x3f;
    1031              :     }
    1032              : 
    1033     20444383 :   return v;
    1034              : }
    1035              : 
    1036              : /* unsigned wide ints are just written as signed wide ints.  */
    1037              : 
    1038              : inline void
    1039     26722964 : bytes_out::wu (unsigned HOST_WIDE_INT v)
    1040              : {
    1041     26722964 :   wi ((HOST_WIDE_INT) v);
    1042              : }
    1043              : 
    1044              : inline unsigned HOST_WIDE_INT
    1045     20443809 : bytes_in::wu ()
    1046              : {
    1047     40279771 :   return (unsigned HOST_WIDE_INT) wi ();
    1048              : }
    1049              : 
    1050              : /* size_t written as unsigned or unsigned wide int.  */
    1051              : 
    1052              : inline void
    1053      2198000 : bytes_out::z (size_t s)
    1054              : {
    1055      2198000 :   if (sizeof (s) == sizeof (unsigned))
    1056              :     u (s);
    1057              :   else
    1058      4362392 :     wu (s);
    1059           12 : }
    1060              : 
    1061              : inline size_t
    1062      1616262 : bytes_in::z ()
    1063              : {
    1064      1616262 :   if (sizeof (size_t) == sizeof (unsigned))
    1065              :     return u ();
    1066              :   else
    1067      3232524 :     return wu ();
    1068              : }
    1069              : 
    1070              : /* location_t written as 32- or 64-bit as needed.  */
    1071              : 
    1072     23697081 : inline void bytes_out::loc (location_t l)
    1073              : {
    1074     23697081 :   if (sizeof (location_t) > sizeof (unsigned))
    1075     44827610 :     wu (l);
    1076              :   else
    1077              :     u (l);
    1078      2563780 : }
    1079              : 
    1080     18222728 : inline location_t bytes_in::loc ()
    1081              : {
    1082     18222728 :   if (sizeof (location_t) > sizeof (unsigned))
    1083     36442398 :     return wu ();
    1084              :   else
    1085              :     return u ();
    1086              : }
    1087              : 
    1088              : /* Buffer simply memcpied.  */
    1089              : void *
    1090      2240169 : bytes_out::buf (size_t len)
    1091              : {
    1092      2240169 :   align (sizeof (void *) * 2);
    1093      2240169 :   return write (len);
    1094              : }
    1095              : 
    1096              : void
    1097      2187710 : bytes_out::buf (const void *src, size_t len)
    1098              : {
    1099      2187710 :   if (void *ptr = buf (len))
    1100      2187710 :     memcpy (ptr, src, len);
    1101      2187710 : }
    1102              : 
    1103              : const void *
    1104      1633378 : bytes_in::buf (size_t len)
    1105              : {
    1106      1633378 :   align (sizeof (void *) * 2);
    1107      1633378 :   const char *ptr = read (len);
    1108              : 
    1109      1633378 :   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      2197982 : bytes_out::str (const char *string, size_t len)
    1117              : {
    1118      2164386 :   z (len);
    1119      2164386 :   if (len)
    1120              :     {
    1121      2164386 :       gcc_checking_assert (!string[len]);
    1122      2164386 :       buf (string, len + 1);
    1123              :     }
    1124        33596 : }
    1125              : 
    1126              : const char *
    1127      1616256 : bytes_in::str (size_t *len_p)
    1128              : {
    1129      1616256 :   size_t len = z ();
    1130              : 
    1131              :   /* We're about to trust some user data.  */
    1132      1616256 :   if (overrun)
    1133            0 :     len = 0;
    1134      1616256 :   if (len_p)
    1135      1612282 :     *len_p = len;
    1136      1616256 :   const char *str = NULL;
    1137      1616256 :   if (len)
    1138              :     {
    1139      1615983 :       str = reinterpret_cast<const char *> (buf (len + 1));
    1140      1615983 :       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       292723 : bytes_in::cpp_node ()
    1151              : {
    1152       292723 :   size_t len;
    1153       292723 :   const char *s = str (&len);
    1154       292723 :   if (!len)
    1155              :     return NULL;
    1156       292450 :   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        28394 : bytes_out::printf (const char *format, ...)
    1164              : {
    1165        28394 :   va_list args;
    1166              :   /* Exercise buffer expansion.  */
    1167        28394 :   size_t len = EXPERIMENT (10, 500);
    1168              : 
    1169        56255 :   while (char *ptr = write (len))
    1170              :     {
    1171        56255 :       va_start (args, format);
    1172        56255 :       size_t actual = vsnprintf (ptr, len, format, args) + 1;
    1173        56255 :       va_end (args);
    1174        56255 :       if (actual <= len)
    1175              :         {
    1176        28394 :           unuse (len - actual);
    1177        28394 :           break;
    1178              :         }
    1179        27861 :       unuse (len);
    1180        27861 :       len = actual;
    1181        27861 :     }
    1182        28394 : }
    1183              : 
    1184              : void
    1185         5544 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
    1186              : {
    1187         5544 :   printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
    1188         5544 :           kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
    1189         5544 :           time->tm_hour, time->tm_min, time->tm_sec, tz);
    1190         5544 : }
    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         5968 :   elf (int fd, int e)
    1298        11936 :     :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
    1299              :   {}
    1300         5886 :   ~elf ()
    1301              :   {
    1302         5886 :     gcc_checking_assert (fd < 0 && !hdr.buffer
    1303              :                          && !sectab.buffer && !strtab.buffer);
    1304         5886 :   }
    1305              : 
    1306              : public:
    1307              :   /* Return the error, if we have an error.  */
    1308       457245 :   int get_error () const
    1309              :   {
    1310       457245 :     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            0 :   }
    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         5850 :   bool begin () const
    1324              :   {
    1325         5850 :     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         8328 : elf::end ()
    1358              : {
    1359              :   /* Close the stream and free the section table.  */
    1360         8328 :   if (fd >= 0 && close (fd))
    1361            0 :     set_error (errno);
    1362         8328 :   fd = -1;
    1363              : 
    1364         8328 :   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         3049 :   elf_in (int fd, int e)
    1381         6098 :     :parent (fd, e)
    1382              :   {
    1383              :   }
    1384         2967 :   ~elf_in ()
    1385              :   {
    1386         2967 :   }
    1387              : 
    1388              : public:
    1389       215286 :   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         1078 :   static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
    1416              :   {
    1417              : #if MAPPED_READING
    1418         1078 :     if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
    1419         1078 :           && bytes.buffer < self->hdr.buffer + self->hdr.pos))
    1420              : #endif
    1421            0 :       data::simple_memory.shrink (bytes.buffer);
    1422         1078 :     bytes.buffer = NULL;
    1423         1078 :     bytes.size = 0;
    1424         1078 :   }
    1425              : 
    1426              : public:
    1427       239979 :   static void grow (data &data, unsigned needed)
    1428              :   {
    1429       239979 :     gcc_checking_assert (!data.buffer);
    1430              : #if !MAPPED_READING
    1431              :     data.buffer = XNEWVEC (char, needed);
    1432              : #endif
    1433       239979 :     data.size = needed;
    1434       239979 :   }
    1435       246630 :   static void shrink (data &data)
    1436              :   {
    1437              : #if !MAPPED_READING
    1438              :     XDELETEVEC (data.buffer);
    1439              : #endif
    1440       246630 :     data.buffer = NULL;
    1441       246630 :     data.size = 0;
    1442            0 :   }
    1443              : 
    1444              : public:
    1445       236954 :   const section *get_section (unsigned s) const
    1446              :   {
    1447       236954 :     if (s * sizeof (section) < sectab.size)
    1448       236954 :       return reinterpret_cast<const section *>
    1449       236954 :         (&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       236954 :   bool read (data *d, const section *s)
    1464              :   {
    1465       236954 :     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         8409 :   void release ()
    1476              :   {
    1477         8409 :     shrink (strtab);
    1478           39 :   }
    1479              : 
    1480              : public:
    1481              :   bool begin (location_t);
    1482         5409 :   bool end ()
    1483              :   {
    1484         5409 :     release ();
    1485              : #if MAPPED_READING
    1486         5409 :     if (hdr.buffer)
    1487         2967 :       munmap (hdr.buffer, hdr.pos);
    1488         5409 :     hdr.buffer = NULL;
    1489              : #endif
    1490         5409 :     shrink (sectab);
    1491              : 
    1492         5409 :     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       346504 :   const char *name (unsigned offset)
    1499              :   {
    1500       693008 :     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         2919 :   elf_out (int fd, int e)
    1523         5726 :     :parent (fd, e), identtab (500), pos (0), began (false)
    1524              :   {
    1525              : #if MAPPED_WRITING
    1526         2919 :     offset = extent = 0;
    1527         2919 :     page_size = sysconf (_SC_PAGE_SIZE);
    1528         2919 :     if (page_size < SECTION_ALIGN)
    1529              :       /* Something really strange.  */
    1530            0 :       set_error (EINVAL);
    1531              : #endif
    1532         2919 :   }
    1533         2919 :   ~elf_out ()
    1534         2919 :   {
    1535         2919 :     data::simple_memory.shrink (hdr);
    1536         2919 :     data::simple_memory.shrink (sectab);
    1537         2919 :     data::simple_memory.shrink (strtab);
    1538         2919 :   }
    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         5844 :   unsigned get_section_limit () const
    1556              :   {
    1557         5844 :     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        21641 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
    1595              : {
    1596        21641 :   unsigned snum = source->find (name);
    1597              : 
    1598        21641 :   return begin (loc, source, snum, name);
    1599              : }
    1600              : 
    1601              : /* Begin reading section numbered SNUM with NAME (may be NULL).  */
    1602              : 
    1603              : bool
    1604       233929 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
    1605              : {
    1606       233929 :   if (!source->read (this, source->find (snum))
    1607       233929 :       || !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            0 :       return false;
    1616              :     }
    1617       233929 :   pos = 4;
    1618       233929 :   return true;
    1619              : }
    1620              : 
    1621              : /* Finish reading a section.  */
    1622              : 
    1623              : bool
    1624       232812 : bytes_in::end (elf_in *src)
    1625              : {
    1626       232812 :   if (more_p ())
    1627           13 :     set_overrun ();
    1628       232812 :   if (overrun)
    1629           13 :     src->set_error ();
    1630              : 
    1631       232812 :   src->shrink (*this);
    1632              : 
    1633       232812 :   return !overrun;
    1634              : }
    1635              : 
    1636              : /* Begin writing buffer.  */
    1637              : 
    1638              : void
    1639       337992 : bytes_out::begin (bool need_crc)
    1640              : {
    1641            0 :   if (need_crc)
    1642            0 :     pos = 4;
    1643            0 :   memory->grow (*this, 0, false);
    1644       316096 : }
    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       337992 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    1652              : {
    1653       337992 :   lengths[3] += pos;
    1654       337992 :   spans[3]++;
    1655              : 
    1656       337992 :   set_crc (crc_ptr);
    1657       337992 :   unsigned sec_num = sink->add (*this, !crc_ptr, name);
    1658       337992 :   memory->shrink (*this);
    1659              : 
    1660       337992 :   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       239979 : elf_in::read (data *data, unsigned pos, unsigned length)
    1726              : {
    1727              : #if MAPPED_READING
    1728       239979 :   if (pos + length > hdr.pos)
    1729              :     {
    1730            0 :       set_error (EINVAL);
    1731            0 :       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       239979 :   grow (*data, length);
    1741              : #if MAPPED_READING
    1742       239979 :   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       239979 :   return data->buffer;
    1753              : }
    1754              : 
    1755              : /* Read section SNUM of TYPE.  Return section pointer or NULL on error.  */
    1756              : 
    1757              : const elf::section *
    1758       236954 : elf_in::find (unsigned snum, unsigned type)
    1759              : {
    1760       236954 :   const section *sec = get_section (snum);
    1761       236954 :   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        21686 : elf_in::find (const char *sname)
    1771              : {
    1772       138885 :   for (unsigned pos = sectab.size; pos -= sizeof (section); )
    1773              :     {
    1774       138885 :       const section *sec
    1775       138885 :         = reinterpret_cast<const section *> (&sectab.buffer[pos]);
    1776              : 
    1777       277770 :       if (0 == strcmp (sname, name (sec->name)))
    1778        21686 :         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         3049 : elf_in::begin (location_t loc)
    1789              : {
    1790         3049 :   if (!parent::begin ())
    1791              :     return false;
    1792              : 
    1793         3025 :   struct stat stat;
    1794         3025 :   unsigned size = 0;
    1795         3025 :   if (!fstat (fd, &stat))
    1796              :     {
    1797              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1798         3025 :       device = stat.st_dev;
    1799         3025 :       inode = stat.st_ino;
    1800              : #endif
    1801              :       /* Never generate files > 4GB, check we've not been given one.  */
    1802         3025 :       if (stat.st_size == unsigned (stat.st_size))
    1803         3025 :         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         3025 :   void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    1810         3025 :   if (mapping == MAP_FAILED)
    1811              :     {
    1812            0 :     fail:
    1813            0 :       set_error (errno);
    1814            0 :       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         3025 :   if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
    1819            0 :     goto fail;
    1820              : 
    1821         3025 :   hdr.buffer = (char *)mapping;
    1822              : #else
    1823              :   read (&hdr, 0, sizeof (header));
    1824              : #endif
    1825         3025 :   hdr.pos = size; /* Record size of the file.  */
    1826              : 
    1827         3025 :   const header *h = reinterpret_cast<const header *> (hdr.buffer);
    1828         3025 :   if (!h)
    1829              :     return false;
    1830              : 
    1831         3025 :   if (h->ident.magic[0] != 0x7f
    1832         3025 :       || h->ident.magic[1] != 'E'
    1833         3025 :       || h->ident.magic[2] != 'L'
    1834         3025 :       || 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         3025 :   if (h->ident.klass != MY_CLASS
    1845         3025 :       || h->ident.data != MY_ENDIAN
    1846         3025 :       || h->ident.version != EV_CURRENT
    1847         3025 :       || h->type != ET_NONE
    1848         3025 :       || h->machine != EM_NONE
    1849         3025 :       || h->ident.osabi != OSABI_NONE)
    1850              :     {
    1851            0 :       error_at (loc, "unexpected encapsulation format or type");
    1852            0 :       goto failed;
    1853              :     }
    1854              : 
    1855         3025 :   int e = -1;
    1856         3025 :   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         3025 :   unsigned strndx = h->shstrndx;
    1865         3025 :   unsigned shnum = h->shnum;
    1866         3025 :   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         3025 :   if (!shnum)
    1881            0 :     goto malformed;
    1882              : 
    1883         3025 :   if (!read (&sectab, h->shoff, shnum * sizeof (section)))
    1884            0 :     goto section_table_fail;
    1885              : 
    1886         3025 :   if (strndx == SHN_XINDEX)
    1887            0 :     strndx = get_section (0)->link;
    1888              : 
    1889         3025 :   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         3025 :   if (!(strtab.size && !strtab.buffer[0]
    1895         3025 :         && !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         3025 :   sectab.pos = h->shoff;
    1901         3025 :   strtab.pos = shnum * sizeof (section);
    1902              : #else
    1903              :   shrink (hdr);
    1904              : #endif
    1905              : 
    1906         3025 :   return true;
    1907              : }
    1908              : 
    1909              : /* Create a new mapping.  */
    1910              : 
    1911              : #if MAPPED_WRITING
    1912              : void
    1913         3662 : 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         7171 :   auto allocate = [](int fd, off_t offset, off_t length)
    1918              :     {
    1919              : #ifdef HAVE_POSIX_FALLOCATE
    1920         3509 :       int result = posix_fallocate (fd, offset, length);
    1921         3509 :       if (result != EINVAL && result != ENOTSUP && result != EOPNOTSUPP)
    1922         3509 :         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         3662 :   void *mapping = MAP_FAILED;
    1929         3662 :   if (extending && ext < 1024 * 1024)
    1930              :     {
    1931         3342 :       if (allocate (fd, offset, ext * 2))
    1932         3342 :         mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
    1933         3342 :                         MAP_SHARED, fd, offset);
    1934         3342 :       if (mapping != MAP_FAILED)
    1935              :         ext *= 2;
    1936              :     }
    1937              :   if (mapping == MAP_FAILED)
    1938              :     {
    1939          320 :       if (!extending || allocate (fd, offset, ext))
    1940          320 :         mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
    1941          320 :                         MAP_SHARED, fd, offset);
    1942          320 :       if (mapping == MAP_FAILED)
    1943              :         {
    1944            0 :           set_error (errno);
    1945              :           mapping = NULL;
    1946              :           ext = 0;
    1947              :         }
    1948              :     }
    1949         3662 :   hdr.buffer = (char *)mapping;
    1950         3662 :   extent = ext;
    1951         3662 : }
    1952              : #endif
    1953              : 
    1954              : /* Flush out the current mapping.  */
    1955              : 
    1956              : #if MAPPED_WRITING
    1957              : void
    1958         3662 : elf_out::remove_mapping ()
    1959              : {
    1960         3662 :   if (hdr.buffer)
    1961              :     {
    1962              :       /* MS_ASYNC dtrt with the removed mapping, including a
    1963              :          subsequent overlapping remap.  */
    1964         3662 :       if (msync (hdr.buffer, extent, MS_ASYNC)
    1965         3662 :           || munmap (hdr.buffer, extent))
    1966              :         /* We're somewhat screwed at this point.  */
    1967            0 :         set_error (errno);
    1968              :     }
    1969              : 
    1970         3662 :   hdr.buffer = NULL;
    1971         3662 : }
    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       815461 : elf_out::grow (char *data, unsigned needed)
    1979              : {
    1980       815461 :   if (!data)
    1981              :     {
    1982              :       /* First allocation, check we're aligned.  */
    1983       343594 :       gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    1984              : #if MAPPED_WRITING
    1985       343594 :       data = hdr.buffer + (pos - offset);
    1986              : #endif
    1987              :     }
    1988              : 
    1989              : #if MAPPED_WRITING
    1990       815461 :   unsigned off = data - hdr.buffer;
    1991       815461 :   if (off + needed > extent)
    1992              :     {
    1993              :       /* We need to grow the mapping.  */
    1994          708 :       unsigned lwm = off & ~(page_size - 1);
    1995          708 :       unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
    1996              : 
    1997          708 :       gcc_checking_assert (hwm > extent);
    1998              : 
    1999          708 :       remove_mapping ();
    2000              : 
    2001          708 :       offset += lwm;
    2002          708 :       create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
    2003              : 
    2004          708 :       data = hdr.buffer + (off - lwm);
    2005              :     }
    2006              : #else
    2007              :   data = allocator::grow (data, needed);
    2008              : #endif
    2009              : 
    2010       815461 :   return data;
    2011              : }
    2012              : 
    2013              : #if MAPPED_WRITING
    2014              : /* Shrinking is a NOP.  */
    2015              : void
    2016       343594 : elf_out::shrink (char *)
    2017              : {
    2018       343594 : }
    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      1524504 : elf_out::strtab_write (const char *s, unsigned l)
    2026              : {
    2027      1524504 :   if (strtab.pos + l > strtab.size)
    2028         1311 :     data::simple_memory.grow (strtab, strtab.pos + l, false);
    2029      1524504 :   memcpy (strtab.buffer + strtab.pos, s, l);
    2030      1524504 :   unsigned res = strtab.pos;
    2031      1524504 :   strtab.pos += l;
    2032      1524504 :   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       609170 : elf_out::strtab_write (tree decl, int inner)
    2040              : {
    2041       609170 :   tree ctx = CP_DECL_CONTEXT (decl);
    2042       609170 :   if (TYPE_P (ctx))
    2043         7201 :     ctx = TYPE_NAME (ctx);
    2044       609170 :   if (ctx != global_namespace)
    2045       311344 :     strtab_write (ctx, -1);
    2046              : 
    2047       609170 :   tree name = DECL_NAME (decl);
    2048       609170 :   if (!name)
    2049          339 :     name = DECL_ASSEMBLER_NAME_RAW (decl);
    2050       609170 :   strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
    2051              : 
    2052       609170 :   if (inner)
    2053       439109 :     strtab_write (&"::{}"[inner+1], 2);
    2054       609170 : }
    2055              : 
    2056              : /* Map IDENTIFIER IDENT to strtab offset.  Inserts into strtab if not
    2057              :    already there.  */
    2058              : 
    2059              : unsigned
    2060       160215 : elf_out::name (tree ident)
    2061              : {
    2062       160215 :   unsigned res = 0;
    2063       160215 :   if (ident)
    2064              :     {
    2065       160160 :       bool existed;
    2066       160160 :       int *slot = &identtab.get_or_insert (ident, &existed);
    2067       160160 :       if (!existed)
    2068       285080 :         *slot = strtab_write (IDENTIFIER_POINTER (ident),
    2069       142540 :                               IDENTIFIER_LENGTH (ident) + 1);
    2070       160160 :       res = *slot;
    2071              :     }
    2072       160215 :   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        35859 : elf_out::name (const char *literal)
    2080              : {
    2081        35859 :   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       297826 : elf_out::qualified_name (tree decl, bool is_defn)
    2089              : {
    2090       297826 :   gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
    2091       297826 :   unsigned result = strtab.pos;
    2092              : 
    2093       297826 :   strtab_write (decl, is_defn);
    2094       297826 :   strtab_write ("", 1);
    2095              : 
    2096       297826 :   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       343594 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
    2105              :               unsigned flags)
    2106              : {
    2107       343594 :   gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
    2108       343594 :   if (sectab.pos + sizeof (section) > sectab.size)
    2109         4877 :     data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
    2110       343594 :   section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
    2111       343594 :   memset (sec, 0, sizeof (section));
    2112       343594 :   sec->type = type;
    2113       343594 :   sec->flags = flags;
    2114       343594 :   sec->name = name;
    2115       343594 :   sec->offset = off;
    2116       343594 :   sec->size = size;
    2117       343594 :   if (flags & SHF_STRINGS)
    2118         5573 :     sec->entsize = 1;
    2119              : 
    2120       343594 :   unsigned res = sectab.pos;
    2121       343594 :   sectab.pos += sizeof (section);
    2122       343594 :   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        11204 : elf_out::write (const data &buffer)
    2130              : {
    2131              : #if MAPPED_WRITING
    2132              :   /* HDR is always mapped.  */
    2133        11204 :   if (&buffer != &hdr)
    2134              :     {
    2135         5602 :       bytes_out out (this);
    2136         5602 :       grow (out, buffer.pos, true);
    2137         5602 :       if (out.buffer)
    2138         5602 :         memcpy (out.buffer, buffer.buffer, buffer.pos);
    2139         5602 :       shrink (out);
    2140         5602 :     }
    2141              :   else
    2142              :     /* We should have been aligned during the first allocation.  */
    2143         5602 :     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        11204 :   unsigned res = pos;
    2152        11204 :   pos += buffer.pos;
    2153              : 
    2154        11204 :   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         9545 :       pos += padding;
    2164              :     }
    2165        11204 :   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       337992 : elf_out::write (const bytes_out &buf)
    2173              : {
    2174       337992 :   gcc_checking_assert (buf.memory == this);
    2175              :   /* A directly mapped buffer.  */
    2176       337992 :   gcc_checking_assert (buf.buffer - hdr.buffer >= 0
    2177              :                        && buf.buffer - hdr.buffer + buf.size <= extent);
    2178       337992 :   unsigned res = pos;
    2179       337992 :   pos += buf.pos;
    2180              : 
    2181              :   /* Align up.  We're not going to advance into the next page. */
    2182       337992 :   pos += -pos & (SECTION_ALIGN - 1);
    2183              : 
    2184       337992 :   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       337992 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
    2195              : {
    2196       337992 :   unsigned off = write (data);
    2197              : 
    2198       675984 :   return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
    2199       337992 :               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         2801 : elf_out::begin ()
    2207              : {
    2208         2801 :   if (!parent::begin ())
    2209              :     return false;
    2210              : 
    2211              :   /* Let the allocators pick a default.  */
    2212         2801 :   data::simple_memory.grow (strtab, 0, false);
    2213         2801 :   data::simple_memory.grow (sectab, 0, false);
    2214              : 
    2215              :   /* The string table starts with an empty string.  */
    2216         2801 :   name ("");
    2217              : 
    2218              :   /* Create the UNDEF section.  */
    2219         2801 :   add (SHT_NONE);
    2220              : 
    2221              : #if MAPPED_WRITING
    2222              :   /* Start a mapping.  */
    2223         2801 :   create_mapping (EXPERIMENT (page_size,
    2224              :                               (32767 + page_size) & ~(page_size - 1)));
    2225         2801 :   if (!hdr.buffer)
    2226              :     return false;
    2227              : #endif
    2228              : 
    2229              :   /* Write an empty header.  */
    2230         2801 :   grow (hdr, sizeof (header), true);
    2231         2801 :   header *h = reinterpret_cast<header *> (hdr.buffer);
    2232         2801 :   memset (h, 0, sizeof (header));
    2233         2801 :   hdr.pos = hdr.size;
    2234         2801 :   write (hdr);
    2235         2801 :   if (get_error ())
    2236              :     return false;
    2237         2801 :   began = true;
    2238         2801 :   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         2919 : elf_out::end ()
    2246              : {
    2247         2919 :   if (fd >= 0 && began)
    2248              :     {
    2249              :       /* Write the string table.  */
    2250         2801 :       unsigned strnam = name (".strtab");
    2251         2801 :       unsigned stroff = write (strtab);
    2252         2801 :       unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
    2253              :                              SHF_STRINGS);
    2254              : 
    2255              :       /* Store escape values in section[0].  */
    2256         2801 :       if (strndx >= SHN_LORESERVE)
    2257              :         {
    2258            0 :           reinterpret_cast<section *> (sectab.buffer)->link = strndx;
    2259            0 :           strndx = SHN_XINDEX;
    2260              :         }
    2261         2801 :       unsigned shnum = sectab.pos / sizeof (section);
    2262         2801 :       if (shnum >= SHN_LORESERVE)
    2263              :         {
    2264            0 :           reinterpret_cast<section *> (sectab.buffer)->size = shnum;
    2265            0 :           shnum = SHN_XINDEX;
    2266              :         }
    2267              : 
    2268         2801 :       unsigned shoff = write (sectab);
    2269              : 
    2270              : #if MAPPED_WRITING
    2271         2801 :       if (offset)
    2272              :         {
    2273          153 :           remove_mapping ();
    2274          153 :           offset = 0;
    2275          153 :           create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
    2276              :                           false);
    2277              :         }
    2278         2801 :       unsigned length = pos;
    2279              : #else
    2280              :       if (lseek (fd, 0, SEEK_SET) < 0)
    2281              :         set_error (errno);
    2282              : #endif
    2283              :       /* Write header.  */
    2284         2801 :       if (!get_error ())
    2285              :         {
    2286              :           /* Write the correct header now.  */
    2287         2801 :           header *h = reinterpret_cast<header *> (hdr.buffer);
    2288         2801 :           h->ident.magic[0] = 0x7f;
    2289         2801 :           h->ident.magic[1] = 'E';   /* Elrond */
    2290         2801 :           h->ident.magic[2] = 'L';   /* is an */
    2291         2801 :           h->ident.magic[3] = 'F';   /* elf.  */
    2292         2801 :           h->ident.klass = MY_CLASS;
    2293         2801 :           h->ident.data =  MY_ENDIAN;
    2294         2801 :           h->ident.version = EV_CURRENT;
    2295         2801 :           h->ident.osabi = OSABI_NONE;
    2296         2801 :           h->type = ET_NONE;
    2297         2801 :           h->machine = EM_NONE;
    2298         2801 :           h->version = EV_CURRENT;
    2299         2801 :           h->shoff = shoff;
    2300         2801 :           h->ehsize = sizeof (header);
    2301         2801 :           h->shentsize = sizeof (section);
    2302         2801 :           h->shnum = shnum;
    2303         2801 :           h->shstrndx = strndx;
    2304              : 
    2305         2801 :           pos = 0;
    2306         2801 :           write (hdr);
    2307              :         }
    2308              : 
    2309              : #if MAPPED_WRITING
    2310         2801 :       remove_mapping ();
    2311         2801 :       if (ftruncate (fd, length))
    2312            0 :         set_error (errno);
    2313              : #endif
    2314              :     }
    2315              : 
    2316         2919 :   data::simple_memory.shrink (sectab);
    2317         2919 :   data::simple_memory.shrink (strtab);
    2318              : 
    2319         2919 :   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      6014799 :   template<unsigned I> void set_flag_bit ()
    2426              :   {
    2427            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2428      6014799 :     discriminator |= 1u << I;
    2429      3798120 :   }
    2430      6241475 :   template<unsigned I> void clear_flag_bit ()
    2431              :   {
    2432            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2433      6241475 :     discriminator &= ~(1u << I);
    2434      6241475 :   }
    2435    595096851 :   template<unsigned I> bool get_flag_bit () const
    2436              :   {
    2437            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2438    724660349 :     return bool ((discriminator >> I) & 1);
    2439              :   }
    2440              : 
    2441              : public:
    2442    583500358 :   bool is_binding () const
    2443              :   {
    2444    139603093 :     return !get_flag_bit<DB_ZERO_BIT> ();
    2445              :   }
    2446    306893292 :   entity_kind get_entity_kind () const
    2447              :   {
    2448        39407 :     if (is_binding ())
    2449              :       return EK_BINDING;
    2450    231440673 :     return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
    2451              :   }
    2452              :   const char *entity_kind_name () const;
    2453              : 
    2454              : public:
    2455      9785885 :   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      9785885 :     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      2430773 :   bool is_pending_entity () const
    2466              :   {
    2467      3864148 :     return (get_entity_kind () == EK_SPECIALIZATION
    2468      1433375 :             || get_entity_kind () == EK_PARTIAL
    2469      3825344 :             || (get_entity_kind () == EK_DECL
    2470      1350091 :                 && 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     51910941 :   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     22729149 :     tree inner = STRIP_TEMPLATE (get_entity ());
    2487     51910941 :     return (get_flag_bit<DB_TU_LOCAL_BIT> ()
    2488     51910941 :             && (strict
    2489         3304 :                 || !VAR_OR_FUNCTION_DECL_P (inner)
    2490         2174 :                 || !NAMESPACE_SCOPE_P (inner)
    2491         2147 :                 || (DECL_LANG_SPECIFIC (inner)
    2492         2108 :                     && DECL_MODULE_PURVIEW_P (inner))));
    2493              :   }
    2494      1464016 :   bool refs_tu_local (bool strict = false) const
    2495              :   {
    2496      1464016 :     return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
    2497      1464016 :             || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
    2498              :   }
    2499      3527124 :   bool is_exposure (bool strict = false) const
    2500              :   {
    2501      3527124 :     return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
    2502      3527124 :             || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
    2503              :   }
    2504      2386293 :   bool is_ignored_exposure_context () const
    2505              :   {
    2506      2386293 :     return get_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
    2507              :   }
    2508              : 
    2509              : public:
    2510     31647970 :   bool is_import () const
    2511              :   {
    2512      9654635 :     return get_flag_bit<DB_IMPORTED_BIT> ();
    2513              :   }
    2514     19206628 :   bool is_unreached () const
    2515              :   {
    2516      1232197 :     return get_flag_bit<DB_UNREACHED_BIT> ();
    2517              :   }
    2518      2026184 :   bool is_hidden () const
    2519              :   {
    2520      2026184 :     return get_flag_bit<DB_HIDDEN_BIT> ();
    2521              :   }
    2522      1230091 :   bool is_maybe_recursive () const
    2523              :   {
    2524      1230091 :     return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
    2525              :   }
    2526         1128 :   bool is_entry () const
    2527              :   {
    2528         1128 :     return get_flag_bit<DB_ENTRY_BIT> ();
    2529              :   }
    2530      1496019 :   bool is_type_spec () const
    2531              :   {
    2532      1496019 :     return get_flag_bit<DB_TYPE_SPEC_BIT> ();
    2533              :   }
    2534      1496019 :   bool is_friend_spec () const
    2535              :   {
    2536      1496019 :     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           89 :   }
    2545           36 :   void clear_hidden_binding ()
    2546              :   {
    2547           36 :     clear_flag_bit<DB_HIDDEN_BIT> ();
    2548           36 :   }
    2549              : 
    2550              : public:
    2551     11596493 :   bool is_special () const
    2552              :   {
    2553     11596493 :     return get_flag_bit<DB_SPECIAL_BIT> ();
    2554              :   }
    2555      2216679 :   void set_special ()
    2556              :   {
    2557      2216679 :     set_flag_bit<DB_SPECIAL_BIT> ();
    2558            0 :   }
    2559              : 
    2560              : public:
    2561    192859658 :   tree get_entity () const
    2562              :   {
    2563     51910941 :     return entity;
    2564              :   }
    2565     20756201 :   tree get_name () const
    2566              :   {
    2567     20756201 :     gcc_checking_assert (is_binding ());
    2568     20756201 :     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     19643016 :     inline static hashval_t hash (const compare_type &p)
    2583              :     {
    2584     19643016 :       hashval_t h = pointer_hash<tree_node>::hash (p.first);
    2585     19643016 :       if (p.second)
    2586              :         {
    2587       225328 :           hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
    2588       225328 :           h = iterative_hash_hashval_t (h, nh);
    2589              :         }
    2590     19643016 :       return h;
    2591              :     }
    2592    107518870 :     inline static bool equal (const value_type b, const compare_type &p)
    2593              :     {
    2594    107518870 :       if (b->entity != p.first)
    2595              :         return false;
    2596              : 
    2597     14130164 :       if (p.second)
    2598        41943 :         return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
    2599              :       else
    2600     14088221 :         return !b->is_binding ();
    2601              :     }
    2602              : 
    2603              :     /* (re)hasher for a binding itself.  */
    2604     82707768 :     inline static hashval_t hash (const value_type b)
    2605              :     {
    2606     82707768 :       hashval_t h = pointer_hash<tree_node>::hash (b->entity);
    2607     82707768 :       if (b->is_binding ())
    2608              :         {
    2609      4923873 :           hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
    2610      4923873 :           h = iterative_hash_hashval_t (h, nh);
    2611              :         }
    2612     82707768 :       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      3153949 :     static void remove (value_type p)
    2625              :     {
    2626      3153949 :       delete (p);
    2627      3153949 :     }
    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       316080 :     hash (size_t size, hash *c = NULL)
    2663       632160 :       : parent (size), chain (c), current (NULL), section (0),
    2664       316080 :         reached_unreached (false), writing_merge_key (false),
    2665       316080 :         ignore_exposure (false)
    2666              :     {
    2667       316080 :       worklist.create (size);
    2668       316080 :       dep_adl_entity_list.create (16);
    2669       316080 :     }
    2670       316080 :     ~hash ()
    2671              :     {
    2672       316080 :       worklist.release ();
    2673       316080 :       dep_adl_entity_list.release ();
    2674       316080 :     }
    2675              : 
    2676              :   public:
    2677    143004902 :     bool is_key_order () const
    2678              :     {
    2679    143004902 :       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       916187 :     temp_override<bool> ignore_exposure_if (bool cond)
    2687              :     {
    2688       718939 :       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       316051 :     tarjan (unsigned size)
    2737       316051 :       : index (0)
    2738              :     {
    2739       316051 :       result.create (size);
    2740       316051 :       stack.create (50);
    2741       316051 :     }
    2742       316051 :     ~tarjan ()
    2743              :     {
    2744       316051 :       gcc_assert (!stack.length ());
    2745       316051 :       stack.release ();
    2746       316051 :     }
    2747              : 
    2748              :   public:
    2749              :     void connect (depset *);
    2750              :   };
    2751              : };
    2752              : 
    2753              : inline
    2754      3153949 : depset::depset (tree entity)
    2755      3153949 :   :entity (entity), discriminator (0), cluster (0), section (0)
    2756              : {
    2757      3153949 :   deps.create (0);
    2758              : }
    2759              : 
    2760              : inline
    2761      3153949 : depset::~depset ()
    2762              : {
    2763      3153949 :   deps.release ();
    2764      3153949 : }
    2765              : 
    2766              : const char *
    2767        44563 : depset::entity_kind_name () const
    2768              : {
    2769              :   /* Same order as entity_kind.  */
    2770        44563 :   static const char *const names[] =
    2771              :     {"decl", "specialization", "partial", "using",
    2772              :      "namespace", "tu-local", "redirect", "binding"};
    2773        44563 :   static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
    2774              :                  "names must have an entry for every explicit entity_kind");
    2775        44563 :   entity_kind kind = get_entity_kind ();
    2776        44563 :   gcc_checking_assert (kind < ARRAY_SIZE (names));
    2777        44563 :   return names[kind];
    2778              : }
    2779              : 
    2780              : /* Create a depset for a namespace binding NS::NAME.  */
    2781              : 
    2782       157866 : depset *depset::make_binding (tree ns, tree name)
    2783              : {
    2784       157866 :   depset *binding = new depset (ns);
    2785              : 
    2786       157866 :   binding->discriminator = reinterpret_cast <uintptr_t> (name);
    2787              : 
    2788       157866 :   return binding;
    2789              : }
    2790              : 
    2791      2996083 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
    2792              : {
    2793      2996083 :   depset *r = new depset (entity);
    2794              : 
    2795      2996083 :   r->discriminator = ((1 << DB_ZERO_BIT)
    2796      2996083 :                       | (ek << DB_KIND_BIT)
    2797      2996083 :                       | is_defn << DB_DEFN_BIT);
    2798              : 
    2799      2996083 :   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     49905594 :   static hashval_t hash (const value_type &k)
    2816              :   {
    2817     49905594 :     hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
    2818     49905594 :     h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
    2819              : 
    2820     49905594 :     return h;
    2821              :   }
    2822     14742107 :   static bool equal (const value_type &k, const value_type &l)
    2823              :   {
    2824     14742107 :     return k.ns == l.ns && k.id == l.id;
    2825              :   }
    2826       229782 :   static void mark_empty (value_type &k)
    2827              :   {
    2828       229782 :     k.ns = k.id = NULL_TREE;
    2829              :   }
    2830         6330 :   static void mark_deleted (value_type &k)
    2831              :   {
    2832         6330 :     k.ns = NULL_TREE;
    2833         6330 :     gcc_checking_assert (k.id);
    2834         6330 :   }
    2835    376538803 :   static bool is_empty (const value_type &k)
    2836              :   {
    2837    376486271 :     return k.ns == NULL_TREE && k.id == NULL_TREE;
    2838              :   }
    2839     20076160 :   static bool is_deleted (const value_type &k)
    2840              :   {
    2841     20076160 :     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      3042767 :   merge_key ()
    2992      3042767 :     :ref_q (REF_QUAL_NONE), coro_disc (0), iobj_p (0), xobj_p (0), index (0),
    2993      3042767 :      ret (NULL_TREE), args (NULL_TREE),
    2994      3042767 :      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       135340 :   bool is_duplicate (tree decl)
    3121              :   {
    3122       270680 :     return find_duplicate (decl) != NULL;
    3123              :   }
    3124       167801 :   tree maybe_duplicate (tree decl)
    3125              :   {
    3126       167801 :     if (uintptr_t *dup = find_duplicate (decl))
    3127         8963 :       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       144058 :   void post_process (post_process_data data)
    3146              :   {
    3147       144058 :     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       216597 : trees_in::trees_in (module_state *state)
    3162       216597 :   :parent (), state (state), unused (0)
    3163              : {
    3164       216597 :   duplicates = NULL;
    3165       216597 :   back_refs.create (500);
    3166       216597 :   post_decls.create (0);
    3167       216597 :   post_types.create (0);
    3168       216597 : }
    3169              : 
    3170       216597 : trees_in::~trees_in ()
    3171              : {
    3172       320433 :   delete (duplicates);
    3173       216597 :   back_refs.release ();
    3174       216597 :   post_decls.release ();
    3175       216597 :   post_types.release ();
    3176       216597 : }
    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    117217103 :   bool is_initial_scan () const
    3232              :   {
    3233    194724685 :     return !streaming_p () && !is_key_order ();
    3234              :   }
    3235     95697247 :   bool is_key_order () const
    3236              :   {
    3237     77507582 :     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       626558 :   void set_importing (int i ATTRIBUTE_UNUSED)
    3251              :   {
    3252              : #if CHECKING_P
    3253       626558 :     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       632176 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
    3357       632176 :                       unsigned section)
    3358      1264352 :   :parent (mem), state (state), tree_map (500),
    3359       632176 :    dep_hash (&deps), ref_num (0), section (section),
    3360       632176 :    writing_local_entities (false), walking_bit_field_unit (false)
    3361              : {
    3362              : #if CHECKING_P
    3363       632176 :   importedness = 0;
    3364              : #endif
    3365       632176 : }
    3366              : 
    3367       632176 : trees_out::~trees_out ()
    3368              : {
    3369       632176 : }
    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         6033 :   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       102234 :   ~loc_spans ()
    3414              :   {
    3415       102234 :     delete spans;
    3416       102234 :   }
    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        15773 :   bool init_p () const
    3430              :   {
    3431        15773 :     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         5852 :   void maybe_init ()
    3439              :   {
    3440         5852 :     if (!init_p ())
    3441            6 :       init (line_table, nullptr);
    3442         5852 :   }
    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       466650 :   location_t main_start () const
    3453              :   {
    3454       466650 :     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    309024396 :   static int compare (const void *a_, const void *b_)
    3497              :   {
    3498    309024396 :     auto *a = static_cast<const ord_loc_info *> (a_);
    3499    309024396 :     auto *b = static_cast<const ord_loc_info *> (b_);
    3500              : 
    3501    309024396 :     if (a->src != b->src)
    3502     67567894 :       return a->src < b->src ? -1 : +1;
    3503              : 
    3504              :     // Ensure no overlap
    3505    263333527 :     gcc_checking_assert (a->offset + a->span <= b->offset
    3506              :                          || b->offset + b->span <= a->offset);
    3507              : 
    3508    263333527 :     gcc_checking_assert (a->offset != b->offset);
    3509    263333527 :     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    125941839 :   static hashval_t hash (const value_type &v)
    3520              :   {
    3521    106084499 :     auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
    3522    125941839 :     return iterative_hash_hashval_t (v.offset, h);
    3523              :   }
    3524    139265687 :   static bool equal (const value_type &v, const compare_type p)
    3525              :   {
    3526    139265687 :     return v.src == p.src && v.offset == p.offset;
    3527              :   }
    3528              : 
    3529      9415546 :   static void mark_empty (value_type &v)
    3530              :   {
    3531      9415546 :     v.src = nullptr;
    3532              :   }
    3533    415953396 :   static bool is_empty (value_type &v)
    3534              :   {
    3535    415953396 :     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      2292830 :   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      9383249 :   static int compare (const void *a_, const void *b_)
    3555              :   {
    3556      9383249 :     auto *a = static_cast<const macro_loc_info *> (a_);
    3557      9383249 :     auto *b = static_cast<const macro_loc_info *> (b_);
    3558              : 
    3559      9383249 :     gcc_checking_assert (MAP_START_LOCATION (a->src)
    3560              :                          != MAP_START_LOCATION (b->src));
    3561      9383249 :     if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
    3562              :       return -1;
    3563              :     else
    3564      4523432 :       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      9931653 :   static hashval_t hash (compare_type p)
    3575              :   {
    3576      9931653 :     return pointer_hash<const line_map_macro>::hash (p);
    3577              :   }
    3578      8411030 :   static hashval_t hash (const value_type &v)
    3579              :   {
    3580      8411030 :     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       705115 :   static void mark_empty (value_type &v)
    3588              :   {
    3589       705115 :     v.src = nullptr;
    3590              :   }
    3591     31886906 :   static bool is_empty (value_type &v)
    3592              :   {
    3593     31886906 :     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       161050 :   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         5409 :   void close ()
    3646              :   {
    3647         5409 :     if (from)
    3648              :       {
    3649         2967 :         from->end ();
    3650         5934 :         delete from;
    3651         2967 :         from = NULL;
    3652              :       }
    3653         5409 :   }
    3654              : 
    3655              :  public:
    3656              :   void release_macros ();
    3657              : 
    3658              :  public:
    3659         3025 :   void alloc_remap (unsigned size)
    3660              :   {
    3661         3025 :     gcc_assert (!remap);
    3662         3025 :     vec_safe_reserve (remap, size);
    3663         6449 :     for (unsigned ix = size; ix--;)
    3664         3424 :       remap->quick_push (0);
    3665         3025 :   }
    3666      1236478 :   unsigned remap_module (unsigned owner)
    3667              :   {
    3668      1236478 :     if (owner < remap->length ())
    3669      1236478 :       return (*remap)[owner] >> 1;
    3670              :     return 0;
    3671              :   }
    3672              : 
    3673              :  public:
    3674              :   /* GC allocation.  But we must explicitly delete it.   */
    3675         3049 :   static void *operator new (size_t x)
    3676              :   {
    3677         6098 :     return ggc_alloc_atomic (x);
    3678              :   }
    3679         2967 :   static void operator delete (void *p)
    3680              :   {
    3681         2967 :     ggc_free (p);
    3682         2967 :   }
    3683              : };
    3684              : 
    3685         3049 : slurping::slurping (elf_in *from)
    3686         3049 :   : remap (NULL), from (from),
    3687         3049 :     headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
    3688         3049 :     loc_deltas (0, 0),
    3689         3049 :     current (~0u), remaining (0), lru (0)
    3690              : {
    3691         3049 : }
    3692              : 
    3693         2967 : slurping::~slurping ()
    3694              : {
    3695         2967 :   vec_free (remap);
    3696         2967 :   remap = NULL;
    3697         2967 :   release_macros ();
    3698         2967 :   close ();
    3699         2967 : }
    3700              : 
    3701         5409 : void slurping::release_macros ()
    3702              : {
    3703         5409 :   if (macro_defs.size)
    3704          896 :     elf_in::release (from, macro_defs);
    3705         5409 :   if (macro_tbl.size)
    3706            0 :     elf_in::release (from, macro_tbl);
    3707         5409 : }
    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         3029 :   void release ()
    3820              :   {
    3821         3029 :     imports = exports = NULL;
    3822         3029 :     slurped ();
    3823         2967 :   }
    3824         5471 :   void slurped ()
    3825              :   {
    3826         5471 :     delete slurp;
    3827         5471 :     slurp = NULL;
    3828         5471 :   }
    3829      1329749 :   elf_in *from () const
    3830              :   {
    3831      1329749 :     return slurp->from;
    3832              :   }
    3833              : 
    3834              :  public:
    3835              :   /* Kind of this module.  */
    3836       144946 :   bool is_module () const
    3837              :   {
    3838       144946 :     return module_p;
    3839              :   }
    3840      2533723 :   bool is_header () const
    3841              :   {
    3842      2533723 :     return header_p;
    3843              :   }
    3844          639 :   bool is_interface () const
    3845              :   {
    3846          639 :     return interface_p;
    3847              :   }
    3848       342837 :   bool is_partition () const
    3849              :   {
    3850       342837 :     return partition_p;
    3851              :   }
    3852              : 
    3853              :   /* How this module is used in the current TU.  */
    3854         3245 :   bool is_exported () const
    3855              :   {
    3856         3245 :     return exported_p;
    3857              :   }
    3858        21135 :   bool is_direct () const
    3859              :   {
    3860        21135 :     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        16472 :   bool has_location () const
    3874              :   {
    3875        16472 :     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        53565 :   const char *get_flatname () const
    4014              :   {
    4015        53565 :     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        11679 : module_state::module_state (tree name, module_state *parent, bool partition)
    4040        11679 :   : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
    4041        11679 :     parent (parent), name (name), slurp (NULL),
    4042        11679 :     flatname (NULL), filename (NULL),
    4043        11679 :     entity_lwm (~0u >> 1), entity_num (0),
    4044        11679 :     ordinary_locs (0, 0), macro_locs (0, 0),
    4045        11679 :     loc (UNKNOWN_LOCATION),
    4046        11679 :     crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
    4047              : {
    4048        11679 :   loadedness = ML_NONE;
    4049              : 
    4050        11679 :   module_p = header_p = interface_p = partition_p = false;
    4051              : 
    4052        11679 :   directness = MD_NONE;
    4053        11679 :   exported_p = false;
    4054              : 
    4055        11679 :   cmi_noted_p = false;
    4056        11679 :   active_init_p = false;
    4057              : 
    4058        11679 :   partition_p = partition;
    4059              : 
    4060        11679 :   inform_cmi_p = false;
    4061        11679 :   visited_p = false;
    4062              : 
    4063        11679 :   extensions = 0;
    4064        11679 :   if (name && TREE_CODE (name) == STRING_CST)
    4065              :     {
    4066         1922 :       header_p = true;
    4067              : 
    4068         1922 :       const char *string = TREE_STRING_POINTER (name);
    4069         1922 :       gcc_checking_assert (string[0] == '.'
    4070              :                            ? IS_DIR_SEPARATOR (string[1])
    4071              :                            : IS_ABSOLUTE_PATH (string));
    4072              :     }
    4073              : 
    4074        11679 :   gcc_checking_assert (!(parent && header_p));
    4075        11679 : }
    4076              : 
    4077           62 : module_state::~module_state ()
    4078              : {
    4079           62 :   release ();
    4080           62 : }
    4081              : 
    4082              : /* Hash module state.  */
    4083              : static hashval_t
    4084        17292 : module_name_hash (const_tree name)
    4085              : {
    4086        17292 :   if (TREE_CODE (name) == STRING_CST)
    4087         3550 :     return htab_hash_string (TREE_STRING_POINTER (name));
    4088              :   else
    4089        13742 :     return IDENTIFIER_HASH_VALUE (name);
    4090              : }
    4091              : 
    4092              : hashval_t
    4093         4860 : module_state_hash::hash (const value_type m)
    4094              : {
    4095         4860 :   hashval_t ph = pointer_hash<void>::hash
    4096         4860 :     (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
    4097         4860 :                                | m->is_partition ()));
    4098         4860 :   hashval_t nh = module_name_hash (m->name);
    4099         4860 :   return iterative_hash_hashval_t (ph, nh);
    4100              : }
    4101              : 
    4102              : /* Hash a name.  */
    4103              : hashval_t
    4104        12432 : module_state_hash::hash (const compare_type &c)
    4105              : {
    4106        12432 :   hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
    4107        12432 :   hashval_t nh = module_name_hash (c.first);
    4108              : 
    4109        12432 :   return iterative_hash_hashval_t (ph, nh);
    4110              : }
    4111              : 
    4112              : bool
    4113         8550 : module_state_hash::equal (const value_type existing,
    4114              :                           const compare_type &candidate)
    4115              : {
    4116         8550 :   uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
    4117         8550 :                   | existing->is_partition ());
    4118         8550 :   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         7047 :   if (existing->name == candidate.first)
    4124              :     return true;
    4125              : 
    4126              :   /* If neither are string csts, they can't be equal.  */
    4127         1483 :   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       304202 : this_module() {
    4200       304202 :   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        33912 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
    4243              : {
    4244        33912 :   auto *res = mapper;
    4245          313 :   if (!res)
    4246         4950 :     res = make_mapper (loc, deps);
    4247        33912 :   return res;
    4248              : }
    4249              : 
    4250              : /********************************************************************/
    4251              : static tree
    4252       444532 : get_clone_target (tree decl)
    4253              : {
    4254       444532 :   tree target;
    4255              : 
    4256       444532 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4257              :     {
    4258        56872 :       tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
    4259              : 
    4260        56872 :       target = DECL_TI_TEMPLATE (res_orig);
    4261              :     }
    4262              :   else
    4263       387660 :     target = DECL_CLONED_FUNCTION (decl);
    4264              : 
    4265       444532 :   gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
    4266              : 
    4267       444532 :   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    140297210 : node_template_info (tree decl, int &use)
    4286              : {
    4287    140297210 :   tree ti = NULL_TREE;
    4288    140297210 :   int use_tpl = -1;
    4289    140297210 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    4290              :     {
    4291     25218140 :       tree type = TREE_TYPE (decl);
    4292              : 
    4293     25218140 :       ti = TYPE_TEMPLATE_INFO (type);
    4294     25218140 :       if (ti)
    4295              :         {
    4296      5704464 :           if (TYPE_LANG_SPECIFIC (type))
    4297      5693024 :             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        11440 :               tree ctx = CP_DECL_CONTEXT (decl);
    4306        11440 :               if (TYPE_P (ctx))
    4307        11223 :                 ctx = TYPE_NAME (ctx);
    4308        11440 :               node_template_info (ctx, use);
    4309        11440 :               use_tpl = use != 2 ? use : 0;
    4310              :             }
    4311              :         }
    4312              :     }
    4313    115079070 :   else if (DECL_LANG_SPECIFIC (decl)
    4314    115079070 :            && (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     98697726 :       use_tpl = DECL_USE_TEMPLATE (decl);
    4322     98697726 :       ti = DECL_TEMPLATE_INFO (decl);
    4323              :     }
    4324              : 
    4325    140297210 :   use = use_tpl;
    4326    140297210 :   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      1761859 : import_entity_index (tree decl, bool null_ok = false)
    4337              : {
    4338      1761859 :   if (unsigned *slot = entity_map->get (DECL_UID (decl)))
    4339      1761814 :     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       137588 : import_entity_module (unsigned index)
    4350              : {
    4351       137588 :   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       137528 :   unsigned pos = 1;
    4357       137528 :   unsigned len = modules->length () - pos;
    4358       314173 :   while (len)
    4359              :     {
    4360       176645 :       unsigned half = len / 2;
    4361       176645 :       module_state *probe = (*modules)[pos + half];
    4362       176645 :       if (index < probe->entity_lwm)
    4363              :         len = half;
    4364       137993 :       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    433859631 :   void indent ()
    4416              :   {
    4417          309 :     if (dumps)
    4418       733487 :       dumps->indent++;
    4419              :   }
    4420    433859631 :   void outdent ()
    4421              :   {
    4422    433859631 :     if (dumps)
    4423              :       {
    4424       733487 :         gcc_checking_assert (dumps->indent);
    4425       733487 :         dumps->indent--;
    4426              :       }
    4427    433859631 :   }
    4428              : 
    4429              : public:
    4430              :   /* Is dump enabled?.  */
    4431    264389420 :   bool operator () (int mask = 0)
    4432              :   {
    4433      5202334 :     if (!dumps || !dumps->stream)
    4434              :       return false;
    4435       511969 :     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       135694 : dumper::push (module_state *m)
    4450              : {
    4451       135694 :   FILE *stream = NULL;
    4452       135694 :   if (!dumps || !dumps->stack.length ())
    4453              :     {
    4454       134390 :       stream = dump_begin (module_dump_id, &flags);
    4455       134390 :       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       135651 : void dumper::pop (unsigned n)
    4494              : {
    4495       135651 :   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       526243 : dumper::impl::nested_name (tree t)
    4520              : {
    4521       526243 :   tree ti = NULL_TREE;
    4522       526243 :   int origin = -1;
    4523       526243 :   tree name = NULL_TREE;
    4524              : 
    4525       526243 :   if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
    4526            0 :     t = TU_LOCAL_ENTITY_NAME (t);
    4527              : 
    4528       526213 :   if (t && TREE_CODE (t) == TREE_BINFO)
    4529          384 :     t = BINFO_TYPE (t);
    4530              : 
    4531       526243 :   if (t && TYPE_P (t))
    4532       256181 :     t = TYPE_NAME (t);
    4533              : 
    4534       526201 :   if (t && DECL_P (t))
    4535              :     {
    4536       442384 :       if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
    4537              :         ;
    4538       410332 :       else if (tree ctx = DECL_CONTEXT (t))
    4539       319963 :         if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
    4540       319963 :             || nested_name (ctx))
    4541       319963 :           fputs ("::", stream);
    4542              : 
    4543       442384 :       int use_tpl;
    4544       442384 :       ti = node_template_info (t, use_tpl);
    4545       139160 :       if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    4546       581499 :           && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
    4547              :         t = TI_TEMPLATE (ti);
    4548       442384 :       tree not_tmpl = t;
    4549       442384 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    4550              :         {
    4551        23856 :           fputs ("template ", stream);
    4552        23856 :           not_tmpl = DECL_TEMPLATE_RESULT (t);
    4553              :         }
    4554              : 
    4555        23856 :       if (not_tmpl
    4556       442380 :           && DECL_P (not_tmpl)
    4557       442380 :           && DECL_LANG_SPECIFIC (not_tmpl)
    4558       266189 :           && 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       445611 :       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       442384 :   if (name)
    4581       490929 :     switch (TREE_CODE (name))
    4582              :       {
    4583        13651 :       default:
    4584        13651 :         fputs ("#unnamed#", stream);
    4585        13651 :         break;
    4586              : 
    4587       452858 :       case IDENTIFIER_NODE:
    4588       452858 :         fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
    4589       452858 :         break;
    4590              : 
    4591        24328 :       case INTEGER_CST:
    4592        24328 :         print_hex (wi::to_wide (name), stream);
    4593        24328 :         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        35314 :     fputs ("#null#", stream);
    4604              : 
    4605       526243 :   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       526243 :   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       501866 :   else if (origin == -2)
    4623           45 :     fprintf (stream, "@???");
    4624              : 
    4625       526243 :   if (ti)
    4626              :     {
    4627       139160 :       tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
    4628       139160 :       fputs ("<", stream);
    4629       139160 :       if (args)
    4630       350663 :         for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
    4631              :           {
    4632       211503 :             if (ix)
    4633        72343 :               fputs (",", stream);
    4634       211503 :             nested_name (TREE_VEC_ELT (args, ix));
    4635              :           }
    4636       139160 :       fputs (">", stream);
    4637              :     }
    4638              : 
    4639       526243 :   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       448780 : dumper::operator () (const char *format, ...)
    4666              : {
    4667       448780 :   if (!(*this) ())
    4668              :     return false;
    4669              : 
    4670       384774 :   bool no_nl = format[0] == '+';
    4671       384774 :   format += no_nl;
    4672              : 
    4673       384774 :   if (dumps->bol)
    4674              :     {
    4675              :       /* Module import indent.  */
    4676       198426 :       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       198426 :       if (unsigned indent = dumps->indent)
    4686              :         {
    4687       106589 :           const char *prefix = "      ";
    4688       208162 :           fprintf (dumps->stream, (indent <= strlen (prefix)
    4689       101573 :                                    ? &prefix[strlen (prefix) - indent]
    4690              :                                    : "  .%d.  "), indent);
    4691              :         }
    4692       198426 :       dumps->bol = false;
    4693              :     }
    4694              : 
    4695       384774 :   va_list args;
    4696       384774 :   va_start (args, format);
    4697      1124630 :   while (const char *esc = strchr (format, '%'))
    4698              :     {
    4699       739856 :       fwrite (format, 1, (size_t)(esc - format), dumps->stream);
    4700       739856 :       format = ++esc;
    4701       739856 :       switch (*format++)
    4702              :         {
    4703            0 :         default:
    4704            0 :           gcc_unreachable ();
    4705              : 
    4706          586 :         case '%':
    4707          586 :           fputc ('%', dumps->stream);
    4708          586 :           break;
    4709              : 
    4710       112533 :         case 'C': /* Code */
    4711       112533 :           {
    4712       112533 :             tree_code code = (tree_code)va_arg (args, unsigned);
    4713       112533 :             fputs (get_tree_code_name (code), dumps->stream);
    4714              :           }
    4715       112533 :           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       126015 :         case 'N': /* Name.  */
    4746       126015 :           {
    4747       126015 :             tree t = va_arg (args, tree);
    4748       252519 :             while (t && TREE_CODE (t) == OVERLOAD)
    4749          489 :               t = OVL_FUNCTION (t);
    4750       126015 :             fputc ('\'', dumps->stream);
    4751       126015 :             dumps->nested_name (t);
    4752       126015 :             fputc ('\'', dumps->stream);
    4753              :           }
    4754       126015 :           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        63200 :         case 'd': /* Decimal Int.  */
    4818        63200 :           {
    4819        63200 :             int d = va_arg (args, int);
    4820        63200 :             fprintf (dumps->stream, "%d", d);
    4821              :           }
    4822        63200 :           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       127378 :         case 's': /* String. */
    4832       127378 :           {
    4833       127378 :             const char *s = va_arg (args, char *);
    4834       127378 :             gcc_checking_assert (s);
    4835       127378 :             fputs (s, dumps->stream);
    4836              :           }
    4837       127378 :           break;
    4838              : 
    4839       250771 :         case 'u': /* Unsigned.  */
    4840       250771 :           {
    4841       250771 :             unsigned u = va_arg (args, unsigned);
    4842       250771 :             fprintf (dumps->stream, "%u", u);
    4843              :           }
    4844       250771 :           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       384774 :   fputs (format, dumps->stream);
    4855       384774 :   va_end (args);
    4856       384774 :   if (!no_nl)
    4857              :     {
    4858       198426 :       dumps->bol = true;
    4859       198426 :       fputc ('\n', dumps->stream);
    4860              :     }
    4861              :   return true;
    4862              : }
    4863              : 
    4864              : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
    4865              : {
    4866       334351 :   static int keep_cache_entry (tree t)
    4867              :   {
    4868       334351 :     if (!CHECKING_P)
    4869              :       /* GTY is unfortunately not clever enough to conditionalize
    4870              :          this.  */
    4871              :       gcc_unreachable ();
    4872              : 
    4873       334351 :     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       347868 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
    4895              :                              bool installing ATTRIBUTE_UNUSED)
    4896              : {
    4897              : #if CHECKING_P
    4898       347868 :   tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
    4899       347868 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    4900       347868 :   if (installing)
    4901              :     {
    4902              :       /* We must be inserting for the first time.  */
    4903       212528 :       gcc_assert (!*slot);
    4904       212528 :       *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       135340 :     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       347868 :   if (not_tmpl != decl)
    4921       205953 :     gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
    4922              : #endif
    4923       347868 : }
    4924              : 
    4925              : void
    4926       464429 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
    4927              : {
    4928              : #if CHECKING_P
    4929       464429 :   tree *slot = note_defs->find_slot (decl, INSERT);
    4930       464429 :   gcc_assert (!*slot);
    4931       464429 :   *slot = decl;
    4932       464429 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4933       261982 :     gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
    4934              : #endif
    4935       464429 : }
    4936              : 
    4937              : /********************************************************************/
    4938              : static bool
    4939        12759 : 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       105430 : set_cmi_repo (const char *r)
    4955              : {
    4956       105430 :   XDELETEVEC (cmi_repo);
    4957       105430 :   XDELETEVEC (cmi_path);
    4958       105430 :   cmi_path_alloc = 0;
    4959              : 
    4960       105430 :   cmi_repo = NULL;
    4961       105430 :   cmi_repo_length = 0;
    4962              : 
    4963       105430 :   if (!r || !r[0])
    4964              :     return;
    4965              : 
    4966         4947 :   size_t len = strlen (r);
    4967         4947 :   cmi_repo = XNEWVEC (char, len + 1);
    4968         4947 :   memcpy (cmi_repo, r, len + 1);
    4969              : 
    4970         4947 :   if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
    4971         4947 :     len--;
    4972         4947 :   if (len == 1 && cmi_repo[0] == '.')
    4973           27 :     len--;
    4974         4947 :   cmi_repo[len] = 0;
    4975         4947 :   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         6053 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
    4983              : {
    4984         6053 :   size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
    4985              : 
    4986         6053 :   if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
    4987              :     {
    4988         6026 :       if (cmi_path_alloc < cmi_repo_length + len + 2)
    4989              :         {
    4990         4825 :           XDELETEVEC (cmi_path);
    4991         4825 :           cmi_path_alloc = cmi_repo_length + len * 2 + 2;
    4992         4825 :           cmi_path = XNEWVEC (char, cmi_path_alloc);
    4993              : 
    4994         4825 :           memcpy (cmi_path, cmi_repo, cmi_repo_length);
    4995         4825 :           cmi_path[cmi_repo_length] = DIR_SEPARATOR;
    4996              :         }
    4997              : 
    4998         6026 :       memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
    4999         6026 :       len += cmi_repo_length + 1;
    5000         6026 :       to = cmi_path;
    5001              :     }
    5002              : 
    5003         6053 :   if (len_p)
    5004         2919 :     *len_p = len;
    5005              : 
    5006         6053 :   return to;
    5007              : }
    5008              : 
    5009              : /* Try and create the directories of PATH.  */
    5010              : 
    5011              : static void
    5012           99 : create_dirs (char *path)
    5013              : {
    5014           99 :   char *base = path;
    5015              :   /* Skip past initial slashes of absolute path.  */
    5016           99 :   while (IS_DIR_SEPARATOR (*base))
    5017            0 :     base++;
    5018              : 
    5019              :   /* Try and create the missing directories.  */
    5020         6338 :   for (; *base; base++)
    5021         6239 :     if (IS_DIR_SEPARATOR (*base))
    5022              :       {
    5023          627 :         char sep = *base;
    5024          627 :         *base = 0;
    5025          627 :         int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
    5026          668 :         dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
    5027          627 :         *base = sep;
    5028          627 :         if (failed
    5029              :             /* Maybe racing with another creator (of a *different*
    5030              :                module).  */
    5031          101 :             && errno != EEXIST)
    5032              :           break;
    5033              :       }
    5034           99 : }
    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        98828 : friend_from_decl_list (tree frnd)
    5041              : {
    5042        98828 :   tree res = frnd;
    5043              : 
    5044        98828 :   if (TREE_CODE (frnd) != TEMPLATE_DECL)
    5045              :     {
    5046        60424 :       tree tmpl = NULL_TREE;
    5047        60424 :       if (TYPE_P (frnd))
    5048              :         {
    5049         9939 :           res = TYPE_NAME (frnd);
    5050         9780 :           if (CLASS_TYPE_P (frnd)
    5051        19719 :               && CLASSTYPE_TEMPLATE_INFO (frnd))
    5052         9771 :             tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
    5053              :         }
    5054        50485 :       else if (DECL_TEMPLATE_INFO (frnd))
    5055              :         {
    5056        50485 :           tmpl = DECL_TI_TEMPLATE (frnd);
    5057        50485 :           if (TREE_CODE (tmpl) != TEMPLATE_DECL)
    5058              :             tmpl = NULL_TREE;
    5059              :         }
    5060              : 
    5061        69266 :       if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
    5062              :         res = tmpl;
    5063              :     }
    5064              : 
    5065        98828 :   return res;
    5066              : }
    5067              : 
    5068              : static tree
    5069        29841 : find_enum_member (tree ctx, tree name)
    5070              : {
    5071        29841 :   for (tree values = TYPE_VALUES (ctx);
    5072       490692 :        values; values = TREE_CHAIN (values))
    5073       481998 :     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          900 :   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         2772 : trees_out::instrument ()
    5097              : {
    5098         2772 :   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         2772 : }
    5109              : 
    5110              : /* Setup and teardown for a tree walk.  */
    5111              : 
    5112              : void
    5113      2747012 : trees_out::begin ()
    5114              : {
    5115      2747012 :   gcc_assert (!streaming_p () || !tree_map.elements ());
    5116              : 
    5117      2747012 :   mark_trees ();
    5118      2747012 :   if (streaming_p ())
    5119       316096 :     parent::begin ();
    5120      2747012 : }
    5121              : 
    5122              : unsigned
    5123       316096 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    5124              : {
    5125       316096 :   gcc_checking_assert (streaming_p ());
    5126              : 
    5127       316096 :   unmark_trees ();
    5128       316096 :   return parent::end (sink, name, crc_ptr);
    5129              : }
    5130              : 
    5131              : void
    5132      2430916 : trees_out::end ()
    5133              : {
    5134      2430916 :   gcc_assert (!streaming_p ());
    5135              : 
    5136      2430916 :   unmark_trees ();
    5137              :   /* Do not parent::end -- we weren't streaming.  */
    5138      2430916 : }
    5139              : 
    5140              : void
    5141      2747012 : trees_out::mark_trees ()
    5142              : {
    5143      2747012 :   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      2130546 :       tree_map.~ptr_int_hash_map ();
    5149      2130546 :       new (&tree_map) ptr_int_hash_map (size);
    5150              :     }
    5151              : 
    5152              :   /* Install the fixed trees, with +ve references.  */
    5153      2747012 :   unsigned limit = fixed_trees->length ();
    5154    529268287 :   for (unsigned ix = 0; ix != limit; ix++)
    5155              :     {
    5156    526521275 :       tree val = (*fixed_trees)[ix];
    5157    526521275 :       bool existed = tree_map.put (val, ix + tag_fixed);
    5158    526521275 :       gcc_checking_assert (!TREE_VISITED (val) && !existed);
    5159    526521275 :       TREE_VISITED (val) = true;
    5160              :     }
    5161              : 
    5162      2747012 :   ref_num = 0;
    5163      2747012 : }
    5164              : 
    5165              : /* Unmark the trees we encountered  */
    5166              : 
    5167              : void
    5168      2747012 : trees_out::unmark_trees ()
    5169              : {
    5170      2747012 :   ptr_int_hash_map::iterator end (tree_map.end ());
    5171    629010197 :   for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
    5172              :     {
    5173    626263185 :       tree node = reinterpret_cast<tree> ((*iter).first);
    5174    626263185 :       int ref = (*iter).second;
    5175              :       /* We should have visited the node, and converted its mergeable
    5176              :          reference to a regular reference.  */
    5177    626263185 :       gcc_checking_assert (TREE_VISITED (node)
    5178              :                            && (ref <= tag_backref || ref >= tag_fixed));
    5179    626263185 :       TREE_VISITED (node) = false;
    5180              :     }
    5181      2747012 : }
    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      4252859 : trees_out::mark_by_value (tree decl)
    5189              : {
    5190      4252859 :   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      4252859 :   if (TREE_VISITED (decl))
    5196              :     /* Must already be forced or fixed.  */
    5197         3976 :     gcc_checking_assert (*tree_map.get (decl) >= tag_value);
    5198              :   else
    5199              :     {
    5200      4248883 :       bool existed = tree_map.put (decl, tag_value);
    5201      4248883 :       gcc_checking_assert (!existed);
    5202      4248883 :       TREE_VISITED (decl) = true;
    5203              :     }
    5204      4252859 : }
    5205              : 
    5206              : int
    5207    119389085 : trees_out::get_tag (tree t)
    5208              : {
    5209    119389085 :   gcc_checking_assert (TREE_VISITED (t));
    5210    119389085 :   return *tree_map.get (t);
    5211              : }
    5212              : 
    5213              : /* Insert T into the map, return its tag number.    */
    5214              : 
    5215              : int
    5216     99741910 : trees_out::insert (tree t, walk_kind walk)
    5217              : {
    5218     99741910 :   gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
    5219     99741910 :   int tag = --ref_num;
    5220     99741910 :   bool existed;
    5221     99741910 :   int &slot = tree_map.get_or_insert (t, &existed);
    5222     99741910 :   gcc_checking_assert (TREE_VISITED (t) == existed
    5223              :                        && (!existed
    5224              :                            || (walk == WK_value && slot == tag_value)));
    5225     99741910 :   TREE_VISITED (t) = true;
    5226     99741910 :   slot = tag;
    5227              : 
    5228     99741910 :   return tag;
    5229              : }
    5230              : 
    5231              : /* Insert T into the backreference array.  Return its back reference
    5232              :    number.  */
    5233              : 
    5234              : int
    5235     21702241 : trees_in::insert (tree t)
    5236              : {
    5237     21702241 :   gcc_checking_assert (t || get_overrun ());
    5238     21702241 :   back_refs.safe_push (t);
    5239     21702241 :   return -(int)back_refs.length ();
    5240              : }
    5241              : 
    5242              : /* A chained set of decls.  */
    5243              : 
    5244              : void
    5245       157971 : trees_out::chained_decls (tree decls)
    5246              : {
    5247       363641 :   for (; decls; decls = DECL_CHAIN (decls))
    5248       205670 :     tree_node (decls);
    5249       157971 :   tree_node (NULL_TREE);
    5250       157971 : }
    5251              : 
    5252              : tree
    5253        60786 : trees_in::chained_decls ()
    5254              : {
    5255        60786 :   tree decls = NULL_TREE;
    5256        60786 :   for (tree *chain = &decls;;)
    5257       145989 :     if (tree decl = tree_node ())
    5258              :       {
    5259        85203 :         if (!DECL_P (decl) || DECL_CHAIN (decl))
    5260              :           {
    5261            0 :             set_overrun ();
    5262            0 :             break;
    5263              :           }
    5264        85203 :         *chain = decl;
    5265        85203 :         chain = &DECL_CHAIN (decl);
    5266              :       }
    5267              :     else
    5268        85203 :       break;
    5269              : 
    5270        60786 :   return decls;
    5271              : }
    5272              : 
    5273              : /* A vector of decls following DECL_CHAIN.  */
    5274              : 
    5275              : void
    5276       397972 : trees_out::vec_chained_decls (tree decls)
    5277              : {
    5278       397972 :   if (streaming_p ())
    5279              :     {
    5280              :       unsigned len = 0;
    5281              : 
    5282      1253643 :       for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5283      1054703 :         len++;
    5284       198940 :       u (len);
    5285              :     }
    5286              : 
    5287      2507788 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5288              :     {
    5289       425303 :       if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5290      2126071 :           && 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      2109808 :         tree_node (decl);
    5296              :     }
    5297       397972 : }
    5298              : 
    5299              : vec<tree, va_heap> *
    5300       135647 : trees_in::vec_chained_decls ()
    5301              : {
    5302       135647 :   vec<tree, va_heap> *v = NULL;
    5303              : 
    5304       135647 :   if (unsigned len = u ())
    5305              :     {
    5306        71166 :       vec_alloc (v, len);
    5307              : 
    5308       866149 :       for (unsigned ix = 0; ix < len; ix++)
    5309              :         {
    5310       794983 :           tree decl = tree_node ();
    5311       794983 :           if (decl && !DECL_P (decl))
    5312              :             {
    5313            0 :               set_overrun ();
    5314            0 :               break;
    5315              :             }
    5316       794983 :           v->quick_push (decl);
    5317              :         }
    5318              : 
    5319        71166 :       if (get_overrun ())
    5320              :         {
    5321            0 :           vec_free (v);
    5322            0 :           v = NULL;
    5323              :         }
    5324              :     }
    5325              : 
    5326       135647 :   return v;
    5327              : }
    5328              : 
    5329              : /* A vector of trees.  */
    5330              : 
    5331              : void
    5332       279822 : trees_out::tree_vec (vec<tree, va_gc> *v)
    5333              : {
    5334       279822 :   unsigned len = vec_safe_length (v);
    5335       279822 :   if (streaming_p ())
    5336       139888 :     u (len);
    5337       358548 :   for (unsigned ix = 0; ix != len; ix++)
    5338        78726 :     tree_node ((*v)[ix]);
    5339       279822 : }
    5340              : 
    5341              : vec<tree, va_gc> *
    5342        94910 : trees_in::tree_vec ()
    5343              : {
    5344        94910 :   vec<tree, va_gc> *v = NULL;
    5345        94910 :   if (unsigned len = u ())
    5346              :     {
    5347        23928 :       vec_alloc (v, len);
    5348        50475 :       for (unsigned ix = 0; ix != len; ix++)
    5349        26547 :         v->quick_push (tree_node ());
    5350              :     }
    5351        94910 :   return v;
    5352              : }
    5353              : 
    5354              : /* A vector of tree pairs.  */
    5355              : 
    5356              : void
    5357         7104 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
    5358              : {
    5359         7104 :   unsigned len = vec_safe_length (v);
    5360         7104 :   if (streaming_p ())
    5361         3552 :     u (len);
    5362         7104 :   if (len)
    5363        38440 :     for (unsigned ix = 0; ix != len; ix++)
    5364              :       {
    5365        31466 :         tree_pair_s const &s = (*v)[ix];
    5366        31466 :         tree_node (s.purpose);
    5367        31466 :         tree_node (s.value);
    5368              :       }
    5369         7104 : }
    5370              : 
    5371              : vec<tree_pair_s, va_gc> *
    5372         2693 : trees_in::tree_pair_vec ()
    5373              : {
    5374         2693 :   vec<tree_pair_s, va_gc> *v = NULL;
    5375         2693 :   if (unsigned len = u ())
    5376              :     {
    5377         2639 :       vec_alloc (v, len);
    5378        14651 :       for (unsigned ix = 0; ix != len; ix++)
    5379              :         {
    5380        12012 :           tree_pair_s s;
    5381        12012 :           s.purpose = tree_node ();
    5382        12012 :           s.value = tree_node ();
    5383        12012 :           v->quick_push (s);
    5384              :       }
    5385              :     }
    5386         2693 :   return v;
    5387              : }
    5388              : 
    5389              : void
    5390       423206 : trees_out::tree_list (tree list, bool has_purpose)
    5391              : {
    5392      1891763 :   for (; list; list = TREE_CHAIN (list))
    5393              :     {
    5394      1468557 :       gcc_checking_assert (TREE_VALUE (list));
    5395      1468557 :       tree_node (TREE_VALUE (list));
    5396      1468557 :       if (has_purpose)
    5397      1419457 :         tree_node (TREE_PURPOSE (list));
    5398              :     }
    5399       423206 :   tree_node (NULL_TREE);
    5400       423206 : }
    5401              : 
    5402              : tree
    5403       146653 : trees_in::tree_list (bool has_purpose)
    5404              : {
    5405       146653 :   tree res = NULL_TREE;
    5406              : 
    5407       705229 :   for (tree *chain = &res; tree value = tree_node ();
    5408      1117152 :        chain = &TREE_CHAIN (*chain))
    5409              :     {
    5410       558576 :       tree purpose = has_purpose ? tree_node () : NULL_TREE;
    5411       558576 :       *chain = build_tree_list (purpose, value);
    5412       558576 :     }
    5413              : 
    5414       146653 :   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     20117116 : trees_out::start (tree t, bool code_streamed)
    5468              : {
    5469     20117116 :   if (TYPE_P (t))
    5470              :     {
    5471       772625 :       enum tree_code code = TREE_CODE (t);
    5472       772625 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5473              :       /* All these types are TYPE_NON_COMMON.  */
    5474       772625 :       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     20117116 :   if (!code_streamed)
    5483     19440131 :     u (TREE_CODE (t));
    5484              : 
    5485     20117116 :   switch (TREE_CODE (t))
    5486              :     {
    5487     17986032 :     default:
    5488     17986032 :       if (VL_EXP_CLASS_P (t))
    5489       772277 :         u (VL_EXP_OPERAND_LENGTH (t));
    5490              :       break;
    5491              : 
    5492       824846 :     case INTEGER_CST:
    5493       824846 :       u (TREE_INT_CST_NUNITS (t));
    5494       824846 :       u (TREE_INT_CST_EXT_NUNITS (t));
    5495       824846 :       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        59937 :     case STRING_CST:
    5514        59937 :       str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
    5515        59937 :       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       136336 :     case TREE_BINFO:
    5547       136336 :       u (BINFO_N_BASE_BINFOS (t));
    5548       136336 :       break;
    5549              : 
    5550      1109890 :     case TREE_VEC:
    5551      1109890 :       u (TREE_VEC_LENGTH (t));
    5552      1109890 :       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     20117116 :       break;
    5565              :     }
    5566     20117116 : }
    5567              : 
    5568              : /* Start tree read.  Allocate the receiving node.  */
    5569              : 
    5570              : tree
    5571     15638960 : trees_in::start (unsigned code)
    5572              : {
    5573     15638960 :   tree t = NULL_TREE;
    5574              : 
    5575     15638960 :   if (!code)
    5576     14176021 :     code = u ();
    5577              : 
    5578     15638960 :   switch (code)
    5579              :     {
    5580     14030971 :     default:
    5581     14030971 :       if (code >= MAX_TREE_CODES)
    5582              :         {
    5583            0 :         fail:
    5584            0 :           set_overrun ();
    5585            0 :           return NULL_TREE;
    5586              :         }
    5587     14030971 :       else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
    5588              :         {
    5589       611203 :           unsigned ops = u ();
    5590       611203 :           t = build_vl_exp (tree_code (code), ops);
    5591              :         }
    5592              :       else
    5593     13419768 :         t = make_node (tree_code (code));
    5594              :       break;
    5595              : 
    5596       602610 :     case INTEGER_CST:
    5597       602610 :       {
    5598       602610 :         unsigned n = u ();
    5599       602610 :         unsigned e = u ();
    5600       602610 :         t = make_int_cst (n, e);
    5601              :       }
    5602       602610 :       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        50829 :     case STRING_CST:
    5627        50829 :       {
    5628        50829 :         size_t l;
    5629        50829 :         const char *chars = str (&l);
    5630        50829 :         t = build_string (l, chars);
    5631              :       }
    5632        50829 :       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        92217 :     case TREE_BINFO:
    5661        92217 :       t = make_tree_binfo (u ());
    5662        92217 :       break;
    5663              : 
    5664       862258 :     case TREE_VEC:
    5665       862258 :       t = make_tree_vec (u ());
    5666       862258 :       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       759664 : get_importer_interface (tree decl)
    5693              : {
    5694              :   /* Internal linkage entities must be emitted in each importer if
    5695              :      there is a definition available.  */
    5696       759664 :   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       359305 :   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       352861 :   if (DECL_LANG_SPECIFIC (decl)
    5710       352861 :       && DECL_EXPLICIT_INSTANTIATION (decl))
    5711        26490 :     return (header_module_p () && !DECL_EXTERNAL (decl)
    5712        26490 :             ? importer_interface::always_emit
    5713              :             : importer_interface::external);
    5714              : 
    5715              :   /* A gnu_inline function is never emitted in any TU.  */
    5716       326371 :   if (TREE_CODE (decl) == FUNCTION_DECL
    5717       238947 :       && DECL_DECLARED_INLINE_P (decl)
    5718       557398 :       && 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     20152260 : 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     20152260 :   tree_code code = TREE_CODE (t);
    5741              : 
    5742     20152260 :   WB (t->base.side_effects_flag);
    5743     20152260 :   WB (t->base.constant_flag);
    5744     20152260 :   WB (t->base.addressable_flag);
    5745     20152260 :   WB (t->base.volatile_flag);
    5746     20152260 :   WB (t->base.readonly_flag);
    5747              :   /* base.asm_written_flag is a property of the current TU's use of
    5748              :      this decl.  */
    5749     20152260 :   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     20152260 :   WB (t->base.nothrow_flag);
    5755     20152260 :   WB (t->base.static_flag);
    5756              :   /* This is TYPE_CACHED_VALUES_P for types.  */
    5757     20152260 :   WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5758     20152260 :   WB (t->base.private_flag);
    5759     20152260 :   WB (t->base.protected_flag);
    5760     20152260 :   WB (t->base.deprecated_flag);
    5761     20152260 :   WB (t->base.default_def_flag);
    5762              : 
    5763     20152260 :   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     17463366 :     default:
    5774     17463366 :       WB (t->base.u.bits.lang_flag_0);
    5775     17463366 :       bool flag_1 = t->base.u.bits.lang_flag_1;
    5776     17463366 :       if (!flag_1)
    5777              :         ;
    5778       571007 :       else if (code == TEMPLATE_INFO)
    5779              :         /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader.  */
    5780              :         flag_1 = false;
    5781       564775 :       else if (code == VAR_DECL)
    5782              :         {
    5783              :           /* This is DECL_INITIALIZED_P.  */
    5784       103638 :           if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    5785              :             /* We'll set this when reading the definition.  */
    5786     17463366 :             flag_1 = false;
    5787              :         }
    5788     17463366 :       WB (flag_1);
    5789     17463366 :       WB (t->base.u.bits.lang_flag_2);
    5790     17463366 :       WB (t->base.u.bits.lang_flag_3);
    5791     17463366 :       WB (t->base.u.bits.lang_flag_4);
    5792     17463366 :       WB (t->base.u.bits.lang_flag_5);
    5793     17463366 :       WB (t->base.u.bits.lang_flag_6);
    5794     17463366 :       WB (t->base.u.bits.saturating_flag);
    5795     17463366 :       WB (t->base.u.bits.unsigned_flag);
    5796     17463366 :       WB (t->base.u.bits.packed_flag);
    5797     17463366 :       WB (t->base.u.bits.user_align);
    5798     17463366 :       WB (t->base.u.bits.nameless_flag);
    5799     17463366 :       WB (t->base.u.bits.atomic_flag);
    5800     17463366 :       WB (t->base.u.bits.unavailable_flag);
    5801     17463366 :       break;
    5802              :     }
    5803              : 
    5804     17463366 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5805              :     {
    5806       807769 :       WB (t->type_common.no_force_blk_flag);
    5807       807769 :       WB (t->type_common.needs_constructing_flag);
    5808       807769 :       WB (t->type_common.transparent_aggr_flag);
    5809       807769 :       WB (t->type_common.restrict_flag);
    5810       807769 :       WB (t->type_common.string_flag);
    5811       807769 :       WB (t->type_common.lang_flag_0);
    5812       807769 :       WB (t->type_common.lang_flag_1);
    5813       807769 :       WB (t->type_common.lang_flag_2);
    5814       807769 :       WB (t->type_common.lang_flag_3);
    5815       807769 :       WB (t->type_common.lang_flag_4);
    5816       807769 :       WB (t->type_common.lang_flag_5);
    5817       807769 :       WB (t->type_common.lang_flag_6);
    5818       807769 :       WB (t->type_common.typeless_storage);
    5819              :     }
    5820              : 
    5821     17463366 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5822              :     return;
    5823              : 
    5824      4372566 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5825              :     {
    5826      4372566 :       WB (t->decl_common.nonlocal_flag);
    5827      4372566 :       WB (t->decl_common.virtual_flag);
    5828      4372566 :       WB (t->decl_common.ignored_flag);
    5829      4372566 :       WB (t->decl_common.abstract_flag);
    5830      4372566 :       WB (t->decl_common.artificial_flag);
    5831      4372566 :       WB (t->decl_common.preserve_flag);
    5832      4372566 :       WB (t->decl_common.debug_expr_is_from);
    5833      4372566 :       WB (t->decl_common.lang_flag_0);
    5834      4372566 :       WB (t->decl_common.lang_flag_1);
    5835      4372566 :       WB (t->decl_common.lang_flag_2);
    5836      4372566 :       WB (t->decl_common.lang_flag_3);
    5837      4372566 :       WB (t->decl_common.lang_flag_4);
    5838              : 
    5839      4372566 :       {
    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      4372566 :         bool interface_known = t->decl_common.lang_flag_5;
    5844      4372566 :         if (interface_known
    5845      4372566 :             && get_importer_interface (t) == importer_interface::unknown)
    5846              :           interface_known = false;
    5847      4372566 :         WB (interface_known);
    5848              :       }
    5849              : 
    5850      4372566 :       WB (t->decl_common.lang_flag_6);
    5851      4372566 :       WB (t->decl_common.lang_flag_7);
    5852      4372566 :       WB (t->decl_common.lang_flag_8);
    5853      4372566 :       WB (t->decl_common.decl_flag_0);
    5854              : 
    5855      4372566 :       {
    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      4372566 :         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      4372566 :         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      4372171 :         if (!is_external
    5869      3674710 :             && VAR_OR_FUNCTION_DECL_P (t)
    5870      4720398 :             && get_importer_interface (t) == importer_interface::external)
    5871              :           is_external = true;
    5872      4372566 :         WB (is_external);
    5873              :       }
    5874              : 
    5875      4372566 :       WB (t->decl_common.decl_flag_2);
    5876      4372566 :       WB (t->decl_common.decl_flag_3);
    5877      4372566 :       WB (t->decl_common.not_gimple_reg_flag);
    5878      4372566 :       WB (t->decl_common.decl_by_reference_flag);
    5879      4372566 :       WB (t->decl_common.decl_read_flag);
    5880      4372566 :       WB (t->decl_common.decl_nonshareable_flag);
    5881      4372566 :       WB (t->decl_common.decl_not_flexarray);
    5882              :     }
    5883              :   else
    5884              :     return;
    5885              : 
    5886      4372566 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5887              :     {
    5888      2060654 :       WB (t->decl_with_vis.defer_output);
    5889      2060654 :       WB (t->decl_with_vis.hard_register);
    5890      2060654 :       WB (t->decl_with_vis.common_flag);
    5891      2060654 :       WB (t->decl_with_vis.in_text_section);
    5892      2060654 :       WB (t->decl_with_vis.in_constant_pool);
    5893      2060654 :       WB (t->decl_with_vis.dllimport_flag);
    5894      2060654 :       WB (t->decl_with_vis.weak_flag);
    5895      2060654 :       WB (t->decl_with_vis.seen_in_bind_expr);
    5896      2060654 :       WB (t->decl_with_vis.comdat_flag);
    5897      2060654 :       WB (t->decl_with_vis.visibility_specified);
    5898      2060654 :       WB (t->decl_with_vis.init_priority_p);
    5899      2060654 :       WB (t->decl_with_vis.shadowed_for_var_p);
    5900      2060654 :       WB (t->decl_with_vis.cxx_constructor);
    5901      2060654 :       WB (t->decl_with_vis.cxx_destructor);
    5902      2060654 :       WB (t->decl_with_vis.final);
    5903      2060654 :       WB (t->decl_with_vis.regdecl_flag);
    5904              :     }
    5905              :   else
    5906              :     return;
    5907              : 
    5908      2060654 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5909              :     {
    5910       604739 :       WB (t->function_decl.static_ctor_flag);
    5911       604739 :       WB (t->function_decl.static_dtor_flag);
    5912       604739 :       WB (t->function_decl.uninlinable);
    5913       604739 :       WB (t->function_decl.possibly_inlined);
    5914       604739 :       WB (t->function_decl.novops_flag);
    5915       604739 :       WB (t->function_decl.returns_twice_flag);
    5916       604739 :       WB (t->function_decl.malloc_flag);
    5917       604739 :       WB (t->function_decl.declared_inline_flag);
    5918       604739 :       WB (t->function_decl.no_inline_warning_flag);
    5919       604739 :       WB (t->function_decl.no_instrument_function_entry_exit);
    5920       604739 :       WB (t->function_decl.no_limit_stack);
    5921       604739 :       WB (t->function_decl.disregard_inline_limits);
    5922       604739 :       WB (t->function_decl.pure_flag);
    5923       604739 :       WB (t->function_decl.looping_const_or_pure_flag);
    5924              : 
    5925       604739 :       WB (t->function_decl.has_debug_args_flag);
    5926       604739 :       WB (t->function_decl.versioned_function);
    5927       604739 :       WB (t->function_decl.replaceable_operator);
    5928              : 
    5929              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5930       604739 :       unsigned kind = (unsigned)t->function_decl.decl_type;
    5931       604739 :       WB ((kind >> 0) & 1);
    5932       604739 :       WB ((kind >> 1) & 1);
    5933              :     }
    5934              : #undef WB_IF
    5935              : #undef WB
    5936              : }
    5937              : 
    5938              : bool
    5939     15660966 : 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     15660966 :   tree_code code = TREE_CODE (t);
    5946              : 
    5947     15660966 :   RB (t->base.side_effects_flag);
    5948     15660966 :   RB (t->base.constant_flag);
    5949     15660966 :   RB (t->base.addressable_flag);
    5950     15660966 :   RB (t->base.volatile_flag);
    5951     15660966 :   RB (t->base.readonly_flag);
    5952              :   /* base.asm_written_flag is not streamed.  */
    5953     15660966 :   RB (t->base.nowarning_flag);
    5954              :   /* base.visited is not streamed.  */
    5955              :   /* base.used_flag is not streamed.  */
    5956     15660966 :   RB (t->base.nothrow_flag);
    5957     15660966 :   RB (t->base.static_flag);
    5958     15660966 :   RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5959     15660966 :   RB (t->base.private_flag);
    5960     15660966 :   RB (t->base.protected_flag);
    5961     15660966 :   RB (t->base.deprecated_flag);
    5962     15660966 :   RB (t->base.default_def_flag);
    5963              : 
    5964     15660966 :   switch (code)
    5965              :     {
    5966      2062368 :     case CALL_EXPR:
    5967      2062368 :     case INTEGER_CST:
    5968      2062368 :     case SSA_NAME:
    5969      2062368 :     case TARGET_MEM_REF:
    5970      2062368 :     case TREE_VEC:
    5971              :       /* These use different base.u fields.  */
    5972      2062368 :       goto done;
    5973              : 
    5974     13598598 :     default:
    5975     13598598 :       RB (t->base.u.bits.lang_flag_0);
    5976     13598598 :       RB (t->base.u.bits.lang_flag_1);
    5977     13598598 :       RB (t->base.u.bits.lang_flag_2);
    5978     13598598 :       RB (t->base.u.bits.lang_flag_3);
    5979     13598598 :       RB (t->base.u.bits.lang_flag_4);
    5980     13598598 :       RB (t->base.u.bits.lang_flag_5);
    5981     13598598 :       RB (t->base.u.bits.lang_flag_6);
    5982     13598598 :       RB (t->base.u.bits.saturating_flag);
    5983     13598598 :       RB (t->base.u.bits.unsigned_flag);
    5984     13598598 :       RB (t->base.u.bits.packed_flag);
    5985     13598598 :       RB (t->base.u.bits.user_align);
    5986     13598598 :       RB (t->base.u.bits.nameless_flag);
    5987     13598598 :       RB (t->base.u.bits.atomic_flag);
    5988     13598598 :       RB (t->base.u.bits.unavailable_flag);
    5989     13598598 :       break;
    5990              :     }
    5991              : 
    5992     13598598 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5993              :     {
    5994       589988 :       RB (t->type_common.no_force_blk_flag);
    5995       589988 :       RB (t->type_common.needs_constructing_flag);
    5996       589988 :       RB (t->type_common.transparent_aggr_flag);
    5997       589988 :       RB (t->type_common.restrict_flag);
    5998       589988 :       RB (t->type_common.string_flag);
    5999       589988 :       RB (t->type_common.lang_flag_0);
    6000       589988 :       RB (t->type_common.lang_flag_1);
    6001       589988 :       RB (t->type_common.lang_flag_2);
    6002       589988 :       RB (t->type_common.lang_flag_3);
    6003       589988 :       RB (t->type_common.lang_flag_4);
    6004       589988 :       RB (t->type_common.lang_flag_5);
    6005       589988 :       RB (t->type_common.lang_flag_6);
    6006       589988 :       RB (t->type_common.typeless_storage);
    6007              :     }
    6008              : 
    6009     13598598 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    6010     10258142 :     goto done;
    6011              : 
    6012      3340456 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6013              :     {
    6014      3340456 :       RB (t->decl_common.nonlocal_flag);
    6015      3340456 :       RB (t->decl_common.virtual_flag);
    6016      3340456 :       RB (t->decl_common.ignored_flag);
    6017      3340456 :       RB (t->decl_common.abstract_flag);
    6018      3340456 :       RB (t->decl_common.artificial_flag);
    6019      3340456 :       RB (t->decl_common.preserve_flag);
    6020      3340456 :       RB (t->decl_common.debug_expr_is_from);
    6021      3340456 :       RB (t->decl_common.lang_flag_0);
    6022      3340456 :       RB (t->decl_common.lang_flag_1);
    6023      3340456 :       RB (t->decl_common.lang_flag_2);
    6024      3340456 :       RB (t->decl_common.lang_flag_3);
    6025      3340456 :       RB (t->decl_common.lang_flag_4);
    6026      3340456 :       RB (t->decl_common.lang_flag_5);
    6027      3340456 :       RB (t->decl_common.lang_flag_6);
    6028      3340456 :       RB (t->decl_common.lang_flag_7);
    6029      3340456 :       RB (t->decl_common.lang_flag_8);
    6030      3340456 :       RB (t->decl_common.decl_flag_0);
    6031      3340456 :       RB (t->decl_common.decl_flag_1);
    6032      3340456 :       RB (t->decl_common.decl_flag_2);
    6033      3340456 :       RB (t->decl_common.decl_flag_3);
    6034      3340456 :       RB (t->decl_common.not_gimple_reg_flag);
    6035      3340456 :       RB (t->decl_common.decl_by_reference_flag);
    6036      3340456 :       RB (t->decl_common.decl_read_flag);
    6037      3340456 :       RB (t->decl_common.decl_nonshareable_flag);
    6038      3340456 :       RB (t->decl_common.decl_not_flexarray);
    6039              :     }
    6040              :   else
    6041            0 :     goto done;
    6042              : 
    6043      3340456 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6044              :     {
    6045      1538235 :       RB (t->decl_with_vis.defer_output);
    6046      1538235 :       RB (t->decl_with_vis.hard_register);
    6047      1538235 :       RB (t->decl_with_vis.common_flag);
    6048      1538235 :       RB (t->decl_with_vis.in_text_section);
    6049      1538235 :       RB (t->decl_with_vis.in_constant_pool);
    6050      1538235 :       RB (t->decl_with_vis.dllimport_flag);
    6051      1538235 :       RB (t->decl_with_vis.weak_flag);
    6052      1538235 :       RB (t->decl_with_vis.seen_in_bind_expr);
    6053      1538235 :       RB (t->decl_with_vis.comdat_flag);
    6054      1538235 :       RB (t->decl_with_vis.visibility_specified);
    6055      1538235 :       RB (t->decl_with_vis.init_priority_p);
    6056      1538235 :       RB (t->decl_with_vis.shadowed_for_var_p);
    6057      1538235 :       RB (t->decl_with_vis.cxx_constructor);
    6058      1538235 :       RB (t->decl_with_vis.cxx_destructor);
    6059      1538235 :       RB (t->decl_with_vis.final);
    6060      1538235 :       RB (t->decl_with_vis.regdecl_flag);
    6061              :     }
    6062              :   else
    6063      1802221 :     goto done;
    6064              : 
    6065      1538235 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    6066              :     {
    6067       471371 :       RB (t->function_decl.static_ctor_flag);
    6068       471371 :       RB (t->function_decl.static_dtor_flag);
    6069       471371 :       RB (t->function_decl.uninlinable);
    6070       471371 :       RB (t->function_decl.possibly_inlined);
    6071       471371 :       RB (t->function_decl.novops_flag);
    6072       471371 :       RB (t->function_decl.returns_twice_flag);
    6073       471371 :       RB (t->function_decl.malloc_flag);
    6074       471371 :       RB (t->function_decl.declared_inline_flag);
    6075       471371 :       RB (t->function_decl.no_inline_warning_flag);
    6076       471371 :       RB (t->function_decl.no_instrument_function_entry_exit);
    6077       471371 :       RB (t->function_decl.no_limit_stack);
    6078       471371 :       RB (t->function_decl.disregard_inline_limits);
    6079       471371 :       RB (t->function_decl.pure_flag);
    6080       471371 :       RB (t->function_decl.looping_const_or_pure_flag);
    6081              : 
    6082       471371 :       RB (t->function_decl.has_debug_args_flag);
    6083       471371 :       RB (t->function_decl.versioned_function);
    6084       471371 :       RB (t->function_decl.replaceable_operator);
    6085              : 
    6086              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    6087       471371 :       unsigned kind = 0;
    6088       471371 :       kind |= unsigned (bits.b ()) << 0;
    6089       471371 :       kind |= unsigned (bits.b ()) << 1;
    6090       471371 :       t->function_decl.decl_type = function_decl_type (kind);
    6091              :     }
    6092              : #undef RB_IF
    6093              : #undef RB
    6094      1066864 : done:
    6095     15660966 :   return !get_overrun ();
    6096              : }
    6097              : 
    6098              : void
    6099      2664096 : trees_out::lang_decl_bools (tree t, bits_out& bits)
    6100              : {
    6101              : #define WB(X) (bits.b (X))
    6102      2664096 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6103              : 
    6104      2664096 :   bits.bflush ();
    6105      2664096 :   WB (lang->u.base.language == lang_cplusplus);
    6106      2664096 :   WB ((lang->u.base.use_template >> 0) & 1);
    6107      2664096 :   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      2664096 :   WB (lang->u.base.initialized_in_class);
    6111              : 
    6112      2664096 :   WB (lang->u.base.threadprivate_or_deleted_p);
    6113      2664096 :   WB (lang->u.base.anticipated_p);
    6114      2664096 :   WB (lang->u.base.friend_or_tls);
    6115      2664096 :   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      2664096 :   WB (lang->u.base.concept_p);
    6119      2664096 :   WB (lang->u.base.var_declared_inline_p);
    6120      2664096 :   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      5241285 :   WB (lang->u.base.module_purview_p && !header_module_p ());
    6126      2664096 :   WB (lang->u.base.module_attach_p);
    6127              :   /* Importer will set module_import_p and module_entity_p themselves
    6128              :      as appropriate.  */
    6129      2664096 :   WB (lang->u.base.module_keyed_decls_p);
    6130              : 
    6131      2664096 :   WB (lang->u.base.omp_declare_mapper_p);
    6132              : 
    6133      2664096 :   switch (lang->u.base.selector)
    6134              :     {
    6135            0 :     default:
    6136            0 :       gcc_unreachable ();
    6137              : 
    6138       604739 :     case lds_fn:  /* lang_decl_fn.  */
    6139       604739 :       WB (lang->u.fn.global_ctor_p);
    6140       604739 :       WB (lang->u.fn.global_dtor_p);
    6141              : 
    6142       604739 :       WB (lang->u.fn.static_function);
    6143       604739 :       WB (lang->u.fn.pure_virtual);
    6144       604739 :       WB (lang->u.fn.defaulted_p);
    6145       604739 :       WB (lang->u.fn.has_in_charge_parm_p);
    6146       604739 :       WB (lang->u.fn.has_vtt_parm_p);
    6147              :       /* There shouldn't be a pending inline at this point.  */
    6148       604739 :       gcc_assert (!lang->u.fn.pending_inline_p);
    6149       604739 :       WB (lang->u.fn.nonconverting);
    6150       604739 :       WB (lang->u.fn.thunk_p);
    6151              : 
    6152       604739 :       WB (lang->u.fn.this_thunk_p);
    6153       604739 :       WB (lang->u.fn.omp_declare_reduction_p);
    6154       604739 :       WB (lang->u.fn.has_dependent_explicit_spec_p);
    6155       604739 :       WB (lang->u.fn.immediate_fn_p);
    6156       604739 :       WB (lang->u.fn.maybe_deleted);
    6157       604739 :       WB (lang->u.fn.coroutine_p);
    6158       604739 :       WB (lang->u.fn.implicit_constexpr);
    6159       604739 :       WB (lang->u.fn.escalated_p);
    6160       604739 :       WB (lang->u.fn.xobj_func);
    6161       604739 :       goto lds_min;
    6162              : 
    6163         3181 :     case lds_decomp:  /* lang_decl_decomp.  */
    6164              :       /* No bools.  */
    6165         3181 :       goto lds_min;
    6166              : 
    6167              :     case lds_min:  /* lang_decl_min.  */
    6168      2664096 :     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      2664096 : }
    6182              : 
    6183              : bool
    6184      2092882 : trees_in::lang_decl_bools (tree t, bits_in& bits)
    6185              : {
    6186              : #define RB(X) ((X) = bits.b ())
    6187      2092882 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6188              : 
    6189      2092882 :   bits.bflush ();
    6190      2092882 :   lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
    6191      2092882 :   unsigned v;
    6192      2092882 :   v = bits.b () << 0;
    6193      2092882 :   v |= bits.b () << 1;
    6194      2092882 :   lang->u.base.use_template = v;
    6195              :   /* lang->u.base.not_really_extern is not streamed.  */
    6196      2092882 :   RB (lang->u.base.initialized_in_class);
    6197              : 
    6198      2092882 :   RB (lang->u.base.threadprivate_or_deleted_p);
    6199      2092882 :   RB (lang->u.base.anticipated_p);
    6200      2092882 :   RB (lang->u.base.friend_or_tls);
    6201      2092882 :   RB (lang->u.base.unknown_bound_p);
    6202              :   /* lang->u.base.odr_used is not streamed.  */
    6203      2092882 :   RB (lang->u.base.concept_p);
    6204      2092882 :   RB (lang->u.base.var_declared_inline_p);
    6205      2092882 :   RB (lang->u.base.dependent_init_p);
    6206              : 
    6207      2092882 :   RB (lang->u.base.module_purview_p);
    6208      2092882 :   RB (lang->u.base.module_attach_p);
    6209              :   /* module_import_p and module_entity_p are not streamed.  */
    6210      2092882 :   RB (lang->u.base.module_keyed_decls_p);
    6211              : 
    6212      2092882 :   RB (lang->u.base.omp_declare_mapper_p);
    6213              : 
    6214      2092882 :   switch (lang->u.base.selector)
    6215              :     {
    6216            0 :     default:
    6217            0 :       gcc_unreachable ();
    6218              : 
    6219       471371 :     case lds_fn:  /* lang_decl_fn.  */
    6220       471371 :       RB (lang->u.fn.global_ctor_p);
    6221       471371 :       RB (lang->u.fn.global_dtor_p);
    6222              : 
    6223       471371 :       RB (lang->u.fn.static_function);
    6224       471371 :       RB (lang->u.fn.pure_virtual);
    6225       471371 :       RB (lang->u.fn.defaulted_p);
    6226       471371 :       RB (lang->u.fn.has_in_charge_parm_p);
    6227       471371 :       RB (lang->u.fn.has_vtt_parm_p);
    6228              :       /* lang->u.f.n.pending_inline_p is not streamed.  */
    6229       471371 :       RB (lang->u.fn.nonconverting);
    6230       471371 :       RB (lang->u.fn.thunk_p);
    6231              : 
    6232       471371 :       RB (lang->u.fn.this_thunk_p);
    6233       471371 :       RB (lang->u.fn.omp_declare_reduction_p);
    6234       471371 :       RB (lang->u.fn.has_dependent_explicit_spec_p);
    6235       471371 :       RB (lang->u.fn.immediate_fn_p);
    6236       471371 :       RB (lang->u.fn.maybe_deleted);
    6237       471371 :       RB (lang->u.fn.coroutine_p);
    6238       471371 :       RB (lang->u.fn.implicit_constexpr);
    6239       471371 :       RB (lang->u.fn.escalated_p);
    6240       471371 :       RB (lang->u.fn.xobj_func);
    6241       471371 :       goto lds_min;
    6242              : 
    6243         3043 :     case lds_decomp:  /* lang_decl_decomp.  */
    6244              :       /* No bools.  */
    6245         3043 :       goto lds_min;
    6246              : 
    6247              :     case lds_min:  /* lang_decl_min.  */
    6248      2092882 :     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      2092882 :   return !get_overrun ();
    6262              : }
    6263              : 
    6264              : void
    6265       210724 : trees_out::lang_type_bools (tree t, bits_out& bits)
    6266              : {
    6267              : #define WB(X) (bits.b (X))
    6268       210724 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6269              : 
    6270       210724 :   bits.bflush ();
    6271       210724 :   WB (lang->has_type_conversion);
    6272       210724 :   WB (lang->has_copy_ctor);
    6273       210724 :   WB (lang->has_default_ctor);
    6274       210724 :   WB (lang->const_needs_init);
    6275       210724 :   WB (lang->ref_needs_init);
    6276       210724 :   WB (lang->has_const_copy_assign);
    6277       210724 :   WB ((lang->use_template >> 0) & 1);
    6278       210724 :   WB ((lang->use_template >> 1) & 1);
    6279              : 
    6280       210724 :   WB (lang->has_mutable);
    6281       210724 :   WB (lang->com_interface);
    6282       210724 :   WB (lang->non_pod_class);
    6283       210724 :   WB (lang->nearly_empty_p);
    6284       210724 :   WB (lang->user_align);
    6285       210724 :   WB (lang->has_copy_assign);
    6286       210724 :   WB (lang->has_new);
    6287       210724 :   WB (lang->has_array_new);
    6288              : 
    6289       210724 :   WB ((lang->gets_delete >> 0) & 1);
    6290       210724 :   WB ((lang->gets_delete >> 1) & 1);
    6291       210724 :   WB (lang->interface_only);
    6292       210724 :   WB (lang->interface_unknown);
    6293       210724 :   WB (lang->contains_empty_class_p);
    6294       210724 :   WB (lang->anon_aggr);
    6295       210724 :   WB (lang->non_zero_init);
    6296       210724 :   WB (lang->empty_p);
    6297              : 
    6298       210724 :   WB (lang->vec_new_uses_cookie);
    6299       210724 :   WB (lang->declared_class);
    6300       210724 :   WB (lang->diamond_shaped);
    6301       210724 :   WB (lang->repeated_base);
    6302       210724 :   gcc_checking_assert (!lang->being_defined);
    6303              :   // lang->debug_requested
    6304       210724 :   WB (lang->fields_readonly);
    6305       210724 :   WB (lang->ptrmemfunc_flag);
    6306              : 
    6307       210724 :   WB (lang->lazy_default_ctor);
    6308       210724 :   WB (lang->lazy_copy_ctor);
    6309       210724 :   WB (lang->lazy_copy_assign);
    6310       210724 :   WB (lang->lazy_destructor);
    6311       210724 :   WB (lang->has_const_copy_ctor);
    6312       210724 :   WB (lang->has_complex_copy_ctor);
    6313       210724 :   WB (lang->has_complex_copy_assign);
    6314       210724 :   WB (lang->non_aggregate);
    6315              : 
    6316       210724 :   WB (lang->has_complex_dflt);
    6317       210724 :   WB (lang->has_list_ctor);
    6318       210724 :   WB (lang->non_std_layout);
    6319       210724 :   WB (lang->is_literal);
    6320       210724 :   WB (lang->lazy_move_ctor);
    6321       210724 :   WB (lang->lazy_move_assign);
    6322       210724 :   WB (lang->has_complex_move_ctor);
    6323       210724 :   WB (lang->has_complex_move_assign);
    6324              : 
    6325       210724 :   WB (lang->has_constexpr_ctor);
    6326       210724 :   WB (lang->unique_obj_representations);
    6327       210724 :   WB (lang->unique_obj_representations_set);
    6328       210724 :   gcc_checking_assert (!lang->erroneous);
    6329       210724 :   WB (lang->non_pod_aggregate);
    6330       210724 :   WB (lang->non_aggregate_pod);
    6331              : #undef WB
    6332       210724 : }
    6333              : 
    6334              : bool
    6335       155199 : trees_in::lang_type_bools (tree t, bits_in& bits)
    6336              : {
    6337              : #define RB(X) ((X) = bits.b ())
    6338       155199 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6339              : 
    6340       155199 :   bits.bflush ();
    6341       155199 :   RB (lang->has_type_conversion);
    6342       155199 :   RB (lang->has_copy_ctor);
    6343       155199 :   RB (lang->has_default_ctor);
    6344       155199 :   RB (lang->const_needs_init);
    6345       155199 :   RB (lang->ref_needs_init);
    6346       155199 :   RB (lang->has_const_copy_assign);
    6347       155199 :   unsigned v;
    6348       155199 :   v = bits.b () << 0;
    6349       155199 :   v |= bits.b () << 1;
    6350       155199 :   lang->use_template = v;
    6351              : 
    6352       155199 :   RB (lang->has_mutable);
    6353       155199 :   RB (lang->com_interface);
    6354       155199 :   RB (lang->non_pod_class);
    6355       155199 :   RB (lang->nearly_empty_p);
    6356       155199 :   RB (lang->user_align);
    6357       155199 :   RB (lang->has_copy_assign);
    6358       155199 :   RB (lang->has_new);
    6359       155199 :   RB (lang->has_array_new);
    6360              : 
    6361       155199 :   v = bits.b () << 0;
    6362       155199 :   v |= bits.b () << 1;
    6363       155199 :   lang->gets_delete = v;
    6364       155199 :   RB (lang->interface_only);
    6365       155199 :   RB (lang->interface_unknown);
    6366       155199 :   RB (lang->contains_empty_class_p);
    6367       155199 :   RB (lang->anon_aggr);
    6368       155199 :   RB (lang->non_zero_init);
    6369       155199 :   RB (lang->empty_p);
    6370              : 
    6371       155199 :   RB (lang->vec_new_uses_cookie);
    6372       155199 :   RB (lang->declared_class);
    6373       155199 :   RB (lang->diamond_shaped);
    6374       155199 :   RB (lang->repeated_base);
    6375       155199 :   gcc_checking_assert (!lang->being_defined);
    6376       155199 :   gcc_checking_assert (!lang->debug_requested);
    6377       155199 :   RB (lang->fields_readonly);
    6378       155199 :   RB (lang->ptrmemfunc_flag);
    6379              : 
    6380       155199 :   RB (lang->lazy_default_ctor);
    6381       155199 :   RB (lang->lazy_copy_ctor);
    6382       155199 :   RB (lang->lazy_copy_assign);
    6383       155199 :   RB (lang->lazy_destructor);
    6384       155199 :   RB (lang->has_const_copy_ctor);
    6385       155199 :   RB (lang->has_complex_copy_ctor);
    6386       155199 :   RB (lang->has_complex_copy_assign);
    6387       155199 :   RB (lang->non_aggregate);
    6388              : 
    6389       155199 :   RB (lang->has_complex_dflt);
    6390       155199 :   RB (lang->has_list_ctor);
    6391       155199 :   RB (lang->non_std_layout);
    6392       155199 :   RB (lang->is_literal);
    6393       155199 :   RB (lang->lazy_move_ctor);
    6394       155199 :   RB (lang->lazy_move_assign);
    6395       155199 :   RB (lang->has_complex_move_ctor);
    6396       155199 :   RB (lang->has_complex_move_assign);
    6397              : 
    6398       155199 :   RB (lang->has_constexpr_ctor);
    6399       155199 :   RB (lang->unique_obj_representations);
    6400       155199 :   RB (lang->unique_obj_representations_set);
    6401       155199 :   gcc_checking_assert (!lang->erroneous);
    6402       155199 :   RB (lang->non_pod_aggregate);
    6403       155199 :   RB (lang->non_aggregate_pod);
    6404              : #undef RB
    6405       155199 :   return !get_overrun ();
    6406              : }
    6407              : 
    6408              : /* Read & write the core values and pointers.  */
    6409              : 
    6410              : void
    6411     52210367 : trees_out::core_vals (tree t)
    6412              : {
    6413              : #define WU(X) (u (X))
    6414              : #define WT(X) (tree_node (X))
    6415     52210367 :   tree_code code = TREE_CODE (t);
    6416              : 
    6417              :   /* First by shape of the tree.  */
    6418              : 
    6419     52210367 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    6420              :     {
    6421              :       /* Write this early, for better log information.  */
    6422     11465454 :       WT (t->decl_minimal.name);
    6423     11465454 :       if (!DECL_TEMPLATE_PARM_P (t))
    6424      8858610 :         WT (t->decl_minimal.context);
    6425              : 
    6426     11465454 :       if (state)
    6427      9434184 :         state->write_location (*this, t->decl_minimal.locus);
    6428              : 
    6429     11465454 :       if (streaming_p ())
    6430      4372566 :         if (has_warning_spec (t))
    6431          903 :           u (get_warning_spec (t));
    6432              :     }
    6433              : 
    6434     52210367 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    6435              :     {
    6436              :       /* The only types we write also have TYPE_NON_COMMON.  */
    6437      2831318 :       gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
    6438              : 
    6439              :       /* We only stream the main variant.  */
    6440      2831318 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    6441              : 
    6442              :       /* Stream the name & context first, for better log information  */
    6443      2831318 :       WT (t->type_common.name);
    6444      2831318 :       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      2831318 :       WT (t->type_common.main_variant);
    6450              : 
    6451      2831318 :       tree canonical = t->type_common.canonical;
    6452      2831318 :       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      2831318 :       WT (canonical);
    6457              : 
    6458              :       /* type_common.next_variant is internally manipulated.  */
    6459              :       /* type_common.pointer_to, type_common.reference_to.  */
    6460              : 
    6461      2831318 :       if (streaming_p ())
    6462              :         {
    6463       772625 :           WU (t->type_common.precision);
    6464       772625 :           WU (t->type_common.contains_placeholder_bits);
    6465       772625 :           WU (t->type_common.mode);
    6466       772625 :           WU (t->type_common.align);
    6467              :         }
    6468              : 
    6469      2831318 :       if (!RECORD_OR_UNION_CODE_P (code))
    6470              :         {
    6471      2349826 :           WT (t->type_common.size);
    6472      2349826 :           WT (t->type_common.size_unit);
    6473              :         }
    6474      2831318 :       WT (t->type_common.attributes);
    6475              : 
    6476      2831318 :       WT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    6477              :     }
    6478              : 
    6479     52210367 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6480              :     {
    6481     11465454 :       if (streaming_p ())
    6482              :         {
    6483      4372566 :           WU (t->decl_common.mode);
    6484      4372566 :           WU (t->decl_common.off_align);
    6485      4372566 :           WU (t->decl_common.align);
    6486              :         }
    6487              : 
    6488              :       /* For templates these hold instantiation (partial and/or
    6489              :          specialization) information.  */
    6490     11465454 :       if (code != TEMPLATE_DECL)
    6491              :         {
    6492     10597207 :           WT (t->decl_common.size);
    6493     10597207 :           WT (t->decl_common.size_unit);
    6494              :         }
    6495              : 
    6496     11465454 :       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     11465454 :       WT (t->decl_common.abstract_origin);
    6503              :     }
    6504              : 
    6505     52210367 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6506              :     {
    6507      5426675 :       WT (t->decl_with_vis.assembler_name);
    6508      5426675 :       if (streaming_p ())
    6509      2060654 :         WU (t->decl_with_vis.visibility);
    6510              :     }
    6511              : 
    6512     52210367 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6513              :     {
    6514      2831318 :       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         9596 :           WT (t->type_non_common.maxval);
    6520         9596 :           WT (t->type_non_common.minval);
    6521              :         }
    6522              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    6523              :          things.  */
    6524      2821722 :       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      2340230 :           gcc_checking_assert (code == TEMPLATE_TYPE_PARM
    6529              :                                || code == TEMPLATE_TEMPLATE_PARM
    6530              :                                || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    6531      2340230 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6532      2340230 :           WT (t->type_non_common.values);
    6533      2340230 :           WT (t->type_non_common.maxval);
    6534      2340230 :           WT (t->type_non_common.minval);
    6535              :         }
    6536              : 
    6537      2831318 :       WT (t->type_non_common.lang_1);
    6538              :     }
    6539              : 
    6540     52210367 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6541              :     {
    6542     15876044 :       if (state)
    6543     15562491 :         state->write_location (*this, t->exp.locus);
    6544              : 
    6545     15876044 :       if (streaming_p ())
    6546      7710269 :         if (has_warning_spec (t))
    6547       612246 :           u (get_warning_spec (t));
    6548              : 
    6549     15876044 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6550     15876044 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6551     15876044 :                         : TREE_OPERAND_LENGTH (t));
    6552     15876044 :       unsigned ix = unsigned (vl);
    6553     43740597 :       for (; ix != limit; ix++)
    6554     27864553 :         WT (TREE_OPERAND (t, ix));
    6555              :     }
    6556     36334323 :   else if (code == REQUIRES_EXPR)
    6557              :     {
    6558        23436 :       if (state)
    6559        19781 :         state->write_location (*this, REQUIRES_EXPR_LOCATION (t));
    6560              : 
    6561        23436 :       if (streaming_p ())
    6562         9650 :         if (has_warning_spec (t))
    6563            0 :           u (get_warning_spec (t));
    6564              : 
    6565        23436 :       chained_decls (REQUIRES_EXPR_PARMS (t));
    6566        23436 :       WT (REQUIRES_EXPR_REQS (t));
    6567        23436 :       WT (REQUIRES_EXPR_EXTRA_ARGS (t));
    6568              :     }
    6569              :   else
    6570              :     /* The CODE_CONTAINS tables were inaccurate when I started.  */
    6571     36310887 :     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     52210367 :   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      4792941 :     case INTEGER_CST:
    6608      4792941 :       if (streaming_p ())
    6609              :         {
    6610       824846 :           unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6611      1652708 :           for (unsigned ix = 0; ix != num; ix++)
    6612       827862 :             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        46740 :     case REAL_CST:
    6623        46740 :       if (streaming_p ())
    6624        23324 :         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       638565 :     case VAR_DECL:
    6654       638565 :       if (DECL_CONTEXT (t)
    6655       638565 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    6656              :         {
    6657       134101 :           if (DECL_HAS_VALUE_EXPR_P (t))
    6658           18 :             WT (DECL_VALUE_EXPR (t));
    6659              :           break;
    6660              :         }
    6661              :       /* FALLTHROUGH  */
    6662              : 
    6663      5228514 :     case RESULT_DECL:
    6664      5228514 :     case PARM_DECL:
    6665      5228514 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6666        50443 :         WT (DECL_VALUE_EXPR (t));
    6667              :       /* FALLTHROUGH  */
    6668              : 
    6669      5447814 :     case CONST_DECL:
    6670      5447814 :     case IMPORTED_DECL:
    6671      5447814 :       WT (t->decl_common.initial);
    6672      5447814 :       break;
    6673              : 
    6674       159571 :     case FIELD_DECL:
    6675       159571 :       WT (t->field_decl.offset);
    6676       159571 :       WT (t->field_decl.bit_field_type);
    6677       159571 :       {
    6678       159571 :         auto ovr = make_temp_override (walking_bit_field_unit, true);
    6679       159571 :         WT (t->field_decl.qualifier); /* bitfield unit.  */
    6680       159571 :       }
    6681       159571 :       WT (t->field_decl.bit_offset);
    6682       159571 :       WT (t->field_decl.fcontext);
    6683       159571 :       WT (t->decl_common.initial);
    6684       159571 :       break;
    6685              : 
    6686        51961 :     case LABEL_DECL:
    6687        51961 :       if (streaming_p ())
    6688              :         {
    6689        25979 :           WU (t->label_decl.label_decl_uid);
    6690        25979 :           WU (t->label_decl.eh_landing_pad_nr);
    6691              :         }
    6692              :       break;
    6693              : 
    6694      1209692 :     case FUNCTION_DECL:
    6695      1209692 :       if (streaming_p ())
    6696              :         {
    6697              :           /* Builtins can be streamed by value when a header declares
    6698              :              them.  */
    6699       604739 :           WU (DECL_BUILT_IN_CLASS (t));
    6700       604739 :           if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
    6701        11766 :             WU (DECL_UNCHECKED_FUNCTION_CODE (t));
    6702              :         }
    6703              : 
    6704      1209692 :       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      1209692 :       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      1209692 :       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      1209692 :       WT (t->function_decl.vindex);
    6714              : 
    6715      1209692 :       if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6716         8286 :         WT (lookup_explicit_specifier (t));
    6717              :       break;
    6718              : 
    6719       144757 :     case USING_DECL:
    6720              :       /* USING_DECL_DECLS  */
    6721       144757 :       WT (t->decl_common.initial);
    6722              :       /* FALLTHROUGH  */
    6723              : 
    6724      3578019 :     case TYPE_DECL:
    6725              :       /* USING_DECL: USING_DECL_SCOPE  */
    6726              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6727      3578019 :       WT (t->decl_non_common.result);
    6728      3578019 :       break;
    6729              : 
    6730              :       /* Miscellaneous common nodes.  */
    6731       796626 :     case BLOCK:
    6732       796626 :       if (state)
    6733              :         {
    6734       796626 :           state->write_location (*this, t->block.locus);
    6735       796626 :           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      1215942 :       for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
    6741              :         {
    6742       419316 :           if (VAR_OR_FUNCTION_DECL_P (decls)
    6743       419316 :               && DECL_LOCAL_DECL_P (decls))
    6744              :             {
    6745              :               /* Make sure this is the first encounter, and mark for
    6746              :                  walk-by-value.  */
    6747          312 :               gcc_checking_assert (!TREE_VISITED (decls)
    6748              :                                    && !DECL_TEMPLATE_INFO (decls));
    6749          312 :               mark_by_value (decls);
    6750              :             }
    6751       419316 :           tree_node (decls);
    6752              :         }
    6753       796626 :       tree_node (NULL_TREE);
    6754              : 
    6755              :       /* nonlocalized_vars is a middle-end thing.  */
    6756       796626 :       WT (t->block.subblocks);
    6757       796626 :       WT (t->block.supercontext);
    6758              :       // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
    6759       796626 :       WT (t->block.abstract_origin);
    6760              :       /* fragment_origin, fragment_chain are middle-end things.  */
    6761       796626 :       WT (t->block.chain);
    6762              :       /* nonlocalized_vars, block_num & die are middle endy/debug
    6763              :          things.  */
    6764       796626 :       break;
    6765              : 
    6766      1575239 :     case CALL_EXPR:
    6767      1575239 :       if (streaming_p ())
    6768       754158 :         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       658627 :     case STATEMENT_LIST:
    6792      2739532 :       for (tree stmt : tsi_range (t))
    6793      2080905 :         if (stmt)
    6794      2080905 :           WT (stmt);
    6795       658627 :       WT (NULL_TREE);
    6796       658627 :       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       272718 :       break;
    6810              : 
    6811       272718 :     case TREE_BINFO:
    6812       272718 :       {
    6813       272718 :         WT (t->binfo.common.chain);
    6814       272718 :         WT (t->binfo.offset);
    6815       272718 :         WT (t->binfo.inheritance);
    6816       272718 :         WT (t->binfo.vptr_field);
    6817              : 
    6818       272718 :         WT (t->binfo.vtable);
    6819       272718 :         WT (t->binfo.virtuals);
    6820       272718 :         WT (t->binfo.vtt_subvtt);
    6821       272718 :         WT (t->binfo.vtt_vptr);
    6822              : 
    6823       272718 :         tree_vec (BINFO_BASE_ACCESSES (t));
    6824       272718 :         unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6825       348396 :         for (unsigned ix = 0; ix != num; ix++)
    6826        75678 :           WT (BINFO_BASE_BINFO (t, ix));
    6827              :       }
    6828              :       break;
    6829              : 
    6830      3970663 :     case TREE_LIST:
    6831      3970663 :       WT (t->list.purpose);
    6832      3970663 :       WT (t->list.value);
    6833      3970663 :       WT (t->list.common.chain);
    6834      3970663 :       break;
    6835              : 
    6836      3384307 :     case TREE_VEC:
    6837      9389912 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6838      6005605 :         WT (TREE_VEC_ELT (t, ix));
    6839              :       /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN!  */
    6840      3384307 :       gcc_checking_assert (!t->type_common.common.chain
    6841              :                            || (TREE_CODE (t->type_common.common.chain)
    6842              :                                == INTEGER_CST));
    6843      3384307 :       WT (t->type_common.common.chain);
    6844      3384307 :       break;
    6845              : 
    6846              :       /* C++-specific nodes ...  */
    6847       276747 :     case BASELINK:
    6848       276747 :       WT (((lang_tree_node *)t)->baselink.binfo);
    6849       276747 :       WT (((lang_tree_node *)t)->baselink.functions);
    6850       276747 :       WT (((lang_tree_node *)t)->baselink.access_binfo);
    6851       276747 :       WT (((lang_tree_node *)t)->baselink.common.chain);
    6852       276747 :       break;
    6853              : 
    6854       123616 :     case CONSTRAINT_INFO:
    6855       123616 :       WT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6856       123616 :       WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6857       123616 :       WT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6858       123616 :       break;
    6859              : 
    6860        19390 :     case DEFERRED_NOEXCEPT:
    6861        19390 :       WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    6862        19390 :       WT (((lang_tree_node *)t)->deferred_noexcept.args);
    6863        19390 :       break;
    6864              : 
    6865        19301 :     case LAMBDA_EXPR:
    6866        19301 :       WT (((lang_tree_node *)t)->lambda_expression.capture_list);
    6867        19301 :       WT (((lang_tree_node *)t)->lambda_expression.this_capture);
    6868        19301 :       WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    6869        19301 :       WT (((lang_tree_node *)t)->lambda_expression.regen_info);
    6870        19301 :       WT (((lang_tree_node *)t)->lambda_expression.extra_args);
    6871              :       /* pending_proxies is a parse-time thing.  */
    6872        19301 :       gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
    6873        19301 :       if (state)
    6874        19298 :         state->write_location
    6875        19298 :           (*this, ((lang_tree_node *)t)->lambda_expression.locus);
    6876        19301 :       if (streaming_p ())
    6877              :         {
    6878         6604 :           WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    6879         6604 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    6880         6604 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    6881              :         }
    6882              :       break;
    6883              : 
    6884      2860037 :     case OVERLOAD:
    6885      2860037 :       WT (((lang_tree_node *)t)->overload.function);
    6886      2860037 :       WT (t->common.chain);
    6887      2860037 :       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        21741 :     case STATIC_ASSERT:
    6896        21741 :       WT (((lang_tree_node *)t)->static_assertion.condition);
    6897        21741 :       WT (((lang_tree_node *)t)->static_assertion.message);
    6898        21741 :       if (state)
    6899        21741 :         state->write_location
    6900        21741 :           (*this, ((lang_tree_node *)t)->static_assertion.location);
    6901              :       break;
    6902              : 
    6903       868247 :     case TEMPLATE_DECL:
    6904              :       /* Streamed with the template_decl node itself.  */
    6905       868247 :       gcc_checking_assert
    6906              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
    6907       868247 :       gcc_checking_assert
    6908              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
    6909       868247 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6910        16578 :         WT (DECL_CHAIN (t));
    6911              :       break;
    6912              : 
    6913      1965380 :     case TEMPLATE_INFO:
    6914      1965380 :       {
    6915      1965380 :         WT (((lang_tree_node *)t)->template_info.tmpl);
    6916      1965380 :         WT (((lang_tree_node *)t)->template_info.args);
    6917      1965380 :         WT (((lang_tree_node *)t)->template_info.partial);
    6918              : 
    6919      1965380 :         const auto *ac = (((lang_tree_node *)t)
    6920              :                           ->template_info.deferred_access_checks);
    6921      1965380 :         unsigned len = vec_safe_length (ac);
    6922      1965380 :         if (streaming_p ())
    6923       980583 :           u (len);
    6924      1965380 :         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      2512312 :     case TEMPLATE_PARM_INDEX:
    6940      2512312 :       if (streaming_p ())
    6941              :         {
    6942       559320 :           WU (((lang_tree_node *)t)->tpi.index);
    6943       559320 :           WU (((lang_tree_node *)t)->tpi.level);
    6944       559320 :           WU (((lang_tree_node *)t)->tpi.orig_level);
    6945              :         }
    6946      2512312 :       WT (((lang_tree_node *)t)->tpi.decl);
    6947              :       /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
    6948              :          cache, do not stream.  */
    6949      2512312 :       break;
    6950              : 
    6951        29914 :     case TRAIT_EXPR:
    6952        29914 :       WT (((lang_tree_node *)t)->trait_expression.type1);
    6953        29914 :       WT (((lang_tree_node *)t)->trait_expression.type2);
    6954        29914 :       if (state)
    6955        23374 :         state->write_location
    6956        23374 :           (*this, ((lang_tree_node *)t)->trait_expression.locus);
    6957        29914 :       if (streaming_p ())
    6958        11517 :         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     52210367 :   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     49283610 :       tree type = t->typed.type;
    6980     49283610 :       unsigned prec = 0;
    6981              : 
    6982     49283610 :       switch (code)
    6983              :         {
    6984              :         default:
    6985              :           break;
    6986              : 
    6987              :         case TEMPLATE_DECL:
    6988              :           /* We fill in the template's type separately.  */
    6989     49283610 :           type = NULL_TREE;
    6990              :           break;
    6991              : 
    6992      3433262 :         case TYPE_DECL:
    6993      3433262 :           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         9596 :         case ENUMERAL_TYPE:
    6999         9596 :           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         5666 :               prec = TYPE_PRECISION (type);
    7004         5666 :               tree name = DECL_NAME (TYPE_NAME (type));
    7005              : 
    7006        74034 :               for (unsigned itk = itk_none; itk--;)
    7007        74034 :                 if (integer_types[itk]
    7008        74034 :                     && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
    7009              :                   {
    7010              :                     type = integer_types[itk];
    7011              :                     break;
    7012              :                   }
    7013         5666 :               gcc_assert (type != t->typed.type);
    7014              :             }
    7015              :           break;
    7016              :         }
    7017              : 
    7018     49283610 :       WT (type);
    7019     49283610 :       if (prec && streaming_p ())
    7020         2831 :         WU (prec);
    7021              :     }
    7022              : 
    7023     52210367 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7024              :     {
    7025       134446 :       unsigned len = vec_safe_length (t->constructor.elts);
    7026       134446 :       if (streaming_p ())
    7027        66324 :         WU (len);
    7028       134446 :       if (len)
    7029       486720 :         for (unsigned ix = 0; ix != len; ix++)
    7030              :           {
    7031       415658 :             const constructor_elt &elt = (*t->constructor.elts)[ix];
    7032              : 
    7033       415658 :             WT (elt.index);
    7034       415658 :             WT (elt.value);
    7035              :           }
    7036              :     }
    7037              : 
    7038              : #undef WT
    7039              : #undef WU
    7040     52210367 : }
    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     15638960 : 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     15638960 :   tree_code code = TREE_CODE (t);
    7055              : 
    7056              :   /* First by tree shape.  */
    7057     15638960 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    7058              :     {
    7059      3340456 :       RT (t->decl_minimal.name);
    7060      3340456 :       if (!DECL_TEMPLATE_PARM_P (t))
    7061      2907962 :         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      3340456 :       t->decl_minimal.locus = state->read_location (*this);
    7066      3340456 :       if (has_warning_spec (t))
    7067          653 :         put_warning_spec (t, u ());
    7068              :     }
    7069              : 
    7070     15638960 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    7071              :     {
    7072       567982 :       RT (t->type_common.name);
    7073       567982 :       RT (t->type_common.context);
    7074              : 
    7075       567982 :       RT (t->type_common.main_variant);
    7076       567982 :       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       567982 :       RU (t->type_common.precision);
    7082       567982 :       RU (t->type_common.contains_placeholder_bits);
    7083       567982 :       RUC (machine_mode, t->type_common.mode);
    7084       567982 :       RU (t->type_common.align);
    7085              : 
    7086       567982 :       if (!RECORD_OR_UNION_CODE_P (code))
    7087              :         {
    7088       392557 :           RT (t->type_common.size);
    7089       392557 :           RT (t->type_common.size_unit);
    7090              :         }
    7091       567982 :       RT (t->type_common.attributes);
    7092              : 
    7093       567982 :       RT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    7094              :     }
    7095              : 
    7096     15638960 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    7097              :     {
    7098      3340456 :       RUC (machine_mode, t->decl_common.mode);
    7099      3340456 :       RU (t->decl_common.off_align);
    7100      3340456 :       RU (t->decl_common.align);
    7101              : 
    7102      3340456 :       if (code != TEMPLATE_DECL)
    7103              :         {
    7104      3006730 :           RT (t->decl_common.size);
    7105      3006730 :           RT (t->decl_common.size_unit);
    7106              :         }
    7107              : 
    7108      3340456 :       RT (t->decl_common.attributes);
    7109      3340456 :       RT (t->decl_common.abstract_origin);
    7110              :     }
    7111              : 
    7112     15638960 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    7113              :     {
    7114      1538235 :       RT (t->decl_with_vis.assembler_name);
    7115      1538235 :       RUC (symbol_visibility, t->decl_with_vis.visibility);
    7116              :     }
    7117              : 
    7118     15638960 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    7119              :     {
    7120       567982 :       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       564716 :       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       389291 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    7135       389291 :           RT (t->type_non_common.values);
    7136       389291 :           RT (t->type_non_common.maxval);
    7137       389291 :           RT (t->type_non_common.minval);
    7138              :         }
    7139              : 
    7140       567982 :       RT (t->type_non_common.lang_1);
    7141              :     }
    7142              : 
    7143     15638960 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    7144              :     {
    7145      6177789 :       t->exp.locus = state->read_location (*this);
    7146      6177789 :       if (has_warning_spec (t))
    7147       487803 :         put_warning_spec (t, u ());
    7148              : 
    7149      6177789 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    7150      6177789 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    7151      6177789 :                         : TREE_OPERAND_LENGTH (t));
    7152      6177789 :       unsigned ix = unsigned (vl);
    7153     16905401 :       for (; ix != limit; ix++)
    7154     10727612 :         RTU (TREE_OPERAND (t, ix));
    7155              :     }
    7156      9461171 :   else if (code == REQUIRES_EXPR)
    7157              :     {
    7158         7711 :       REQUIRES_EXPR_LOCATION (t) = state->read_location (*this);
    7159         7711 :       if (has_warning_spec (t))
    7160            0 :         put_warning_spec (t, u ());
    7161              : 
    7162         7711 :       REQUIRES_EXPR_PARMS (t) = chained_decls ();
    7163         7711 :       RTU (REQUIRES_EXPR_REQS (t));
    7164         7711 :       RTU (REQUIRES_EXPR_EXTRA_ARGS (t));
    7165              :     }
    7166              : 
    7167              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    7168              :      correspondence. */
    7169     15638960 :   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       602610 :     case INTEGER_CST:
    7194       602610 :       {
    7195       602610 :         unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    7196      1207399 :         for (unsigned ix = 0; ix != num; ix++)
    7197       604789 :           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        16775 :     case REAL_CST:
    7207        16775 :       if (const void *bytes = buf (sizeof (real_value)))
    7208        16775 :         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       229027 :     case VAR_DECL:
    7229       229027 :       if (DECL_CONTEXT (t)
    7230       229027 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    7231              :         {
    7232        42697 :           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      1527413 :     case RESULT_DECL:
    7242      1527413 :     case PARM_DECL:
    7243      1527413 :       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        14552 :           tree val = tree_node ();
    7249        14552 :           SET_DECL_VALUE_EXPR (t, val);
    7250              :         }
    7251              :       /* FALLTHROUGH  */
    7252              : 
    7253      1573118 :     case CONST_DECL:
    7254      1573118 :     case IMPORTED_DECL:
    7255      1573118 :       RT (t->decl_common.initial);
    7256      1573118 :       break;
    7257              : 
    7258        56999 :     case FIELD_DECL:
    7259        56999 :       RT (t->field_decl.offset);
    7260        56999 :       RT (t->field_decl.bit_field_type);
    7261        56999 :       RT (t->field_decl.qualifier);
    7262        56999 :       RT (t->field_decl.bit_offset);
    7263        56999 :       RT (t->field_decl.fcontext);
    7264        56999 :       RT (t->decl_common.initial);
    7265        56999 :       break;
    7266              : 
    7267        18331 :     case LABEL_DECL:
    7268        18331 :       RU (t->label_decl.label_decl_uid);
    7269        18331 :       RU (t->label_decl.eh_landing_pad_nr);
    7270        18331 :       break;
    7271              : 
    7272       471371 :     case FUNCTION_DECL:
    7273       471371 :       {
    7274       471371 :         unsigned bltin = u ();
    7275       471371 :         t->function_decl.built_in_class = built_in_class (bltin);
    7276       471371 :         if (bltin != NOT_BUILT_IN)
    7277              :           {
    7278         9576 :             bltin = u ();
    7279         9576 :             DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
    7280              :           }
    7281              : 
    7282       471371 :         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       471371 :         RT (t->function_decl.vindex);
    7288              : 
    7289       471371 :         if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    7290              :           {
    7291         3870 :             tree spec;
    7292         3870 :             RT (spec);
    7293         3870 :             store_explicit_specifier (t, spec);
    7294              :           }
    7295              :       }
    7296              :       break;
    7297              : 
    7298        52382 :     case USING_DECL:
    7299              :       /* USING_DECL_DECLS  */
    7300        52382 :       RT (t->decl_common.initial);
    7301              :       /* FALLTHROUGH  */
    7302              : 
    7303       837660 :     case TYPE_DECL:
    7304              :       /* USING_DECL: USING_DECL_SCOPE  */
    7305              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    7306       837660 :       RT (t->decl_non_common.result);
    7307       837660 :       break;
    7308              : 
    7309              :       /* Miscellaneous common nodes.  */
    7310       317071 :     case BLOCK:
    7311       317071 :       t->block.locus = state->read_location (*this);
    7312       317071 :       t->block.end_locus = state->read_location (*this);
    7313              : 
    7314       317071 :       for (tree *chain = &t->block.vars;;)
    7315       477882 :         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       160811 :             decl = maybe_duplicate (decl);
    7324              : 
    7325       160811 :             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       160811 :             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       160811 :             *chain = decl;
    7343       160811 :             chain = &DECL_CHAIN (decl);
    7344              :           }
    7345              :         else
    7346       160811 :           break;
    7347              : 
    7348              :       /* nonlocalized_vars is middle-end.  */
    7349       317071 :       RT (t->block.subblocks);
    7350       317071 :       RT (t->block.supercontext);
    7351       317071 :       RT (t->block.abstract_origin);
    7352              :       /* fragment_origin, fragment_chain are middle-end.  */
    7353       317071 :       RT (t->block.chain);
    7354              :       /* nonlocalized_vars, block_num, die are middle endy/debug
    7355              :          things.  */
    7356       317071 :       break;
    7357              : 
    7358       597500 :     case CALL_EXPR:
    7359       597500 :       RUC (internal_fn, t->base.u.ifn);
    7360       597500 :       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       283405 :     case STATEMENT_LIST:
    7378       283405 :       {
    7379       283405 :         tree_stmt_iterator iter = tsi_start (t);
    7380      1175430 :         for (tree stmt; RT (stmt);)
    7381              :           {
    7382       892025 :             if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
    7383       168846 :                 && !MAY_HAVE_DEBUG_MARKER_STMTS)
    7384            0 :               continue;
    7385       892025 :             tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
    7386              :           }
    7387              :       }
    7388       283405 :       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        92217 :       break;
    7395              : 
    7396        92217 :     case TREE_BINFO:
    7397        92217 :       RT (t->binfo.common.chain);
    7398        92217 :       RT (t->binfo.offset);
    7399        92217 :       RT (t->binfo.inheritance);
    7400        92217 :       RT (t->binfo.vptr_field);
    7401              : 
    7402              :       /* Do not mark the vtables as USED in the address expressions
    7403              :          here.  */
    7404        92217 :       unused++;
    7405        92217 :       RT (t->binfo.vtable);
    7406        92217 :       RT (t->binfo.virtuals);
    7407        92217 :       RT (t->binfo.vtt_subvtt);
    7408        92217 :       RT (t->binfo.vtt_vptr);
    7409        92217 :       unused--;
    7410              : 
    7411        92217 :       BINFO_BASE_ACCESSES (t) = tree_vec ();
    7412        92217 :       if (!get_overrun ())
    7413              :         {
    7414        92217 :           unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    7415       117370 :           for (unsigned ix = 0; ix != num; ix++)
    7416        25153 :             BINFO_BASE_APPEND (t, tree_node ());
    7417              :         }
    7418              :       break;
    7419              : 
    7420      1345467 :     case TREE_LIST:
    7421      1345467 :       RT (t->list.purpose);
    7422      1345467 :       RT (t->list.value);
    7423      1345467 :       RT (t->list.common.chain);
    7424      1345467 :       break;
    7425              : 
    7426       862258 :     case TREE_VEC:
    7427      2318215 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    7428      1455957 :         RT (TREE_VEC_ELT (t, ix));
    7429       862258 :       RT (t->type_common.common.chain);
    7430       862258 :       break;
    7431              : 
    7432              :       /* C++-specific nodes ...  */
    7433       109659 :     case BASELINK:
    7434       109659 :       RT (((lang_tree_node *)t)->baselink.binfo);
    7435       109659 :       RTU (((lang_tree_node *)t)->baselink.functions);
    7436       109659 :       RT (((lang_tree_node *)t)->baselink.access_binfo);
    7437       109659 :       RT (((lang_tree_node *)t)->baselink.common.chain);
    7438       109659 :       break;
    7439              : 
    7440        45327 :     case CONSTRAINT_INFO:
    7441        45327 :       RT (((lang_tree_node *)t)->constraint_info.template_reqs);
    7442        45327 :       RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    7443        45327 :       RT (((lang_tree_node *)t)->constraint_info.associated_constr);
    7444        45327 :       break;
    7445              : 
    7446         7635 :     case DEFERRED_NOEXCEPT:
    7447         7635 :       RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    7448         7635 :       RT (((lang_tree_node *)t)->deferred_noexcept.args);
    7449         7635 :       break;
    7450              : 
    7451         4203 :     case LAMBDA_EXPR:
    7452         4203 :       RT (((lang_tree_node *)t)->lambda_expression.capture_list);
    7453         4203 :       RT (((lang_tree_node *)t)->lambda_expression.this_capture);
    7454         4203 :       RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    7455         4203 :       RT (((lang_tree_node *)t)->lambda_expression.regen_info);
    7456         4203 :       RT (((lang_tree_node *)t)->lambda_expression.extra_args);
    7457              :       /* lambda_expression.pending_proxies is NULL  */
    7458         4203 :       ((lang_tree_node *)t)->lambda_expression.locus
    7459         4203 :         = state->read_location (*this);
    7460         4203 :       RUC (cp_lambda_default_capture_mode_type,
    7461              :            ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    7462         4203 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    7463         4203 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    7464         4203 :       break;
    7465              : 
    7466       577799 :     case OVERLOAD:
    7467       577799 :       RT (((lang_tree_node *)t)->overload.function);
    7468       577799 :       RT (t->common.chain);
    7469       577799 :       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         8296 :     case STATIC_ASSERT:
    7477         8296 :       RT (((lang_tree_node *)t)->static_assertion.condition);
    7478         8296 :       RT (((lang_tree_node *)t)->static_assertion.message);
    7479         8296 :       ((lang_tree_node *)t)->static_assertion.location
    7480         8296 :         = state->read_location (*this);
    7481         8296 :       break;
    7482              : 
    7483       333726 :     case TEMPLATE_DECL:
    7484              :       /* Streamed when reading the raw template decl itself.  */
    7485       333726 :       gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
    7486       333726 :       gcc_assert (((lang_tree_node *)t)->template_decl.result);
    7487       333726 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    7488         7202 :         RT (DECL_CHAIN (t));
    7489              :       break;
    7490              : 
    7491       744166 :     case TEMPLATE_INFO:
    7492       744166 :       RT (((lang_tree_node *)t)->template_info.tmpl);
    7493       744166 :       RT (((lang_tree_node *)t)->template_info.args);
    7494       744166 :       RT (((lang_tree_node *)t)->template_info.partial);
    7495       744166 :       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       414538 :     case TEMPLATE_PARM_INDEX:
    7514       414538 :       RU (((lang_tree_node *)t)->tpi.index);
    7515       414538 :       RU (((lang_tree_node *)t)->tpi.level);
    7516       414538 :       RU (((lang_tree_node *)t)->tpi.orig_level);
    7517       414538 :       RT (((lang_tree_node *)t)->tpi.decl);
    7518       414538 :       break;
    7519              : 
    7520         8502 :     case TRAIT_EXPR:
    7521         8502 :       RT (((lang_tree_node *)t)->trait_expression.type1);
    7522         8502 :       RT (((lang_tree_node *)t)->trait_expression.type2);
    7523         8502 :       ((lang_tree_node *)t)->trait_expression.locus
    7524         8502 :         = state->read_location (*this);
    7525         8502 :       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
    7526         8502 :       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     15638960 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    7535              :     {
    7536     14516463 :       tree type = tree_node ();
    7537              : 
    7538     14516463 :       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     14516463 :       if (code != TEMPLATE_DECL)
    7549     14182737 :         t->typed.type = type;
    7550              :     }
    7551              : 
    7552     15638960 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7553        54191 :     if (unsigned len = u ())
    7554              :       {
    7555        32675 :         vec_alloc (t->constructor.elts, len);
    7556       213251 :         for (unsigned ix = 0; ix != len; ix++)
    7557              :           {
    7558       180576 :             constructor_elt elt;
    7559              : 
    7560       180576 :             RT (elt.index);
    7561       180576 :             RTU (elt.value);
    7562       180576 :             t->constructor.elts->quick_push (elt);
    7563              :           }
    7564              :       }
    7565              : 
    7566              : #undef RT
    7567              : #undef RM
    7568              : #undef RU
    7569     15638960 :   return !get_overrun ();
    7570              : }
    7571              : 
    7572              : void
    7573      6187341 : trees_out::lang_decl_vals (tree t)
    7574              : {
    7575      6187341 :   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      6187341 :   switch (lang->u.base.selector)
    7580              :     {
    7581            0 :     default:
    7582            0 :       gcc_unreachable ();
    7583              : 
    7584      1209692 :     case lds_fn:  /* lang_decl_fn.  */
    7585      1209692 :       if (streaming_p ())
    7586              :         {
    7587       604739 :           if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7588       103695 :             WU (lang->u.fn.ovl_op_code);
    7589              :         }
    7590              : 
    7591      1506854 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7592       943786 :         WT (lang->u.fn.context);
    7593              : 
    7594      1209692 :       if (lang->u.fn.thunk_p)
    7595              :         {
    7596              :           /* The thunked-to function.  */
    7597         1736 :           WT (lang->u.fn.befriending_classes);
    7598         1736 :           if (streaming_p ())
    7599          868 :             wi (lang->u.fn.u5.fixed_offset);
    7600              :         }
    7601      1207956 :       else if (decl_tls_wrapper_p (t))
    7602              :         /* The wrapped variable.  */
    7603           18 :         WT (lang->u.fn.befriending_classes);
    7604              :       else
    7605      1207938 :         WT (lang->u.fn.u5.cloned_function);
    7606              : 
    7607      1209692 :       if (FNDECL_USED_AUTO (t))
    7608         4667 :         WT (lang->u.fn.u.saved_auto_return_type);
    7609              : 
    7610      1209692 :       goto lds_min;
    7611              : 
    7612         6384 :     case lds_decomp:  /* lang_decl_decomp.  */
    7613         6384 :       WT (lang->u.decomp.base);
    7614         6384 :       goto lds_min;
    7615              : 
    7616      3561140 :     case lds_min:  /* lang_decl_min.  */
    7617      3561140 :     lds_min:
    7618      3561140 :       WT (lang->u.min.template_info);
    7619      3561140 :       {
    7620      3561140 :         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      1209692 :         if (!DECL_THUNK_P (t)
    7626      4769096 :             && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
    7627              :           access = NULL_TREE;
    7628              : 
    7629      3561140 :         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      3561140 :       if (decl_specialization_friend_p (t))
    7636          120 :         WT (t->common.chain);
    7637              :       break;
    7638              : 
    7639              :     case lds_ns:  /* lang_decl_ns.  */
    7640              :       break;
    7641              : 
    7642      2625878 :     case lds_parm:  /* lang_decl_parm.  */
    7643      2625878 :       if (streaming_p ())
    7644              :         {
    7645       893264 :           WU (lang->u.parm.level);
    7646       893264 :           WU (lang->u.parm.index);
    7647              :         }
    7648              :       break;
    7649              :     }
    7650              : #undef WU
    7651              : #undef WT
    7652      6187341 : }
    7653              : 
    7654              : bool
    7655      2092882 : trees_in::lang_decl_vals (tree t)
    7656              : {
    7657      2092882 :   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      2092882 :   switch (lang->u.base.selector)
    7663              :     {
    7664            0 :     default:
    7665            0 :       gcc_unreachable ();
    7666              : 
    7667       471371 :     case lds_fn:  /* lang_decl_fn.  */
    7668       471371 :       if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7669              :         {
    7670        81082 :           unsigned code = u ();
    7671              : 
    7672              :           /* Check consistency.  */
    7673        81082 :           if (code >= OVL_OP_MAX
    7674        81082 :               || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
    7675        81082 :                   .ovl_op_code) == OVL_OP_ERROR_MARK)
    7676            0 :             set_overrun ();
    7677              :           else
    7678        81082 :             lang->u.fn.ovl_op_code = code;
    7679              :         }
    7680              : 
    7681       583461 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7682       372831 :         RT (lang->u.fn.context);
    7683              : 
    7684       471371 :       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       470797 :       else if (decl_tls_wrapper_p (t))
    7690           15 :         RT (lang->u.fn.befriending_classes);
    7691              :       else
    7692       470782 :         RT (lang->u.fn.u5.cloned_function);
    7693              : 
    7694       471371 :       if (FNDECL_USED_AUTO (t))
    7695         1483 :         RT (lang->u.fn.u.saved_auto_return_type);
    7696       471371 :       goto lds_min;
    7697              : 
    7698         3043 :     case lds_decomp:  /* lang_decl_decomp.  */
    7699         3043 :       RT (lang->u.decomp.base);
    7700         3043 :       goto lds_min;
    7701              : 
    7702      1399907 :     case lds_min:  /* lang_decl_min.  */
    7703      1399907 :     lds_min:
    7704      1399907 :       RT (lang->u.min.template_info);
    7705      1399907 :       RT (lang->u.min.access);
    7706      1399907 :       if (decl_specialization_friend_p (t))
    7707           51 :         RT (t->common.chain);
    7708              :       break;
    7709              : 
    7710              :     case lds_ns:  /* lang_decl_ns.  */
    7711              :       break;
    7712              : 
    7713       692820 :     case lds_parm:  /* lang_decl_parm.  */
    7714       692820 :       RU (lang->u.parm.level);
    7715       692820 :       RU (lang->u.parm.index);
    7716       692820 :       break;
    7717              :     }
    7718              : #undef RU
    7719              : #undef RT
    7720      2092882 :   return !get_overrun ();
    7721              : }
    7722              : 
    7723              : /* Most of the value contents of lang_type is streamed in
    7724              :    define_class.  */
    7725              : 
    7726              : void
    7727       421494 : trees_out::lang_type_vals (tree t)
    7728              : {
    7729       421494 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7730              : #define WU(X) (u (X))
    7731              : #define WT(X) (tree_node (X))
    7732       421494 :   if (streaming_p ())
    7733       210724 :     WU (lang->align);
    7734              : #undef WU
    7735              : #undef WT
    7736       421494 : }
    7737              : 
    7738              : bool
    7739       155199 : trees_in::lang_type_vals (tree t)
    7740              : {
    7741       155199 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7742              : #define RU(X) ((X) = u ())
    7743              : #define RT(X) ((X) = tree_node ())
    7744       155199 :   RU (lang->align);
    7745              : #undef RU
    7746              : #undef RT
    7747       155199 :   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     20152260 : trees_out::tree_node_bools (tree t)
    7756              : {
    7757     20152260 :   gcc_checking_assert (streaming_p ());
    7758              : 
    7759              :   /* We should never stream a namespace.  */
    7760     20152260 :   gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
    7761              :                        || DECL_NAMESPACE_ALIAS (t));
    7762              : 
    7763     20152260 :   bits_out bits = stream_bits ();
    7764     20152260 :   core_bools (t, bits);
    7765              : 
    7766     20152260 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7767              :     {
    7768      4372566 :     case tcc_declaration:
    7769      4372566 :       {
    7770      4372566 :         bool specific = DECL_LANG_SPECIFIC (t) != NULL;
    7771      4372566 :         bits.b (specific);
    7772      4372566 :         if (specific && VAR_P (t))
    7773       366891 :           bits.b (DECL_DECOMPOSITION_P (t));
    7774      2664096 :         if (specific)
    7775      2664096 :           lang_decl_bools (t, bits);
    7776              :       }
    7777              :       break;
    7778              : 
    7779       807769 :     case tcc_type:
    7780       807769 :       {
    7781       807769 :         bool specific = (TYPE_MAIN_VARIANT (t) == t
    7782       807769 :                          && TYPE_LANG_SPECIFIC (t) != NULL);
    7783       807769 :         gcc_assert (TYPE_LANG_SPECIFIC (t)
    7784              :                     == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
    7785              : 
    7786       807769 :         bits.b (specific);
    7787       807769 :         if (specific)
    7788       210724 :           lang_type_bools (t, bits);
    7789              :       }
    7790              :       break;
    7791              : 
    7792              :     default:
    7793              :       break;
    7794              :     }
    7795              : 
    7796     20152260 :   bits.bflush ();
    7797     20152260 : }
    7798              : 
    7799              : bool
    7800     15660966 : trees_in::tree_node_bools (tree t)
    7801              : {
    7802     15660966 :   bits_in bits = stream_bits ();
    7803     15660966 :   bool ok = core_bools (t, bits);
    7804              : 
    7805     15660966 :   if (ok)
    7806     15660966 :     switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7807              :       {
    7808      3340456 :       case tcc_declaration:
    7809      3340456 :         if (bits.b ())
    7810              :           {
    7811      2092882 :             bool decomp = VAR_P (t) && bits.b ();
    7812              : 
    7813      2092882 :             ok = maybe_add_lang_decl_raw (t, decomp);
    7814      2092882 :             if (ok)
    7815      2092882 :               ok = lang_decl_bools (t, bits);
    7816              :           }
    7817              :         break;
    7818              : 
    7819       589988 :       case tcc_type:
    7820       589988 :         if (bits.b ())
    7821              :           {
    7822       155199 :             ok = maybe_add_lang_type_raw (t);
    7823       155199 :             if (ok)
    7824       155199 :               ok = lang_type_bools (t, bits);
    7825              :           }
    7826              :         break;
    7827              : 
    7828              :       default:
    7829              :         break;
    7830              :       }
    7831              : 
    7832     15660966 :   bits.bflush ();
    7833     15660966 :   if (!ok || get_overrun ())
    7834            0 :     return false;
    7835              : 
    7836              :   return true;
    7837     15660966 : }
    7838              : 
    7839              : 
    7840              : /* Write out the lang-specific vals of node T.  */
    7841              : 
    7842              : void
    7843     52210367 : trees_out::lang_vals (tree t)
    7844              : {
    7845     52210367 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7846              :     {
    7847     11465454 :     case tcc_declaration:
    7848     11465454 :       if (DECL_LANG_SPECIFIC (t))
    7849      6187341 :         lang_decl_vals (t);
    7850              :       break;
    7851              : 
    7852      2831318 :     case tcc_type:
    7853      2831318 :       if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
    7854       421494 :         lang_type_vals (t);
    7855              :       break;
    7856              : 
    7857              :     default:
    7858              :       break;
    7859              :     }
    7860     52210367 : }
    7861              : 
    7862              : bool
    7863     15638960 : trees_in::lang_vals (tree t)
    7864              : {
    7865     15638960 :   bool ok = true;
    7866              : 
    7867     15638960 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7868              :     {
    7869      3340456 :     case tcc_declaration:
    7870      3340456 :       if (DECL_LANG_SPECIFIC (t))
    7871      2092882 :         ok = lang_decl_vals (t);
    7872              :       break;
    7873              : 
    7874       567982 :     case tcc_type:
    7875       567982 :       if (TYPE_LANG_SPECIFIC (t))
    7876       155199 :         ok = lang_type_vals (t);
    7877              :       else
    7878       412783 :         TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
    7879              :       break;
    7880              : 
    7881              :     default:
    7882              :       break;
    7883              :     }
    7884              : 
    7885     15638960 :   return ok;
    7886              : }
    7887              : 
    7888              : /* Write out the value fields of node T.  */
    7889              : 
    7890              : void
    7891     52210367 : trees_out::tree_node_vals (tree t)
    7892              : {
    7893     52210367 :   core_vals (t);
    7894     52210367 :   lang_vals (t);
    7895     52210367 : }
    7896              : 
    7897              : bool
    7898     15638960 : trees_in::tree_node_vals (tree t)
    7899              : {
    7900     15638960 :   bool ok = core_vals (t);
    7901     15638960 :   if (ok)
    7902     15638960 :     ok = lang_vals (t);
    7903              : 
    7904     15638960 :   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    342021104 : trees_out::ref_node (tree t)
    7914              : {
    7915    342021104 :   if (!t)
    7916              :     {
    7917    137422601 :       if (streaming_p ())
    7918              :         {
    7919              :           /* NULL_TREE -> tt_null.  */
    7920     52308248 :           null_count++;
    7921     52308248 :           i (tt_null);
    7922              :         }
    7923    137422601 :       return WK_none;
    7924              :     }
    7925              : 
    7926    204598503 :   if (!TREE_VISITED (t))
    7927              :     return WK_normal;
    7928              : 
    7929              :   /* An already-visited tree.  It must be in the map.  */
    7930    119389085 :   int val = get_tag (t);
    7931              : 
    7932    119389085 :   if (val == tag_value)
    7933              :     /* An entry we should walk into.  */
    7934              :     return WK_value;
    7935              : 
    7936    117557211 :   const char *kind;
    7937              : 
    7938    117557211 :   if (val <= tag_backref)
    7939              :     {
    7940              :       /* Back reference -> -ve number  */
    7941     89639917 :       if (streaming_p ())
    7942     42427021 :         i (val);
    7943              :       kind = "backref";
    7944              :     }
    7945     27917294 :   else if (val >= tag_fixed)
    7946              :     {
    7947              :       /* Fixed reference -> tt_fixed */
    7948     27917294 :       val -= tag_fixed;
    7949     27917294 :       if (streaming_p ())
    7950      9836468 :         i (tt_fixed), u (val);
    7951              :       kind = "fixed";
    7952              :     }
    7953              : 
    7954    117557211 :   if (streaming_p ())
    7955              :     {
    7956     52263489 :       back_ref_count++;
    7957     52263489 :       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     32788442 : trees_in::back_ref (int tag)
    7965              : {
    7966     32788442 :   tree res = NULL_TREE;
    7967              : 
    7968     32788442 :   if (tag < 0 && unsigned (~tag) < back_refs.length ())
    7969     32788442 :     res = back_refs[~tag];
    7970              : 
    7971     32788442 :   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     32788442 :       || TREE_CODE (res) >= MAX_TREE_CODES)
    7976            0 :     set_overrun ();
    7977              :   else
    7978     32802982 :     dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
    7979              :                                  TREE_CODE (res), res, res);
    7980     32788442 :   return res;
    7981              : }
    7982              : 
    7983              : unsigned
    7984      4835390 : trees_out::add_indirect_tpl_parms (tree parms)
    7985              : {
    7986      4835390 :   unsigned len = 0;
    7987      9094420 :   for (; parms; parms = TREE_CHAIN (parms), len++)
    7988              :     {
    7989      5117406 :       if (TREE_VISITED (parms))
    7990              :         break;
    7991              : 
    7992      4259030 :       int tag = insert (parms);
    7993      4259030 :       if (streaming_p ())
    7994      4259357 :         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      4835390 :   if (streaming_p ())
    8000       806585 :     u (len);
    8001              : 
    8002      4835390 :   return len;
    8003              : }
    8004              : 
    8005              : unsigned
    8006       547424 : trees_in::add_indirect_tpl_parms (tree parms)
    8007              : {
    8008       547424 :   unsigned len = u ();
    8009       976854 :   for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
    8010              :     {
    8011       429430 :       int tag = insert (parms);
    8012       429910 :       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       547424 :   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     10705677 : trees_out::add_indirects (tree decl)
    8025              : {
    8026     10705677 :   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     10705677 :   tree inner = decl;
    8034     10705677 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8035              :     {
    8036      4835390 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8037              : 
    8038      4835390 :       inner = DECL_TEMPLATE_RESULT (decl);
    8039      4835390 :       int tag = insert (inner);
    8040      4835390 :       if (streaming_p ())
    8041       806585 :         dump (dumper::TREE)
    8042          219 :           && dump ("Indirect:%d template's result %C:%N",
    8043          219 :                    tag, TREE_CODE (inner), inner);
    8044      4835390 :       count++;
    8045              :     }
    8046              : 
    8047     10705677 :   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      5649607 :       tree type = TREE_TYPE (inner);
    8053      5649607 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8054              :                            || TYPE_NAME (type) == inner);
    8055      5649607 :       int tag = insert (type);
    8056      5649607 :       if (streaming_p ())
    8057       635578 :         dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
    8058          362 :                                      TREE_CODE (type), type);
    8059      5649607 :       count++;
    8060              :     }
    8061              : 
    8062     10705677 :   if (streaming_p ())
    8063              :     {
    8064      1642391 :       u (count);
    8065      1642920 :       dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8066              :     }
    8067     10705677 : }
    8068              : 
    8069              : bool
    8070      1101842 : trees_in::add_indirects (tree decl)
    8071              : {
    8072      1101842 :   unsigned count = 0;
    8073              : 
    8074      1101842 :   tree inner = decl;
    8075      1101842 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8076              :     {
    8077       547424 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8078              : 
    8079       547424 :       inner = DECL_TEMPLATE_RESULT (decl);
    8080       547424 :       int tag = insert (inner);
    8081       547424 :       dump (dumper::TREE)
    8082          228 :         && dump ("Indirect:%d templates's result %C:%N", tag,
    8083          228 :                  TREE_CODE (inner), inner);
    8084       547424 :       count++;
    8085              :     }
    8086              : 
    8087      1101842 :   if (TREE_CODE (inner) == TYPE_DECL)
    8088              :     {
    8089       423525 :       tree type = TREE_TYPE (inner);
    8090       423525 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8091              :                            || TYPE_NAME (type) == inner);
    8092       423525 :       int tag = insert (type);
    8093       423525 :       dump (dumper::TREE)
    8094          362 :         && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
    8095       423525 :       count++;
    8096              :     }
    8097              : 
    8098      1102447 :   dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8099      1101842 :   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      2606844 : trees_out::tpl_parm_value (tree parm)
    8117              : {
    8118      2606844 :   gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
    8119              : 
    8120      2606844 :   int parm_tag = insert (parm);
    8121      2606844 :   if (streaming_p ())
    8122              :     {
    8123       582713 :       i (tt_tpl_parm);
    8124       582713 :       dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
    8125          114 :                                    parm_tag, TREE_CODE (parm), parm);
    8126       582713 :       start (parm);
    8127       582713 :       tree_node_bools (parm);
    8128              :     }
    8129              : 
    8130      2606844 :   tree inner = parm;
    8131      2606844 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8132              :     {
    8133         6789 :       inner = DECL_TEMPLATE_RESULT (inner);
    8134         6789 :       int inner_tag = insert (inner);
    8135         6789 :       if (streaming_p ())
    8136              :         {
    8137         1788 :           dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
    8138            0 :                                        inner_tag, TREE_CODE (inner), inner);
    8139         1788 :           start (inner);
    8140         1788 :           tree_node_bools (inner);
    8141              :         }
    8142              :     }
    8143              : 
    8144      2606844 :   tree type = NULL_TREE;
    8145      2606844 :   if (TREE_CODE (inner) == TYPE_DECL)
    8146              :     {
    8147      2340230 :       type = TREE_TYPE (inner);
    8148      2340230 :       int type_tag = insert (type);
    8149      2340230 :       if (streaming_p ())
    8150              :         {
    8151       527118 :           dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
    8152          108 :                                        type_tag, TREE_CODE (type), type);
    8153       527118 :           start (type);
    8154       527118 :           tree_node_bools (type);
    8155              :         }
    8156              :     }
    8157              : 
    8158      2606844 :   if (inner != parm)
    8159              :     {
    8160              :       /* This is a template-template parameter.  */
    8161         6789 :       unsigned tpl_levels = 0;
    8162         6789 :       tpl_header (parm, &tpl_levels);
    8163         6789 :       tpl_parms_fini (parm, tpl_levels);
    8164              :     }
    8165              : 
    8166      2606844 :   tree_node_vals (parm);
    8167      2606844 :   if (inner != parm)
    8168         6789 :     tree_node_vals (inner);
    8169      2606844 :   if (type)
    8170              :     {
    8171      2340230 :       tree_node_vals (type);
    8172      2340230 :       if (DECL_NAME (inner) == auto_identifier
    8173      2340230 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8174              :         {
    8175              :           /* Placeholder auto.  */
    8176       107530 :           tree_node (DECL_INITIAL (inner));
    8177       107530 :           tree_node (DECL_SIZE_UNIT (inner));
    8178              :         }
    8179              :     }
    8180              : 
    8181      2606844 :   if (streaming_p ())
    8182       582713 :     dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
    8183          114 :                                  parm_tag, TREE_CODE (parm), parm);
    8184      2606844 : }
    8185              : 
    8186              : tree
    8187       432494 : trees_in::tpl_parm_value ()
    8188              : {
    8189       432494 :   tree parm = start ();
    8190       432494 :   if (!parm || !tree_node_bools (parm))
    8191            0 :     return NULL_TREE;
    8192              : 
    8193       432494 :   int parm_tag = insert (parm);
    8194       432494 :   dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
    8195          198 :                                parm_tag, TREE_CODE (parm), parm);
    8196              : 
    8197       432494 :   tree inner = parm;
    8198       432494 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8199              :     {
    8200         1229 :       inner = start ();
    8201         1229 :       if (!inner || !tree_node_bools (inner))
    8202            0 :         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       432494 :   tree type = NULL_TREE;
    8210       432494 :   if (TREE_CODE (inner) == TYPE_DECL)
    8211              :     {
    8212       389291 :       type = start ();
    8213       389291 :       if (!type || !tree_node_bools (type))
    8214            0 :         return NULL_TREE;
    8215       389291 :       int type_tag = insert (type);
    8216       389291 :       dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
    8217          120 :                                    type_tag, TREE_CODE (type), type);
    8218              : 
    8219       389291 :       TREE_TYPE (inner) = TREE_TYPE (parm) = type;
    8220       389291 :       TYPE_NAME (type) = parm;
    8221              :     }
    8222              : 
    8223       432494 :   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       432494 :   tree_node_vals (parm);
    8232       432494 :   if (inner != parm)
    8233         1229 :     tree_node_vals (inner);
    8234       432494 :   if (type)
    8235              :     {
    8236       389291 :       tree_node_vals (type);
    8237       389291 :       if (DECL_NAME (inner) == auto_identifier
    8238       389291 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8239              :         {
    8240              :           /* Placeholder auto.  */
    8241        33716 :           DECL_INITIAL (inner) = tree_node ();
    8242        33716 :           DECL_SIZE_UNIT (inner) = tree_node ();
    8243              :         }
    8244       389291 :       if (TYPE_CANONICAL (type))
    8245              :         {
    8246       389291 :           gcc_checking_assert (TYPE_CANONICAL (type) == type);
    8247       389291 :           TYPE_CANONICAL (type) = canonical_type_parameter (type);
    8248              :         }
    8249              :     }
    8250              : 
    8251       432494 :   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      1643514 : trees_out::install_entity (tree decl, depset *dep)
    8259              : {
    8260      1643514 :   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      1643514 :   u (dep ? dep->cluster + 1 : 0);
    8265      1643514 :   if (CHECKING_P && dep)
    8266              :     {
    8267              :       /* Add it to the entity map, such that we can tell it is
    8268              :          part of us.  */
    8269      1192838 :       bool existed;
    8270      1192838 :       unsigned *slot = &entity_map->get_or_insert
    8271      1192838 :         (DECL_UID (decl), &existed);
    8272      1192838 :       if (existed)
    8273              :         /* If it existed, it should match.  */
    8274         1639 :         gcc_checking_assert (decl == (*entity_ary)[*slot]);
    8275      1192838 :       *slot = ~dep->cluster;
    8276              :     }
    8277      1643514 : }
    8278              : 
    8279              : bool
    8280      1231942 : trees_in::install_entity (tree decl)
    8281              : {
    8282      1231942 :   unsigned entity_index = u ();
    8283      1231942 :   if (!entity_index)
    8284              :     return false;
    8285              : 
    8286       892472 :   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       892472 :   unsigned ident = state->entity_lwm + entity_index - 1;
    8294       892472 :   (*entity_ary)[ident] = decl;
    8295              : 
    8296              :   /* And into the entity map, if it's not already there.  */
    8297       892472 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    8298       892472 :   if (!DECL_LANG_SPECIFIC (not_tmpl)
    8299      1688297 :       || !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       891330 :       if (!DECL_LANG_SPECIFIC (not_tmpl))
    8304              :         {
    8305        96647 :           maybe_add_lang_decl_raw (not_tmpl, false);
    8306        96647 :           SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
    8307              :         }
    8308       891330 :       DECL_MODULE_ENTITY_P (not_tmpl) = true;
    8309              : 
    8310              :       /* Insert into the entity hash (it cannot already be there).  */
    8311       891330 :       bool existed;
    8312       891330 :       unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
    8313       891330 :       gcc_checking_assert (!existed);
    8314       891330 :       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      4501915 : trees_out::decl_value (tree decl, depset *dep)
    8359              : {
    8360              :   /* We should not be writing clones or template parms.  */
    8361      4501915 :   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      4501915 :   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      4501915 :   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      4501499 :   merge_kind mk = get_merge_kind (decl, dep);
    8382              : 
    8383      4501499 :   if (CHECKING_P)
    8384              :     {
    8385              :       /* Never start in the middle of a template.  */
    8386      4501499 :       int use_tpl = -1;
    8387      4501499 :       if (tree ti = node_template_info (decl, use_tpl))
    8388      1564984 :         gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
    8389              :                              || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
    8390              :                              || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
    8391              :                                  != decl));
    8392              :     }
    8393              : 
    8394      4501499 :   if (streaming_p ())
    8395              :     {
    8396              :       /* A new node -> tt_decl.  */
    8397      1643514 :       decl_val_count++;
    8398      1643514 :       i (tt_decl);
    8399      1643514 :       u (mk);
    8400      1643514 :       start (decl);
    8401              : 
    8402      1643514 :       if (mk != MK_unique)
    8403              :         {
    8404      1356471 :           bits_out bits = stream_bits ();
    8405      1356471 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8406              :             {
    8407              :               /* Tell the importer whether this is a global module entity,
    8408              :                  or a module entity.  */
    8409       311865 :               tree o = get_originating_module_decl (decl);
    8410       311865 :               bool is_attached = false;
    8411              : 
    8412       311865 :               tree not_tmpl = STRIP_TEMPLATE (o);
    8413       311865 :               if (DECL_LANG_SPECIFIC (not_tmpl)
    8414       498360 :                   && DECL_MODULE_ATTACH_P (not_tmpl))
    8415              :                 is_attached = true;
    8416              : 
    8417       311865 :               bits.b (is_attached);
    8418              :             }
    8419      1356471 :           bits.b (dep && dep->has_defn ());
    8420      1356471 :         }
    8421      1643514 :       tree_node_bools (decl);
    8422              :     }
    8423              : 
    8424      4501499 :   int tag = insert (decl, WK_value);
    8425      4501499 :   if (streaming_p ())
    8426      1643514 :     dump (dumper::TREE)
    8427          683 :       && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
    8428          683 :                TREE_CODE (decl), decl, decl);
    8429              : 
    8430      4501499 :   tree inner = decl;
    8431      4501499 :   int inner_tag = 0;
    8432      4501499 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8433              :     {
    8434      1292164 :       inner = DECL_TEMPLATE_RESULT (decl);
    8435      1292164 :       inner_tag = insert (inner, WK_value);
    8436              : 
    8437              :       /* On stream-in we assume that a template and its result will
    8438              :          have the same type.  */
    8439      1292164 :       gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
    8440              : 
    8441      1292164 :       if (streaming_p ())
    8442              :         {
    8443       430706 :           int code = TREE_CODE (inner);
    8444       430706 :           u (code);
    8445       430706 :           start (inner, true);
    8446       430706 :           tree_node_bools (inner);
    8447       430706 :           dump (dumper::TREE)
    8448          132 :             && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
    8449          132 :                      TREE_CODE (inner), inner, inner);
    8450              :         }
    8451              :     }
    8452              : 
    8453      4501499 :   tree type = NULL_TREE;
    8454      4501499 :   int type_tag = 0;
    8455      4501499 :   tree stub_decl = NULL_TREE;
    8456      4501499 :   int stub_tag = 0;
    8457      4501499 :   if (TREE_CODE (inner) == TYPE_DECL)
    8458              :     {
    8459      1610984 :       type = TREE_TYPE (inner);
    8460      1610984 :       bool has_type = (type == TYPE_MAIN_VARIANT (type)
    8461      1610984 :                        && TYPE_NAME (type) == inner);
    8462              : 
    8463      1610984 :       if (streaming_p ())
    8464       544079 :         u (has_type ? (unsigned) TREE_CODE (type) : 0);
    8465              : 
    8466      1610984 :       if (has_type)
    8467              :         {
    8468       736553 :           type_tag = insert (type, WK_value);
    8469       736553 :           if (streaming_p ())
    8470              :             {
    8471       245493 :               start (type, true);
    8472       245493 :               tree_node_bools (type);
    8473       245493 :               dump (dumper::TREE)
    8474          155 :                 && dump ("Writing type:%d %C:%N", type_tag,
    8475          155 :                          TREE_CODE (type), type);
    8476              :             }
    8477              : 
    8478       736553 :           stub_decl = TYPE_STUB_DECL (type);
    8479       736553 :           bool has_stub = inner != stub_decl;
    8480       736553 :           if (streaming_p ())
    8481       245493 :             u (has_stub ? (unsigned) TREE_CODE (stub_decl) : 0);
    8482       736553 :           if (has_stub)
    8483              :             {
    8484         2361 :               stub_tag = insert (stub_decl);
    8485         2361 :               if (streaming_p ())
    8486              :                 {
    8487          786 :                   start (stub_decl, true);
    8488          786 :                   tree_node_bools (stub_decl);
    8489          786 :                   dump (dumper::TREE)
    8490            0 :                     && dump ("Writing stub_decl:%d %C:%N", stub_tag,
    8491            0 :                              TREE_CODE (stub_decl), stub_decl);
    8492              :                 }
    8493              :             }
    8494              :           else
    8495              :             stub_decl = NULL_TREE;
    8496              :         }
    8497              :       else
    8498              :         /* Regular typedef.  */
    8499              :         type = NULL_TREE;
    8500              :     }
    8501              : 
    8502              :   /* Stream the container, we want it correctly canonicalized before
    8503              :      we start emitting keys for this decl.  */
    8504      4501499 :   tree container = decl_container (decl);
    8505      4501499 :   unsigned tpl_levels = 0;
    8506              : 
    8507              :   /* Also tell the importer whether this is a temploid friend attached
    8508              :      to a different module (which has implications for merging), so that
    8509              :      importers can reconstruct this information on stream-in.  */
    8510      4501499 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8511              :     {
    8512      3424400 :       tree* temploid_friend_slot = imported_temploid_friends->get (decl);
    8513      3424400 :       gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
    8514      3424400 :       tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
    8515              :     }
    8516              : 
    8517      4501499 :   {
    8518      4501499 :     auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
    8519      4501499 :     if (decl != inner)
    8520      1292164 :       tpl_header (decl, &tpl_levels);
    8521      4501499 :     if (TREE_CODE (inner) == FUNCTION_DECL)
    8522      1813416 :       fn_parms_init (inner);
    8523              : 
    8524              :     /* Now write out the merging information, and then really
    8525              :        install the tag values.  */
    8526      4501499 :     key_mergeable (tag, mk, decl, inner, container, dep);
    8527              : 
    8528      4501499 :     if (streaming_p ())
    8529      4505398 :       dump (dumper::MERGE)
    8530         1101 :         && dump ("Wrote:%d's %s merge key %C:%N", tag,
    8531         1101 :                  merge_kind_name[mk], TREE_CODE (decl), decl);
    8532      4501499 :   }
    8533              : 
    8534      4501499 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8535      4501499 :     fn_parms_fini (inner);
    8536              : 
    8537      4501499 :   if (!is_key_order ())
    8538      3306419 :     tree_node_vals (decl);
    8539              : 
    8540      4501499 :   if (inner_tag)
    8541              :     {
    8542      1292164 :       if (!is_key_order ())
    8543       861458 :         tree_node_vals (inner);
    8544      1292164 :       tpl_parms_fini (decl, tpl_levels);
    8545              :     }
    8546              : 
    8547      4501499 :   if (type && !is_key_order ())
    8548              :     {
    8549       491060 :       tree_node_vals (type);
    8550       491060 :       if (stub_decl)
    8551         1575 :         tree_node_vals (stub_decl);
    8552              :     }
    8553              : 
    8554      4501499 :   if (!is_key_order ())
    8555              :     {
    8556      3306419 :       if (mk & MK_template_mask
    8557      2309073 :           || mk == MK_partial
    8558      2309073 :           || mk == MK_friend_spec)
    8559              :         {
    8560        38804 :           if (mk != MK_partial)
    8561              :             {
    8562              :               // FIXME: We should make use of the merge-key by
    8563              :               // exposing it outside of key_mergeable.  But this gets
    8564              :               // the job done.
    8565       997346 :               auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
    8566              : 
    8567       997346 :               if (streaming_p ())
    8568       498673 :                 u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
    8569              :                                                        entry->tmpl, decl));
    8570       997346 :               tree_node (entry->tmpl);
    8571       997346 :               tree_node (entry->args);
    8572              :             }
    8573              :           else
    8574              :             {
    8575        38804 :               tree ti = get_template_info (inner);
    8576        38804 :               tree_node (TI_TEMPLATE (ti));
    8577        38804 :               tree_node (TI_ARGS (ti));
    8578              :             }
    8579              :         }
    8580      3306419 :       tree_node (get_constraints (decl));
    8581              :     }
    8582              : 
    8583      4501499 :   if (streaming_p ())
    8584              :     {
    8585              :       /* Do not stray outside this section.  */
    8586      1643514 :       gcc_checking_assert (!dep || dep->section == dep_hash->section);
    8587              : 
    8588              :       /* Write the entity index, so we can insert it as soon as we
    8589              :          know this is new.  */
    8590      1643514 :       install_entity (decl, dep);
    8591              :     }
    8592              : 
    8593      4501499 :   if (DECL_LANG_SPECIFIC (inner)
    8594      3808762 :       && DECL_MODULE_KEYED_DECLS_P (inner)
    8595      4502702 :       && streaming_p ())
    8596              :     {
    8597              :       /* Stream the keyed entities.  There may be keyed entities that we
    8598              :          choose not to stream, such as a lambda in a non-inline variable's
    8599              :          initializer, so don't build dependencies for them here; any deps
    8600              :          we need should be acquired during write_definition (possibly
    8601              :          indirectly).  */
    8602          394 :       auto *attach_vec = keyed_table->get (inner);
    8603          394 :       unsigned num = attach_vec->length ();
    8604          394 :       u (num);
    8605          824 :       for (unsigned ix = 0; ix != num; ix++)
    8606              :         {
    8607          430 :           tree attached = (*attach_vec)[ix];
    8608          430 :           if (attached)
    8609              :             {
    8610          430 :               tree ti = TYPE_TEMPLATE_INFO (TREE_TYPE (attached));
    8611          430 :               if (!dep_hash->find_dependency (attached)
    8612          430 :                   && !(ti && dep_hash->find_dependency (TI_TEMPLATE (ti))))
    8613              :                 attached = NULL_TREE;
    8614              :             }
    8615              : 
    8616          430 :           tree_node (attached);
    8617          472 :           dump (dumper::MERGE)
    8618           30 :             && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
    8619              :         }
    8620              :     }
    8621              : 
    8622      4501499 :   bool is_typedef = false;
    8623      4501499 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8624              :     {
    8625       874431 :       tree t = TREE_TYPE (inner);
    8626       874431 :       unsigned tdef_flags = 0;
    8627       874431 :       if (DECL_ORIGINAL_TYPE (inner)
    8628       874431 :           && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8629              :         {
    8630       874431 :           tdef_flags |= 1;
    8631       874431 :           if (TYPE_STRUCTURAL_EQUALITY_P (t)
    8632       192014 :               && TYPE_DEPENDENT_P_VALID (t)
    8633      1057727 :               && TYPE_DEPENDENT_P (t))
    8634              :             tdef_flags |= 2;
    8635              :         }
    8636       874431 :       if (streaming_p ())
    8637       298586 :         u (tdef_flags);
    8638              : 
    8639       874431 :       if (tdef_flags & 1)
    8640              :         {
    8641              :           /* A typedef type.  */
    8642       874431 :           int type_tag = insert (t);
    8643       874431 :           if (streaming_p ())
    8644       298586 :             dump (dumper::TREE)
    8645          206 :               && dump ("Cloned:%d %s %C:%N", type_tag,
    8646              :                        tdef_flags & 2 ? "depalias" : "typedef",
    8647          206 :                        TREE_CODE (t), t);
    8648              : 
    8649              :           is_typedef = true;
    8650              :         }
    8651              :     }
    8652              : 
    8653      4501499 :   if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    8654              :     {
    8655       136151 :       bool cloned_p
    8656       136151 :         = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    8657        96680 :       bool needs_vtt_parm_p
    8658        96680 :         = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
    8659        96680 :       bool omit_inherited_parms_p
    8660        96680 :         = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
    8661        77340 :            && base_ctor_omit_inherited_parms (decl));
    8662       136151 :       unsigned flags = (int (cloned_p) << 0
    8663       136151 :                         | int (needs_vtt_parm_p) << 1
    8664       136151 :                         | int (omit_inherited_parms_p) << 2);
    8665       136151 :       u (flags);
    8666       136230 :       dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    8667              :                                    decl, cloned_p ? "" : "not ");
    8668              :     }
    8669              : 
    8670      4501499 :   if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    8671          172 :     u (decl_tls_model (decl));
    8672              : 
    8673      4501499 :   if (streaming_p ())
    8674      1643514 :     dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
    8675          683 :                                  TREE_CODE (decl), decl);
    8676              : 
    8677      4501499 :   if (NAMESPACE_SCOPE_P (inner))
    8678      2536650 :     gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
    8679              :                                   && DECL_LOCAL_DECL_P (inner)));
    8680      3233021 :   else if ((TREE_CODE (inner) == TYPE_DECL
    8681       868475 :             && !is_typedef
    8682       166148 :             && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8683      3935348 :            || TREE_CODE (inner) == FUNCTION_DECL)
    8684              :     {
    8685      1534010 :       bool write_defn = !dep && has_definition (decl);
    8686      1534010 :       if (streaming_p ())
    8687       511567 :         u (write_defn);
    8688      1534010 :       if (write_defn)
    8689            6 :         write_definition (decl);
    8690              :     }
    8691              : }
    8692              : 
    8693              : tree
    8694      1231942 : trees_in::decl_value ()
    8695              : {
    8696      1231942 :   int tag = 0;
    8697      1231942 :   bool is_attached = false;
    8698      1231942 :   bool has_defn = false;
    8699      1231942 :   unsigned mk_u = u ();
    8700      1231942 :   if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
    8701              :     {
    8702            0 :       set_overrun ();
    8703            0 :       return NULL_TREE;
    8704              :     }
    8705              : 
    8706      1231942 :   unsigned saved_unused = unused;
    8707      1231942 :   unused = 0;
    8708              : 
    8709      1231942 :   merge_kind mk = merge_kind (mk_u);
    8710              : 
    8711      1231942 :   tree decl = start ();
    8712      1231942 :   if (decl)
    8713              :     {
    8714      1231942 :       if (mk != MK_unique)
    8715              :         {
    8716      1013830 :           bits_in bits = stream_bits ();
    8717      1013830 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8718        87346 :             is_attached = bits.b ();
    8719              : 
    8720      1013830 :           has_defn = bits.b ();
    8721      1013830 :         }
    8722              : 
    8723      1231942 :       if (!tree_node_bools (decl))
    8724            0 :         decl = NULL_TREE;
    8725              :     }
    8726              : 
    8727              :   /* Insert into map.  */
    8728      1231942 :   tag = insert (decl);
    8729      1231942 :   if (decl)
    8730      1231942 :     dump (dumper::TREE)
    8731          964 :       && dump ("Reading:%d %C", tag, TREE_CODE (decl));
    8732              : 
    8733      1231942 :   tree inner = decl;
    8734      1231942 :   int inner_tag = 0;
    8735      1231942 :   if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
    8736              :     {
    8737       332497 :       int code = u ();
    8738       332497 :       inner = start (code);
    8739       332497 :       if (inner && tree_node_bools (inner))
    8740       332497 :         DECL_TEMPLATE_RESULT (decl) = inner;
    8741              :       else
    8742            0 :         decl = NULL_TREE;
    8743              : 
    8744       332497 :       inner_tag = insert (inner);
    8745       332497 :       if (decl)
    8746       332497 :         dump (dumper::TREE)
    8747          204 :           && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
    8748              :     }
    8749              : 
    8750      1231942 :   tree type = NULL_TREE;
    8751      1231942 :   int type_tag = 0;
    8752      1231942 :   tree stub_decl = NULL_TREE;
    8753      1231942 :   int stub_tag = 0;
    8754      1231942 :   if (decl && TREE_CODE (inner) == TYPE_DECL)
    8755              :     {
    8756       395532 :       if (unsigned type_code = u ())
    8757              :         {
    8758       178677 :           type = start (type_code);
    8759       178677 :           if (type && tree_node_bools (type))
    8760              :             {
    8761       178677 :               TREE_TYPE (inner) = type;
    8762       178677 :               TYPE_NAME (type) = inner;
    8763              :             }
    8764              :           else
    8765            0 :             decl = NULL_TREE;
    8766              : 
    8767       178677 :           type_tag = insert (type);
    8768       178677 :           if (decl)
    8769       178677 :             dump (dumper::TREE)
    8770          212 :               && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
    8771              : 
    8772       178677 :           if (unsigned stub_code = u ())
    8773              :             {
    8774          441 :               stub_decl = start (stub_code);
    8775          441 :               if (stub_decl && tree_node_bools (stub_decl))
    8776              :                 {
    8777          441 :                   TREE_TYPE (stub_decl) = type;
    8778          441 :                   TYPE_STUB_DECL (type) = stub_decl;
    8779              :                 }
    8780              :               else
    8781            0 :                 decl = NULL_TREE;
    8782              : 
    8783          441 :               stub_tag = insert (stub_decl);
    8784          441 :               if (decl)
    8785          441 :                 dump (dumper::TREE)
    8786            0 :                   && dump ("Reading stub_decl:%d %C", stub_tag,
    8787            0 :                            TREE_CODE (stub_decl));
    8788              :             }
    8789              :         }
    8790              :     }
    8791              : 
    8792      1231942 :   if (!decl)
    8793              :     {
    8794            0 :     bail:
    8795            0 :       if (inner_tag != 0)
    8796            0 :         back_refs[~inner_tag] = NULL_TREE;
    8797            0 :       if (type_tag != 0)
    8798            0 :         back_refs[~type_tag] = NULL_TREE;
    8799            0 :       if (stub_tag != 0)
    8800            0 :         back_refs[~stub_tag] = NULL_TREE;
    8801            0 :       if (tag != 0)
    8802            0 :         back_refs[~tag] = NULL_TREE;
    8803            0 :       set_overrun ();
    8804              :       /* Bail.  */
    8805            0 :       unused = saved_unused;
    8806            0 :       return NULL_TREE;
    8807              :     }
    8808              : 
    8809              :   /* Read the container, to ensure it's already been streamed in.  */
    8810      1231942 :   tree container = decl_container ();
    8811      1231942 :   unsigned tpl_levels = 0;
    8812              : 
    8813              :   /* If this is an imported temploid friend, get the owning decl its
    8814              :      attachment is determined by (or NULL_TREE otherwise).  */
    8815      1231942 :   tree temploid_friend = NULL_TREE;
    8816      1231942 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8817       866903 :     temploid_friend = tree_node ();
    8818              : 
    8819              :   /* Figure out if this decl is already known about.  */
    8820      1231942 :   int parm_tag = 0;
    8821              : 
    8822      1231942 :   if (decl != inner)
    8823       332497 :     if (!tpl_header (decl, &tpl_levels))
    8824            0 :       goto bail;
    8825      1231942 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8826       471371 :     parm_tag = fn_parms_init (inner);
    8827              : 
    8828      1231942 :   tree existing = key_mergeable (tag, mk, decl, inner, type, container,
    8829              :                                  is_attached, temploid_friend);
    8830      1231942 :   tree existing_inner = existing;
    8831      1231942 :   if (existing)
    8832              :     {
    8833       439592 :       if (existing == error_mark_node)
    8834            0 :         goto bail;
    8835              : 
    8836       439592 :       if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
    8837              :         {
    8838       163066 :           tree etype = TREE_TYPE (existing);
    8839       163066 :           if (TYPE_LANG_SPECIFIC (etype)
    8840       117710 :               && COMPLETE_TYPE_P (etype)
    8841       231656 :               && !CLASSTYPE_MEMBER_VEC (etype))
    8842              :             /* Give it a member vec, we're likely gonna be looking
    8843              :                inside it.  */
    8844        14268 :             set_class_bindings (etype, -1);
    8845              :         }
    8846              : 
    8847              :       /* Install the existing decl into the back ref array.  */
    8848       439592 :       register_duplicate (decl, existing);
    8849       439592 :       back_refs[~tag] = existing;
    8850       439592 :       if (inner_tag != 0)
    8851              :         {
    8852       146294 :           existing_inner = DECL_TEMPLATE_RESULT (existing);
    8853       146294 :           back_refs[~inner_tag] = existing_inner;
    8854              :         }
    8855              : 
    8856       439592 :       if (type_tag != 0)
    8857              :         {
    8858        78215 :           tree existing_type = TREE_TYPE (existing);
    8859        78215 :           back_refs[~type_tag] = existing_type;
    8860        78215 :           if (stub_tag != 0)
    8861          245 :             back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
    8862              :         }
    8863              :     }
    8864              : 
    8865      1231942 :   if (parm_tag)
    8866       471371 :     fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
    8867              : 
    8868      1231942 :   if (!tree_node_vals (decl))
    8869            0 :     goto bail;
    8870              : 
    8871      1231942 :   if (inner_tag)
    8872              :     {
    8873       332497 :       gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
    8874              : 
    8875       332497 :       if (!tree_node_vals (inner))
    8876            0 :         goto bail;
    8877              : 
    8878       332497 :       if (!tpl_parms_fini (decl, tpl_levels))
    8879            0 :         goto bail;
    8880              :     }
    8881              : 
    8882      1231942 :   if (type && (!tree_node_vals (type)
    8883       178677 :                || (stub_decl && !tree_node_vals (stub_decl))))
    8884            0 :     goto bail;
    8885              : 
    8886      1231942 :   spec_entry spec;
    8887      1231942 :   unsigned spec_flags = 0;
    8888      1231942 :   if (mk & MK_template_mask
    8889       850802 :       || mk == MK_partial
    8890       850802 :       || mk == MK_friend_spec)
    8891              :     {
    8892        10961 :       if (mk == MK_partial)
    8893              :         spec_flags = 2;
    8894              :       else
    8895       381140 :         spec_flags = u ();
    8896              : 
    8897       392101 :       spec.tmpl = tree_node ();
    8898       392101 :       spec.args = tree_node ();
    8899              :     }
    8900              :   /* Hold constraints on the spec field, for a short while.  */
    8901      1231942 :   spec.spec = tree_node ();
    8902              : 
    8903      1232906 :   dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
    8904              : 
    8905      1231942 :   existing = back_refs[~tag];
    8906      1231942 :   bool installed = install_entity (existing);
    8907      1231942 :   bool is_new = existing == decl;
    8908              : 
    8909      1231942 :   if (DECL_LANG_SPECIFIC (inner)
    8910      2311698 :       && DECL_MODULE_KEYED_DECLS_P (inner))
    8911              :     {
    8912              :       /* Read and maybe install the attached entities.  */
    8913          377 :       bool existed;
    8914          377 :       auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
    8915              :                                               &existed);
    8916          377 :       unsigned num = u ();
    8917          377 :       if (is_new == existed)
    8918            0 :         set_overrun ();
    8919          377 :       if (is_new)
    8920          244 :         set.reserve (num);
    8921          803 :       for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
    8922              :         {
    8923          426 :           tree attached = tree_node ();
    8924          426 :           dump (dumper::MERGE)
    8925          105 :             && dump ("Read %d[%u] %s attached decl %N", tag, ix,
    8926              :                      is_new ? "new" : "matched", attached);
    8927          426 :           if (is_new)
    8928          274 :             set.quick_push (attached);
    8929          152 :           else if (set[ix] != attached)
    8930              :             {
    8931            3 :               if (!set[ix] || !attached)
    8932              :                 /* One import left a hole for a lambda dep we chose not
    8933              :                    to stream, but another import chose to stream that lambda.
    8934              :                    Let's not error here: hopefully we'll complain later in
    8935              :                    is_matching_decl about whatever caused us to make a
    8936              :                    different decision.  */
    8937              :                 ;
    8938              :               else
    8939            0 :                 set_overrun ();
    8940              :             }
    8941              :         }
    8942              :     }
    8943              : 
    8944              :   /* Regular typedefs will have a NULL TREE_TYPE at this point.  */
    8945      1231942 :   unsigned tdef_flags = 0;
    8946      1231942 :   bool is_typedef = false;
    8947      1231942 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8948              :     {
    8949       216855 :       tdef_flags = u ();
    8950       216855 :       if (tdef_flags & 1)
    8951       216855 :         is_typedef = true;
    8952              :     }
    8953              : 
    8954      1231942 :   if (is_new)
    8955              :     {
    8956              :       /* A newly discovered node.  */
    8957       792350 :       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
    8958              :         /* Mark this identifier as naming a virtual function --
    8959              :            lookup_overrides relies on this optimization.  */
    8960         6620 :         IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
    8961              : 
    8962       792350 :       if (installed)
    8963              :         {
    8964              :           /* Mark the entity as imported.  */
    8965       506237 :           retrofit_lang_decl (inner);
    8966       506237 :           DECL_MODULE_IMPORT_P (inner) = true;
    8967              :         }
    8968              : 
    8969       792350 :       if (temploid_friend)
    8970           38 :         imported_temploid_friends->put (decl, temploid_friend);
    8971              : 
    8972       792350 :       if (spec.spec)
    8973        33740 :         set_constraints (decl, spec.spec);
    8974              : 
    8975       792350 :       if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
    8976              :         {
    8977            0 :           decl = cache_integer_cst (decl, true);
    8978            0 :           back_refs[~tag] = decl;
    8979              :         }
    8980              : 
    8981       792350 :       if (is_typedef)
    8982              :         {
    8983              :           /* Frob it to be ready for cloning.  */
    8984       132004 :           TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
    8985       132004 :           DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
    8986       132004 :           if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
    8987              :             {
    8988       132001 :               set_underlying_type (inner);
    8989       132001 :               if (tdef_flags & 2)
    8990              :                 {
    8991              :                   /* Match instantiate_alias_template's handling.  */
    8992        33551 :                   tree type = TREE_TYPE (inner);
    8993        33551 :                   TYPE_DEPENDENT_P (type) = true;
    8994        33551 :                   TYPE_DEPENDENT_P_VALID (type) = true;
    8995        33551 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
    8996              :                 }
    8997              :             }
    8998              :         }
    8999              : 
    9000       792350 :       if (inner_tag)
    9001              :         /* Set the TEMPLATE_DECL's type.  */
    9002       186203 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9003              : 
    9004              :       /* Redetermine whether we need to import or export this declaration
    9005              :          for this TU.  But for extern templates we know we must import:
    9006              :          they'll be defined in a different TU.
    9007              :          FIXME: How do dllexport and dllimport interact across a module?
    9008              :          See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
    9009              :          May have to revisit?  */
    9010       792350 :       if (type
    9011       100462 :           && CLASS_TYPE_P (type)
    9012        86663 :           && TYPE_LANG_SPECIFIC (type)
    9013       879013 :           && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
    9014          769 :                && CLASSTYPE_INTERFACE_KNOWN (type)
    9015          769 :                && CLASSTYPE_INTERFACE_ONLY (type)))
    9016              :         {
    9017        85946 :           CLASSTYPE_INTERFACE_ONLY (type) = false;
    9018        85946 :           CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
    9019              :         }
    9020              : 
    9021              :       /* Add to specialization tables now that constraints etc are
    9022              :          added.  */
    9023       792350 :       if (mk == MK_partial)
    9024              :         {
    9025         4771 :           bool is_type = TREE_CODE (inner) == TYPE_DECL;
    9026         4771 :           spec.spec = is_type ? type : inner;
    9027         4771 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9028              :         }
    9029       787579 :       else if (mk & MK_template_mask)
    9030              :         {
    9031       228879 :           bool is_type = !(mk & MK_tmpl_decl_mask);
    9032       228879 :           spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
    9033       228879 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9034              :         }
    9035              : 
    9036       792350 :       if (NAMESPACE_SCOPE_P (decl)
    9037       166494 :           && (mk == MK_named || mk == MK_unique
    9038       166494 :               || mk == MK_enum || mk == MK_friend_spec)
    9039       866948 :           && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
    9040        74497 :         add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
    9041              : 
    9042       792350 :       if (DECL_ARTIFICIAL (decl)
    9043       217184 :           && TREE_CODE (decl) == FUNCTION_DECL
    9044        23239 :           && !DECL_TEMPLATE_INFO (decl)
    9045        22927 :           && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
    9046        22749 :           && TYPE_SIZE (DECL_CONTEXT (decl))
    9047       793932 :           && !DECL_THUNK_P (decl))
    9048              :         /* A new implicit member function, when the class is
    9049              :            complete.  This means the importee declared it, and
    9050              :            we must now add it to the class.  Note that implicit
    9051              :            member fns of template instantiations do not themselves
    9052              :            look like templates.  */
    9053         1008 :         if (!install_implicit_member (inner))
    9054            0 :           set_overrun ();
    9055              : 
    9056              :       /* When importing a TLS wrapper from a header unit, we haven't
    9057              :          actually emitted its definition yet. Remember it so we can
    9058              :          do this later.  */
    9059       792350 :       if (state->is_header ()
    9060       792350 :           && decl_tls_wrapper_p (decl))
    9061            6 :         note_vague_linkage_fn (decl);
    9062              : 
    9063              :       /* Apply relevant attributes.
    9064              :          FIXME should probably use cplus_decl_attributes for this,
    9065              :          but it's not yet ready for modules.  */
    9066              : 
    9067       792350 :       if (VAR_OR_FUNCTION_DECL_P (inner))
    9068       478447 :         if (tree attr = lookup_attribute ("section", DECL_ATTRIBUTES (inner)))
    9069              :           {
    9070            6 :             tree section_name = TREE_VALUE (TREE_VALUE (attr));
    9071            6 :             set_decl_section_name (inner, TREE_STRING_POINTER (section_name));
    9072              :           }
    9073              : 
    9074              :       /* Setup aliases for the declaration.  */
    9075       792350 :       if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
    9076              :         {
    9077            3 :           alias = TREE_VALUE (TREE_VALUE (alias));
    9078            3 :           alias = get_identifier (TREE_STRING_POINTER (alias));
    9079            3 :           assemble_alias (decl, alias);
    9080              :         }
    9081              :     }
    9082              :   else
    9083              :     {
    9084              :       /* DECL is the to-be-discarded decl.  Its internal pointers will
    9085              :          be to the EXISTING's structure.  Frob it to point to its
    9086              :          own other structures, so loading its definition will alter
    9087              :          it, and not the existing decl.  */
    9088       441354 :       dump (dumper::MERGE) && dump ("Deduping %N", existing);
    9089              : 
    9090       439592 :       if (inner_tag)
    9091       146294 :         DECL_TEMPLATE_RESULT (decl) = inner;
    9092              : 
    9093       439592 :       if (type)
    9094              :         {
    9095              :           /* Point at the to-be-discarded type & decl.  */
    9096        78215 :           TYPE_NAME (type) = inner;
    9097        78215 :           TREE_TYPE (inner) = type;
    9098              : 
    9099       156185 :           TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
    9100        78215 :           if (stub_decl)
    9101          245 :             TREE_TYPE (stub_decl) = type;
    9102              : 
    9103        78215 :           tree etype = TREE_TYPE (existing);
    9104              : 
    9105              :           /* Handle separate declarations with different attributes.  */
    9106        78215 :           tree &dattr = TYPE_ATTRIBUTES (type);
    9107        78215 :           tree &eattr = TYPE_ATTRIBUTES (etype);
    9108        78215 :           check_abi_tags (existing, decl, eattr, dattr);
    9109              :           // TODO: handle other conflicting type attributes
    9110        78215 :           eattr = merge_attributes (eattr, dattr);
    9111              : 
    9112              :           /* When merging a partial specialisation, the existing decl may have
    9113              :              had its TYPE_CANONICAL adjusted.  If so we should use structural
    9114              :              equality to ensure is_matching_decl doesn't get confused.  */
    9115        78215 :           if ((spec_flags & 2)
    9116        78215 :               && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
    9117            3 :             SET_TYPE_STRUCTURAL_EQUALITY (type);
    9118              :         }
    9119              : 
    9120       439592 :       if (inner_tag)
    9121              :         /* Set the TEMPLATE_DECL's type.  */
    9122       146294 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9123              : 
    9124       439592 :       if (!is_matching_decl (existing, decl, is_typedef))
    9125           42 :         unmatched_duplicate (existing);
    9126              : 
    9127       439592 :       if (TREE_CODE (inner) == FUNCTION_DECL)
    9128              :         {
    9129       201056 :           tree e_inner = STRIP_TEMPLATE (existing);
    9130       201056 :           for (auto parm = DECL_ARGUMENTS (inner);
    9131       601408 :                parm; parm = DECL_CHAIN (parm))
    9132       400352 :             DECL_CONTEXT (parm) = e_inner;
    9133              :         }
    9134              : 
    9135              :       /* And our result is the existing node.  */
    9136       439592 :       decl = existing;
    9137              :     }
    9138              : 
    9139      1231942 :   if (mk == MK_friend_spec)
    9140              :     {
    9141            0 :       tree e = match_mergeable_specialization (true, &spec);
    9142            0 :       if (!e)
    9143              :         {
    9144            0 :           spec.spec = inner;
    9145            0 :           add_mergeable_specialization (true, &spec, decl, spec_flags);
    9146              :         }
    9147            0 :       else if (e != existing)
    9148            0 :         set_overrun ();
    9149              :     }
    9150              : 
    9151      1231942 :   if (is_typedef)
    9152              :     {
    9153              :       /* Insert the type into the array now.  */
    9154       216855 :       tag = insert (TREE_TYPE (decl));
    9155       216855 :       dump (dumper::TREE)
    9156          247 :         && dump ("Cloned:%d typedef %C:%N",
    9157          247 :                  tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
    9158              :     }
    9159              : 
    9160      1231942 :   unused = saved_unused;
    9161              : 
    9162      1231942 :   if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    9163              :     {
    9164       105920 :       unsigned flags = u ();
    9165              : 
    9166       105920 :       if (is_new)
    9167              :         {
    9168        65375 :           bool cloned_p = flags & 1;
    9169        65475 :           dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    9170              :                                        decl, cloned_p ? "" : "not ");
    9171        65375 :           if (cloned_p)
    9172              :             {
    9173              :               /* Update the member vec, if there is one (we're in a different
    9174              :                  cluster to the class defn) and this isn't a primary template
    9175              :                  specialization (as in tsubst_function_decl).  */
    9176        46259 :               bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
    9177        46259 :                          && !primary_template_specialization_p (decl));
    9178        46259 :               build_cdtor_clones (decl, flags & 2, flags & 4, up);
    9179              :             }
    9180              :         }
    9181              :     }
    9182              : 
    9183      1231942 :   if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    9184              :     {
    9185          160 :       enum tls_model model = tls_model (u ());
    9186          160 :       if (is_new)
    9187          140 :         set_decl_tls_model (decl, model);
    9188              :     }
    9189              : 
    9190      1231942 :   if (!NAMESPACE_SCOPE_P (inner)
    9191       918844 :       && ((TREE_CODE (inner) == TYPE_DECL
    9192       212998 :            && !is_typedef
    9193        38144 :            && TYPE_NAME (TREE_TYPE (inner)) == inner)
    9194       880700 :           || TREE_CODE (inner) == FUNCTION_DECL)
    9195      1629370 :       && u ())
    9196            3 :     read_definition (decl);
    9197              : 
    9198              :   return decl;
    9199              : }
    9200              : 
    9201              : /* DECL is an unnameable member of CTX.  Return a suitable identifying
    9202              :    index.  */
    9203              : 
    9204              : static unsigned
    9205         1526 : get_field_ident (tree ctx, tree decl)
    9206              : {
    9207         1526 :   gcc_checking_assert (TREE_CODE (decl) == USING_DECL
    9208              :                        || !DECL_NAME (decl)
    9209              :                        || IDENTIFIER_ANON_P (DECL_NAME (decl)));
    9210              : 
    9211         1526 :   unsigned ix = 0;
    9212         1526 :   for (tree fields = TYPE_FIELDS (ctx);
    9213        11385 :        fields; fields = DECL_CHAIN (fields))
    9214              :     {
    9215        11385 :       if (fields == decl)
    9216         1526 :         return ix;
    9217              : 
    9218         9859 :       if (DECL_CONTEXT (fields) == ctx
    9219         9859 :           && (TREE_CODE (fields) == USING_DECL
    9220         9840 :               || (TREE_CODE (fields) == FIELD_DECL
    9221           88 :                   && (!DECL_NAME (fields)
    9222           44 :                       || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9223              :         /* Count this field.  */
    9224           47 :         ix++;
    9225              :     }
    9226            0 :   gcc_unreachable ();
    9227              : }
    9228              : 
    9229              : static tree
    9230         1055 : lookup_field_ident (tree ctx, unsigned ix)
    9231              : {
    9232         1055 :   for (tree fields = TYPE_FIELDS (ctx);
    9233         8104 :        fields; fields = DECL_CHAIN (fields))
    9234         8104 :     if (DECL_CONTEXT (fields) == ctx
    9235         8104 :         && (TREE_CODE (fields) == USING_DECL
    9236         8092 :             || (TREE_CODE (fields) == FIELD_DECL
    9237         1120 :                 && (!DECL_NAME (fields)
    9238           25 :                     || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9239         1101 :       if (!ix--)
    9240              :         return fields;
    9241              : 
    9242              :   return NULL_TREE;
    9243              : }
    9244              : 
    9245              : /* Reference DECL.  REF indicates the walk kind we are performing.
    9246              :    Return true if we should write this decl by value.  */
    9247              : 
    9248              : bool
    9249     15717650 : trees_out::decl_node (tree decl, walk_kind ref)
    9250              : {
    9251     15717650 :   gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
    9252              :                        && DECL_CONTEXT (decl));
    9253              : 
    9254     15717650 :   if (ref == WK_value)
    9255              :     {
    9256      1523104 :       depset *dep = dep_hash->find_dependency (decl);
    9257      1523104 :       decl_value (decl, dep);
    9258      1523104 :       return false;
    9259              :     }
    9260              : 
    9261     14194546 :   switch (TREE_CODE (decl))
    9262              :     {
    9263              :     default:
    9264              :       break;
    9265              : 
    9266      1500717 :     case FUNCTION_DECL:
    9267      1500717 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9268              :       break;
    9269              : 
    9270              :     case RESULT_DECL:
    9271              :       /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
    9272              :          referenced when we're inside the function itself.  */
    9273              :       return true;
    9274              : 
    9275       231764 :     case PARM_DECL:
    9276       231764 :       {
    9277       231764 :         if (streaming_p ())
    9278       100648 :           i (tt_parm);
    9279       231764 :         tree_node (DECL_CONTEXT (decl));
    9280              : 
    9281              :         /* That must have put this in the map.  */
    9282       231764 :         walk_kind ref = ref_node (decl);
    9283       231764 :         if (ref != WK_none)
    9284              :           // FIXME:OPTIMIZATION We can wander into bits of the
    9285              :           // template this was instantiated from, for instance
    9286              :           // deferred noexcept and default parms, or references
    9287              :           // to parms from earlier forward-decls (PR c++/119608).
    9288              :           //
    9289              :           // Currently we'll end up cloning those bits of tree.
    9290              :           // It would be nice to reference those specific nodes.
    9291              :           // I think putting those things in the map when we
    9292              :           // reference their template by name.
    9293              :           //
    9294              :           // See the note in add_indirects.
    9295              :           return true;
    9296              : 
    9297            0 :         if (streaming_p ())
    9298            0 :           dump (dumper::TREE)
    9299            0 :             && dump ("Wrote %s reference %N",
    9300            0 :                      TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
    9301              :                      decl);
    9302              :       }
    9303              :       return false;
    9304              : 
    9305              :     case IMPORTED_DECL:
    9306              :       /* This describes a USING_DECL to the ME's debug machinery.  It
    9307              :          originates from the fortran FE, and has nothing to do with
    9308              :          C++ modules.  */
    9309              :       return true;
    9310              : 
    9311              :     case LABEL_DECL:
    9312              :       return true;
    9313              : 
    9314        85753 :     case CONST_DECL:
    9315        85753 :       {
    9316              :         /* If I end up cloning enum decls, implementing C++20 using
    9317              :            E::v, this will need tweaking.   */
    9318        85753 :         if (streaming_p ())
    9319        21324 :           i (tt_enum_decl);
    9320        85753 :         tree ctx = DECL_CONTEXT (decl);
    9321        85753 :         gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
    9322        85753 :         tree_node (ctx);
    9323        85753 :         tree_node (DECL_NAME (decl));
    9324              : 
    9325        85753 :         int tag = insert (decl);
    9326        85753 :         if (streaming_p ())
    9327        21324 :           dump (dumper::TREE)
    9328           21 :             && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
    9329              :         return false;
    9330              :       }
    9331        33293 :       break;
    9332              : 
    9333        33293 :     case USING_DECL:
    9334        33293 :       if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
    9335              :         break;
    9336              :       /* FALLTHROUGH  */
    9337              : 
    9338       213387 :     case FIELD_DECL:
    9339       213387 :       {
    9340       213387 :         if (streaming_p ())
    9341        15392 :           i (tt_data_member);
    9342              : 
    9343       213387 :         tree ctx = DECL_CONTEXT (decl);
    9344       213387 :         tree_node (ctx);
    9345              : 
    9346       213387 :         tree name = NULL_TREE;
    9347              : 
    9348       213387 :         if (TREE_CODE (decl) == USING_DECL)
    9349              :           ;
    9350              :         else
    9351              :           {
    9352       211792 :             name = DECL_NAME (decl);
    9353       411227 :             if (name && IDENTIFIER_ANON_P (name))
    9354              :               name = NULL_TREE;
    9355              :           }
    9356              : 
    9357       213387 :         tree_node (name);
    9358       213387 :         if (!name && streaming_p ())
    9359              :           {
    9360         1526 :             unsigned ix = get_field_ident (ctx, decl);
    9361         1526 :             u (ix);
    9362              :           }
    9363              : 
    9364       213387 :         int tag = insert (decl);
    9365       213387 :         if (streaming_p ())
    9366        15392 :           dump (dumper::TREE)
    9367           26 :             && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
    9368              :         return false;
    9369              :       }
    9370       648818 :       break;
    9371              : 
    9372       648818 :     case VAR_DECL:
    9373       648818 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9374       648818 :       if (DECL_VTABLE_OR_VTT_P (decl))
    9375              :         {
    9376              :           /* VTT or VTABLE, they are all on the vtables list.  */
    9377         4204 :           tree ctx = CP_DECL_CONTEXT (decl);
    9378         4204 :           tree vtable = CLASSTYPE_VTABLES (ctx);
    9379         4309 :           for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
    9380         4309 :             if (vtable == decl)
    9381              :               {
    9382         4204 :                 gcc_checking_assert (DECL_VIRTUAL_P (decl));
    9383         4204 :                 if (streaming_p ())
    9384              :                   {
    9385           43 :                     u (tt_vtable);
    9386           43 :                     u (ix);
    9387           43 :                     dump (dumper::TREE)
    9388            0 :                       && dump ("Writing vtable %N[%u]", ctx, ix);
    9389              :                   }
    9390         4204 :                 tree_node (ctx);
    9391         4204 :                 return false;
    9392              :               }
    9393              :           gcc_unreachable ();
    9394              :         }
    9395              : 
    9396       644614 :       if (DECL_TINFO_P (decl))
    9397              :         {
    9398         8458 :         tinfo:
    9399              :           /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var.  */
    9400        15589 :           bool is_var = VAR_P (decl);
    9401        15589 :           tree type = TREE_TYPE (decl);
    9402        15589 :           unsigned ix = get_pseudo_tinfo_index (type);
    9403        15589 :           if (streaming_p ())
    9404              :             {
    9405         9866 :               i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
    9406         6984 :               u (ix);
    9407              :             }
    9408              : 
    9409        15589 :           if (is_var)
    9410              :             {
    9411              :               /* We also need the type it is for and mangled name, so
    9412              :                  the reader doesn't need to complete the type (which
    9413              :                  would break section ordering).  The type it is for is
    9414              :                  stashed on the name's TREE_TYPE.  */
    9415         8458 :               tree name = DECL_NAME (decl);
    9416         8458 :               tree_node (name);
    9417         8458 :               type = TREE_TYPE (name);
    9418         8458 :               tree_node (type);
    9419              :             }
    9420              : 
    9421        15589 :           int tag = insert (decl);
    9422        15589 :           if (streaming_p ())
    9423         6984 :             dump (dumper::TREE)
    9424           27 :               && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
    9425              :                        tag, ix, type);
    9426              : 
    9427        15589 :           if (!is_var)
    9428              :             {
    9429         7131 :               tag = insert (type);
    9430         7131 :               if (streaming_p ())
    9431         2882 :                 dump (dumper::TREE)
    9432            9 :                   && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
    9433              :             }
    9434        15589 :           return false;
    9435              :         }
    9436              : 
    9437       636156 :       if (DECL_NTTP_OBJECT_P (decl))
    9438              :         {
    9439              :           /* A NTTP parm object.  */
    9440           42 :           if (streaming_p ())
    9441           10 :             i (tt_nttp_var);
    9442           42 :           tree_node (tparm_object_argument (decl));
    9443           42 :           tree_node (DECL_NAME (decl));
    9444           42 :           int tag = insert (decl);
    9445           42 :           if (streaming_p ())
    9446           10 :             dump (dumper::TREE)
    9447            0 :               && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
    9448           42 :           return false;
    9449              :         }
    9450              : 
    9451              :       break;
    9452              : 
    9453      4868183 :     case TYPE_DECL:
    9454      4868183 :       if (DECL_TINFO_P (decl))
    9455         7131 :         goto tinfo;
    9456              :       /* c++/125768: For an imported typedef, also mark the original type
    9457              :          reachable in case it was instantiated here.  */
    9458      4439245 :       if (!streaming_p () && DECL_ORIGINAL_TYPE (decl)
    9459      5906880 :           && (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl)))
    9460         1675 :         tree_node (DECL_ORIGINAL_TYPE (decl));
    9461              :       break;
    9462              :     }
    9463              : 
    9464     12893434 :   if (DECL_THUNK_P (decl))
    9465              :     {
    9466              :       /* Thunks are similar to binfos -- write the thunked-to decl and
    9467              :          then thunk-specific key info.  */
    9468            0 :       if (streaming_p ())
    9469              :         {
    9470            0 :           i (tt_thunk);
    9471            0 :           i (THUNK_FIXED_OFFSET (decl));
    9472              :         }
    9473              : 
    9474              :       tree target = decl;
    9475            0 :       while (DECL_THUNK_P (target))
    9476            0 :         target = THUNK_TARGET (target);
    9477            0 :       tree_node (target);
    9478            0 :       tree_node (THUNK_VIRTUAL_OFFSET (decl));
    9479            0 :       int tag = insert (decl);
    9480            0 :       if (streaming_p ())
    9481            0 :         dump (dumper::TREE)
    9482            0 :           && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
    9483            0 :       return false;
    9484              :     }
    9485              : 
    9486     12893434 :   if (DECL_CLONED_FUNCTION_P (decl))
    9487              :     {
    9488       444532 :       tree target = get_clone_target (decl);
    9489       444532 :       if (streaming_p ())
    9490       211173 :         i (tt_clone_ref);
    9491              : 
    9492       444532 :       tree_node (target);
    9493       444532 :       tree_node (DECL_NAME (decl));
    9494       444532 :       if (DECL_VIRTUAL_P (decl))
    9495        30432 :         tree_node (DECL_VINDEX (decl));
    9496       444532 :       int tag = insert (decl);
    9497       444532 :       if (streaming_p ())
    9498       211173 :         dump (dumper::TREE)
    9499          164 :           && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
    9500       444532 :       return false;
    9501              :     }
    9502              : 
    9503              :   /* Everything left should be a thing that is in the entity table.
    9504              :      Mostly things that can be defined outside of their (original
    9505              :      declaration) context.  */
    9506     12448902 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
    9507              :                        || VAR_P (decl)
    9508              :                        || TREE_CODE (decl) == FUNCTION_DECL
    9509              :                        || TREE_CODE (decl) == TYPE_DECL
    9510              :                        || TREE_CODE (decl) == USING_DECL
    9511              :                        || TREE_CODE (decl) == CONCEPT_DECL
    9512              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
    9513              : 
    9514     12448902 :   int use_tpl = -1;
    9515     12448902 :   tree ti = node_template_info (decl, use_tpl);
    9516     12448902 :   tree tpl = NULL_TREE;
    9517              : 
    9518              :   /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
    9519              :      TEMPLATE_DECL.  Note TI_TEMPLATE is not a TEMPLATE_DECL for
    9520              :      (some) friends, so we need to check that.  */
    9521              :   // FIXME: Should local friend template specializations be by value?
    9522              :   // They don't get idents so we'll never know they're imported, but I
    9523              :   // think we can only reach them from the TU that defines the
    9524              :   // befriending class?
    9525      4551048 :   if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    9526     16999884 :       && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
    9527              :     {
    9528              :       tpl = TI_TEMPLATE (ti);
    9529      1150707 :     partial_template:
    9530      1150707 :       if (streaming_p ())
    9531              :         {
    9532         3761 :           i (tt_template);
    9533         3761 :           dump (dumper::TREE)
    9534            9 :             && dump ("Writing implicit template %C:%N%S",
    9535            9 :                      TREE_CODE (tpl), tpl, tpl);
    9536              :         }
    9537      1150707 :       tree_node (tpl);
    9538              : 
    9539              :       /* Streaming TPL caused us to visit DECL and maybe its type,
    9540              :          if it wasn't TU-local.  */
    9541      1150707 :       if (CHECKING_P && !has_tu_local_dep (tpl))
    9542              :         {
    9543      1150680 :           gcc_checking_assert (TREE_VISITED (decl));
    9544      1150680 :           if (DECL_IMPLICIT_TYPEDEF_P (decl))
    9545       593633 :             gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
    9546              :         }
    9547              :       return false;
    9548              :     }
    9549              : 
    9550     11416100 :   tree ctx = CP_DECL_CONTEXT (decl);
    9551     11416100 :   depset *dep = NULL;
    9552     11416100 :   if (streaming_p ())
    9553      1927947 :     dep = dep_hash->find_dependency (decl);
    9554      9488153 :   else if (TREE_CODE (ctx) != FUNCTION_DECL
    9555       379310 :            || TREE_CODE (decl) == TEMPLATE_DECL
    9556       342860 :            || DECL_IMPLICIT_TYPEDEF_P (decl)
    9557      9794855 :            || (DECL_LANG_SPECIFIC (decl)
    9558       164120 :                && DECL_MODULE_IMPORT_P (decl)))
    9559              :     {
    9560      9181451 :       auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
    9561       714503 :                    && !DECL_NAMESPACE_ALIAS (decl)
    9562      9181451 :                    ? depset::EK_NAMESPACE : depset::EK_DECL);
    9563      9181451 :       dep = dep_hash->add_dependency (decl, kind);
    9564              :     }
    9565              : 
    9566     11109398 :   if (!dep || dep->is_tu_local ())
    9567              :     {
    9568              :       /* Some internal entity of context.  Do by value.  */
    9569       592518 :       decl_value (decl, dep);
    9570       592518 :       return false;
    9571              :     }
    9572              : 
    9573     10823582 :   if (dep->get_entity_kind () == depset::EK_REDIRECT)
    9574              :     {
    9575              :       /* The DECL_TEMPLATE_RESULT of a partial specialization.
    9576              :          Write the partial specialization's template.  */
    9577       117905 :       depset *redirect = dep->deps[0];
    9578       117905 :       gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
    9579       117905 :       tpl = redirect->get_entity ();
    9580       117905 :       goto partial_template;
    9581              :     }
    9582              : 
    9583     10705677 :   if (streaming_p ())
    9584              :     {
    9585              :       /* Locate the entity.  */
    9586      1642391 :       unsigned index = dep->cluster;
    9587      1642391 :       unsigned import = 0;
    9588              : 
    9589      1642391 :       if (dep->is_import ())
    9590        11096 :         import = dep->section;
    9591      1631295 :       else if (CHECKING_P)
    9592              :         /* It should be what we put there.  */
    9593      1631295 :         gcc_checking_assert (index == ~import_entity_index (decl));
    9594              : 
    9595              : #if CHECKING_P
    9596        11096 :       gcc_assert (!import || importedness >= 0);
    9597              : #endif
    9598      1642391 :       i (tt_entity);
    9599      1642391 :       u (import);
    9600      1642391 :       u (index);
    9601              :     }
    9602              : 
    9603     10705677 :   int tag = insert (decl);
    9604     10705677 :   if (streaming_p () && dump (dumper::TREE))
    9605              :     {
    9606          529 :       char const *kind = "import";
    9607          529 :       module_state *from = this_module ();
    9608          529 :       if (dep->is_import ())
    9609              :         /* Rediscover the unremapped index.  */
    9610           78 :         from = import_entity_module (import_entity_index (decl));
    9611              :       else
    9612              :         {
    9613          451 :           tree o = get_originating_module_decl (decl);
    9614          451 :           o = STRIP_TEMPLATE (o);
    9615          902 :           kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
    9616          451 :                   ? "purview" : "GMF");
    9617              :         }
    9618          529 :       dump ("Wrote %s:%d %C:%N@%M", kind,
    9619          529 :             tag, TREE_CODE (decl), decl, from);
    9620              :     }
    9621              : 
    9622     10705677 :   add_indirects (decl);
    9623              : 
    9624     10705677 :   return false;
    9625              : }
    9626              : 
    9627              : void
    9628     12773752 : trees_out::type_node (tree type)
    9629              : {
    9630     12773752 :   gcc_assert (TYPE_P (type));
    9631              : 
    9632     12773752 :   tree root = (TYPE_NAME (type)
    9633     12773752 :                ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
    9634     12773752 :   gcc_checking_assert (root);
    9635              : 
    9636     12773752 :   if (type != root)
    9637              :     {
    9638      2859308 :       if (streaming_p ())
    9639       595673 :         i (tt_variant_type);
    9640      2859308 :       tree_node (root);
    9641              : 
    9642      2859308 :       int flags = -1;
    9643              : 
    9644      2859308 :       if (TREE_CODE (type) == FUNCTION_TYPE
    9645      2859308 :           || TREE_CODE (type) == METHOD_TYPE)
    9646              :         {
    9647       669841 :           int quals = type_memfn_quals (type);
    9648       669841 :           int rquals = type_memfn_rqual (type);
    9649       669841 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9650       669841 :           bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
    9651              : 
    9652       669841 :           if (raises != TYPE_RAISES_EXCEPTIONS (root)
    9653        21569 :               || rquals != type_memfn_rqual (root)
    9654        15481 :               || quals != type_memfn_quals (root)
    9655       685304 :               || late != TYPE_HAS_LATE_RETURN_TYPE (root))
    9656       669841 :             flags = rquals | (int (late) << 2) | (quals << 3);
    9657              :         }
    9658              :       else
    9659              :         {
    9660      2189467 :           if (TYPE_USER_ALIGN (type))
    9661        24066 :             flags = TYPE_ALIGN_RAW (type);
    9662              :         }
    9663              : 
    9664      2859308 :       if (streaming_p ())
    9665       595673 :         i (flags);
    9666              : 
    9667      2859308 :       if (flags < 0)
    9668              :         ;
    9669       693907 :       else if (TREE_CODE (type) == FUNCTION_TYPE
    9670       693907 :                || TREE_CODE (type) == METHOD_TYPE)
    9671              :         {
    9672       669841 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9673       669841 :           if (raises == TYPE_RAISES_EXCEPTIONS (root))
    9674        21569 :             raises = error_mark_node;
    9675       669841 :           tree_node (raises);
    9676              :         }
    9677              : 
    9678              :       /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
    9679              :          variants should all have the same set of attributes.  */
    9680      2859308 :       gcc_checking_assert (TYPE_ATTRIBUTES (type)
    9681              :                            == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
    9682              : 
    9683      2859308 :       if (streaming_p ())
    9684              :         {
    9685              :           /* Qualifiers.  */
    9686       595673 :           int rquals = cp_type_quals (root);
    9687       595673 :           int quals = cp_type_quals (type);
    9688       595673 :           if (quals == rquals)
    9689       274194 :             quals = -1;
    9690       595673 :           i (quals);
    9691              :         }
    9692              : 
    9693      2859308 :       if (ref_node (type) != WK_none)
    9694              :         {
    9695      2859308 :           int tag = insert (type);
    9696      2859308 :           if (streaming_p ())
    9697              :             {
    9698       595673 :               i (0);
    9699       595673 :               dump (dumper::TREE)
    9700          203 :                 && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
    9701              :             }
    9702              :         }
    9703      2859308 :       return;
    9704              :     }
    9705              : 
    9706      9914444 :   if (tree name = TYPE_NAME (type))
    9707      3921740 :     if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
    9708      3080927 :         || DECL_TEMPLATE_PARM_P (name)
    9709      2095096 :         || TREE_CODE (type) == RECORD_TYPE
    9710       365421 :         || TREE_CODE (type) == UNION_TYPE
    9711      4279120 :         || TREE_CODE (type) == ENUMERAL_TYPE)
    9712              :       {
    9713      3666462 :         gcc_checking_assert (DECL_P (name));
    9714              : 
    9715              :         /* We can meet template parms that we didn't meet in the
    9716              :            tpl_parms walk, because we're referring to a derived type
    9717              :            that was previously constructed from equivalent template
    9718              :            parms. */
    9719      3666462 :         if (streaming_p ())
    9720              :           {
    9721       253346 :             i (tt_typedef_type);
    9722       253346 :             dump (dumper::TREE)
    9723           59 :               && dump ("Writing %stypedef %C:%N",
    9724           59 :                        DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
    9725           59 :                        TREE_CODE (name), name);
    9726              :           }
    9727      3666462 :         tree_node (name);
    9728      3666462 :         if (streaming_p ())
    9729       253346 :           dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
    9730           59 :                                        TREE_CODE (name), name, name);
    9731              : 
    9732              :         /* We'll have either visited this type or have newly discovered
    9733              :            that it's TU-local; either way we won't need to visit it again.  */
    9734      3666462 :         gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
    9735      3666462 :         return;
    9736              :       }
    9737              : 
    9738      6247982 :   if (TYPE_PTRMEMFUNC_P (type))
    9739              :     {
    9740              :       /* This is a distinct type node, masquerading as a structure. */
    9741         5453 :       tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
    9742         5453 :       if (streaming_p ())
    9743         1521 :         i (tt_ptrmem_type);
    9744         5453 :       tree_node (fn_type);
    9745         5453 :       int tag = insert (type);
    9746         5453 :       if (streaming_p ())
    9747         1524 :         dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
    9748         5453 :       return;
    9749              :     }
    9750              : 
    9751      6242529 :   if (streaming_p ())
    9752              :     {
    9753      1993509 :       u (tt_derived_type);
    9754      1993509 :       u (TREE_CODE (type));
    9755              :     }
    9756              : 
    9757      6242529 :   tree_node (TREE_TYPE (type));
    9758      6242529 :   switch (TREE_CODE (type))
    9759              :     {
    9760            0 :     default:
    9761              :       /* We should never meet a type here that is indescribable in
    9762              :          terms of other types.  */
    9763            0 :       gcc_unreachable ();
    9764              : 
    9765        94734 :     case ARRAY_TYPE:
    9766        94734 :       tree_node (TYPE_DOMAIN (type));
    9767        94734 :       if (streaming_p ())
    9768              :         /* Dependent arrays are constructed with TYPE_DEPENENT_P
    9769              :            already set.  */
    9770        30769 :         u (TYPE_DEPENDENT_P (type));
    9771              :       break;
    9772              : 
    9773              :     case COMPLEX_TYPE:
    9774              :       /* No additional data.  */
    9775              :       break;
    9776              : 
    9777           12 :     case BOOLEAN_TYPE:
    9778              :       /* A non-standard boolean type.  */
    9779           12 :       if (streaming_p ())
    9780            6 :         u (TYPE_PRECISION (type));
    9781              :       break;
    9782              : 
    9783        88375 :     case INTEGER_TYPE:
    9784        88375 :       if (TREE_TYPE (type))
    9785              :         {
    9786              :           /* A range type (representing an array domain).  */
    9787        82440 :           tree_node (TYPE_MIN_VALUE (type));
    9788        82440 :           tree_node (TYPE_MAX_VALUE (type));
    9789              :         }
    9790              :       else
    9791              :         {
    9792              :           /* A new integral type (representing a bitfield).  */
    9793         5935 :           if (streaming_p ())
    9794              :             {
    9795         1808 :               unsigned prec = TYPE_PRECISION (type);
    9796         1808 :               bool unsigned_p = TYPE_UNSIGNED (type);
    9797              : 
    9798         1808 :               u ((prec << 1) | unsigned_p);
    9799              :             }
    9800              :         }
    9801              :       break;
    9802              : 
    9803      1361406 :     case METHOD_TYPE:
    9804      1361406 :     case FUNCTION_TYPE:
    9805      1361406 :       {
    9806      1361406 :         gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
    9807              : 
    9808      1361406 :         tree arg_types = TYPE_ARG_TYPES (type);
    9809      1361406 :         if (TREE_CODE (type) == METHOD_TYPE)
    9810              :           {
    9811       877043 :             tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
    9812       877043 :             arg_types = TREE_CHAIN (arg_types);
    9813              :           }
    9814      1361406 :         tree_node (arg_types);
    9815              :       }
    9816      1361406 :       break;
    9817              : 
    9818         1616 :     case OFFSET_TYPE:
    9819         1616 :       tree_node (TYPE_OFFSET_BASETYPE (type));
    9820         1616 :       break;
    9821              : 
    9822              :     case POINTER_TYPE:
    9823              :       /* No additional data.  */
    9824              :       break;
    9825              : 
    9826      1069879 :     case REFERENCE_TYPE:
    9827      1069879 :       if (streaming_p ())
    9828       238388 :         u (TYPE_REF_IS_RVALUE (type));
    9829              :       break;
    9830              : 
    9831      1267504 :     case DECLTYPE_TYPE:
    9832      1267504 :     case TYPEOF_TYPE:
    9833      1267504 :     case DEPENDENT_OPERATOR_TYPE:
    9834      1267504 :       tree_node (TYPE_VALUES_RAW (type));
    9835      1267504 :       if (TREE_CODE (type) == DECLTYPE_TYPE)
    9836              :         /* We stash a whole bunch of things into decltype's
    9837              :            flags.  */
    9838       103198 :         if (streaming_p ())
    9839        35144 :           tree_node_bools (type);
    9840              :       break;
    9841              : 
    9842         8661 :     case TRAIT_TYPE:
    9843         8661 :       tree_node (TRAIT_TYPE_KIND_RAW (type));
    9844         8661 :       tree_node (TRAIT_TYPE_TYPE1 (type));
    9845         8661 :       tree_node (TRAIT_TYPE_TYPE2 (type));
    9846         8661 :       break;
    9847              : 
    9848              :     case TYPE_ARGUMENT_PACK:
    9849              :       /* No additional data.  */
    9850              :       break;
    9851              : 
    9852       211140 :     case TYPE_PACK_EXPANSION:
    9853       211140 :       if (streaming_p ())
    9854        85321 :         u (PACK_EXPANSION_LOCAL_P (type));
    9855       422280 :       tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
    9856       211140 :       tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
    9857       211140 :       break;
    9858              : 
    9859           40 :     case PACK_INDEX_TYPE:
    9860           40 :       tree_node (PACK_INDEX_PACK (type));
    9861           40 :       tree_node (PACK_INDEX_INDEX (type));
    9862           40 :       break;
    9863              : 
    9864       256394 :     case TYPENAME_TYPE:
    9865       256394 :       {
    9866       256394 :         tree_node (TYPE_CONTEXT (type));
    9867       256394 :         tree_node (DECL_NAME (TYPE_NAME (type)));
    9868       256394 :         tree_node (TYPENAME_TYPE_FULLNAME (type));
    9869       256394 :         if (streaming_p ())
    9870        88972 :           u (get_typename_tag (type));
    9871              :         }
    9872              :       break;
    9873              : 
    9874          264 :     case UNBOUND_CLASS_TEMPLATE:
    9875          264 :       {
    9876          264 :         tree decl = TYPE_NAME (type);
    9877          264 :         tree_node (DECL_CONTEXT (decl));
    9878          264 :         tree_node (DECL_NAME (decl));
    9879          264 :         tree_node (DECL_TEMPLATE_PARMS (decl));
    9880              :       }
    9881          264 :       break;
    9882              : 
    9883           42 :     case VECTOR_TYPE:
    9884           42 :       if (streaming_p ())
    9885              :         {
    9886           21 :           poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
    9887           42 :           for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    9888           21 :             wu (nunits.coeffs[ix]);
    9889              :         }
    9890              :       break;
    9891              : 
    9892              :     case META_TYPE:
    9893              :       /* No additional data.  */
    9894              :       break;
    9895              : 
    9896            8 :     case SPLICE_SCOPE:
    9897            8 :       if (streaming_p ())
    9898            4 :         u (SPLICE_SCOPE_TYPE_P (type));
    9899            8 :       tree_node (SPLICE_SCOPE_EXPR (type));
    9900            8 :       break;
    9901              :     }
    9902              : 
    9903      6242529 :   tree_node (TYPE_ATTRIBUTES (type));
    9904              : 
    9905              :   /* We may have met the type during emitting the above.  */
    9906      6242529 :   if (ref_node (type) != WK_none)
    9907              :     {
    9908      5660376 :       int tag = insert (type);
    9909      5660376 :       if (streaming_p ())
    9910              :         {
    9911      1721248 :           i (0);
    9912      1721248 :           dump (dumper::TREE)
    9913          558 :             && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
    9914              :         }
    9915              :     }
    9916              : 
    9917              :   return;
    9918              : }
    9919              : 
    9920              : /* T is (mostly*) a non-mergeable node that must be written by value.
    9921              :    The mergeable case is a BINFO, which are as-if DECLSs.   */
    9922              : 
    9923              : void
    9924     38926448 : trees_out::tree_value (tree t)
    9925              : {
    9926              :   /* We should never be writing a type by value.  tree_type should
    9927              :      have streamed it, or we're going via its TYPE_DECL.  */
    9928     38926448 :   gcc_checking_assert (!TYPE_P (t));
    9929              : 
    9930     38926448 :   if (DECL_P (t))
    9931              :     /* No template, type, var or function, except anonymous
    9932              :        non-context vars and types.  */
    9933      1012853 :     gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
    9934              :                           && (TREE_CODE (t) != TYPE_DECL
    9935              :                               || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
    9936              :                           && (TREE_CODE (t) != VAR_DECL
    9937              :                               || ((!DECL_NAME (t)
    9938              :                                    || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
    9939              :                                   && !DECL_CONTEXT (t)))
    9940              :                           && TREE_CODE (t) != FUNCTION_DECL));
    9941              : 
    9942     58773869 :   if (is_initial_scan () && EXPR_P (t))
    9943      7852222 :     dep_hash->add_dependent_adl_entities (t);
    9944              : 
    9945     38926448 :   if (streaming_p ())
    9946              :     {
    9947              :       /* A new node -> tt_node.  */
    9948     15461849 :       tree_val_count++;
    9949     15461849 :       i (tt_node);
    9950     15461849 :       start (t);
    9951     15461849 :       tree_node_bools (t);
    9952              :     }
    9953              : 
    9954     38926448 :   if (TREE_CODE (t) == TREE_BINFO)
    9955              :     /* Binfos are decl-like and need merging information.  */
    9956       272718 :     binfo_mergeable (t);
    9957              : 
    9958     38926448 :   int tag = insert (t, WK_value);
    9959     38926448 :   if (streaming_p ())
    9960     15461849 :     dump (dumper::TREE)
    9961         2823 :       && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
    9962              : 
    9963     38926448 :   int type_tag = 0;
    9964     38926448 :   tree type = NULL_TREE;
    9965     38926448 :   if (TREE_CODE (t) == TYPE_DECL)
    9966              :     {
    9967           28 :       type = TREE_TYPE (t);
    9968              : 
    9969              :       /* We only support a limited set of features for uncontexted types;
    9970              :          these are typically types created in the language-independent
    9971              :          parts of the frontend (such as ubsan).  */
    9972           28 :       gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
    9973              :                            && TYPE_MAIN_VARIANT (type) == type
    9974              :                            && TYPE_NAME (type) == t
    9975              :                            && TYPE_STUB_DECL (type) == t
    9976              :                            && !TYPE_VFIELD (type)
    9977              :                            && !TYPE_BINFO (type)
    9978              :                            && !CLASS_TYPE_P (type));
    9979              : 
    9980           28 :       if (streaming_p ())
    9981              :         {
    9982           14 :           start (type);
    9983           14 :           tree_node_bools (type);
    9984              :         }
    9985              : 
    9986           28 :       type_tag = insert (type, WK_value);
    9987           28 :       if (streaming_p ())
    9988           14 :         dump (dumper::TREE)
    9989            0 :           && dump ("Writing type: %d %C:%N", type_tag,
    9990            0 :                    TREE_CODE (type), type);
    9991              :     }
    9992              : 
    9993     38926448 :   tree_node_vals (t);
    9994              : 
    9995     38926448 :   if (type)
    9996              :     {
    9997           28 :       tree_node_vals (type);
    9998           28 :       tree_node (TYPE_SIZE (type));
    9999           28 :       tree_node (TYPE_SIZE_UNIT (type));
   10000           28 :       chained_decls (TYPE_FIELDS (type));
   10001           28 :       if (streaming_p ())
   10002           14 :         dump (dumper::TREE)
   10003            0 :           && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10004              :     }
   10005              : 
   10006              :   /* For uncontexted VAR_DECLs we need to stream the definition so that
   10007              :      importers can recreate their value.  */
   10008     38926448 :   if (TREE_CODE (t) == VAR_DECL)
   10009              :     {
   10010          786 :       gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
   10011          786 :       tree_node (DECL_INITIAL (t));
   10012              :     }
   10013              : 
   10014     38926448 :   if (streaming_p ())
   10015     15464672 :     dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
   10016     38926448 : }
   10017              : 
   10018              : tree
   10019     12121051 : trees_in::tree_value ()
   10020              : {
   10021     12121051 :   tree t = start ();
   10022     12121051 :   if (!t || !tree_node_bools (t))
   10023            0 :     return NULL_TREE;
   10024              : 
   10025     12121051 :   tree existing = t;
   10026     12121051 :   if (TREE_CODE (t) == TREE_BINFO)
   10027              :     {
   10028        92217 :       tree type;
   10029        92217 :       unsigned ix = binfo_mergeable (&type);
   10030        92217 :       if (TYPE_BINFO (type))
   10031              :         {
   10032              :           /* We already have a definition, this must be a duplicate.  */
   10033        41634 :           dump (dumper::MERGE)
   10034          271 :             && dump ("Deduping binfo %N[%u]", type, ix);
   10035        41634 :           existing = TYPE_BINFO (type);
   10036        56948 :           while (existing && ix--)
   10037        15314 :             existing = TREE_CHAIN (existing);
   10038        41634 :           if (existing)
   10039        41634 :             register_duplicate (t, existing);
   10040              :           else
   10041              :             /* Error, mismatch -- diagnose in read_class_def's
   10042              :                checking.  */
   10043              :             existing = t;
   10044              :         }
   10045              :     }
   10046              : 
   10047              :   /* Insert into map.  */
   10048     12121051 :   int tag = insert (existing);
   10049     12121051 :   dump (dumper::TREE)
   10050         3677 :     && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
   10051              : 
   10052     12121051 :   int type_tag = 0;
   10053     12121051 :   tree type = NULL_TREE;
   10054     12121051 :   if (TREE_CODE (t) == TYPE_DECL)
   10055              :     {
   10056           14 :       type = start ();
   10057           14 :       if (!type || !tree_node_bools (type))
   10058              :         t = NULL_TREE;
   10059              : 
   10060           14 :       type_tag = insert (type);
   10061           14 :       if (t)
   10062           14 :         dump (dumper::TREE)
   10063            0 :           && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
   10064              :     }
   10065              : 
   10066              :   if (!t)
   10067              :     {
   10068            0 : bail:
   10069            0 :       back_refs[~tag] = NULL_TREE;
   10070            0 :       if (type_tag)
   10071            0 :         back_refs[~type_tag] = NULL_TREE;
   10072            0 :       set_overrun ();
   10073            0 :       return NULL_TREE;
   10074              :     }
   10075              : 
   10076     12121051 :   if (!tree_node_vals (t))
   10077            0 :     goto bail;
   10078              : 
   10079     12121051 :   if (type)
   10080              :     {
   10081           14 :       if (!tree_node_vals (type))
   10082            0 :         goto bail;
   10083              : 
   10084           14 :       TYPE_SIZE (type) = tree_node ();
   10085           14 :       TYPE_SIZE_UNIT (type) = tree_node ();
   10086           14 :       TYPE_FIELDS (type) = chained_decls ();
   10087           14 :       if (get_overrun ())
   10088            0 :         goto bail;
   10089              : 
   10090           14 :       dump (dumper::TREE)
   10091            0 :         && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10092              :     }
   10093              : 
   10094     12121051 :   if (TREE_CODE (t) == VAR_DECL)
   10095              :     {
   10096          359 :       DECL_INITIAL (t) = tree_node ();
   10097          359 :       if (TREE_STATIC (t))
   10098            8 :         varpool_node::finalize_decl (t);
   10099              :     }
   10100              : 
   10101     12121051 :   if (TREE_CODE (t) == LAMBDA_EXPR
   10102     12121051 :       && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
   10103              :     {
   10104         1971 :       existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
   10105         1971 :       back_refs[~tag] = existing;
   10106              :     }
   10107              : 
   10108     12124728 :   dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
   10109              : 
   10110     12121051 :   if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
   10111              :     {
   10112       602610 :       existing = cache_integer_cst (t, true);
   10113       602610 :       back_refs[~tag] = existing;
   10114              :     }
   10115              : 
   10116              :   return existing;
   10117              : }
   10118              : 
   10119              : /* Whether DECL has a TU-local dependency in the hash.  */
   10120              : 
   10121              : bool
   10122      1151068 : trees_out::has_tu_local_dep (tree decl) const
   10123              : {
   10124              :   /* Only the contexts of fields or enums remember that they're
   10125              :      TU-local.  */
   10126      1151068 :   if (DECL_CONTEXT (decl)
   10127      1151068 :       && (TREE_CODE (decl) == FIELD_DECL
   10128      1151065 :           || TREE_CODE (decl) == CONST_DECL))
   10129            3 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   10130              : 
   10131      1151068 :   depset *dep = dep_hash->find_dependency (decl);
   10132      1151068 :   if (!dep)
   10133              :     {
   10134              :       /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
   10135              :          which we found was TU-local and gave up early.  */
   10136        14468 :       int use_tpl = -1;
   10137        14468 :       if (tree ti = node_template_info (decl, use_tpl))
   10138         2354 :         dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
   10139              :     }
   10140              : 
   10141      1151068 :   return dep && dep->is_tu_local ();
   10142              : }
   10143              : 
   10144              : /* If T depends on a TU-local entity, return that decl.  */
   10145              : 
   10146              : tree
   10147          395 : trees_out::find_tu_local_decl (tree t)
   10148              : {
   10149              :   /* We need to have walked all deps first before we can check.  */
   10150          395 :   gcc_checking_assert (!is_initial_scan ());
   10151              : 
   10152          951 :   auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
   10153              :     {
   10154          556 :       auto self = (trees_out *)data;
   10155              : 
   10156          556 :       tree decl = NULL_TREE;
   10157          556 :       if (TYPE_P (*tp))
   10158              :         {
   10159              :           /* A PMF type is a record type, which we otherwise wouldn't walk;
   10160              :              return whether the function type is TU-local.  */
   10161          370 :           if (TYPE_PTRMEMFUNC_P (*tp))
   10162              :             {
   10163            3 :               *walk_subtrees = 0;
   10164            3 :               return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
   10165              :             }
   10166              :           else
   10167          367 :             decl = TYPE_MAIN_DECL (*tp);
   10168              :         }
   10169          186 :       else if (DECL_P (*tp))
   10170              :         decl = *tp;
   10171              : 
   10172          373 :       if (decl)
   10173              :         {
   10174              :           /* We found a DECL, this will tell us whether we're TU-local.  */
   10175           59 :           *walk_subtrees = 0;
   10176           59 :           return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
   10177              :         }
   10178              :       return NULL_TREE;
   10179              :     };
   10180              : 
   10181              :   /* We need to walk without duplicates so that we step into the pointed-to
   10182              :      types of array types.  */
   10183          395 :   return cp_walk_tree_without_duplicates (&t, walker, this);
   10184              : }
   10185              : 
   10186              : /* Get the name for TU-local decl T to be used in diagnostics.  */
   10187              : 
   10188              : static tree
   10189          206 : name_for_tu_local_decl (tree t)
   10190              : {
   10191          206 :   int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
   10192          206 :   const char *str = decl_as_string (t, flags);
   10193          206 :   return get_identifier (str);
   10194              : }
   10195              : 
   10196              : /* Stream out tree node T.  We automatically create local back
   10197              :    references, which is essentially a single pass lisp
   10198              :    self-referential structure pretty-printer.  */
   10199              : 
   10200              : void
   10201    332085752 : trees_out::tree_node (tree t)
   10202              : {
   10203    332085752 :   dump.indent ();
   10204    332085752 :   walk_kind ref = ref_node (t);
   10205    332085752 :   if (ref == WK_none)
   10206    253795908 :     goto done;
   10207              : 
   10208              :   /* Find TU-local entities and intercept streaming to instead write a
   10209              :      placeholder value; this way we don't need to emit such decls.
   10210              :      We only need to do this when writing a definition of an entity
   10211              :      that we know names a TU-local entity.  */
   10212     91594458 :   if (!is_initial_scan () && writing_local_entities)
   10213              :     {
   10214          952 :       tree local_decl = NULL_TREE;
   10215          952 :       if (DECL_P (t) && has_tu_local_dep (t))
   10216              :         local_decl = t;
   10217              :       /* Consider a type to be TU-local if it refers to any TU-local decl,
   10218              :          no matter how deep.
   10219              : 
   10220              :          This worsens diagnostics slightly, as we often no longer point
   10221              :          directly to the at-fault entity when instantiating.  However, this
   10222              :          reduces the module size slightly and means that much less of pt.cc
   10223              :          needs to know about us.  */
   10224          848 :       else if (TYPE_P (t))
   10225          142 :         local_decl = find_tu_local_decl (t);
   10226          706 :       else if (EXPR_P (t))
   10227          250 :         local_decl = find_tu_local_decl (TREE_TYPE (t));
   10228              : 
   10229          496 :       if (local_decl)
   10230              :         {
   10231          158 :           int tag = insert (t, WK_value);
   10232          158 :           if (streaming_p ())
   10233              :             {
   10234          158 :               tu_local_count++;
   10235          158 :               i (tt_tu_local);
   10236          158 :               dump (dumper::TREE)
   10237            0 :                 && dump ("Writing TU-local entity:%d %C:%N",
   10238            0 :                          tag, TREE_CODE (t), t);
   10239              :             }
   10240          158 :           tree_node (name_for_tu_local_decl (local_decl));
   10241          158 :           if (state)
   10242          158 :             state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
   10243          158 :           goto done;
   10244              :         }
   10245              :     }
   10246              : 
   10247     78289686 :   if (ref != WK_normal)
   10248      1831874 :     goto skip_normal;
   10249              : 
   10250     76457812 :   if (TREE_CODE (t) == IDENTIFIER_NODE)
   10251              :     {
   10252              :       /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
   10253              :          tt_internal_id.  */
   10254      9124944 :       int code = tt_id;
   10255      9124944 :       if (IDENTIFIER_ANON_P (t))
   10256        35574 :         code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
   10257      9089370 :       else if (IDENTIFIER_INTERNAL_P (t))
   10258              :         code = tt_internal_id;
   10259      9089356 :       else if (IDENTIFIER_CONV_OP_P (t))
   10260        13731 :         code = tt_conv_id;
   10261              : 
   10262      9124944 :       if (streaming_p ())
   10263      1798702 :         i (code);
   10264              : 
   10265      9124944 :       if (code == tt_conv_id)
   10266              :         {
   10267        13731 :           tree type = TREE_TYPE (t);
   10268        13731 :           gcc_checking_assert (type || t == conv_op_identifier);
   10269        13731 :           tree_node (type);
   10270              :         }
   10271      9111213 :       else if (code == tt_id && streaming_p ())
   10272      1783697 :         str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
   10273      7327516 :       else if (code == tt_internal_id && streaming_p ())
   10274            7 :         str (prefix_for_internal_label (t));
   10275              : 
   10276      9124944 :       int tag = insert (t);
   10277      9124944 :       if (streaming_p ())
   10278              :         {
   10279              :           /* We know the ordering of the 5 id tags.  */
   10280      1798702 :           static const char *const kinds[] =
   10281              :             {"", "conv_op ", "anon ", "lambda ", "internal "};
   10282      1798702 :           dump (dumper::TREE)
   10283         1074 :             && dump ("Written:%d %sidentifier:%N", tag,
   10284         1071 :                      kinds[code - tt_id],
   10285            3 :                      code == tt_conv_id ? TREE_TYPE (t) : t);
   10286              :         }
   10287      9124944 :       goto done;
   10288              :     }
   10289              : 
   10290     67332868 :   if (TREE_CODE (t) == TREE_BINFO)
   10291              :     {
   10292              :       /* A BINFO -> tt_binfo.
   10293              :          We must do this by reference.  We stream the binfo tree
   10294              :          itself when streaming its owning RECORD_TYPE.  That we got
   10295              :          here means the dominating type is not in this SCC.  */
   10296        78386 :       if (streaming_p ())
   10297         2466 :         i (tt_binfo);
   10298        78386 :       binfo_mergeable (t);
   10299        78386 :       gcc_checking_assert (!TREE_VISITED (t));
   10300        78386 :       int tag = insert (t);
   10301        78386 :       if (streaming_p ())
   10302         2466 :         dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
   10303        78386 :       goto done;
   10304              :     }
   10305              : 
   10306     67254482 :   if (TREE_CODE (t) == INTEGER_CST
   10307      4800688 :       && !TREE_OVERFLOW (t)
   10308     72055170 :       && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
   10309              :     {
   10310              :       /* An integral constant of enumeral type.  See if it matches one
   10311              :          of the enumeration values.  */
   10312        46205 :       for (tree values = TYPE_VALUES (TREE_TYPE (t));
   10313       966693 :            values; values = TREE_CHAIN (values))
   10314              :         {
   10315       964287 :           tree decl = TREE_VALUE (values);
   10316       964287 :           if (tree_int_cst_equal (DECL_INITIAL (decl), t))
   10317              :             {
   10318        43799 :               if (streaming_p ())
   10319        12748 :                 u (tt_enum_value);
   10320        43799 :               tree_node (decl);
   10321        43841 :               dump (dumper::TREE) && dump ("Written enum value %N", decl);
   10322        43799 :               goto done;
   10323              :             }
   10324              :         }
   10325              :       /* It didn't match.  We'll write it a an explicit INTEGER_CST
   10326              :          node.  */
   10327              :     }
   10328              : 
   10329     67210683 :   if (TYPE_P (t))
   10330              :     {
   10331     12773752 :       type_node (t);
   10332     12773752 :       goto done;
   10333              :     }
   10334              : 
   10335     54436931 :   if (DECL_P (t))
   10336              :     {
   10337     16832106 :       if (DECL_TEMPLATE_PARM_P (t))
   10338              :         {
   10339      2606844 :           tpl_parm_value (t);
   10340      2606844 :           goto done;
   10341              :         }
   10342              : 
   10343     14225262 :       if (!DECL_CONTEXT (t))
   10344              :         {
   10345              :           /* There are a few cases of decls with no context.  We'll write
   10346              :              these by value, but first assert they are cases we expect.  */
   10347        30716 :           gcc_checking_assert (ref == WK_normal);
   10348        30716 :           switch (TREE_CODE (t))
   10349              :             {
   10350            0 :             default: gcc_unreachable ();
   10351              : 
   10352        11796 :             case LABEL_DECL:
   10353              :               /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs.  */
   10354        11796 :               gcc_checking_assert (!DECL_NAME (t));
   10355              :               break;
   10356              : 
   10357          786 :             case VAR_DECL:
   10358              :               /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
   10359              :                  and internal vars are created by sanitizers and
   10360              :                  __builtin_source_location.  */
   10361          786 :               gcc_checking_assert ((!DECL_NAME (t)
   10362              :                                     || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
   10363              :                                    && DECL_ARTIFICIAL (t));
   10364              :               break;
   10365              : 
   10366        18106 :             case PARM_DECL:
   10367              :               /* REQUIRES_EXPRs have a chain of uncontexted PARM_DECLS,
   10368              :                  and an implicit this parm in an NSDMI has no context.  */
   10369        18106 :               gcc_checking_assert (CONSTRAINT_VAR_P (t)
   10370              :                                    || DECL_NAME (t) == this_identifier);
   10371              :               break;
   10372              : 
   10373           28 :             case TYPE_DECL:
   10374              :               /* Some parts of the compiler need internal struct types;
   10375              :                  these types may not have an appropriate context to use.
   10376              :                  Walk the whole type (including its definition) by value.  */
   10377           28 :               gcc_checking_assert (DECL_ARTIFICIAL (t)
   10378              :                                    && TYPE_ARTIFICIAL (TREE_TYPE (t))
   10379              :                                    && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
   10380              :                                    && !CLASS_TYPE_P (TREE_TYPE (t)));
   10381              :               break;
   10382              :             }
   10383        30716 :           mark_declaration (t, has_definition (t));
   10384        30716 :           goto by_value;
   10385              :         }
   10386              :     }
   10387              : 
   10388     37604825 :  skip_normal:
   10389     53631245 :   if (DECL_P (t) && !decl_node (t, ref))
   10390     14735513 :     goto done;
   10391              : 
   10392              :   /* Otherwise by value */
   10393     38926448 :  by_value:
   10394     38926448 :   tree_value (t);
   10395              : 
   10396    332085752 :  done:
   10397              :   /* And, breath out.  */
   10398    332085752 :   dump.outdent ();
   10399    332085752 : }
   10400              : 
   10401              : /* Stream in a tree node.  */
   10402              : 
   10403              : tree
   10404     98458755 : trees_in::tree_node (bool is_use)
   10405              : {
   10406     98458755 :   if (get_overrun ())
   10407              :     return NULL_TREE;
   10408              : 
   10409     98458755 :   dump.indent ();
   10410     98458755 :   int tag = i ();
   10411     98458755 :   tree res = NULL_TREE;
   10412     98458755 :   switch (tag)
   10413              :     {
   10414     32368262 :     default:
   10415              :       /* backref, pull it out of the map.  */
   10416     32368262 :       res = back_ref (tag);
   10417     32368262 :       break;
   10418              : 
   10419              :     case tt_null:
   10420              :       /* NULL_TREE.  */
   10421              :       break;
   10422              : 
   10423          158 :     case tt_tu_local:
   10424          158 :       {
   10425              :         /* A translation-unit-local entity.  */
   10426          158 :         res = make_node (TU_LOCAL_ENTITY);
   10427          158 :         int tag = insert (res);
   10428              : 
   10429          158 :         TU_LOCAL_ENTITY_NAME (res) = tree_node ();
   10430          158 :         TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
   10431          158 :         dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
   10432              :       }
   10433              :       break;
   10434              : 
   10435      7582690 :     case tt_fixed:
   10436              :       /* A fixed ref, find it in the fixed_ref array.   */
   10437      7582690 :       {
   10438      7582690 :         unsigned fix = u ();
   10439      7582690 :         if (fix < (*fixed_trees).length ())
   10440              :           {
   10441      7582690 :             res = (*fixed_trees)[fix];
   10442      7582690 :             dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
   10443         5046 :                                          TREE_CODE (res), res, res);
   10444              :           }
   10445              : 
   10446      7582690 :         if (!res)
   10447            0 :           set_overrun ();
   10448              :       }
   10449              :       break;
   10450              : 
   10451        83126 :     case tt_parm:
   10452        83126 :       {
   10453        83126 :         tree fn = tree_node ();
   10454        83126 :         if (fn && TREE_CODE (fn) == FUNCTION_DECL)
   10455        83126 :           res = tree_node ();
   10456        83126 :         if (res)
   10457        83126 :           dump (dumper::TREE)
   10458           21 :             && dump ("Read %s reference %N",
   10459           21 :                      TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
   10460              :                      res);
   10461              :       }
   10462              :       break;
   10463              : 
   10464     12121051 :     case tt_node:
   10465              :       /* A new node.  Stream it in.  */
   10466     12121051 :       res = tree_value ();
   10467     12121051 :       break;
   10468              : 
   10469      1231942 :     case tt_decl:
   10470              :       /* A new decl.  Stream it in.  */
   10471      1231942 :       res = decl_value ();
   10472      1231942 :       break;
   10473              : 
   10474       432494 :     case tt_tpl_parm:
   10475              :       /* A template parameter.  Stream it in.  */
   10476       432494 :       res = tpl_parm_value ();
   10477       432494 :       break;
   10478              : 
   10479      1251472 :     case tt_id:
   10480              :       /* An identifier node.  */
   10481      1251472 :       {
   10482      1251472 :         size_t l;
   10483      1251472 :         const char *chars = str (&l);
   10484      1251472 :         res = get_identifier_with_length (chars, l);
   10485      1251472 :         int tag = insert (res);
   10486      1251472 :         dump (dumper::TREE)
   10487         1488 :           && dump ("Read identifier:%d %N", tag, res);
   10488              :       }
   10489      1251472 :       break;
   10490              : 
   10491         3318 :     case tt_conv_id:
   10492              :       /* A conversion operator.  Get the type and recreate the
   10493              :          identifier.  */
   10494         3318 :       {
   10495         3318 :         tree type = tree_node ();
   10496         3318 :         if (!get_overrun ())
   10497              :           {
   10498         3318 :             res = type ? make_conv_op_name (type) : conv_op_identifier;
   10499         3318 :             int tag = insert (res);
   10500         3318 :             dump (dumper::TREE)
   10501           27 :               && dump ("Created conv_op:%d %S for %N", tag, res, type);
   10502              :           }
   10503              :       }
   10504              :       break;
   10505              : 
   10506         6789 :     case tt_anon_id:
   10507         6789 :     case tt_lambda_id:
   10508              :       /* An anonymous or lambda id.  */
   10509         6789 :       {
   10510         6789 :         res = make_anon_name ();
   10511         6789 :         if (tag == tt_lambda_id)
   10512         4021 :           IDENTIFIER_LAMBDA_P (res) = true;
   10513         6789 :         int tag = insert (res);
   10514         6789 :         dump (dumper::TREE)
   10515            3 :           && dump ("Read %s identifier:%d %N",
   10516            3 :                    IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
   10517              :       }
   10518              :       break;
   10519              : 
   10520            8 :     case tt_internal_id:
   10521              :       /* An internal label.  */
   10522            8 :       {
   10523            8 :         const char *prefix = str ();
   10524            8 :         res = generate_internal_label (prefix);
   10525            8 :         int tag = insert (res);
   10526            8 :         dump (dumper::TREE)
   10527            1 :           && dump ("Read internal identifier:%d %N", tag, res);
   10528              :       }
   10529              :       break;
   10530              : 
   10531       197131 :     case tt_typedef_type:
   10532       197131 :       res = tree_node ();
   10533       197131 :       if (res)
   10534              :         {
   10535       197131 :           dump (dumper::TREE)
   10536           74 :             && dump ("Read %stypedef %C:%N",
   10537           74 :                      DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
   10538           74 :                      TREE_CODE (res), res);
   10539       197131 :           if (TREE_CODE (res) != TU_LOCAL_ENTITY)
   10540       197130 :             res = TREE_TYPE (res);
   10541              :         }
   10542              :       break;
   10543              : 
   10544      1508501 :     case tt_derived_type:
   10545              :       /* A type derived from some other type.  */
   10546      1508501 :       {
   10547      1508501 :         enum tree_code code = tree_code (u ());
   10548      1508501 :         res = tree_node ();
   10549              : 
   10550      1508501 :         switch (code)
   10551              :           {
   10552            0 :           default:
   10553            0 :             set_overrun ();
   10554            0 :             break;
   10555              : 
   10556        21351 :           case ARRAY_TYPE:
   10557        21351 :             {
   10558        21351 :               tree elt_type = res;
   10559        21351 :               tree domain = tree_node ();
   10560        21351 :               int dep = u ();
   10561        21351 :               if (!get_overrun ())
   10562              :                 {
   10563        21351 :                   res = build_cplus_array_type (elt_type, domain, dep);
   10564              :                   /* If we're an array of an incomplete imported type,
   10565              :                      save it for post-processing so that we can attempt
   10566              :                      to complete the type later if it will get a
   10567              :                      definition later in the cluster.  */
   10568        21351 :                   if (!dep
   10569        18568 :                       && !COMPLETE_TYPE_P (elt_type)
   10570           36 :                       && CLASS_TYPE_P (elt_type)
   10571           36 :                       && DECL_LANG_SPECIFIC (TYPE_NAME (elt_type))
   10572        21387 :                       && DECL_MODULE_IMPORT_P (TYPE_NAME (elt_type)))
   10573           36 :                     post_process_type (res);
   10574              :                 }
   10575              :             }
   10576              :             break;
   10577              : 
   10578          265 :           case COMPLEX_TYPE:
   10579          265 :             if (!get_overrun ())
   10580          265 :               res = build_complex_type (res);
   10581              :             break;
   10582              : 
   10583            9 :           case BOOLEAN_TYPE:
   10584            9 :             {
   10585            9 :               unsigned precision = u ();
   10586            9 :               if (!get_overrun ())
   10587            9 :                 res = build_nonstandard_boolean_type (precision);
   10588              :             }
   10589              :             break;
   10590              : 
   10591        19621 :           case INTEGER_TYPE:
   10592        19621 :             if (res)
   10593              :               {
   10594              :                 /* A range type (representing an array domain).  */
   10595        18573 :                 tree min = tree_node ();
   10596        18573 :                 tree max = tree_node ();
   10597              : 
   10598        18573 :                 if (!get_overrun ())
   10599        18573 :                   res = build_range_type (res, min, max);
   10600              :               }
   10601              :             else
   10602              :               {
   10603              :                 /* A new integral type (representing a bitfield).  */
   10604         1048 :                 unsigned enc = u ();
   10605         1048 :                 if (!get_overrun ())
   10606         1048 :                   res = build_nonstandard_integer_type (enc >> 1, enc & 1);
   10607              :               }
   10608              :             break;
   10609              : 
   10610       426849 :           case FUNCTION_TYPE:
   10611       426849 :           case METHOD_TYPE:
   10612       426849 :             {
   10613       426849 :               tree klass =  code == METHOD_TYPE ? tree_node () : NULL_TREE;
   10614       426849 :               tree args = tree_node ();
   10615       426849 :               if (!get_overrun ())
   10616              :                 {
   10617       426849 :                   if (klass)
   10618       271418 :                     res = build_method_type_directly (klass, res, args);
   10619              :                   else
   10620       155431 :                     res = cp_build_function_type (res, args);
   10621              :                 }
   10622              :             }
   10623              :             break;
   10624              : 
   10625          264 :           case OFFSET_TYPE:
   10626          264 :             {
   10627          264 :               tree base = tree_node ();
   10628          264 :               if (!get_overrun ())
   10629          264 :                 res = build_offset_type (base, res);
   10630              :             }
   10631              :             break;
   10632              : 
   10633       205399 :           case POINTER_TYPE:
   10634       205399 :             if (!get_overrun ())
   10635       205399 :               res = build_pointer_type (res);
   10636              :             break;
   10637              : 
   10638       177200 :           case REFERENCE_TYPE:
   10639       177200 :             {
   10640       177200 :               bool rval = bool (u ());
   10641       177200 :               if (!get_overrun ())
   10642       177200 :                 res = cp_build_reference_type (res, rval);
   10643              :             }
   10644              :             break;
   10645              : 
   10646       458995 :           case DECLTYPE_TYPE:
   10647       458995 :           case TYPEOF_TYPE:
   10648       458995 :           case DEPENDENT_OPERATOR_TYPE:
   10649       458995 :             {
   10650       458995 :               tree expr = tree_node ();
   10651       458995 :               if (!get_overrun ())
   10652              :                 {
   10653       458995 :                   res = cxx_make_type (code);
   10654       458995 :                   TYPE_VALUES_RAW (res) = expr;
   10655       458995 :                   if (code == DECLTYPE_TYPE)
   10656        22006 :                     tree_node_bools (res);
   10657       458995 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10658              :                 }
   10659              :             }
   10660              :             break;
   10661              : 
   10662         2307 :           case TRAIT_TYPE:
   10663         2307 :             {
   10664         2307 :               tree kind = tree_node ();
   10665         2307 :               tree type1 = tree_node ();
   10666         2307 :               tree type2 = tree_node ();
   10667         2307 :               if (!get_overrun ())
   10668              :                 {
   10669         2307 :                   res = cxx_make_type (TRAIT_TYPE);
   10670         2307 :                   TRAIT_TYPE_KIND_RAW (res) = kind;
   10671         2307 :                   TRAIT_TYPE_TYPE1 (res) = type1;
   10672         2307 :                   TRAIT_TYPE_TYPE2 (res) = type2;
   10673         2307 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10674              :                 }
   10675              :             }
   10676              :             break;
   10677              : 
   10678        62445 :           case TYPE_ARGUMENT_PACK:
   10679        62445 :             if (!get_overrun ())
   10680              :               {
   10681        62445 :                 tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   10682        62445 :                 ARGUMENT_PACK_ARGS (pack) = res;
   10683        62445 :                 res = pack;
   10684              :               }
   10685              :             break;
   10686              : 
   10687        65865 :           case TYPE_PACK_EXPANSION:
   10688        65865 :             {
   10689        65865 :               bool local = u ();
   10690        65865 :               tree param_packs = tree_node ();
   10691        65865 :               tree extra_args = tree_node ();
   10692        65865 :               if (!get_overrun ())
   10693              :                 {
   10694        65865 :                   tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
   10695        65865 :                   SET_TYPE_STRUCTURAL_EQUALITY (expn);
   10696        65865 :                   PACK_EXPANSION_PATTERN (expn) = res;
   10697       131730 :                   PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
   10698        65865 :                   PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
   10699        65865 :                   PACK_EXPANSION_LOCAL_P (expn) = local;
   10700        65865 :                   res = expn;
   10701              :                 }
   10702              :             }
   10703              :             break;
   10704              : 
   10705           25 :           case PACK_INDEX_TYPE:
   10706           25 :             {
   10707           25 :               tree pack = tree_node ();
   10708           25 :               tree index = tree_node ();
   10709           25 :               if (!get_overrun ())
   10710           25 :                 res = make_pack_index (pack, index);
   10711              :             }
   10712              :             break;
   10713              : 
   10714        67730 :           case TYPENAME_TYPE:
   10715        67730 :             {
   10716        67730 :               tree ctx = tree_node ();
   10717        67730 :               tree name = tree_node ();
   10718        67730 :               tree fullname = tree_node ();
   10719        67730 :               enum tag_types tag_type = tag_types (u ());
   10720              : 
   10721        67730 :               if (!get_overrun ())
   10722        67730 :                 res = build_typename_type (ctx, name, fullname, tag_type);
   10723              :             }
   10724              :             break;
   10725              : 
   10726           52 :           case UNBOUND_CLASS_TEMPLATE:
   10727           52 :             {
   10728           52 :               tree ctx = tree_node ();
   10729           52 :               tree name = tree_node ();
   10730           52 :               tree parms = tree_node ();
   10731              : 
   10732           52 :               if (!get_overrun ())
   10733           52 :                 res = make_unbound_class_template_raw (ctx, name, parms);
   10734              :             }
   10735              :             break;
   10736              : 
   10737              :           case VECTOR_TYPE:
   10738              :             {
   10739              :               poly_uint64 nunits;
   10740           60 :               for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
   10741           30 :                 nunits.coeffs[ix] = wu ();
   10742           30 :               if (!get_overrun ())
   10743           30 :                 res = build_vector_type (res, nunits);
   10744              :             }
   10745              :             break;
   10746              : 
   10747           90 :           case META_TYPE:
   10748           90 :             if (!get_overrun ())
   10749           90 :               res = meta_info_type_node;
   10750              :             break;
   10751              : 
   10752            4 :           case SPLICE_SCOPE:
   10753            4 :             {
   10754            4 :               bool type = u ();
   10755            4 :               tree expr = tree_node ();
   10756              : 
   10757            4 :               if (!get_overrun ())
   10758            4 :                 res = make_splice_scope (expr, type);
   10759              :             }
   10760              :             break;
   10761              :           }
   10762              : 
   10763              :         /* In the exporting TU, a derived type with attributes was built by
   10764              :            build_type_attribute_variant as a distinct copy, with itself as
   10765              :            TYPE_MAIN_VARIANT.  We repeat that on import to get the version
   10766              :            without attributes as TYPE_CANONICAL.  */
   10767      1508501 :         if (tree attribs = tree_node ())
   10768        17299 :           res = cp_build_type_attribute_variant (res, attribs);
   10769              : 
   10770      1508501 :         int tag = i ();
   10771      1508501 :         if (!tag)
   10772              :           {
   10773      1290008 :             tag = insert (res);
   10774      1290008 :             if (res)
   10775      1290008 :               dump (dumper::TREE)
   10776          678 :                 && dump ("Created:%d derived type %C", tag, code);
   10777              :           }
   10778              :         else
   10779       218493 :           res = back_ref (tag);
   10780              :       }
   10781              :       break;
   10782              : 
   10783       432940 :     case tt_variant_type:
   10784              :       /* Variant of some type.  */
   10785       432940 :       {
   10786       432940 :         res = tree_node ();
   10787       432940 :         int flags = i ();
   10788       432940 :         if (get_overrun ())
   10789              :           ;
   10790       432940 :         else if (flags < 0)
   10791              :           /* No change.  */;
   10792       208507 :         else if (TREE_CODE (res) == FUNCTION_TYPE
   10793       208507 :                  || TREE_CODE (res) == METHOD_TYPE)
   10794              :           {
   10795       206934 :             cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
   10796       206934 :             bool late = (flags >> 2) & 1;
   10797       206934 :             cp_cv_quals quals = cp_cv_quals (flags >> 3);
   10798              : 
   10799       206934 :             tree raises = tree_node ();
   10800       206934 :             if (raises == error_mark_node)
   10801         7240 :               raises = TYPE_RAISES_EXCEPTIONS (res);
   10802              : 
   10803       206934 :             res = build_cp_fntype_variant (res, rqual, raises, late);
   10804       206934 :             if (TREE_CODE (res) == FUNCTION_TYPE)
   10805        73211 :               res = apply_memfn_quals (res, quals, rqual);
   10806              :           }
   10807              :         else
   10808              :           {
   10809         1573 :             res = build_aligned_type (res, (1u << flags) >> 1);
   10810         1573 :             TYPE_USER_ALIGN (res) = true;
   10811              :           }
   10812              : 
   10813       432940 :         int quals = i ();
   10814       432940 :         if (quals >= 0 && !get_overrun ())
   10815       225574 :           res = cp_build_qualified_type (res, quals);
   10816              : 
   10817       432940 :         int tag = i ();
   10818       432940 :         if (!tag)
   10819              :           {
   10820       432940 :             tag = insert (res);
   10821       432940 :             if (res)
   10822       432940 :               dump (dumper::TREE)
   10823          292 :                 && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
   10824              :           }
   10825              :         else
   10826            0 :           res = back_ref (tag);
   10827              :       }
   10828              :       break;
   10829              : 
   10830         5096 :     case tt_tinfo_var:
   10831         5096 :     case tt_tinfo_typedef:
   10832              :       /* A tinfo var or typedef.  */
   10833         5096 :       {
   10834         5096 :         bool is_var = tag == tt_tinfo_var;
   10835         5096 :         unsigned ix = u ();
   10836         5096 :         tree type = NULL_TREE;
   10837              : 
   10838         5096 :         if (is_var)
   10839              :           {
   10840         3092 :             tree name = tree_node ();
   10841         3092 :             type = tree_node ();
   10842              : 
   10843         3092 :             if (!get_overrun ())
   10844         3092 :               res = get_tinfo_decl_direct (type, name, int (ix));
   10845              :           }
   10846              :         else
   10847              :           {
   10848         2004 :             if (!get_overrun ())
   10849              :               {
   10850         2004 :                 type = get_pseudo_tinfo_type (ix);
   10851         2004 :                 res = TYPE_NAME (type);
   10852              :               }
   10853              :           }
   10854         5096 :         if (res)
   10855              :           {
   10856         5096 :             int tag = insert (res);
   10857         5096 :             dump (dumper::TREE)
   10858           36 :               && dump ("Created tinfo_%s:%d %S:%u for %N",
   10859              :                        is_var ? "var" : "decl", tag, res, ix, type);
   10860         5096 :             if (!is_var)
   10861              :               {
   10862         2004 :                 tag = insert (type);
   10863         2004 :                 dump (dumper::TREE)
   10864           12 :                   && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
   10865              :               }
   10866              :           }
   10867              :       }
   10868              :       break;
   10869              : 
   10870         1066 :     case tt_ptrmem_type:
   10871              :       /* A pointer to member function.  */
   10872         1066 :       {
   10873         1066 :         tree type = tree_node ();
   10874         1066 :         if (type && TREE_CODE (type) == POINTER_TYPE
   10875         2132 :             && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
   10876              :           {
   10877         1066 :             res = build_ptrmemfunc_type (type);
   10878         1066 :             int tag = insert (res);
   10879         1069 :             dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
   10880              :           }
   10881              :         else
   10882            0 :           set_overrun ();
   10883              :       }
   10884              :       break;
   10885              : 
   10886            9 :     case tt_nttp_var:
   10887              :       /* An NTTP object. */
   10888            9 :       {
   10889            9 :         tree init = tree_node ();
   10890            9 :         tree name = tree_node ();
   10891            9 :         if (!get_overrun ())
   10892              :           {
   10893              :             /* We don't want to check the initializer as that may require
   10894              :                name lookup, which could recursively start lazy loading.
   10895              :                Instead we know that INIT is already valid so we can just
   10896              :                apply that directly.  */
   10897            9 :             res = get_template_parm_object (init, name, /*check_init=*/false);
   10898            9 :             int tag = insert (res);
   10899            9 :             dump (dumper::TREE)
   10900            0 :               && dump ("Created nttp object:%d %N", tag, name);
   10901            9 :             vec_safe_push (post_load_decls, res);
   10902              :           }
   10903              :       }
   10904              :       break;
   10905              : 
   10906         7961 :     case tt_enum_value:
   10907              :       /* An enum const value.  */
   10908         7961 :       {
   10909         7961 :         if (tree decl = tree_node ())
   10910              :           {
   10911         7979 :             dump (dumper::TREE) && dump ("Read enum value %N", decl);
   10912         7961 :             res = DECL_INITIAL (decl);
   10913              :           }
   10914              : 
   10915         7961 :         if (!res)
   10916            0 :           set_overrun ();
   10917              :       }
   10918              :       break;
   10919              : 
   10920        14169 :     case tt_enum_decl:
   10921              :       /* An enum decl.  */
   10922        14169 :       {
   10923        14169 :         tree ctx = tree_node ();
   10924        14169 :         tree name = tree_node ();
   10925              : 
   10926        14169 :         if (!get_overrun ()
   10927        14169 :             && TREE_CODE (ctx) == ENUMERAL_TYPE)
   10928        14169 :           res = find_enum_member (ctx, name);
   10929              : 
   10930        14169 :         if (!res)
   10931            0 :           set_overrun ();
   10932              :         else
   10933              :           {
   10934        14169 :             int tag = insert (res);
   10935        14169 :             dump (dumper::TREE)
   10936           18 :               && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
   10937              :           }
   10938              :       }
   10939              :       break;
   10940              : 
   10941         9076 :     case tt_data_member:
   10942              :       /* A data member.  */
   10943         9076 :       {
   10944         9076 :         tree ctx = tree_node ();
   10945         9076 :         tree name = tree_node ();
   10946              : 
   10947         9076 :         if (!get_overrun ()
   10948         9076 :             && RECORD_OR_UNION_TYPE_P (ctx))
   10949              :           {
   10950         9076 :             if (name)
   10951         8021 :               res = lookup_class_binding (ctx, name);
   10952              :             else
   10953         1055 :               res = lookup_field_ident (ctx, u ());
   10954              : 
   10955         9076 :             if (!res
   10956         9076 :                 || (TREE_CODE (res) != FIELD_DECL
   10957         9076 :                     && TREE_CODE (res) != USING_DECL)
   10958        18152 :                 || DECL_CONTEXT (res) != ctx)
   10959            0 :               res = NULL_TREE;
   10960              :           }
   10961              : 
   10962         9076 :         if (!res)
   10963            0 :           set_overrun ();
   10964              :         else
   10965              :           {
   10966         9076 :             int tag = insert (res);
   10967         9076 :             dump (dumper::TREE)
   10968           26 :               && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
   10969              :           }
   10970              :       }
   10971              :       break;
   10972              : 
   10973         1639 :     case tt_binfo:
   10974              :       /* A BINFO.  Walk the tree of the dominating type.  */
   10975         1639 :       {
   10976         1639 :         tree type;
   10977         1639 :         unsigned ix = binfo_mergeable (&type);
   10978         1639 :         if (type)
   10979              :           {
   10980         1639 :             res = TYPE_BINFO (type);
   10981         1725 :             for (; ix && res; res = TREE_CHAIN (res))
   10982           86 :               ix--;
   10983         1639 :             if (!res)
   10984            0 :               set_overrun ();
   10985              :           }
   10986              : 
   10987         1639 :         if (get_overrun ())
   10988              :           break;
   10989              : 
   10990              :         /* Insert binfo into backreferences.  */
   10991         1639 :         tag = insert (res);
   10992         1639 :         dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
   10993              :       }
   10994         1639 :       break;
   10995              : 
   10996           73 :     case tt_vtable:
   10997           73 :       {
   10998           73 :         unsigned ix = u ();
   10999           73 :         tree ctx = tree_node ();
   11000           73 :         dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
   11001           73 :         if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
   11002           85 :           for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
   11003           85 :             if (!ix--)
   11004              :               break;
   11005           73 :         if (!res)
   11006            0 :           set_overrun ();
   11007              :       }
   11008              :       break;
   11009              : 
   11010            0 :     case tt_thunk:
   11011            0 :       {
   11012            0 :         int fixed = i ();
   11013            0 :         tree target = tree_node ();
   11014            0 :         tree virt = tree_node ();
   11015              : 
   11016            0 :         for (tree thunk = DECL_THUNKS (target);
   11017            0 :              thunk; thunk = DECL_CHAIN (thunk))
   11018            0 :           if (THUNK_FIXED_OFFSET (thunk) == fixed
   11019            0 :               && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
   11020            0 :               && (!virt
   11021            0 :                   || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
   11022              :             {
   11023            0 :               res = thunk;
   11024            0 :               break;
   11025              :             }
   11026              : 
   11027            0 :         int tag = insert (res);
   11028            0 :         if (res)
   11029            0 :           dump (dumper::TREE)
   11030            0 :             && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
   11031              :         else
   11032            0 :           set_overrun ();
   11033              :       }
   11034              :       break;
   11035              : 
   11036       157187 :     case tt_clone_ref:
   11037       157187 :       {
   11038       157187 :         tree target = tree_node ();
   11039       157187 :         tree name = tree_node ();
   11040              : 
   11041       157187 :         if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
   11042              :           {
   11043       157187 :             tree clone;
   11044       244016 :             FOR_EVERY_CLONE (clone, target)
   11045       244016 :               if (DECL_NAME (clone) == name)
   11046              :                 {
   11047       157187 :                   res = clone;
   11048       157187 :                   break;
   11049              :                 }
   11050              :           }
   11051              : 
   11052              :         /* A clone might have a different vtable entry.  */
   11053       157187 :         if (res && DECL_VIRTUAL_P (res))
   11054         8619 :           DECL_VINDEX (res) = tree_node ();
   11055              : 
   11056       157187 :         if (!res)
   11057            0 :           set_overrun ();
   11058       157187 :         int tag = insert (res);
   11059       157187 :         if (res)
   11060       157187 :           dump (dumper::TREE)
   11061          230 :             && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
   11062              :         else
   11063            0 :           set_overrun ();
   11064              :        }
   11065              :       break;
   11066              : 
   11067      1101842 :     case tt_entity:
   11068              :       /* Index into the entity table.  Perhaps not loaded yet!  */
   11069      1101842 :       {
   11070      1101842 :         unsigned origin = state->slurp->remap_module (u ());
   11071      1101842 :         unsigned ident = u ();
   11072      1101842 :         module_state *from = (*modules)[origin];
   11073              : 
   11074      1101842 :         if (!origin || ident >= from->entity_num)
   11075            0 :           set_overrun ();
   11076      1101842 :         if (!get_overrun ())
   11077              :           {
   11078      1101842 :             binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
   11079      1101842 :             if (slot->is_lazy ())
   11080        55477 :               if (!from->lazy_load (ident, slot))
   11081            0 :                 set_overrun ();
   11082      1101842 :             res = *slot;
   11083              :           }
   11084              : 
   11085      1101842 :         if (res)
   11086              :           {
   11087      1101842 :             const char *kind = (origin != state->mod ? "Imported" : "Named");
   11088      1101842 :             int tag = insert (res);
   11089      1101842 :             dump (dumper::TREE)
   11090          605 :               && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
   11091          605 :                        res, (*modules)[origin]);
   11092              : 
   11093      1101842 :             if (!add_indirects (res))
   11094              :               {
   11095            0 :                 set_overrun ();
   11096            0 :                 res = NULL_TREE;
   11097              :               }
   11098              :           }
   11099              :       }
   11100              :       break;
   11101              : 
   11102         3094 :     case tt_template:
   11103              :       /* A template.  */
   11104         3094 :       if (tree tpl = tree_node ())
   11105              :         {
   11106         3094 :           res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
   11107         3094 :                  tpl : DECL_TEMPLATE_RESULT (tpl));
   11108         3094 :           dump (dumper::TREE)
   11109            9 :             && dump ("Read template %C:%N", TREE_CODE (res), res);
   11110              :         }
   11111              :       break;
   11112              :     }
   11113              : 
   11114     98458755 :   if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
   11115              :     {
   11116              :       /* Mark decl used as mark_used does -- we cannot call
   11117              :          mark_used in the middle of streaming, we only need a subset
   11118              :          of its functionality.   */
   11119       769191 :       TREE_USED (res) = true;
   11120              : 
   11121              :       /* And for structured bindings also the underlying decl.  */
   11122       769191 :       if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
   11123         2010 :         TREE_USED (DECL_DECOMP_BASE (res)) = true;
   11124              : 
   11125       769191 :       if (DECL_CLONED_FUNCTION_P (res))
   11126         7352 :         TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
   11127              :     }
   11128              : 
   11129     98458755 :   dump.outdent ();
   11130     98458755 :   return res;
   11131              : }
   11132              : 
   11133              : void
   11134      2138821 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
   11135              : {
   11136      2138821 :   if (!parms)
   11137              :     return;
   11138              : 
   11139      1441619 :   if (TREE_VISITED (parms))
   11140              :     {
   11141       601751 :       ref_node (parms);
   11142       601751 :       return;
   11143              :     }
   11144              : 
   11145       839868 :   tpl_parms (TREE_CHAIN (parms), tpl_levels);
   11146              : 
   11147       839868 :   tree vec = TREE_VALUE (parms);
   11148       839868 :   unsigned len = TREE_VEC_LENGTH (vec);
   11149              :   /* Depth.  */
   11150       839868 :   int tag = insert (parms);
   11151       839868 :   if (streaming_p ())
   11152              :     {
   11153       223887 :       i (len + 1);
   11154       223953 :       dump (dumper::TREE)
   11155           66 :         && dump ("Writing template parms:%d level:%N length:%d",
   11156           66 :                  tag, TREE_PURPOSE (parms), len);
   11157              :     }
   11158       839868 :   tree_node (TREE_PURPOSE (parms));
   11159              : 
   11160      2324040 :   for (unsigned ix = 0; ix != len; ix++)
   11161              :     {
   11162      1484172 :       tree parm = TREE_VEC_ELT (vec, ix);
   11163      1484172 :       tree decl = TREE_VALUE (parm);
   11164              : 
   11165      1484172 :       gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
   11166      1484172 :       if (CHECKING_P)
   11167      1484172 :         switch (TREE_CODE (decl))
   11168              :           {
   11169            0 :           default: gcc_unreachable ();
   11170              : 
   11171         3879 :           case TEMPLATE_DECL:
   11172         3879 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
   11173              :                         && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
   11174              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11175              :             break;
   11176              : 
   11177      1385633 :           case TYPE_DECL:
   11178      1385633 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
   11179              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11180              :             break;
   11181              : 
   11182        94660 :           case PARM_DECL:
   11183        94660 :             gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
   11184              :                         && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
   11185              :                             == CONST_DECL)
   11186              :                         && (DECL_TEMPLATE_PARM_P
   11187              :                             (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
   11188              :             break;
   11189              :           }
   11190              : 
   11191      1484172 :       tree_node (decl);
   11192      1484172 :       tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
   11193              :     }
   11194              : 
   11195       839868 :   tpl_levels++;
   11196              : }
   11197              : 
   11198              : tree
   11199       333726 : trees_in::tpl_parms (unsigned &tpl_levels)
   11200              : {
   11201       333726 :   tree parms = NULL_TREE;
   11202              : 
   11203       704679 :   while (int len = i ())
   11204              :     {
   11205       370953 :       if (len < 0)
   11206              :         {
   11207       201687 :           parms = back_ref (len);
   11208       201687 :           continue;
   11209              :         }
   11210              : 
   11211       169266 :       len -= 1;
   11212       169266 :       parms = tree_cons (NULL_TREE, NULL_TREE, parms);
   11213       169266 :       int tag = insert (parms);
   11214       169266 :       TREE_PURPOSE (parms) = tree_node ();
   11215              : 
   11216       169266 :       dump (dumper::TREE)
   11217          105 :         && dump ("Reading template parms:%d level:%N length:%d",
   11218          105 :                  tag, TREE_PURPOSE (parms), len);
   11219              : 
   11220       169266 :       tree vec = make_tree_vec (len);
   11221       445685 :       for (int ix = 0; ix != len; ix++)
   11222              :         {
   11223       276419 :           tree decl = tree_node ();
   11224       276419 :           if (!decl)
   11225              :             return NULL_TREE;
   11226              : 
   11227       276419 :           tree parm = build_tree_list (NULL, decl);
   11228       276419 :           TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
   11229              : 
   11230       276419 :           TREE_VEC_ELT (vec, ix) = parm;
   11231              :         }
   11232              : 
   11233       169266 :       TREE_VALUE (parms) = vec;
   11234       169266 :       tpl_levels++;
   11235              :     }
   11236              : 
   11237              :   return parms;
   11238              : }
   11239              : 
   11240              : void
   11241      1298953 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11242              : {
   11243      1298953 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11244      2138821 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11245              :     {
   11246       839868 :       tree vec = TREE_VALUE (parms);
   11247              : 
   11248       839868 :       tree_node (TREE_TYPE (vec));
   11249      2324040 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11250              :         {
   11251      1484172 :           tree parm = TREE_VEC_ELT (vec, ix);
   11252      1484172 :           tree dflt = TREE_PURPOSE (parm);
   11253      1484172 :           tree_node (dflt);
   11254              : 
   11255              :           /* Template template parameters need a context of their owning
   11256              :              template. This is quite tricky to infer correctly on stream-in
   11257              :              (see PR c++/98881) so we'll just provide it directly.  */
   11258      1484172 :           tree decl = TREE_VALUE (parm);
   11259      1484172 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11260         3879 :             tree_node (DECL_CONTEXT (decl));
   11261              :         }
   11262              :     }
   11263      1298953 : }
   11264              : 
   11265              : bool
   11266       333726 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11267              : {
   11268       333726 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11269       502992 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11270              :     {
   11271       169266 :       tree vec = TREE_VALUE (parms);
   11272              : 
   11273       169266 :       TREE_TYPE (vec) = tree_node ();
   11274       445685 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11275              :         {
   11276       276419 :           tree parm = TREE_VEC_ELT (vec, ix);
   11277       276419 :           tree dflt = tree_node ();
   11278       276419 :           TREE_PURPOSE (parm) = dflt;
   11279              : 
   11280       276419 :           tree decl = TREE_VALUE (parm);
   11281       276419 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11282          852 :             DECL_CONTEXT (decl) = tree_node ();
   11283              : 
   11284       276419 :           if (get_overrun ())
   11285              :             return false;
   11286              :         }
   11287              :     }
   11288              :   return true;
   11289              : }
   11290              : 
   11291              : /* PARMS is a LIST, one node per level.
   11292              :    TREE_VALUE is a TREE_VEC of parm info for that level.
   11293              :    each ELT is a TREE_LIST
   11294              :    TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
   11295              :    TREE_PURPOSE is the default value.  */
   11296              : 
   11297              : void
   11298      1298953 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
   11299              : {
   11300      1298953 :   tree parms = DECL_TEMPLATE_PARMS (tpl);
   11301      1298953 :   tpl_parms (parms, *tpl_levels);
   11302              : 
   11303              :   /* Mark end.  */
   11304      1298953 :   if (streaming_p ())
   11305       432494 :     u (0);
   11306              : 
   11307      1298953 :   if (*tpl_levels)
   11308       789297 :     tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
   11309      1298953 : }
   11310              : 
   11311              : bool
   11312       333726 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
   11313              : {
   11314       333726 :   tree parms = tpl_parms (*tpl_levels);
   11315       333726 :   if (!parms)
   11316              :     return false;
   11317              : 
   11318       333726 :   DECL_TEMPLATE_PARMS (tpl) = parms;
   11319              : 
   11320       333726 :   if (*tpl_levels)
   11321       167097 :     TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
   11322              : 
   11323              :   return true;
   11324              : }
   11325              : 
   11326              : /* Stream skeleton parm nodes, with their flags, type & parm indices.
   11327              :    All the parms will have consecutive tags.  */
   11328              : 
   11329              : void
   11330      1813416 : trees_out::fn_parms_init (tree fn)
   11331              : {
   11332              :   /* First init them.  */
   11333      1813416 :   int base_tag = ref_num - 1;
   11334      1813416 :   int ix = 0;
   11335      1813416 :   for (tree parm = DECL_ARGUMENTS (fn);
   11336      5482932 :        parm; parm = DECL_CHAIN (parm), ix++)
   11337              :     {
   11338      3669516 :       if (streaming_p ())
   11339              :         {
   11340      1223135 :           start (parm);
   11341      1223135 :           tree_node_bools (parm);
   11342              :         }
   11343      3669516 :       int tag = insert (parm);
   11344      3669516 :       gcc_checking_assert (base_tag - ix == tag);
   11345              :     }
   11346              :   /* Mark the end.  */
   11347      1813416 :   if (streaming_p ())
   11348       604739 :     u (0);
   11349              : 
   11350              :   /* Now stream their contents.  */
   11351      1813416 :   ix = 0;
   11352      1813416 :   for (tree parm = DECL_ARGUMENTS (fn);
   11353      5482932 :        parm; parm = DECL_CHAIN (parm), ix++)
   11354              :     {
   11355      3669516 :       if (streaming_p ())
   11356      1223135 :         dump (dumper::TREE)
   11357          222 :           && dump ("Writing parm:%d %u (%N) of %N",
   11358              :                    base_tag - ix, ix, parm, fn);
   11359      3669516 :       tree_node_vals (parm);
   11360              :     }
   11361              : 
   11362      1813416 :   if (!streaming_p ())
   11363              :     {
   11364              :       /* We must walk contract specifiers so the dependency graph is
   11365              :          complete.  */
   11366      1208677 :       tree contract = get_fn_contract_specifiers (fn);
   11367      2417354 :       for (; contract; contract = TREE_CHAIN (contract))
   11368            0 :         tree_node (contract);
   11369              :     }
   11370              : 
   11371              :   /* Write a reference to contracts pre/post functions, if any, to avoid
   11372              :      regenerating them in importers.  */
   11373      1813416 :   tree_node (DECL_PRE_FN (fn));
   11374      1813416 :   tree_node (DECL_POST_FN (fn));
   11375      1813416 : }
   11376              : 
   11377              : /* Build skeleton parm nodes, read their flags, type & parm indices.  */
   11378              : 
   11379              : int
   11380       471371 : trees_in::fn_parms_init (tree fn)
   11381              : {
   11382       471371 :   int base_tag = ~(int)back_refs.length ();
   11383              : 
   11384       471371 :   tree *parm_ptr = &DECL_ARGUMENTS (fn);
   11385       471371 :   int ix = 0;
   11386      1422695 :   for (; int code = u (); ix++)
   11387              :     {
   11388       951324 :       tree parm = start (code);
   11389       951324 :       if (!tree_node_bools (parm))
   11390              :         return 0;
   11391              : 
   11392       951324 :       int tag = insert (parm);
   11393       951324 :       gcc_checking_assert (base_tag - ix == tag);
   11394       951324 :       *parm_ptr = parm;
   11395       951324 :       parm_ptr = &DECL_CHAIN (parm);
   11396       951324 :     }
   11397              : 
   11398       471371 :   ix = 0;
   11399       471371 :   for (tree parm = DECL_ARGUMENTS (fn);
   11400      1422695 :        parm; parm = DECL_CHAIN (parm), ix++)
   11401              :     {
   11402       951324 :       dump (dumper::TREE)
   11403          362 :         && dump ("Reading parm:%d %u (%N) of %N",
   11404              :                  base_tag - ix, ix, parm, fn);
   11405       951324 :       if (!tree_node_vals (parm))
   11406              :         return 0;
   11407              : 
   11408              :       /* Apply relevant attributes.
   11409              :          FIXME should probably use cplus_decl_attributes for this,
   11410              :          but it's not yet ready for modules.  */
   11411              : 
   11412              :       /* TREE_USED is deliberately not streamed for most declarations,
   11413              :          but needs to be set if we have the [[maybe_unused]] attribute.  */
   11414       951324 :       if (lookup_attribute ("unused", DECL_ATTRIBUTES (parm))
   11415       951324 :           || lookup_attribute ("maybe_unused", DECL_ATTRIBUTES (parm)))
   11416              :         {
   11417         2153 :           TREE_USED (parm) = true;
   11418         2153 :           DECL_READ_P (parm) = true;
   11419              :         }
   11420              :     }
   11421              : 
   11422              :   /* Reload references to contract functions, if any.  */
   11423       471371 :   tree pre_fn = tree_node ();
   11424       471371 :   tree post_fn = tree_node ();
   11425       471371 :   set_contract_functions (fn, pre_fn, post_fn);
   11426              : 
   11427       471371 :   return base_tag;
   11428              : }
   11429              : 
   11430              : /* Read the remaining parm node data.  Replace with existing (if
   11431              :    non-null) in the map.  */
   11432              : 
   11433              : void
   11434       471371 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
   11435              : {
   11436       672427 :   tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
   11437       471371 :   tree parms = DECL_ARGUMENTS (fn);
   11438      1422695 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   11439              :     {
   11440       951324 :       if (existing_parm)
   11441              :         {
   11442       585856 :           if (is_defn && !DECL_SAVED_TREE (existing))
   11443              :             {
   11444              :               /* If we're about to become the definition, set the
   11445              :                  names of the parms from us.  */
   11446        15283 :               DECL_NAME (existing_parm) = DECL_NAME (parm);
   11447        15283 :               DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
   11448              : 
   11449              :               /* And some other flags important for codegen are only set
   11450              :                  by the definition.  */
   11451        15283 :               TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
   11452        15283 :               DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
   11453        15283 :               DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
   11454        15283 :               DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
   11455              : 
   11456              :               /* Invisiref parms had their types adjusted by cp_genericize. */
   11457        15283 :               if (DECL_BY_REFERENCE (parm))
   11458              :                 {
   11459            6 :                   TREE_TYPE (existing_parm) = TREE_TYPE (parm);
   11460            6 :                   relayout_decl (existing_parm);
   11461              :                 }
   11462              :             }
   11463              : 
   11464       397497 :           back_refs[~tag] = existing_parm;
   11465       397497 :           existing_parm = DECL_CHAIN (existing_parm);
   11466              :         }
   11467       951324 :       tag--;
   11468              :     }
   11469       471371 : }
   11470              : 
   11471              : /* Encode into KEY the position of the local type (class or enum)
   11472              :    declaration DECL within FN.  The position is encoded as the
   11473              :    index of the innermost BLOCK (numbered in BFS order) along with
   11474              :    the index within its BLOCK_VARS list.  */
   11475              : 
   11476              : void
   11477        19935 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
   11478              : {
   11479        19935 :   auto_vec<tree, 4> blocks;
   11480        19935 :   blocks.quick_push (DECL_INITIAL (fn));
   11481        19935 :   unsigned block_ix = 0;
   11482        96531 :   while (block_ix != blocks.length ())
   11483              :     {
   11484        38298 :       tree block = blocks[block_ix];
   11485        38298 :       unsigned decl_ix = 0;
   11486       114963 :       for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11487              :         {
   11488        96600 :           if (TREE_CODE (var) != TYPE_DECL)
   11489        59955 :             continue;
   11490        36645 :           if (var == decl)
   11491              :             {
   11492        19935 :               key.index = (block_ix << 10) | decl_ix;
   11493        19935 :               return;
   11494              :             }
   11495        16710 :           ++decl_ix;
   11496              :         }
   11497        38292 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11498        19929 :         blocks.safe_push (sub);
   11499        18363 :       ++block_ix;
   11500              :     }
   11501              : 
   11502              :   /* Not-found value.  */
   11503            0 :   key.index = 1023;
   11504        19935 : }
   11505              : 
   11506              : /* Look up the local type corresponding at the position encoded by
   11507              :    KEY within FN and named NAME.  */
   11508              : 
   11509              : tree
   11510         4355 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
   11511              : {
   11512         4355 :   if (!DECL_INITIAL (fn))
   11513              :     return NULL_TREE;
   11514              : 
   11515         1936 :   const unsigned block_pos = key.index >> 10;
   11516         1936 :   const unsigned decl_pos = key.index & 1023;
   11517              : 
   11518         1936 :   if (decl_pos == 1023)
   11519              :     return NULL_TREE;
   11520              : 
   11521         1936 :   auto_vec<tree, 4> blocks;
   11522         1936 :   blocks.quick_push (DECL_INITIAL (fn));
   11523         1936 :   unsigned block_ix = 0;
   11524         8916 :   while (block_ix != blocks.length ())
   11525              :     {
   11526         3490 :       tree block = blocks[block_ix];
   11527         3490 :       if (block_ix == block_pos)
   11528              :         {
   11529         1936 :           unsigned decl_ix = 0;
   11530         5254 :           for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11531              :             {
   11532         5254 :               if (TREE_CODE (var) != TYPE_DECL)
   11533         2340 :                 continue;
   11534              :               /* Prefer using the identifier as the key for more robustness
   11535              :                  to ODR violations, except for anonymous types since their
   11536              :                  compiler-generated identifiers aren't stable.  */
   11537         5828 :               if (IDENTIFIER_ANON_P (name)
   11538         2914 :                   ? decl_ix == decl_pos
   11539          323 :                   : DECL_NAME (var) == name)
   11540              :                 return var;
   11541          978 :               ++decl_ix;
   11542              :             }
   11543              :           return NULL_TREE;
   11544              :         }
   11545         3217 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11546         1663 :         blocks.safe_push (sub);
   11547         1554 :       ++block_ix;
   11548              :     }
   11549              : 
   11550              :   return NULL_TREE;
   11551         1936 : }
   11552              : 
   11553              : /* DEP is the depset of some decl we're streaming by value.  Determine
   11554              :    the merging behaviour.  */
   11555              : 
   11556              : merge_kind
   11557      4501499 : trees_out::get_merge_kind (tree decl, depset *dep)
   11558              : {
   11559      4501499 :   if (!dep)
   11560              :     {
   11561       922524 :       if (VAR_OR_FUNCTION_DECL_P (decl))
   11562              :         {
   11563              :           /* Any var or function with template info should have DEP.  */
   11564       515324 :           gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   11565              :                                || !DECL_TEMPLATE_INFO (decl));
   11566       515324 :           if (DECL_LOCAL_DECL_P (decl))
   11567              :             return MK_unique;
   11568              :         }
   11569              : 
   11570              :       /* Either unique, or some member of a class that cannot have an
   11571              :          out-of-class definition.  For instance a FIELD_DECL.  */
   11572       922212 :       tree ctx = CP_DECL_CONTEXT (decl);
   11573       922212 :       if (TREE_CODE (ctx) == FUNCTION_DECL)
   11574              :         {
   11575              :           /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
   11576              :              this isn't permitting them to have one.   */
   11577       592258 :           gcc_checking_assert (TREE_CODE (decl) == USING_DECL
   11578              :                                || TREE_CODE (decl) == NAMESPACE_DECL
   11579              :                                || !DECL_LANG_SPECIFIC (decl)
   11580              :                                || !DECL_TEMPLATE_INFO (decl));
   11581              : 
   11582              :           return MK_unique;
   11583              :         }
   11584              : 
   11585       329954 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   11586       329954 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   11587              :         return MK_local_friend;
   11588              : 
   11589       329954 :       gcc_checking_assert (TYPE_P (ctx));
   11590              : 
   11591              :       /* Internal-only types will not need to dedup their members.  */
   11592       329954 :       if (!DECL_CONTEXT (TYPE_NAME (ctx)))
   11593              :         return MK_unique;
   11594              : 
   11595       329898 :       if (TREE_CODE (decl) == USING_DECL)
   11596              :         return MK_field;
   11597              : 
   11598       216839 :       if (TREE_CODE (decl) == FIELD_DECL)
   11599              :         {
   11600       159515 :           if (DECL_NAME (decl))
   11601              :             {
   11602              :               /* Anonymous FIELD_DECLs have a NULL name.  */
   11603       128882 :               gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
   11604              :               return MK_named;
   11605              :             }
   11606              : 
   11607        30633 :           if (walking_bit_field_unit)
   11608              :             {
   11609              :               /* The underlying storage unit for a bitfield.  We do not
   11610              :                  need to dedup it, because it's only reachable through
   11611              :                  the bitfields it represents.  And those are deduped.  */
   11612              :               // FIXME: Is that assertion correct -- do we ever fish it
   11613              :               // out and put it in an expr?
   11614          528 :               gcc_checking_assert (!DECL_NAME (decl)
   11615              :                                    && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   11616              :                                    && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
   11617          528 :               gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
   11618              :                                     ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   11619              :                                     : TREE_CODE (TREE_TYPE (decl)))
   11620              :                                    == INTEGER_TYPE);
   11621              :               return MK_unique;
   11622              :             }
   11623              : 
   11624              :           return MK_field;
   11625              :         }
   11626              : 
   11627        57324 :       if (TREE_CODE (decl) == CONST_DECL)
   11628              :         return MK_named;
   11629              : 
   11630        10182 :       if (TREE_CODE (decl) == VAR_DECL
   11631        10182 :           && DECL_VTABLE_OR_VTT_P (decl))
   11632              :         return MK_vtable;
   11633              : 
   11634         1736 :       if (DECL_THUNK_P (decl))
   11635              :         /* Thunks are unique-enough, because they're only referenced
   11636              :            from the vtable.  And that's either new (so we want the
   11637              :            thunks), or it's a duplicate (so it will be dropped).  */
   11638              :         return MK_unique;
   11639              : 
   11640              :       /* There should be no other cases.  */
   11641            0 :       gcc_unreachable ();
   11642              :     }
   11643              : 
   11644      3578975 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   11645              :                        && TREE_CODE (decl) != USING_DECL
   11646              :                        && TREE_CODE (decl) != CONST_DECL);
   11647              : 
   11648      3578975 :   if (is_key_order ())
   11649              :     {
   11650              :       /* When doing the mergeablilty graph, there's an indirection to
   11651              :          the actual depset.  */
   11652      1192838 :       gcc_assert (dep->is_special ());
   11653      1192838 :       dep = dep->deps[0];
   11654              :     }
   11655              : 
   11656      3578975 :   gcc_checking_assert (decl == dep->get_entity ());
   11657              : 
   11658      3578975 :   merge_kind mk = MK_named;
   11659      3578975 :   switch (dep->get_entity_kind ())
   11660              :     {
   11661            0 :     default:
   11662            0 :       gcc_unreachable ();
   11663              : 
   11664              :     case depset::EK_PARTIAL:
   11665              :       mk = MK_partial;
   11666              :       break;
   11667              : 
   11668      2024750 :     case depset::EK_DECL:
   11669      2024750 :       {
   11670      2024750 :         tree ctx = CP_DECL_CONTEXT (decl);
   11671              : 
   11672      2024750 :         switch (TREE_CODE (ctx))
   11673              :           {
   11674            0 :           default:
   11675            0 :             gcc_unreachable ();
   11676              : 
   11677        20199 :           case FUNCTION_DECL:
   11678        20199 :             gcc_checking_assert
   11679              :               (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   11680              : 
   11681        20199 :             if (has_definition (ctx))
   11682              :               mk = MK_local_type;
   11683              :             else
   11684              :               /* We're not providing a definition of the context to key
   11685              :                  the local type into; use the keyed map instead.  */
   11686         1216 :               mk = MK_keyed;
   11687              :             break;
   11688              : 
   11689      2004551 :           case RECORD_TYPE:
   11690      2004551 :           case UNION_TYPE:
   11691      2004551 :           case NAMESPACE_DECL:
   11692      2004551 :             if (DECL_NAME (decl) == as_base_identifier)
   11693              :               {
   11694              :                 mk = MK_as_base;
   11695              :                 break;
   11696              :               }
   11697              : 
   11698              :             /* A lambda may have a class as its context, even though it
   11699              :                isn't a member in the traditional sense; see the test
   11700              :                g++.dg/modules/lambda-6_a.C.  */
   11701      2499911 :             if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
   11702      2179580 :                 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   11703              :               {
   11704          988 :                 if (get_keyed_decl_scope (decl))
   11705              :                   mk = MK_keyed;
   11706              :                 else
   11707              :                   /* Lambdas not attached to any mangling scope are TU-local
   11708              :                      and so cannot be deduplicated.  */
   11709       595403 :                   mk = MK_unique;
   11710              :                 break;
   11711              :               }
   11712              : 
   11713      1913614 :             if (TREE_CODE (decl) == TEMPLATE_DECL
   11714      1913614 :                 ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11715       821052 :                 : decl_specialization_friend_p (decl))
   11716              :               {
   11717              :                 mk = MK_local_friend;
   11718              :                 break;
   11719              :               }
   11720              : 
   11721      1888567 :             if (DECL_DECOMPOSITION_P (decl))
   11722              :               {
   11723              :                 mk = MK_unique;
   11724              :                 break;
   11725              :               }
   11726              : 
   11727      1888090 :             if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   11728              :               {
   11729        33005 :                 if (RECORD_OR_UNION_TYPE_P (ctx))
   11730              :                   mk = MK_field;
   11731         1177 :                 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   11732         1177 :                          && UNSCOPED_ENUM_P (TREE_TYPE (decl))
   11733         2354 :                          && TYPE_VALUES (TREE_TYPE (decl)))
   11734              :                   /* Keyed by first enum value, and underlying type.  */
   11735              :                   mk = MK_enum;
   11736              :                 else
   11737              :                   /* No way to merge it, it is an ODR land-mine.  */
   11738              :                   mk = MK_unique;
   11739              :               }
   11740              :           }
   11741              :       }
   11742              :       break;
   11743              : 
   11744      1496019 :     case depset::EK_SPECIALIZATION:
   11745      1496019 :       {
   11746      1496019 :         gcc_checking_assert (dep->is_special ());
   11747              : 
   11748      1496019 :         if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   11749              :           /* An block-scope classes of templates are themselves
   11750              :              templates.  */
   11751         5577 :           gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
   11752              : 
   11753      1496019 :         if (dep->is_friend_spec ())
   11754              :           mk = MK_friend_spec;
   11755      1496019 :         else if (dep->is_type_spec ())
   11756              :           mk = MK_type_spec;
   11757              :         else
   11758      1061091 :           mk = MK_decl_spec;
   11759              : 
   11760      1496019 :         if (TREE_CODE (decl) == TEMPLATE_DECL)
   11761              :           {
   11762       123465 :             spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11763       123465 :             if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
   11764        12708 :               mk = merge_kind (mk | MK_tmpl_tmpl_mask);
   11765              :           }
   11766              :       }
   11767              :       break;
   11768              :     }
   11769              : 
   11770              :   return mk;
   11771              : }
   11772              : 
   11773              : 
   11774              : /* The container of DECL -- not necessarily its context!  */
   11775              : 
   11776              : tree
   11777      4501499 : trees_out::decl_container (tree decl)
   11778              : {
   11779      4501499 :   int use_tpl;
   11780      4501499 :   tree tpl = NULL_TREE;
   11781      4501499 :   if (tree template_info = node_template_info (decl, use_tpl))
   11782      1564984 :     tpl = TI_TEMPLATE (template_info);
   11783      4501499 :   if (tpl == decl)
   11784            0 :     tpl = nullptr;
   11785              : 
   11786              :   /* Stream the template we're instantiated from.  */
   11787      4501499 :   tree_node (tpl);
   11788              : 
   11789      4501499 :   tree container = NULL_TREE;
   11790      4501499 :   if (TREE_CODE (decl) == TEMPLATE_DECL
   11791      4501499 :       ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11792      3209335 :       : decl_specialization_friend_p (decl))
   11793        25047 :     container = DECL_CHAIN (decl);
   11794              :   else
   11795      4476452 :     container = CP_DECL_CONTEXT (decl);
   11796              : 
   11797      4501499 :   if (TYPE_P (container))
   11798      2639905 :     container = TYPE_NAME (container);
   11799              : 
   11800      4501499 :   tree_node (container);
   11801              : 
   11802      4501499 :   return container;
   11803              : }
   11804              : 
   11805              : tree
   11806      1231942 : trees_in::decl_container ()
   11807              : {
   11808              :   /* The maybe-template.  */
   11809      1231942 :   (void)tree_node ();
   11810              : 
   11811      1231942 :   tree container = tree_node ();
   11812              : 
   11813      1231942 :   return container;
   11814              : }
   11815              : 
   11816              : /* Gets a 2-bit discriminator to distinguish coroutine actor or destroy
   11817              :    functions from a normal function.  */
   11818              : 
   11819              : static int
   11820      1260708 : get_coroutine_discriminator (tree inner)
   11821              : {
   11822      1260708 :   if (DECL_COROUTINE_P (inner))
   11823           72 :     if (tree ramp = DECL_RAMP_FN (inner))
   11824              :       {
   11825           18 :         if (DECL_ACTOR_FN (ramp) == inner)
   11826              :           return 1;
   11827            9 :         else if (DECL_DESTROY_FN (ramp) == inner)
   11828              :           return 2;
   11829              :         else
   11830            0 :           gcc_unreachable ();
   11831              :       }
   11832              :   return 0;
   11833              : }
   11834              : 
   11835              : /* Write out key information about a mergeable DEP.  Does not write
   11836              :    the contents of DEP itself.  The context has already been
   11837              :    written.  The container has already been streamed.  */
   11838              : 
   11839              : void
   11840      4501499 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   11841              :                           tree container, depset *dep)
   11842              : {
   11843      4501499 :   if (dep && is_key_order ())
   11844              :     {
   11845      1192838 :       gcc_checking_assert (dep->is_special ());
   11846      1192838 :       dep = dep->deps[0];
   11847              :     }
   11848              : 
   11849      4501499 :   if (streaming_p ())
   11850      1643514 :     dump (dumper::MERGE)
   11851         1101 :       && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   11852          993 :                dep ? dep->entity_kind_name () : "contained",
   11853         1101 :                TREE_CODE (decl), decl);
   11854              : 
   11855              :   /* Now write the locating information. */
   11856      4501499 :   if (mk & MK_template_mask)
   11857              :     {
   11858              :       /* Specializations are located via their originating template,
   11859              :          and the set of template args they specialize.  */
   11860      1496019 :       gcc_checking_assert (dep && dep->is_special ());
   11861      1496019 :       spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11862              : 
   11863      1496019 :       tree_node (entry->tmpl);
   11864      1496019 :       tree_node (entry->args);
   11865      1496019 :       if (mk & MK_tmpl_decl_mask)
   11866      1061091 :         if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   11867              :           {
   11868              :             /* Variable template partial specializations might need
   11869              :                constraints (see spec_hasher::equal).  It's simpler to
   11870              :                write NULL when we don't need them.  */
   11871        22881 :             tree constraints = NULL_TREE;
   11872              : 
   11873        22881 :             if (uses_template_parms (entry->args))
   11874          714 :               constraints = get_constraints (inner);
   11875        22881 :             tree_node (constraints);
   11876              :           }
   11877              : 
   11878      1496019 :       if (CHECKING_P)
   11879              :         {
   11880              :           /* Make sure we can locate the decl.  */
   11881      1496019 :           tree existing = match_mergeable_specialization
   11882      1496019 :             (bool (mk & MK_tmpl_decl_mask), entry);
   11883              : 
   11884      1496019 :           gcc_assert (existing);
   11885      1496019 :           if (mk & MK_tmpl_decl_mask)
   11886              :             {
   11887      1061091 :               if (mk & MK_tmpl_tmpl_mask)
   11888        10032 :                 existing = DECL_TI_TEMPLATE (existing);
   11889              :             }
   11890              :           else
   11891              :             {
   11892       434928 :               if (mk & MK_tmpl_tmpl_mask)
   11893         2676 :                 existing = CLASSTYPE_TI_TEMPLATE (existing);
   11894              :               else
   11895       432252 :                 existing = TYPE_NAME (existing);
   11896              :             }
   11897              : 
   11898              :           /* The walkabout should have found ourselves.  */
   11899      1496019 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
   11900              :                                ? same_type_p (TREE_TYPE (decl),
   11901              :                                               TREE_TYPE (existing))
   11902              :                                : existing == decl);
   11903              :         }
   11904              :     }
   11905      3005480 :   else if (mk != MK_unique)
   11906              :     {
   11907      2410077 :       merge_key key;
   11908      2410077 :       tree name = DECL_NAME (decl);
   11909              : 
   11910      2410077 :       switch (mk)
   11911              :         {
   11912            0 :         default:
   11913            0 :           gcc_unreachable ();
   11914              : 
   11915      2031109 :         case MK_named:
   11916      2031109 :         case MK_friend_spec:
   11917      2031109 :           if (IDENTIFIER_CONV_OP_P (name))
   11918         7123 :             name = conv_op_identifier;
   11919              : 
   11920      2031109 :           if (TREE_CODE (inner) == FUNCTION_DECL)
   11921              :             {
   11922              :               /* Functions are distinguished by parameter types.  */
   11923      1128793 :               tree fn_type = TREE_TYPE (inner);
   11924              : 
   11925      1128793 :               key.ref_q = type_memfn_rqual (fn_type);
   11926      1128793 :               key.coro_disc = get_coroutine_discriminator (inner);
   11927      1128793 :               key.iobj_p = DECL_IOBJ_MEMBER_FUNCTION_P (inner);
   11928      1128793 :               key.xobj_p = DECL_XOBJ_MEMBER_FUNCTION_P (inner);
   11929      1128793 :               key.args = TYPE_ARG_TYPES (fn_type);
   11930              : 
   11931      1128793 :               if (tree reqs = get_constraints (inner))
   11932              :                 {
   11933        81564 :                   if (cxx_dialect < cxx20)
   11934           48 :                     reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
   11935              :                   else
   11936       163080 :                     reqs = CI_DECLARATOR_REQS (reqs);
   11937        81564 :                   key.constraints = reqs;
   11938              :                 }
   11939              : 
   11940      1128793 :               if (IDENTIFIER_CONV_OP_P (name)
   11941      1128793 :                   || (decl != inner
   11942       618573 :                       && !(name == fun_identifier
   11943              :                            /* In case the user names something _FUN  */
   11944          144 :                            && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
   11945              :                 /* And a function template, or conversion operator needs
   11946              :                    the return type.  Except for the _FUN thunk of a
   11947              :                    generic lambda, which has a recursive decl_type'd
   11948              :                    return type.  */
   11949              :                 // FIXME: What if the return type is a voldemort?
   11950       625624 :                 key.ret = fndecl_declared_return_type (inner);
   11951              :             }
   11952              :           break;
   11953              : 
   11954       174992 :         case MK_field:
   11955       174992 :           {
   11956       174992 :             unsigned ix = 0;
   11957       174992 :             if (TREE_CODE (inner) != FIELD_DECL)
   11958              :               name = NULL_TREE;
   11959              :             else
   11960        30105 :               gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
   11961              : 
   11962       174992 :             for (tree field = TYPE_FIELDS (TREE_TYPE (container));
   11963      3506165 :                  ; field = DECL_CHAIN (field))
   11964              :               {
   11965      3681157 :                 tree finner = STRIP_TEMPLATE (field);
   11966      3681157 :                 if (TREE_CODE (finner) == TREE_CODE (inner))
   11967              :                   {
   11968      1266838 :                     if (finner == inner)
   11969              :                       break;
   11970      1091846 :                     ix++;
   11971              :                   }
   11972      3506165 :               }
   11973       174992 :             key.index = ix;
   11974              :           }
   11975       174992 :           break;
   11976              : 
   11977         8446 :         case MK_vtable:
   11978         8446 :           {
   11979         8446 :             tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
   11980        11062 :             for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
   11981        11062 :               if (vtable == decl)
   11982              :                 {
   11983         8446 :                   key.index = ix;
   11984         8446 :                   break;
   11985              :                 }
   11986         8446 :             name = NULL_TREE;
   11987              :           }
   11988         8446 :           break;
   11989              : 
   11990        89949 :         case MK_as_base:
   11991        89949 :           gcc_checking_assert
   11992              :             (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
   11993              :           break;
   11994              : 
   11995        25047 :         case MK_local_friend:
   11996        25047 :           {
   11997              :             /* Find by index on the class's DECL_LIST.  We set TREE_CHAIN to
   11998              :                point to the class in push_template_decl or grokfndecl.  */
   11999        25047 :             unsigned ix = 0;
   12000        25047 :             for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
   12001       698769 :                  decls; decls = TREE_CHAIN (decls))
   12002       698769 :               if (!TREE_PURPOSE (decls))
   12003              :                 {
   12004        95781 :                   tree frnd = friend_from_decl_list (TREE_VALUE (decls));
   12005        95781 :                   if (frnd == decl)
   12006              :                     break;
   12007        70734 :                   ix++;
   12008              :                 }
   12009        25047 :             key.index = ix;
   12010        25047 :             name = NULL_TREE;
   12011              :           }
   12012        25047 :           break;
   12013              : 
   12014        19935 :         case MK_local_type:
   12015        19935 :           key_local_type (key, STRIP_TEMPLATE (decl), container);
   12016        19935 :           break;
   12017              : 
   12018         1177 :         case MK_enum:
   12019         1177 :           {
   12020              :             /* Anonymous enums are located by their first identifier,
   12021              :                and underlying type.  */
   12022         1177 :             tree type = TREE_TYPE (decl);
   12023              : 
   12024         1177 :             gcc_checking_assert (UNSCOPED_ENUM_P (type));
   12025              :             /* Using the type name drops the bit precision we might
   12026              :                have been using on the enum.  */
   12027         1177 :             key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
   12028         1177 :             if (tree values = TYPE_VALUES (type))
   12029         1177 :               name = DECL_NAME (TREE_VALUE (values));
   12030              :           }
   12031              :           break;
   12032              : 
   12033         1216 :         case MK_keyed:
   12034         1216 :           {
   12035         1216 :             tree scope = get_keyed_decl_scope (inner);
   12036         1216 :             gcc_checking_assert (scope);
   12037              : 
   12038         1216 :             auto *root = keyed_table->get (scope);
   12039         1216 :             unsigned ix = root->length ();
   12040              :             /* If we don't find it, we'll write a really big number
   12041              :                that the reader will ignore.  */
   12042         1334 :             while (ix--)
   12043         1334 :               if ((*root)[ix] == inner)
   12044              :                 break;
   12045              : 
   12046              :             /* Use the keyed-to decl as the 'name'.  */
   12047         1216 :             name = scope;
   12048         1216 :             key.index = ix;
   12049              :           }
   12050         1216 :           break;
   12051              : 
   12052        58206 :         case MK_partial:
   12053        58206 :           {
   12054        58206 :             tree ti = get_template_info (inner);
   12055        58206 :             key.constraints = get_constraints (inner);
   12056        58206 :             key.ret = TI_TEMPLATE (ti);
   12057        58206 :             key.args = TI_ARGS (ti);
   12058              :           }
   12059        58206 :           break;
   12060              :         }
   12061              : 
   12062      2410077 :       tree_node (name);
   12063      2410077 :       if (streaming_p ())
   12064              :         {
   12065              :           /* Check we have enough bits for the index.  */
   12066       857798 :           gcc_checking_assert (key.index < (1u << (sizeof (unsigned) * 8 - 6)));
   12067              : 
   12068       857798 :           unsigned code = ((key.ref_q << 0)
   12069       857798 :                            | (key.coro_disc << 2)
   12070       857798 :                            | (key.iobj_p << 4)
   12071       857798 :                            | (key.xobj_p << 5)
   12072       857798 :                            | (key.index << 6));
   12073       857798 :           u (code);
   12074              :         }
   12075              : 
   12076      2410077 :       if (mk == MK_enum)
   12077         1177 :         tree_node (key.ret);
   12078      2408900 :       else if (mk == MK_partial
   12079      2350694 :                || (mk == MK_named && inner
   12080      2031109 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12081              :         {
   12082      1186999 :           tree_node (key.ret);
   12083      1186999 :           tree arg = key.args;
   12084      1186999 :           if (mk == MK_named)
   12085      3292518 :             while (arg && arg != void_list_node)
   12086              :               {
   12087      2163725 :                 tree_node (TREE_VALUE (arg));
   12088      2163725 :                 arg = TREE_CHAIN (arg);
   12089              :               }
   12090      1186999 :           tree_node (arg);
   12091      1186999 :           tree_node (key.constraints);
   12092              :         }
   12093              :     }
   12094      4501499 : }
   12095              : 
   12096              : /* DECL is a new declaration that may be duplicated in OVL.  Use KEY
   12097              :    to find its clone, or NULL.  If DECL's DECL_NAME is NULL, this
   12098              :    has been found by a proxy.  It will be an enum type located by its
   12099              :    first member.
   12100              : 
   12101              :    We're conservative with matches, so ambiguous decls will be
   12102              :    registered as different, then lead to a lookup error if the two
   12103              :    modules are both visible.  Perhaps we want to do something similar
   12104              :    to duplicate decls to get ODR errors on loading?  We already have
   12105              :    some special casing for namespaces.  */
   12106              : 
   12107              : static tree
   12108       305486 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
   12109              : {
   12110       305486 :   tree found = NULL_TREE;
   12111      1458142 :   for (ovl_iterator iter (ovl); !found && iter; ++iter)
   12112              :     {
   12113       698904 :       tree match = *iter;
   12114              : 
   12115       698904 :       tree d_inner = decl;
   12116       698904 :       tree m_inner = match;
   12117              : 
   12118       806871 :     again:
   12119       806871 :       if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
   12120              :         {
   12121       124758 :           if (TREE_CODE (match) == NAMESPACE_DECL
   12122       124758 :               && !DECL_NAMESPACE_ALIAS (match))
   12123              :             /* Namespaces are never overloaded.  */
   12124              :             found = match;
   12125              : 
   12126       124758 :           continue;
   12127              :         }
   12128              : 
   12129       682113 :       switch (TREE_CODE (d_inner))
   12130              :         {
   12131       259686 :         case TEMPLATE_DECL:
   12132       259686 :           if (template_heads_equivalent_p (d_inner, m_inner))
   12133              :             {
   12134       107967 :               d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12135       107967 :               m_inner = DECL_TEMPLATE_RESULT (m_inner);
   12136       107967 :               if (d_inner == error_mark_node
   12137       107967 :                   && TYPE_DECL_ALIAS_P (m_inner))
   12138              :                 {
   12139              :                   found = match;
   12140              :                   break;
   12141              :                 }
   12142       107967 :               goto again;
   12143              :             }
   12144              :           break;
   12145              : 
   12146       321363 :         case FUNCTION_DECL:
   12147       321363 :           if (tree m_type = TREE_TYPE (m_inner))
   12148       321363 :             if ((!key.ret
   12149       159137 :                  || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
   12150       294502 :                 && type_memfn_rqual (m_type) == key.ref_q
   12151       294236 :                 && key.iobj_p == DECL_IOBJ_MEMBER_FUNCTION_P (m_inner)
   12152       588212 :                 && key.xobj_p == DECL_XOBJ_MEMBER_FUNCTION_P (m_inner)
   12153       294215 :                 && compparms (key.args, TYPE_ARG_TYPES (m_type))
   12154       131915 :                 && get_coroutine_discriminator (m_inner) == key.coro_disc
   12155              :                 /* Reject if old is a "C" builtin and new is not "C".
   12156              :                    Matches decls_match behaviour.  */
   12157       131912 :                 && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
   12158         8209 :                     || !DECL_EXTERN_C_P (m_inner)
   12159         7982 :                     || DECL_EXTERN_C_P (d_inner))
   12160              :                 /* Reject if one is a different member of a
   12161              :                    guarded/pre/post fn set.  */
   12162       131887 :                 && (!flag_contracts
   12163       242054 :                     || (DECL_IS_PRE_FN_P (d_inner)
   12164       121027 :                         == DECL_IS_PRE_FN_P (m_inner)))
   12165       453250 :                 && (!flag_contracts
   12166       242054 :                     || (DECL_IS_POST_FN_P (d_inner)
   12167       121027 :                         == DECL_IS_POST_FN_P (m_inner))))
   12168              :               {
   12169       131887 :                 tree m_reqs = get_constraints (m_inner);
   12170       131887 :                 if (m_reqs)
   12171              :                   {
   12172         9735 :                     if (cxx_dialect < cxx20)
   12173            8 :                       m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
   12174              :                     else
   12175        19462 :                       m_reqs = CI_DECLARATOR_REQS (m_reqs);
   12176              :                   }
   12177              : 
   12178       131887 :                 if (cp_tree_equal (key.constraints, m_reqs))
   12179       171839 :                   found = match;
   12180              :               }
   12181              :           break;
   12182              : 
   12183        61037 :         case TYPE_DECL:
   12184       122074 :           if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
   12185        61037 :               == DECL_IMPLICIT_TYPEDEF_P (m_inner))
   12186              :             {
   12187        61001 :               if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
   12188        60872 :                 return match;
   12189          129 :               else if (mk == MK_enum
   12190          129 :                        && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
   12191          129 :                            == key.ret))
   12192              :                 found = match;
   12193              :             }
   12194              :           /* With -freflection, typedef struct { } A is now represented the same
   12195              :              as typedef struct A_ { } A except the TYPE_DECL for A_ is invisible
   12196              :              to name lookup, so we won't be able to find and match it directly.
   12197              :              But we will find the in-TU A (m_inner), through which we can obtain
   12198              :              the in-TU A_ when d_inner is the streamed-in A_.  */
   12199           36 :           else if (flag_reflection
   12200           10 :                    && TYPE_DECL_WAS_UNNAMED (d_inner)
   12201           42 :                    && DECL_ORIGINAL_TYPE (m_inner))
   12202              :             {
   12203            6 :               tree orig = TYPE_NAME (DECL_ORIGINAL_TYPE (m_inner));
   12204            6 :               if (TYPE_DECL_WAS_UNNAMED (orig)
   12205           12 :                   && DECL_NAME (orig) == DECL_NAME (d_inner))
   12206              :                 found = orig;
   12207              :             }
   12208              :           break;
   12209              : 
   12210              :         default:
   12211              :           found = match;
   12212              :           break;
   12213              :         }
   12214              :     }
   12215              : 
   12216       244614 :   return found;
   12217              : }
   12218              : 
   12219              : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl.  Only
   12220              :    the bools have been filled in.  Read its merging key and merge it.
   12221              :    Returns the existing decl if there is one.  */
   12222              : 
   12223              : tree
   12224      1231942 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   12225              :                          tree type, tree container, bool is_attached,
   12226              :                          bool is_imported_temploid_friend)
   12227              : {
   12228      1231942 :   const char *kind = "new";
   12229      1231942 :   tree existing = NULL_TREE;
   12230              : 
   12231      1231942 :   if (mk & MK_template_mask)
   12232              :     {
   12233              :       // FIXME: We could stream the specialization hash?
   12234       381140 :       spec_entry spec;
   12235       381140 :       spec.tmpl = tree_node ();
   12236       381140 :       spec.args = tree_node ();
   12237              : 
   12238       381140 :       if (get_overrun ())
   12239            0 :         return error_mark_node;
   12240              : 
   12241       381140 :       DECL_NAME (decl) = DECL_NAME (spec.tmpl);
   12242       381140 :       DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
   12243       381140 :       DECL_NAME (inner) = DECL_NAME (decl);
   12244       381140 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12245              : 
   12246       381140 :       tree constr = NULL_TREE;
   12247       381140 :       bool is_decl = mk & MK_tmpl_decl_mask;
   12248       381140 :       if (is_decl)
   12249              :         {
   12250       271968 :           if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   12251              :             {
   12252         5351 :               constr = tree_node ();
   12253         5351 :               if (constr)
   12254            0 :                 set_constraints (inner, constr);
   12255              :             }
   12256       541268 :           spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
   12257              :         }
   12258              :       else
   12259       109172 :         spec.spec = type;
   12260       381140 :       existing = match_mergeable_specialization (is_decl, &spec);
   12261       381140 :       if (constr)
   12262              :         /* We'll add these back later, if this is the new decl.  */
   12263            0 :         remove_constraints (inner);
   12264              : 
   12265       381140 :       if (!existing)
   12266              :         ; /* We'll add to the table once read.  */
   12267       152261 :       else if (mk & MK_tmpl_decl_mask)
   12268              :         {
   12269              :           /* A declaration specialization.  */
   12270       105421 :           if (mk & MK_tmpl_tmpl_mask)
   12271         1095 :             existing = DECL_TI_TEMPLATE (existing);
   12272              :         }
   12273              :       else
   12274              :         {
   12275              :           /* A type specialization.  */
   12276        46840 :           if (mk & MK_tmpl_tmpl_mask)
   12277          241 :             existing = CLASSTYPE_TI_TEMPLATE (existing);
   12278              :           else
   12279        46599 :             existing = TYPE_NAME (existing);
   12280              :         }
   12281              :     }
   12282       850802 :   else if (mk == MK_unique)
   12283              :     kind = "unique";
   12284              :   else
   12285              :     {
   12286       632690 :       tree name = tree_node ();
   12287              : 
   12288       632690 :       merge_key key;
   12289       632690 :       unsigned code = u ();
   12290       632690 :       key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
   12291       632690 :       key.coro_disc = (code >> 2) & 3;
   12292       632690 :       key.iobj_p = (code >> 4) & 1;
   12293       632690 :       key.xobj_p = (code >> 5) & 1;
   12294       632690 :       key.index = code >> 6;
   12295              : 
   12296       632690 :       if (mk == MK_enum)
   12297          238 :         key.ret = tree_node ();
   12298       632452 :       else if (mk == MK_partial
   12299       621491 :                || ((mk == MK_named || mk == MK_friend_spec)
   12300       528063 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12301              :         {
   12302       304298 :           key.ret = tree_node ();
   12303       304298 :           tree arg, *arg_ptr = &key.args;
   12304       304298 :           while ((arg = tree_node ())
   12305       860951 :                  && arg != void_list_node
   12306      1431597 :                  && mk != MK_partial)
   12307              :             {
   12308       558169 :               *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
   12309       558169 :               arg_ptr = &TREE_CHAIN (*arg_ptr);
   12310              :             }
   12311       304298 :           *arg_ptr = arg;
   12312       304298 :           key.constraints = tree_node ();
   12313              :         }
   12314              : 
   12315       632690 :       if (get_overrun ())
   12316            0 :         return error_mark_node;
   12317              : 
   12318       632690 :       if (mk < MK_indirect_lwm)
   12319              :         {
   12320       620445 :           DECL_NAME (decl) = name;
   12321       620445 :           DECL_CONTEXT (decl) = FROB_CONTEXT (container);
   12322              :         }
   12323       632690 :       DECL_NAME (inner) = DECL_NAME (decl);
   12324       632690 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12325              : 
   12326       632690 :       if (mk == MK_partial)
   12327              :         {
   12328        10961 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
   12329        56413 :                spec; spec = TREE_CHAIN (spec))
   12330              :             {
   12331        51642 :               tree tmpl = TREE_VALUE (spec);
   12332        51642 :               tree ti = get_template_info (tmpl);
   12333        51642 :               if (template_args_equal (key.args, TI_ARGS (ti))
   12334        58625 :                   && cp_tree_equal (key.constraints,
   12335              :                                     get_constraints
   12336         6983 :                                     (DECL_TEMPLATE_RESULT (tmpl))))
   12337              :                 {
   12338              :                   existing = tmpl;
   12339              :                   break;
   12340              :                 }
   12341              :             }
   12342              :         }
   12343       621729 :       else if (mk == MK_keyed
   12344          399 :                && DECL_LANG_SPECIFIC (name)
   12345       622128 :                && DECL_MODULE_KEYED_DECLS_P (name))
   12346              :         {
   12347          399 :           gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
   12348              :                                || TREE_CODE (container) == TYPE_DECL
   12349              :                                || TREE_CODE (container) == FUNCTION_DECL);
   12350          399 :           if (auto *set = keyed_table->get (name))
   12351       632839 :             if (key.index < set->length ())
   12352              :               {
   12353          149 :                 existing = (*set)[key.index];
   12354          149 :                 if (existing)
   12355              :                   {
   12356          149 :                     gcc_checking_assert
   12357              :                       (DECL_IMPLICIT_TYPEDEF_P (existing));
   12358          149 :                     if (inner != decl)
   12359           91 :                       existing
   12360           91 :                         = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12361              :                   }
   12362              :               }
   12363              :         }
   12364              :       else
   12365       621330 :         switch (TREE_CODE (container))
   12366              :           {
   12367            0 :           default:
   12368            0 :             gcc_unreachable ();
   12369              : 
   12370       151634 :           case NAMESPACE_DECL:
   12371       151634 :             if (is_attached
   12372       151634 :                 && !is_imported_temploid_friend
   12373       151634 :                 && !(state->is_module () || state->is_partition ()))
   12374              :               kind = "unique";
   12375              :             else
   12376              :               {
   12377       149533 :                 gcc_checking_assert (mk == MK_named || mk == MK_enum);
   12378       149533 :                 tree mvec;
   12379       149533 :                 tree *vslot = mergeable_namespace_slots (container, name,
   12380              :                                                          is_attached, &mvec);
   12381       149533 :                 existing = check_mergeable_decl (mk, decl, *vslot, key);
   12382       149533 :                 if (!existing)
   12383        72225 :                   add_mergeable_namespace_entity (vslot, decl);
   12384              :                 else
   12385              :                   {
   12386              :                     /* Note that we now have duplicates to deal with in
   12387              :                        name lookup.  */
   12388        77308 :                     if (is_attached)
   12389           66 :                       BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
   12390              :                     else
   12391        77242 :                       BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
   12392              :                   }
   12393              :               }
   12394       155989 :             break;
   12395              : 
   12396         4355 :           case FUNCTION_DECL:
   12397         4355 :             gcc_checking_assert (mk == MK_local_type);
   12398         4355 :             existing = key_local_type (key, container, name);
   12399         4355 :             if (existing && inner != decl)
   12400         3494 :               existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12401              :             break;
   12402              : 
   12403       465341 :           case TYPE_DECL:
   12404       465341 :             gcc_checking_assert (!is_imported_temploid_friend);
   12405       465341 :             int use_tmpl = 0;
   12406         5985 :             if (is_attached && !(state->is_module () || state->is_partition ())
   12407              :                 /* Implicit or in-class defaulted member functions
   12408              :                    can come from anywhere.  */
   12409         4303 :                 && !(TREE_CODE (decl) == FUNCTION_DECL
   12410         1271 :                      && !DECL_THUNK_P (decl)
   12411         1271 :                      && DECL_DEFAULTED_FN (decl)
   12412          820 :                      && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
   12413              :                 /* As can members of template specialisations.  */
   12414       468824 :                 && !(node_template_info (container, use_tmpl)
   12415         1307 :                      && use_tmpl != 0))
   12416              :               kind = "unique";
   12417              :             else
   12418              :               {
   12419       462106 :                 tree ctx = TREE_TYPE (container);
   12420              : 
   12421              :                 /* For some reason templated enumeral types are not marked
   12422              :                    as COMPLETE_TYPE_P, even though they have members.
   12423              :                    This may well be a bug elsewhere.  */
   12424       462106 :                 if (TREE_CODE (ctx) == ENUMERAL_TYPE)
   12425        15672 :                   existing = find_enum_member (ctx, name);
   12426       446434 :                 else if (COMPLETE_TYPE_P (ctx))
   12427              :                   {
   12428       196031 :                     switch (mk)
   12429              :                       {
   12430            0 :                       default:
   12431            0 :                         gcc_unreachable ();
   12432              : 
   12433       156417 :                       case MK_named:
   12434       156417 :                         existing = lookup_class_binding (ctx, name);
   12435       156417 :                         if (existing)
   12436              :                           {
   12437       155953 :                             tree inner = decl;
   12438       155953 :                             if (TREE_CODE (inner) == TEMPLATE_DECL
   12439       155953 :                                 && !DECL_MEMBER_TEMPLATE_P (inner))
   12440        69158 :                               inner = DECL_TEMPLATE_RESULT (inner);
   12441              : 
   12442       155953 :                             existing = check_mergeable_decl
   12443       155953 :                               (mk, inner, existing, key);
   12444              : 
   12445       155953 :                             if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
   12446              :                               {} // FIXME: Insert into specialization
   12447              :                             // tables, we'll need the arguments for that!
   12448              :                           }
   12449              :                         break;
   12450              : 
   12451        27008 :                       case MK_field:
   12452        27008 :                         {
   12453        27008 :                           unsigned ix = key.index;
   12454        27008 :                           for (tree field = TYPE_FIELDS (ctx);
   12455       893378 :                                field; field = DECL_CHAIN (field))
   12456              :                             {
   12457       893378 :                               tree finner = STRIP_TEMPLATE (field);
   12458       893378 :                               if (TREE_CODE (finner) == TREE_CODE (inner))
   12459       299882 :                                 if (!ix--)
   12460              :                                   {
   12461              :                                     existing = field;
   12462              :                                     break;
   12463              :                                   }
   12464              :                             }
   12465              :                         }
   12466              :                         break;
   12467              : 
   12468         1484 :                       case MK_vtable:
   12469         1484 :                         {
   12470         1484 :                           unsigned ix = key.index;
   12471         1484 :                           for (tree vtable = CLASSTYPE_VTABLES (ctx);
   12472         1823 :                                vtable; vtable = DECL_CHAIN (vtable))
   12473         1570 :                             if (!ix--)
   12474              :                               {
   12475              :                                 existing = vtable;
   12476              :                                 break;
   12477              :                               }
   12478              :                         }
   12479              :                         break;
   12480              : 
   12481         8075 :                       case MK_as_base:
   12482         8075 :                         {
   12483         8075 :                           tree as_base = CLASSTYPE_AS_BASE (ctx);
   12484         8075 :                           if (as_base && as_base != ctx)
   12485         8075 :                             existing = TYPE_NAME (as_base);
   12486              :                         }
   12487              :                         break;
   12488              : 
   12489         3047 :                       case MK_local_friend:
   12490         3047 :                         {
   12491         3047 :                           unsigned ix = key.index;
   12492         3047 :                           for (tree decls = CLASSTYPE_DECL_LIST (ctx);
   12493        85483 :                                decls; decls = TREE_CHAIN (decls))
   12494        85483 :                             if (!TREE_PURPOSE (decls) && !ix--)
   12495              :                               {
   12496         3047 :                                 existing
   12497         3047 :                                   = friend_from_decl_list (TREE_VALUE (decls));
   12498         3047 :                                 break;
   12499              :                               }
   12500              :                         }
   12501              :                         break;
   12502              :                       }
   12503              : 
   12504       196031 :                     if (existing && mk < MK_indirect_lwm && mk != MK_partial
   12505       191723 :                         && TREE_CODE (decl) == TEMPLATE_DECL
   12506       284291 :                         && !DECL_MEMBER_TEMPLATE_P (decl))
   12507              :                       {
   12508        71198 :                         tree ti;
   12509        71198 :                         if (DECL_IMPLICIT_TYPEDEF_P (existing))
   12510          920 :                           ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
   12511              :                         else
   12512        70278 :                           ti = DECL_TEMPLATE_INFO (existing);
   12513        71198 :                         existing = TI_TEMPLATE (ti);
   12514              :                       }
   12515              :                   }
   12516              :               }
   12517              :           }
   12518              :     }
   12519              : 
   12520      1231942 :   dump (dumper::MERGE)
   12521         3208 :     && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   12522         3208 :              existing ? "matched" : kind, TREE_CODE (decl), decl);
   12523              : 
   12524              :   return existing;
   12525              : }
   12526              : 
   12527              : void
   12528       351104 : trees_out::binfo_mergeable (tree binfo)
   12529              : {
   12530       351104 :   tree dom = binfo;
   12531       444146 :   while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
   12532              :     dom = parent;
   12533       351104 :   tree type = BINFO_TYPE (dom);
   12534       351104 :   gcc_checking_assert (TYPE_BINFO (type) == dom);
   12535       351104 :   tree_node (type);
   12536       351104 :   if (streaming_p ())
   12537              :     {
   12538              :       unsigned ix = 0;
   12539       187743 :       for (; dom != binfo; dom = TREE_CHAIN (dom))
   12540        48941 :         ix++;
   12541       138802 :       u (ix);
   12542              :     }
   12543       351104 : }
   12544              : 
   12545              : unsigned
   12546        93856 : trees_in::binfo_mergeable (tree *type)
   12547              : {
   12548        93856 :   *type = tree_node ();
   12549        93856 :   return u ();
   12550              : }
   12551              : 
   12552              : /* DECL is a just streamed declaration with attributes DATTR that should
   12553              :    have matching ABI tags as EXISTING's attributes EATTR.  Check that the
   12554              :    ABI tags match, and report an error if not.  */
   12555              : 
   12556              : void
   12557       299792 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
   12558              : {
   12559       299792 :   tree etags = lookup_attribute ("abi_tag", eattr);
   12560       299792 :   tree dtags = lookup_attribute ("abi_tag", dattr);
   12561       299792 :   if ((etags == nullptr) != (dtags == nullptr)
   12562       299792 :       || (etags && !attribute_value_equal (etags, dtags)))
   12563              :     {
   12564           33 :       if (etags)
   12565           24 :         etags = TREE_VALUE (etags);
   12566           33 :       if (dtags)
   12567           24 :         dtags = TREE_VALUE (dtags);
   12568              : 
   12569              :       /* We only error if mangling wouldn't consider the tags equivalent.
   12570              :          Since tags might have been inherited during mangling, ignore
   12571              :          inherited tags if there's a mangled-ness mismatch.  */
   12572           33 :       bool ignore_inherited_p
   12573           33 :         = (DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (existing))
   12574           33 :            != DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (decl)));
   12575           33 :       if (!equal_abi_tags (etags, dtags, ignore_inherited_p))
   12576              :         {
   12577           21 :           auto_diagnostic_group d;
   12578           21 :           if (dtags)
   12579           15 :             error_at (DECL_SOURCE_LOCATION (decl),
   12580              :                       "mismatching abi tags for %qD with tags %qE",
   12581              :                       decl, dtags);
   12582              :           else
   12583            6 :             error_at (DECL_SOURCE_LOCATION (decl),
   12584              :                       "mismatching abi tags for %qD with no tags", decl);
   12585           21 :           if (etags)
   12586           15 :             inform (DECL_SOURCE_LOCATION (existing),
   12587              :                     "existing declaration here with tags %qE", etags);
   12588              :           else
   12589            6 :             inform (DECL_SOURCE_LOCATION (existing),
   12590              :                     "existing declaration here with no tags");
   12591           21 :         }
   12592              : 
   12593              :       /* Always use the existing abi_tags as the canonical set so that
   12594              :          later processing doesn't get confused.  */
   12595           33 :       if (dtags)
   12596           24 :         dattr = remove_attribute ("abi_tag", dattr);
   12597           33 :       if (etags)
   12598           24 :         duplicate_one_attribute (&dattr, eattr, "abi_tag");
   12599              :     }
   12600       299792 : }
   12601              : 
   12602              : /* DECL is a just streamed mergeable decl that should match EXISTING.  Check
   12603              :    it does and issue an appropriate diagnostic if not.  Merge any
   12604              :    bits from DECL to EXISTING.  This is stricter matching than
   12605              :    decls_match, because we can rely on ODR-sameness, and we cannot use
   12606              :    decls_match because it can cause instantiations of constraints.  */
   12607              : 
   12608              : bool
   12609       439592 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
   12610              : {
   12611              :   // FIXME: We should probably do some duplicate decl-like stuff here
   12612              :   // (beware, default parms should be the same?)  Can we just call
   12613              :   // duplicate_decls and teach it how to handle the module-specific
   12614              :   // permitted/required duplications?
   12615              : 
   12616              :   // We know at this point that the decls have matched by key, so we
   12617              :   // can elide some of the checking
   12618       439592 :   gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
   12619              : 
   12620       439592 :   tree d_inner = decl;
   12621       439592 :   tree e_inner = existing;
   12622       439592 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12623              :     {
   12624       146294 :       d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12625       146294 :       e_inner = DECL_TEMPLATE_RESULT (e_inner);
   12626       146294 :       gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
   12627              :     }
   12628              : 
   12629              :   // FIXME: do more precise errors at point of mismatch
   12630       439592 :   const char *mismatch_msg = nullptr;
   12631              : 
   12632       439592 :   if (VAR_OR_FUNCTION_DECL_P (d_inner)
   12633       439592 :       && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
   12634              :     {
   12635            6 :       mismatch_msg = G_("conflicting language linkage for imported "
   12636              :                         "declaration %#qD");
   12637            6 :       goto mismatch;
   12638              :     }
   12639       439586 :   else if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12640              :     {
   12641       201056 :       tree e_ret = fndecl_declared_return_type (existing);
   12642       201056 :       tree d_ret = fndecl_declared_return_type (decl);
   12643              : 
   12644        85874 :       if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
   12645       201080 :           && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
   12646              :         /* This has a recursive type that will compare different.  */;
   12647       201044 :       else if (!same_type_p (d_ret, e_ret))
   12648              :         {
   12649           25 :           mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12650           25 :           goto mismatch;
   12651              :         }
   12652              : 
   12653       201031 :       tree& e_type = TREE_TYPE (e_inner);
   12654       201031 :       tree d_type = TREE_TYPE (d_inner);
   12655              : 
   12656       201031 :       for (tree e_args = TYPE_ARG_TYPES (e_type),
   12657       201031 :              d_args = TYPE_ARG_TYPES (d_type);
   12658       345228 :            e_args != d_args && (e_args || d_args);
   12659       144197 :            e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
   12660              :         {
   12661       144197 :           if (!(e_args && d_args))
   12662              :             {
   12663            0 :               mismatch_msg = G_("conflicting argument list for imported "
   12664              :                                 "declaration %#qD");
   12665            0 :               goto mismatch;
   12666              :             }
   12667              : 
   12668       144197 :           if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
   12669              :             {
   12670            0 :               mismatch_msg = G_("conflicting argument types for imported "
   12671              :                                 "declaration %#qD");
   12672            0 :               goto mismatch;
   12673              :             }
   12674              :         }
   12675              : 
   12676              :       /* If EXISTING has an undeduced or uninstantiated exception
   12677              :          specification, but DECL does not, propagate the exception
   12678              :          specification.  Otherwise we end up asserting or trying to
   12679              :          instantiate it in the middle of loading.   */
   12680       201031 :       tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
   12681       201031 :       tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
   12682       294226 :       if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
   12683              :         {
   12684        31792 :           if (!(DECL_MAYBE_DELETED (d_inner)
   12685        15838 :                 || DEFERRED_NOEXCEPT_SPEC_P (d_spec))
   12686        47383 :               || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
   12687        25898 :                   && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
   12688              :             {
   12689          155 :               dump (dumper::MERGE)
   12690            6 :                 && dump ("Propagating instantiated noexcept to %N", existing);
   12691          155 :               gcc_checking_assert (existing == e_inner);
   12692          155 :               e_type = build_exception_variant (e_type, d_spec);
   12693              : 
   12694              :               /* Propagate to existing clones.  */
   12695          155 :               tree clone;
   12696          441 :               FOR_EACH_CLONE (clone, existing)
   12697          286 :                 TREE_TYPE (clone)
   12698          572 :                   = build_exception_variant (TREE_TYPE (clone), d_spec);
   12699              :             }
   12700              :         }
   12701       185077 :       else if (!DECL_MAYBE_DELETED (d_inner)
   12702       264099 :                && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   12703       370153 :                && !comp_except_specs (d_spec, e_spec, ce_type))
   12704              :         {
   12705         1736 :           mismatch_msg = G_("conflicting %<noexcept%> specifier for "
   12706              :                             "imported declaration %#qD");
   12707         1736 :           goto mismatch;
   12708              :         }
   12709              : 
   12710              :       /* Similarly if EXISTING has an undeduced return type, but DECL's
   12711              :          is already deduced.  */
   12712       199295 :       bool e_undeduced = undeduced_auto_decl (existing);
   12713       199295 :       bool d_undeduced = undeduced_auto_decl (decl);
   12714       199295 :       if (e_undeduced && !d_undeduced)
   12715              :         {
   12716           16 :           dump (dumper::MERGE)
   12717            0 :             && dump ("Propagating deduced return type to %N", existing);
   12718           16 :           gcc_checking_assert (existing == e_inner);
   12719           16 :           FNDECL_USED_AUTO (existing) = true;
   12720           16 :           DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
   12721           16 :           e_type = change_return_type (TREE_TYPE (d_type), e_type);
   12722              :         }
   12723       199279 :       else if (d_undeduced && !e_undeduced)
   12724              :         /* EXISTING was deduced, leave it alone.  */;
   12725       199276 :       else if (type_uses_auto (d_ret)
   12726       199276 :                && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
   12727              :         {
   12728            9 :           mismatch_msg = G_("conflicting deduced return type for "
   12729              :                             "imported declaration %#qD");
   12730            9 :           goto mismatch;
   12731              :         }
   12732              : 
   12733              :       /* Similarly if EXISTING has undeduced constexpr, but DECL's
   12734              :          is already deduced.  */
   12735       199286 :       if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12736       199286 :           == DECL_DECLARED_CONSTEXPR_P (d_inner))
   12737              :         /* Already matches.  */;
   12738            2 :       else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
   12739            2 :                && (DECL_MAYBE_DELETED (e_inner)
   12740            0 :                    || decl_implicit_constexpr_p (d_inner)))
   12741              :         /* DECL was deduced, copy to EXISTING.  */
   12742              :         {
   12743            1 :           DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
   12744            1 :           if (decl_implicit_constexpr_p (d_inner))
   12745            0 :             DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
   12746              :         }
   12747            1 :       else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12748            1 :                && (DECL_MAYBE_DELETED (d_inner)
   12749            0 :                    || decl_implicit_constexpr_p (e_inner)))
   12750              :         /* EXISTING was deduced, leave it alone.  */;
   12751              :       else
   12752              :         {
   12753            0 :           mismatch_msg = G_("conflicting %<constexpr%> for imported "
   12754              :                             "declaration %#qD");
   12755            0 :           goto mismatch;
   12756              :         }
   12757              : 
   12758              :       /* Don't synthesize a defaulted function if we're importing one
   12759              :          we've already determined.  */
   12760       199286 :       if (!DECL_MAYBE_DELETED (d_inner))
   12761       199169 :         DECL_MAYBE_DELETED (e_inner) = false;
   12762              :     }
   12763       238530 :   else if (is_typedef)
   12764              :     {
   12765        84851 :       if (!DECL_ORIGINAL_TYPE (e_inner)
   12766        84851 :           || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
   12767              :                            DECL_ORIGINAL_TYPE (e_inner)))
   12768              :         {
   12769            3 :           mismatch_msg = G_("conflicting imported declaration %q#D");
   12770            3 :           goto mismatch;
   12771              :         }
   12772              :     }
   12773              :   /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
   12774              :      here. I suspect the entities that directly do that are things
   12775              :      that shouldn't go to duplicate_decls (FIELD_DECLs etc).   */
   12776       153679 :   else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
   12777              :     {
   12778              :       mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12779         2525 :     mismatch:
   12780         2525 :       if (DECL_IS_UNDECLARED_BUILTIN (existing))
   12781              :         /* Just like duplicate_decls, presum the user knows what
   12782              :            they're doing in overriding a builtin.   */
   12783         1758 :         TREE_TYPE (existing) = TREE_TYPE (decl);
   12784          767 :       else if (decl_function_context (decl))
   12785              :         /* The type of a mergeable local entity (such as a function scope
   12786              :            capturing lambda's closure type fields) can depend on an
   12787              :            unmergeable local entity (such as a local variable), so type
   12788              :            equality isn't feasible in general for local entities.  */;
   12789              :       else
   12790              :         {
   12791           21 :           gcc_checking_assert (mismatch_msg);
   12792           21 :           auto_diagnostic_group d;
   12793           21 :           error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
   12794           21 :           inform (DECL_SOURCE_LOCATION (existing),
   12795              :                   "existing declaration %#qD", existing);
   12796           21 :           return false;
   12797           21 :         }
   12798              :     }
   12799              : 
   12800       439571 :   if (DECL_IS_UNDECLARED_BUILTIN (existing)
   12801       439571 :       && !DECL_IS_UNDECLARED_BUILTIN (decl))
   12802              :     {
   12803              :       /* We're matching a builtin that the user has yet to declare.
   12804              :          We are the one!  This is very much duplicate-decl
   12805              :          shenanigans. */
   12806         2041 :       DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
   12807         2041 :       if (TREE_CODE (decl) != TYPE_DECL)
   12808              :         {
   12809              :           /* Propagate exceptions etc.  */
   12810         2018 :           TREE_TYPE (existing) = TREE_TYPE (decl);
   12811         2018 :           TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
   12812              :         }
   12813              :       /* This is actually an import! */
   12814         2041 :       DECL_MODULE_IMPORT_P (existing) = true;
   12815              : 
   12816              :       /* Yay, sliced!  */
   12817         2041 :       existing->base = decl->base;
   12818              : 
   12819         2041 :       if (TREE_CODE (decl) == FUNCTION_DECL)
   12820              :         {
   12821              :           /* Ew :(  */
   12822         2018 :           memcpy (&existing->decl_common.size,
   12823              :                   &decl->decl_common.size,
   12824              :                   (offsetof (tree_decl_common, pt_uid)
   12825              :                    - offsetof (tree_decl_common, size)));
   12826         2018 :           auto bltin_class = DECL_BUILT_IN_CLASS (decl);
   12827         2018 :           existing->function_decl.built_in_class = bltin_class;
   12828         2018 :           auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
   12829         2018 :           DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
   12830         2018 :           if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
   12831              :             {
   12832         1808 :               if (builtin_decl_explicit_p (built_in_function (fncode)))
   12833         1808 :                 switch (fncode)
   12834              :                   {
   12835            0 :                   case BUILT_IN_STPCPY:
   12836            0 :                     set_builtin_decl_implicit_p
   12837            0 :                       (built_in_function (fncode), true);
   12838            0 :                     break;
   12839         1808 :                   default:
   12840         1808 :                     set_builtin_decl_declared_p
   12841         1808 :                       (built_in_function (fncode), true);
   12842         1808 :                     break;
   12843              :                   }
   12844         1808 :               copy_attributes_to_builtin (decl);
   12845              :             }
   12846              :         }
   12847              :     }
   12848              : 
   12849       439571 :   if (VAR_OR_FUNCTION_DECL_P (decl)
   12850       439571 :       && DECL_TEMPLATE_INSTANTIATED (decl))
   12851              :     /* Don't instantiate again!  */
   12852         9224 :     DECL_TEMPLATE_INSTANTIATED (existing) = true;
   12853              : 
   12854       439571 :   if (TREE_CODE (d_inner) == FUNCTION_DECL
   12855       439571 :       && DECL_DECLARED_INLINE_P (d_inner))
   12856              :     {
   12857       162788 :       DECL_DECLARED_INLINE_P (e_inner) = true;
   12858       162788 :       if (!DECL_SAVED_TREE (e_inner)
   12859        80790 :           && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
   12860       162805 :           && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
   12861              :         {
   12862           51 :           DECL_INTERFACE_KNOWN (e_inner)
   12863           17 :             |= DECL_INTERFACE_KNOWN (d_inner);
   12864           17 :           DECL_DISREGARD_INLINE_LIMITS (e_inner)
   12865           17 :             |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
   12866              :           // TODO: we will eventually want to merge all decl attributes
   12867           17 :           duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
   12868           17 :                                    DECL_ATTRIBUTES (d_inner), "gnu_inline");
   12869              :         }
   12870              :     }
   12871       439571 :   if (!DECL_EXTERNAL (d_inner))
   12872       206414 :     DECL_EXTERNAL (e_inner) = false;
   12873              : 
   12874       439571 :   if (VAR_OR_FUNCTION_DECL_P (d_inner))
   12875       443154 :     check_abi_tags (existing, decl,
   12876       221577 :                     DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
   12877              : 
   12878       439571 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12879              :     {
   12880              :       /* Merge default template arguments.  */
   12881       146292 :       tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
   12882       146292 :       tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
   12883       146292 :       gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
   12884              :                            == TREE_VEC_LENGTH (e_parms));
   12885       418916 :       for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
   12886              :         {
   12887       272633 :           tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
   12888       272633 :           tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
   12889       272633 :           if (e_default == NULL_TREE)
   12890       230803 :             e_default = d_default;
   12891        41830 :           else if (d_default != NULL_TREE
   12892        41830 :                    && !cp_tree_equal (d_default, e_default))
   12893              :             {
   12894            9 :               auto_diagnostic_group d;
   12895            9 :               tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
   12896            9 :               tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
   12897            9 :               error_at (DECL_SOURCE_LOCATION (d_parm),
   12898              :                         "conflicting default argument for %#qD", d_parm);
   12899            9 :               inform (DECL_SOURCE_LOCATION (e_parm),
   12900              :                       "existing default declared here");
   12901            9 :               return false;
   12902            9 :             }
   12903              :         }
   12904              :     }
   12905              : 
   12906       439562 :   if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12907              :     {
   12908              :       /* Merge default function arguments.  */
   12909       201047 :       tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
   12910       201047 :       tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
   12911       201047 :       int i = 0;
   12912       479281 :       for (; d_parm && d_parm != void_list_node;
   12913       278234 :            d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
   12914              :         {
   12915       278246 :           tree d_default = TREE_PURPOSE (d_parm);
   12916       278246 :           tree& e_default = TREE_PURPOSE (e_parm);
   12917       278246 :           if (e_default == NULL_TREE)
   12918       261677 :             e_default = d_default;
   12919        16569 :           else if (d_default != NULL_TREE
   12920        16569 :                    && !cp_tree_equal (d_default, e_default))
   12921              :             {
   12922           12 :               auto_diagnostic_group d;
   12923           12 :               error_at (get_fndecl_argument_location (d_inner, i),
   12924              :                         "conflicting default argument for parameter %P of %#qD",
   12925              :                         i, decl);
   12926           12 :               inform (get_fndecl_argument_location (e_inner, i),
   12927              :                       "existing default declared here");
   12928           12 :               return false;
   12929           12 :             }
   12930              :         }
   12931              :     }
   12932              : 
   12933              :   return true;
   12934              : }
   12935              : 
   12936              : /* FN is an implicit member function that we've discovered is new to
   12937              :    the class.  Add it to the TYPE_FIELDS chain and the method vector.
   12938              :    Reset the appropriate classtype lazy flag.   */
   12939              : 
   12940              : bool
   12941         1008 : trees_in::install_implicit_member (tree fn)
   12942              : {
   12943         1008 :   tree ctx = DECL_CONTEXT (fn);
   12944         1008 :   tree name = DECL_NAME (fn);
   12945              :   /* We know these are synthesized, so the set of expected prototypes
   12946              :      is quite restricted.  We're not validating correctness, just
   12947              :      distinguishing between the small set of possibilities.  */
   12948         1008 :   tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
   12949         1008 :   if (IDENTIFIER_CTOR_P (name))
   12950              :     {
   12951          675 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
   12952          675 :           && VOID_TYPE_P (parm_type))
   12953          167 :         CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
   12954          508 :       else if (!TYPE_REF_P (parm_type))
   12955              :         return false;
   12956          508 :       else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
   12957          508 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12958          257 :         CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
   12959          251 :       else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
   12960          251 :         CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
   12961              :       else
   12962              :         return false;
   12963              :     }
   12964          333 :   else if (IDENTIFIER_DTOR_P (name))
   12965              :     {
   12966          261 :       if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
   12967          261 :         CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
   12968              :       else
   12969              :         return false;
   12970          261 :       if (DECL_VIRTUAL_P (fn))
   12971              :         /* A virtual dtor should have been created when the class
   12972              :            became complete.  */
   12973              :         return false;
   12974              :     }
   12975           72 :   else if (name == assign_op_identifier)
   12976              :     {
   12977           72 :       if (!TYPE_REF_P (parm_type))
   12978              :         return false;
   12979           72 :       else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
   12980           72 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12981           36 :         CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
   12982           36 :       else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
   12983           36 :         CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
   12984              :       else
   12985              :         return false;
   12986              :     }
   12987              :   else
   12988              :     return false;
   12989              : 
   12990         1053 :   dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
   12991              : 
   12992         1008 :   DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
   12993         1008 :   TYPE_FIELDS (ctx) = fn;
   12994              : 
   12995         1008 :   add_method (ctx, fn, false);
   12996              : 
   12997              :     /* Propagate TYPE_FIELDS.  */
   12998         1008 :   fixup_type_variants (ctx);
   12999              : 
   13000         1008 :   return true;
   13001              : }
   13002              : 
   13003              : /* Return non-zero if DECL has a definition that would be interesting to
   13004              :    write out.  */
   13005              : 
   13006              : static bool
   13007      2082712 : has_definition (tree decl)
   13008              : {
   13009      2082718 :   bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
   13010      2082718 :   if (is_tmpl)
   13011       458626 :     decl = DECL_TEMPLATE_RESULT (decl);
   13012              : 
   13013      2082718 :   switch (TREE_CODE (decl))
   13014              :     {
   13015              :     default:
   13016              :       break;
   13017              : 
   13018       781204 :     case FUNCTION_DECL:
   13019       781204 :       if (!DECL_SAVED_TREE (decl))
   13020              :         /* Not defined.  */
   13021              :         break;
   13022              : 
   13023       352443 :       if (DECL_DECLARED_INLINE_P (decl))
   13024              :         return true;
   13025              : 
   13026        28881 :       if (header_module_p ())
   13027              :         /* We always need to write definitions in header modules,
   13028              :            since there's no TU to emit them in otherwise.  */
   13029              :         return true;
   13030              : 
   13031        17128 :       if (DECL_TEMPLATE_INFO (decl))
   13032              :         {
   13033        15837 :           int use_tpl = DECL_USE_TEMPLATE (decl);
   13034              : 
   13035              :           // FIXME: Partial specializations have definitions too.
   13036        15837 :           if (use_tpl < 2)
   13037              :             return true;
   13038              :         }
   13039              : 
   13040              :       /* Coroutine transform functions always need to be emitted
   13041              :          into the importing TU if the ramp function will be.  */
   13042         1351 :       if (DECL_COROUTINE_P (decl))
   13043           12 :         if (tree ramp = DECL_RAMP_FN (decl))
   13044              :           return has_definition (ramp);
   13045              :       break;
   13046              : 
   13047       868250 :     case TYPE_DECL:
   13048       868250 :       {
   13049       868250 :         tree type = TREE_TYPE (decl);
   13050       868250 :         if (type == TYPE_MAIN_VARIANT (type)
   13051       399380 :             && decl == TYPE_NAME (type)
   13052      1267630 :             && (TREE_CODE (type) == ENUMERAL_TYPE
   13053       399380 :                 ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
   13054              :           return true;
   13055              :       }
   13056              :       break;
   13057              : 
   13058       110369 :     case VAR_DECL:
   13059              :       /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL.  */
   13060       110369 :       if (DECL_LANG_SPECIFIC (decl)
   13061       109523 :           && DECL_TEMPLATE_INFO (decl)
   13062       180264 :           && DECL_INITIAL (decl))
   13063              :         return true;
   13064              :       else
   13065              :         {
   13066        45616 :           if (!DECL_INITIALIZED_P (decl))
   13067              :             /* Not defined.  */
   13068              :             return false;
   13069              : 
   13070        38215 :           if (header_module_p ())
   13071              :             /* We always need to write definitions in header modules,
   13072              :                since there's no TU to emit them in otherwise.  */
   13073              :             return true;
   13074              : 
   13075        12739 :           if (decl_maybe_constant_var_p (decl))
   13076              :             /* We might need its constant value.  */
   13077              :             return true;
   13078              : 
   13079          488 :           if (vague_linkage_p (decl))
   13080              :             /* These are emitted as needed.  */
   13081              :             return true;
   13082              : 
   13083              :           return false;
   13084              :         }
   13085         7940 :       break;
   13086              : 
   13087         7940 :     case CONCEPT_DECL:
   13088         7940 :       if (DECL_INITIAL (decl))
   13089              :         return true;
   13090              : 
   13091              :       break;
   13092              :     }
   13093              : 
   13094              :   return false;
   13095              : }
   13096              : 
   13097              : uintptr_t *
   13098       651069 : trees_in::find_duplicate (tree existing)
   13099              : {
   13100       303201 :   if (!duplicates)
   13101              :     return NULL;
   13102              : 
   13103       451147 :   return duplicates->get (existing);
   13104              : }
   13105              : 
   13106              : /* We're starting to read a duplicate DECL.  EXISTING is the already
   13107              :    known node.  */
   13108              : 
   13109              : void
   13110       627520 : trees_in::register_duplicate (tree decl, tree existing)
   13111              : {
   13112       627520 :   if (!duplicates)
   13113       103836 :     duplicates = new duplicate_hash_map (40);
   13114              : 
   13115       627520 :   bool existed;
   13116       627520 :   uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
   13117       627520 :   gcc_checking_assert (!existed);
   13118       627520 :   slot = reinterpret_cast<uintptr_t> (decl);
   13119              : 
   13120       627520 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   13121              :     /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
   13122              :        that passing decl's _RESULT to maybe_duplicate naturally
   13123              :        gives us existing's _RESULT back.  */
   13124       292588 :     register_duplicate (DECL_TEMPLATE_RESULT (decl),
   13125       146294 :                         DECL_TEMPLATE_RESULT (existing));
   13126       627520 : }
   13127              : 
   13128              : /* We've read a definition of MAYBE_EXISTING.  If not a duplicate,
   13129              :    return MAYBE_EXISTING (into which the definition should be
   13130              :    installed).  Otherwise return NULL if already known bad, or the
   13131              :    duplicate we read (for ODR checking, or extracting additional merge
   13132              :    information).  */
   13133              : 
   13134              : tree
   13135       347868 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
   13136              : {
   13137       347868 :   tree res = NULL_TREE;
   13138              : 
   13139       559788 :   if (uintptr_t *dup = find_duplicate (maybe_existing))
   13140              :     {
   13141       148195 :       if (!(*dup & 1))
   13142       148192 :         res = reinterpret_cast<tree> (*dup);
   13143              :     }
   13144              :   else
   13145              :     res = maybe_existing;
   13146              : 
   13147       347868 :   assert_definition (maybe_existing, res && !has_defn);
   13148              : 
   13149              :   // FIXME: We probably need to return the template, so that the
   13150              :   // template header can be checked?
   13151       347868 :   return res ? STRIP_TEMPLATE (res) : NULL_TREE;
   13152              : }
   13153              : 
   13154              : /* The following writer functions rely on the current behaviour of
   13155              :    depset::hash::add_dependency making the decl and defn depset nodes
   13156              :    depend on each other.  That way we don't have to worry about seeding
   13157              :    the tree map with named decls that cannot be looked up by name (I.e
   13158              :    template and function parms).  We know the decl and definition will
   13159              :    be in the same cluster, which is what we want.  */
   13160              : 
   13161              : void
   13162       579247 : trees_out::write_function_def (tree decl)
   13163              : {
   13164       579247 :   tree_node (DECL_RESULT (decl));
   13165              : 
   13166       579247 :   {
   13167              :     /* The function body for a non-inline function or function template
   13168              :        is ignored for determining exposures.  This should only matter
   13169              :        for templates (we don't emit the bodies of non-inline functions
   13170              :        to begin with).  */
   13171       579247 :     auto ovr = dep_hash->ignore_exposure_if (!DECL_DECLARED_INLINE_P (decl));
   13172       579247 :     tree_node (DECL_INITIAL (decl));
   13173       579247 :     tree_node (DECL_SAVED_TREE (decl));
   13174       579247 :   }
   13175              : 
   13176      1236378 :   tree_node (DECL_FRIEND_CONTEXT (decl));
   13177              : 
   13178       579247 :   constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
   13179              : 
   13180       579247 :   if (streaming_p ())
   13181       289582 :     u (cexpr != nullptr);
   13182       579247 :   if (cexpr)
   13183              :     {
   13184       133199 :       chained_decls (cexpr->parms);
   13185       133199 :       tree_node (cexpr->result);
   13186       133199 :       tree_node (cexpr->body);
   13187              :     }
   13188              : 
   13189       579247 :   function* f = DECL_STRUCT_FUNCTION (decl);
   13190              : 
   13191       579247 :   if (streaming_p ())
   13192              :     {
   13193       289582 :       unsigned flags = 0;
   13194              : 
   13195              :       /* Whether the importer should emit this definition, if used.  */
   13196       289582 :       flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
   13197       289582 :                     && (get_importer_interface (decl)
   13198              :                         != importer_interface::external));
   13199              : 
   13200              :       /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
   13201              :          on non-templates or we'll crash later in import_export_decl.  */
   13202       193467 :       gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
   13203              :                            || (DECL_LANG_SPECIFIC (decl)
   13204              :                                && DECL_LOCAL_DECL_P (decl)
   13205              :                                && DECL_OMP_DECLARE_REDUCTION_P (decl))
   13206              :                            || (DECL_LANG_SPECIFIC (decl)
   13207              :                                && DECL_TEMPLATE_INFO (decl)
   13208              :                                && uses_template_parms (DECL_TI_ARGS (decl))));
   13209              : 
   13210       289582 :       if (f)
   13211              :         {
   13212       288479 :           flags |= 2;
   13213              :           /* These flags are needed in tsubst_lambda_expr.  */
   13214       288479 :           flags |= 4 * f->language->returns_value;
   13215       288479 :           flags |= 8 * f->language->returns_null;
   13216       288479 :           flags |= 16 * f->language->returns_abnormally;
   13217       288479 :           flags |= 32 * f->language->infinite_loop;
   13218              :         }
   13219              : 
   13220       289582 :       u (flags);
   13221              :     }
   13222              : 
   13223       579247 :   if (state && f)
   13224              :     {
   13225       577041 :       state->write_location (*this, f->function_start_locus);
   13226       577041 :       state->write_location (*this, f->function_end_locus);
   13227              :     }
   13228              : 
   13229       579247 :   if (DECL_COROUTINE_P (decl))
   13230              :     {
   13231           40 :       tree ramp = DECL_RAMP_FN (decl);
   13232           40 :       tree_node (ramp);
   13233           40 :       if (!ramp)
   13234              :         {
   13235           28 :           tree_node (DECL_ACTOR_FN (decl));
   13236           28 :           tree_node (DECL_DESTROY_FN (decl));
   13237              :         }
   13238              :     }
   13239       579247 : }
   13240              : 
   13241              : void
   13242            0 : trees_out::mark_function_def (tree)
   13243              : {
   13244            0 : }
   13245              : 
   13246              : bool
   13247       230754 : trees_in::read_function_def (tree decl, tree maybe_template)
   13248              : {
   13249       231304 :   dump () && dump ("Reading function definition %N", decl);
   13250       230754 :   tree result = tree_node ();
   13251       230754 :   tree initial = tree_node ();
   13252       230754 :   tree saved = tree_node ();
   13253       230754 :   tree context = tree_node ();
   13254       230754 :   post_process_data pdata {};
   13255       230754 :   pdata.decl = maybe_template;
   13256              : 
   13257       230754 :   tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
   13258       461505 :   bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
   13259              : 
   13260       230754 :   constexpr_fundef cexpr;
   13261       230754 :   if (u ())
   13262              :     {
   13263        52623 :       cexpr.parms = chained_decls ();
   13264        52623 :       cexpr.result = tree_node ();
   13265        52623 :       cexpr.body = tree_node ();
   13266        52623 :       cexpr.decl = decl;
   13267              :     }
   13268              :   else
   13269       178131 :     cexpr.decl = NULL_TREE;
   13270              : 
   13271       230754 :   unsigned flags = u ();
   13272       230754 :   if (flags & 2)
   13273              :     {
   13274       229871 :       pdata.start_locus = state->read_location (*this);
   13275       229871 :       pdata.end_locus = state->read_location (*this);
   13276       229871 :       pdata.returns_value = flags & 4;
   13277       229871 :       pdata.returns_null = flags & 8;
   13278       229871 :       pdata.returns_abnormally = flags & 16;
   13279       229871 :       pdata.infinite_loop = flags & 32;
   13280              :     }
   13281              : 
   13282       230754 :   tree coro_actor = NULL_TREE;
   13283       230754 :   tree coro_destroy = NULL_TREE;
   13284       230754 :   tree coro_ramp = NULL_TREE;
   13285       230754 :   if (DECL_COROUTINE_P (decl))
   13286              :     {
   13287           18 :       coro_ramp = tree_node ();
   13288           18 :       if (!coro_ramp)
   13289              :         {
   13290           12 :           coro_actor = tree_node ();
   13291           12 :           coro_destroy = tree_node ();
   13292           12 :           if ((coro_actor == NULL_TREE) != (coro_destroy == NULL_TREE))
   13293            0 :             set_overrun ();
   13294              :         }
   13295              :     }
   13296              : 
   13297       230754 :   if (get_overrun ())
   13298              :     return NULL_TREE;
   13299              : 
   13300       230754 :   if (installing)
   13301              :     {
   13302       144061 :       DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
   13303       144061 :       DECL_RESULT (decl) = result;
   13304       144061 :       DECL_INITIAL (decl) = initial;
   13305       144061 :       DECL_SAVED_TREE (decl) = saved;
   13306              : 
   13307              :       /* Some entities (like anticipated builtins) were declared without
   13308              :          DECL_ARGUMENTS, so update them now.  But don't do it if there's
   13309              :          already an argument list, because we've already built the
   13310              :          definition referencing those merged PARM_DECLs.  */
   13311       144061 :       if (!DECL_ARGUMENTS (decl))
   13312         6871 :         DECL_ARGUMENTS (decl) = DECL_ARGUMENTS (maybe_dup);
   13313              : 
   13314       144061 :       if (context)
   13315         6229 :         SET_DECL_FRIEND_CONTEXT (decl, context);
   13316       144061 :       if (cexpr.decl)
   13317        38126 :         register_constexpr_fundef (cexpr);
   13318              : 
   13319       144061 :       if (coro_ramp)
   13320            6 :         coro_set_ramp_function (decl, coro_ramp);
   13321       144055 :       else if (coro_actor && coro_destroy)
   13322            3 :         coro_set_transform_functions (decl, coro_actor, coro_destroy);
   13323              : 
   13324       144061 :       if (DECL_LOCAL_DECL_P (decl))
   13325              :         /* Block-scope OMP UDRs aren't real functions, and don't need a
   13326              :            function structure to be allocated or to be expanded.  */
   13327            3 :         gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (decl));
   13328              :       else
   13329       144058 :         post_process (pdata);
   13330              :     }
   13331              :   else if (maybe_dup)
   13332              :     {
   13333              :       // FIXME:QOI Check matching defn
   13334              :     }
   13335              : 
   13336              :   return true;
   13337              : }
   13338              : 
   13339              : /* Also for CONCEPT_DECLs.  */
   13340              : 
   13341              : void
   13342       139692 : trees_out::write_var_def (tree decl)
   13343              : {
   13344              :   /* The initializer of a non-inline variable or variable template is
   13345              :      ignored for determining exposures.  */
   13346       139692 :   auto ovr = dep_hash->ignore_exposure_if (VAR_P (decl)
   13347       151471 :                                            && !DECL_INLINE_VAR_P (decl));
   13348              : 
   13349       139692 :   tree init = DECL_INITIAL (decl);
   13350       139692 :   tree_node (init);
   13351       139692 :   if (!init)
   13352              :     {
   13353         1744 :       tree dyn_init = NULL_TREE;
   13354              : 
   13355              :       /* We only need to write initializers in header modules.  */
   13356         2928 :       if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
   13357              :         {
   13358          450 :           dyn_init = value_member (decl,
   13359          450 :                                    CP_DECL_THREAD_LOCAL_P (decl)
   13360              :                                    ? tls_aggregates : static_aggregates);
   13361          450 :           gcc_checking_assert (dyn_init);
   13362              :           /* Mark it so write_inits knows this is needed.  */
   13363          450 :           TREE_LANG_FLAG_0 (dyn_init) = true;
   13364          450 :           dyn_init = TREE_PURPOSE (dyn_init);
   13365              :         }
   13366         1744 :       tree_node (dyn_init);
   13367              :     }
   13368       139692 : }
   13369              : 
   13370              : void
   13371            0 : trees_out::mark_var_def (tree)
   13372              : {
   13373            0 : }
   13374              : 
   13375              : bool
   13376        45419 : trees_in::read_var_def (tree decl, tree maybe_template)
   13377              : {
   13378              :   /* Do not mark the virtual table entries as used.  */
   13379        45419 :   bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
   13380        45419 :   unused += vtable;
   13381        45419 :   tree init = tree_node ();
   13382        45419 :   tree dyn_init = init ? NULL_TREE : tree_node ();
   13383        45419 :   unused -= vtable;
   13384              : 
   13385        45419 :   if (get_overrun ())
   13386              :     return false;
   13387              : 
   13388        45419 :   bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
   13389        45419 :                       : bool (DECL_INITIAL (decl)));
   13390        45419 :   tree maybe_dup = odr_duplicate (maybe_template, initialized);
   13391        45419 :   bool installing = maybe_dup && !initialized;
   13392        45419 :   if (installing)
   13393              :     {
   13394        28476 :       DECL_INITIAL (decl) = init;
   13395        28476 :       if (DECL_EXTERNAL (decl))
   13396         3574 :         DECL_NOT_REALLY_EXTERN (decl) = true;
   13397        28476 :       if (VAR_P (decl))
   13398              :         {
   13399        24880 :           DECL_INITIALIZED_P (decl) = true;
   13400        24880 :           if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
   13401        24469 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   13402        24880 :           tentative_decl_linkage (decl);
   13403        24880 :           if (DECL_EXPLICIT_INSTANTIATION (decl)
   13404        24880 :               && !DECL_EXTERNAL (decl))
   13405            9 :             setup_explicit_instantiation_definition_linkage (decl);
   13406              :           /* Class non-template static members are handled in read_class_def.
   13407              :              But still handle specialisations of member templates.  */
   13408        49760 :           if ((!DECL_CLASS_SCOPE_P (decl)
   13409        16054 :                || primary_template_specialization_p (decl))
   13410        33811 :               && (DECL_IMPLICIT_INSTANTIATION (decl)
   13411         8862 :                   || (DECL_EXPLICIT_INSTANTIATION (decl)
   13412           21 :                       && !DECL_EXTERNAL (decl))))
   13413           78 :             note_vague_linkage_variable (decl);
   13414              :         }
   13415        28476 :       if (!dyn_init)
   13416              :         ;
   13417          216 :       else if (CP_DECL_THREAD_LOCAL_P (decl))
   13418           96 :         tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
   13419              :       else
   13420          120 :         static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
   13421              :     }
   13422              :   else if (maybe_dup)
   13423              :     {
   13424              :       // FIXME:QOI Check matching defn
   13425              :     }
   13426              : 
   13427              :   return true;
   13428              : }
   13429              : 
   13430              : /* If MEMBER doesn't have an independent life outside the class,
   13431              :    return it (or its TEMPLATE_DECL).  Otherwise NULL.  */
   13432              : 
   13433              : static tree
   13434       279854 : member_owned_by_class (tree member)
   13435              : {
   13436       279854 :   gcc_assert (DECL_P (member));
   13437              : 
   13438              :   /* Clones are owned by their origin.  */
   13439       279854 :   if (DECL_CLONED_FUNCTION_P (member))
   13440              :     return NULL;
   13441              : 
   13442       279854 :   if (TREE_CODE (member) == FIELD_DECL)
   13443              :     /* FIELD_DECLS can have template info in some cases.  We always
   13444              :        want the FIELD_DECL though, as there's never a TEMPLATE_DECL
   13445              :        wrapping them.  */
   13446              :     return member;
   13447              : 
   13448       120811 :   int use_tpl = -1;
   13449       120811 :   if (tree ti = node_template_info (member, use_tpl))
   13450              :     {
   13451              :       // FIXME: Don't bail on things that CANNOT have their own
   13452              :       // template header.  No, make sure they're in the same cluster.
   13453            0 :       if (use_tpl > 0)
   13454              :         return NULL_TREE;
   13455              : 
   13456            0 :       if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
   13457       279854 :         member = TI_TEMPLATE (ti);
   13458              :     }
   13459              :   return member;
   13460              : }
   13461              : 
   13462              : void
   13463       200724 : trees_out::write_class_def (tree defn)
   13464              : {
   13465       200724 :   gcc_assert (DECL_P (defn));
   13466       200724 :   if (streaming_p ())
   13467       100759 :     dump () && dump ("Writing class definition %N", defn);
   13468              : 
   13469       200724 :   tree type = TREE_TYPE (defn);
   13470       200724 :   tree_node (TYPE_SIZE (type));
   13471       200724 :   tree_node (TYPE_SIZE_UNIT (type));
   13472       200724 :   tree_node (TYPE_VFIELD (type));
   13473       200724 :   tree_node (TYPE_BINFO (type));
   13474              : 
   13475       200724 :   vec_chained_decls (TYPE_FIELDS (type));
   13476              : 
   13477              :   /* Every class but __as_base has a type-specific.  */
   13478       397972 :   gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
   13479              : 
   13480       200724 :   if (TYPE_LANG_SPECIFIC (type))
   13481              :     {
   13482       197248 :       {
   13483       197248 :         vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
   13484       197248 :         if (!v)
   13485              :           {
   13486        47154 :             gcc_checking_assert (!streaming_p ());
   13487              :             /* Force a class vector.  */
   13488        47154 :             v = set_class_bindings (type, -1);
   13489        47154 :             gcc_checking_assert (v);
   13490              :           }
   13491              : 
   13492       197248 :         unsigned len = v->length ();
   13493       197248 :         if (streaming_p ())
   13494        98601 :           u (len);
   13495      1660742 :         for (unsigned ix = 0; ix != len; ix++)
   13496              :           {
   13497      1463494 :             tree m = (*v)[ix];
   13498      1463494 :             if (TREE_CODE (m) == TYPE_DECL
   13499       426011 :                 && DECL_ARTIFICIAL (m)
   13500      1677693 :                 && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
   13501              :               /* This is a using-decl for a type, or an anonymous
   13502              :                  struct (maybe with a typedef name).  Write the type.  */
   13503        15714 :               m = TREE_TYPE (m);
   13504      1463494 :             tree_node (m);
   13505              :           }
   13506              :       }
   13507       197248 :       tree_node (CLASSTYPE_LAMBDA_EXPR (type));
   13508              : 
   13509              :       /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
   13510              :          reader won't know at this point.  */
   13511       197248 :       int has_vptr = TYPE_CONTAINS_VPTR_P (type);
   13512              : 
   13513       197248 :       if (streaming_p ())
   13514              :         {
   13515        98601 :           unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
   13516        98601 :           u (nvbases);
   13517        98601 :           i (has_vptr);
   13518              :         }
   13519              : 
   13520       197248 :       if (has_vptr)
   13521              :         {
   13522         7104 :           tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
   13523         7104 :           tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
   13524         7104 :           tree_node (CLASSTYPE_KEY_METHOD (type));
   13525              :         }
   13526              :     }
   13527              : 
   13528       200724 :   if (TYPE_LANG_SPECIFIC (type))
   13529              :     {
   13530       197248 :       tree_node (CLASSTYPE_PRIMARY_BINFO (type));
   13531              : 
   13532       197248 :       tree as_base = CLASSTYPE_AS_BASE (type);
   13533       197248 :       if (as_base)
   13534       102453 :         as_base = TYPE_NAME (as_base);
   13535       197248 :       tree_node (as_base);
   13536              : 
   13537              :       /* Write the vtables.  */
   13538       197248 :       tree vtables = CLASSTYPE_VTABLES (type);
   13539       197248 :       vec_chained_decls (vtables);
   13540       402942 :       for (; vtables; vtables = TREE_CHAIN (vtables))
   13541         8446 :         write_definition (vtables);
   13542              : 
   13543       197248 :       {
   13544              :         /* Friend declarations in class definitions are ignored when
   13545              :            determining exposures.  */
   13546       197248 :         auto ovr = dep_hash->ignore_exposure_if (true);
   13547              : 
   13548              :         /* Write the friend classes.  */
   13549       197248 :         tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
   13550              : 
   13551              :         /* Write the friend functions.  */
   13552       197248 :         for (tree friends = DECL_FRIENDLIST (defn);
   13553       225958 :              friends; friends = TREE_CHAIN (friends))
   13554              :           {
   13555        28710 :             tree_node (FRIEND_NAME (friends));
   13556        28710 :             tree_list (FRIEND_DECLS (friends), false);
   13557              :           }
   13558              :         /* End of friend fns.  */
   13559       197248 :         tree_node (NULL_TREE);
   13560       197248 :       }
   13561              : 
   13562              :       /* Write the decl list.  We don't need to ignore exposures of friend
   13563              :          decls here as any such decls should already have been added and
   13564              :          ignored above.  */
   13565       197248 :       tree_list (CLASSTYPE_DECL_LIST (type), true);
   13566              : 
   13567       197248 :       if (TYPE_CONTAINS_VPTR_P (type))
   13568              :         {
   13569              :           /* Write the thunks.  */
   13570         7104 :           for (tree decls = TYPE_FIELDS (type);
   13571       201396 :                decls; decls = DECL_CHAIN (decls))
   13572       194292 :             if (TREE_CODE (decls) == FUNCTION_DECL
   13573       140334 :                 && DECL_VIRTUAL_P (decls)
   13574       231102 :                 && DECL_THUNKS (decls))
   13575              :               {
   13576         1308 :                 tree_node (decls);
   13577              :                 /* Thunks are always unique, so chaining is ok.  */
   13578         1308 :                 chained_decls (DECL_THUNKS (decls));
   13579              :               }
   13580         7104 :           tree_node (NULL_TREE);
   13581              :         }
   13582              :     }
   13583       200724 : }
   13584              : 
   13585              : void
   13586       279854 : trees_out::mark_class_member (tree member, bool do_defn)
   13587              : {
   13588       279854 :   gcc_assert (DECL_P (member));
   13589              : 
   13590       279854 :   member = member_owned_by_class (member);
   13591       279854 :   if (member)
   13592       559708 :     mark_declaration (member, do_defn && has_definition (member));
   13593       279854 : }
   13594              : 
   13595              : void
   13596       200752 : trees_out::mark_class_def (tree defn)
   13597              : {
   13598       200752 :   gcc_assert (DECL_P (defn));
   13599       200752 :   tree type = TREE_TYPE (defn);
   13600              :   /* Mark the class members that are not type-decls and cannot have
   13601              :      independent definitions.  */
   13602      2302178 :   for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
   13603      2101426 :     if (TREE_CODE (member) == FIELD_DECL
   13604      2101426 :         || TREE_CODE (member) == USING_DECL
   13605              :         /* A cloned enum-decl from 'using enum unrelated;'   */
   13606      2101426 :         || (TREE_CODE (member) == CONST_DECL
   13607        16514 :             && DECL_CONTEXT (member) == type))
   13608              :       {
   13609       279854 :         mark_class_member (member);
   13610       279854 :         if (TREE_CODE (member) == FIELD_DECL)
   13611       159043 :           if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
   13612              :             /* If we're marking a class template definition, then
   13613              :                this'll contain the width (as set by grokbitfield)
   13614              :                instead of a decl.  */
   13615         2990 :             if (DECL_P (repr))
   13616         2334 :               mark_declaration (repr, false);
   13617              :       }
   13618              : 
   13619              :   /* Mark the binfo hierarchy.  */
   13620       473470 :   for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
   13621       272718 :     mark_by_value (child);
   13622              : 
   13623       200752 :   if (TYPE_LANG_SPECIFIC (type))
   13624              :     {
   13625       197252 :       for (tree vtable = CLASSTYPE_VTABLES (type);
   13626       205698 :            vtable; vtable = TREE_CHAIN (vtable))
   13627         8446 :         mark_declaration (vtable, true);
   13628              : 
   13629       197252 :       if (TYPE_CONTAINS_VPTR_P (type))
   13630              :         /* Mark the thunks, they belong to the class definition,
   13631              :            /not/ the thunked-to function.  */
   13632         7104 :         for (tree decls = TYPE_FIELDS (type);
   13633       201396 :              decls; decls = DECL_CHAIN (decls))
   13634       194292 :           if (TREE_CODE (decls) == FUNCTION_DECL)
   13635       140334 :             for (tree thunks = DECL_THUNKS (decls);
   13636       142070 :                  thunks; thunks = DECL_CHAIN (thunks))
   13637         1736 :               mark_declaration (thunks, false);
   13638              :     }
   13639       200752 : }
   13640              : 
   13641              : /* Nop sorting, needed for resorting the member vec.  */
   13642              : 
   13643              : static void
   13644     11499174 : nop (void *, void *, void *)
   13645              : {
   13646     11499174 : }
   13647              : 
   13648              : bool
   13649        68515 : trees_in::read_class_def (tree defn, tree maybe_template)
   13650              : {
   13651        68515 :   gcc_assert (DECL_P (defn));
   13652        69170 :   dump () && dump ("Reading class definition %N", defn);
   13653        68515 :   tree type = TREE_TYPE (defn);
   13654        68515 :   tree size = tree_node ();
   13655        68515 :   tree size_unit = tree_node ();
   13656        68515 :   tree vfield = tree_node ();
   13657        68515 :   tree binfo = tree_node ();
   13658        68515 :   vec<tree, va_gc> *vbase_vec = NULL;
   13659        68515 :   vec<tree, va_gc> *member_vec = NULL;
   13660        68515 :   vec<tree, va_gc> *pure_virts = NULL;
   13661        68515 :   vec<tree_pair_s, va_gc> *vcall_indices = NULL;
   13662        68515 :   tree key_method = NULL_TREE;
   13663        68515 :   tree lambda = NULL_TREE;
   13664              : 
   13665              :   /* Read the fields.  */
   13666        68515 :   vec<tree, va_heap> *fields = vec_chained_decls ();
   13667              : 
   13668        68515 :   if (TYPE_LANG_SPECIFIC (type))
   13669              :     {
   13670        67132 :       if (unsigned len = u ())
   13671              :         {
   13672        67132 :           vec_alloc (member_vec, len);
   13673       607792 :           for (unsigned ix = 0; ix != len; ix++)
   13674              :             {
   13675       540660 :               tree m = tree_node ();
   13676       540660 :               if (get_overrun ())
   13677              :                 break;
   13678       540660 :               if (TYPE_P (m))
   13679         5726 :                 m = TYPE_STUB_DECL (m);
   13680       540660 :               member_vec->quick_push (m);
   13681              :             }
   13682              :         }
   13683        67132 :       lambda = tree_node ();
   13684              : 
   13685        67132 :       if (!get_overrun ())
   13686              :         {
   13687        67132 :           unsigned nvbases = u ();
   13688        67132 :           if (nvbases)
   13689              :             {
   13690          285 :               vec_alloc (vbase_vec, nvbases);
   13691         1300 :               for (tree child = binfo; child; child = TREE_CHAIN (child))
   13692         1015 :                 if (BINFO_VIRTUAL_P (child))
   13693          285 :                   vbase_vec->quick_push (child);
   13694              :             }
   13695              :         }
   13696              : 
   13697        67132 :       if (!get_overrun ())
   13698              :         {
   13699        67132 :           int has_vptr = i ();
   13700        67132 :           if (has_vptr)
   13701              :             {
   13702         2693 :               pure_virts = tree_vec ();
   13703         2693 :               vcall_indices = tree_pair_vec ();
   13704         2693 :               key_method = tree_node ();
   13705              :             }
   13706              :         }
   13707              :     }
   13708              : 
   13709        68515 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
   13710        68515 :   bool installing = maybe_dup && !TYPE_SIZE (type);
   13711        38355 :   if (installing)
   13712              :     {
   13713        38355 :       if (maybe_dup != defn)
   13714              :         {
   13715              :           // FIXME: This is needed on other defns too, almost
   13716              :           // duplicate-decl like?  See is_matching_decl too.
   13717              :           /* Copy flags from the duplicate.  */
   13718          316 :           tree type_dup = TREE_TYPE (maybe_dup);
   13719              : 
   13720              :           /* Core pieces.  */
   13721          316 :           TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
   13722          316 :           TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
   13723          632 :           TYPE_WARN_IF_NOT_ALIGN_RAW (type)
   13724          316 :             = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
   13725          316 :           TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
   13726              : 
   13727          316 :           SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
   13728          316 :           DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
   13729          316 :           DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
   13730          316 :           DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
   13731          632 :           DECL_WARN_IF_NOT_ALIGN_RAW (defn)
   13732          316 :             = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
   13733          316 :           DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
   13734              : 
   13735          316 :           TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
   13736          316 :           TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
   13737          316 :           TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
   13738          316 :           TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
   13739          632 :           TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
   13740          316 :             = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
   13741              : 
   13742          316 :           TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
   13743          316 :           TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
   13744              : 
   13745              :           /* C++ pieces.  */
   13746          316 :           TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
   13747          316 :           CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
   13748              : 
   13749          632 :           TYPE_HAS_USER_CONSTRUCTOR (type)
   13750          316 :             = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
   13751          632 :           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   13752          316 :             = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
   13753          632 :           TYPE_NEEDS_CONSTRUCTING (type)
   13754          316 :             = TYPE_NEEDS_CONSTRUCTING (type_dup);
   13755              : 
   13756          316 :           if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
   13757              :             {
   13758          316 :               if (TYPE_LANG_SPECIFIC (type))
   13759              :                 {
   13760          948 :                   CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
   13761          316 :                     = CLASSTYPE_BEFRIENDING_CLASSES (type);
   13762          632 :                   SET_CLASSTYPE_TYPEINFO_VAR (type_dup,
   13763              :                                               CLASSTYPE_TYPEINFO_VAR (type));
   13764              :                 }
   13765         1307 :               for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
   13766          991 :                 TYPE_LANG_SPECIFIC (v) = ls;
   13767              :             }
   13768              :         }
   13769              : 
   13770        38355 :       TYPE_SIZE (type) = size;
   13771        38355 :       TYPE_SIZE_UNIT (type) = size_unit;
   13772              : 
   13773        38355 :       if (fields)
   13774              :         {
   13775        38355 :           tree *chain = &TYPE_FIELDS (type);
   13776        38355 :           unsigned len = fields->length ();
   13777       501960 :           for (unsigned ix = 0; ix != len; ix++)
   13778              :             {
   13779       463605 :               tree decl = (*fields)[ix];
   13780              : 
   13781       463605 :               if (!decl)
   13782              :                 {
   13783              :                   /* An anonymous struct with typedef name.  */
   13784            3 :                   tree tdef = (*fields)[ix+1];
   13785            3 :                   decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
   13786            3 :                   gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
   13787              :                                        && decl != tdef);
   13788              :                 }
   13789              : 
   13790       837297 :               gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
   13791       463605 :               *chain = decl;
   13792       463605 :               chain = &DECL_CHAIN (decl);
   13793              : 
   13794       463605 :               if (TREE_CODE (decl) == FIELD_DECL
   13795       463605 :                   && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   13796              :                 {
   13797          285 :                   tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
   13798          285 :                   if (DECL_NAME (defn) == as_base_identifier)
   13799              :                     /* ANON_AGGR_TYPE_FIELD should already point to the
   13800              :                        original FIELD_DECL; don't overwrite it to point
   13801              :                        to the as-base FIELD_DECL copy.  */
   13802           26 :                     gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
   13803              :                   else
   13804          272 :                     SET_ANON_AGGR_TYPE_FIELD (anon_type, decl);
   13805              :                 }
   13806              : 
   13807       463605 :               if (TREE_CODE (decl) == USING_DECL
   13808       463605 :                   && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
   13809              :                 {
   13810              :                   /* Reconstruct DECL_ACCESS.  */
   13811        19112 :                   tree decls = USING_DECL_DECLS (decl);
   13812        19112 :                   tree access = declared_access (decl);
   13813              : 
   13814        22586 :                   for (ovl_iterator iter (decls); iter; ++iter)
   13815              :                     {
   13816         2263 :                       tree d = *iter;
   13817              : 
   13818         2263 :                       retrofit_lang_decl (d);
   13819         2263 :                       tree list = DECL_ACCESS (d);
   13820              : 
   13821         2263 :                       if (!purpose_member (type, list))
   13822         2728 :                         DECL_ACCESS (d) = tree_cons (type, access, list);
   13823              :                     }
   13824              :                 }
   13825              : 
   13826       463605 :               if (TREE_CODE (decl) == VAR_DECL
   13827        13464 :                   && TREE_CODE (maybe_template) != TEMPLATE_DECL)
   13828        11582 :                 note_vague_linkage_variable (decl);
   13829              :             }
   13830              :         }
   13831              : 
   13832        38355 :       TYPE_VFIELD (type) = vfield;
   13833        38355 :       TYPE_BINFO (type) = binfo;
   13834              : 
   13835        38355 :       if (TYPE_LANG_SPECIFIC (type))
   13836              :         {
   13837        37500 :           if (!TYPE_POLYMORPHIC_P (type))
   13838        35909 :             SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
   13839              :           else
   13840         1591 :             gcc_checking_assert (lambda == NULL_TREE);
   13841              : 
   13842        37500 :           CLASSTYPE_MEMBER_VEC (type) = member_vec;
   13843        37500 :           CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
   13844        37500 :           CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
   13845              : 
   13846        37500 :           if (TYPE_POLYMORPHIC_P (type))
   13847         1591 :             SET_CLASSTYPE_KEY_METHOD (type, key_method);
   13848              :           else
   13849        35909 :             gcc_checking_assert (key_method == NULL_TREE);
   13850              : 
   13851        37500 :           CLASSTYPE_VBASECLASSES (type) = vbase_vec;
   13852              : 
   13853              :           /* Resort the member vector.  */
   13854        37500 :           resort_type_member_vec (member_vec, NULL, nop, NULL);
   13855              :         }
   13856              :     }
   13857              :   else if (maybe_dup)
   13858              :     {
   13859              :       // FIXME:QOI Check matching defn
   13860              :     }
   13861              : 
   13862        68515 :   if (TYPE_LANG_SPECIFIC (type))
   13863              :     {
   13864        67132 :       tree primary = tree_node ();
   13865        67132 :       tree as_base = tree_node ();
   13866              : 
   13867        67132 :       if (as_base)
   13868        34581 :         as_base = TREE_TYPE (as_base);
   13869              : 
   13870              :       /* Read the vtables.  */
   13871        67132 :       vec<tree, va_heap> *vtables = vec_chained_decls ();
   13872        67132 :       if (vtables)
   13873              :         {
   13874         2651 :           unsigned len = vtables->length ();
   13875         5783 :           for (unsigned ix = 0; ix != len; ix++)
   13876              :             {
   13877         3132 :               tree vtable = (*vtables)[ix];
   13878         3132 :               read_var_def (vtable, vtable);
   13879              :             }
   13880              :         }
   13881              : 
   13882        67132 :       tree friend_classes = tree_list (false);
   13883        67132 :       tree friend_functions = NULL_TREE;
   13884        67132 :       for (tree *chain = &friend_functions;
   13885        79521 :            tree name = tree_node (); chain = &TREE_CHAIN (*chain))
   13886              :         {
   13887        12389 :           tree val = tree_list (false);
   13888        12389 :           *chain = build_tree_list (name, val);
   13889        12389 :         }
   13890        67132 :       tree decl_list = tree_list (true);
   13891              : 
   13892        67132 :       if (installing)
   13893              :         {
   13894        37500 :           CLASSTYPE_PRIMARY_BINFO (type) = primary;
   13895        37500 :           CLASSTYPE_AS_BASE (type) = as_base;
   13896              : 
   13897        37500 :           if (vtables)
   13898              :             {
   13899         1603 :               if ((!CLASSTYPE_KEY_METHOD (type)
   13900              :                    /* Sneaky user may have defined it inline
   13901              :                       out-of-class.  */
   13902         1152 :                    || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
   13903              :                   /* An imported non-template class attached to a module
   13904              :                      doesn't need to have its vtables emitted here.  */
   13905         1759 :                   && (CLASSTYPE_USE_TEMPLATE (type)
   13906          295 :                       || !DECL_MODULE_ATTACH_P (defn)))
   13907         1135 :                 vec_safe_push (keyed_classes, type);
   13908         1603 :               unsigned len = vtables->length ();
   13909         1603 :               tree *chain = &CLASSTYPE_VTABLES (type);
   13910         3504 :               for (unsigned ix = 0; ix != len; ix++)
   13911              :                 {
   13912         1901 :                   tree vtable = (*vtables)[ix];
   13913         1901 :                   gcc_checking_assert (!*chain);
   13914         1901 :                   *chain = vtable;
   13915         1901 :                   chain = &DECL_CHAIN (vtable);
   13916              :                 }
   13917              :             }
   13918        37500 :           CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
   13919        37500 :           DECL_FRIENDLIST (defn) = friend_functions;
   13920        37500 :           CLASSTYPE_DECL_LIST (type) = decl_list;
   13921              : 
   13922        40545 :           for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
   13923              :             {
   13924         3045 :               tree f = TREE_VALUE (friend_classes);
   13925         3045 :               if (TREE_CODE (f) == TEMPLATE_DECL)
   13926         1255 :                 f = TREE_TYPE (f);
   13927              : 
   13928         3045 :               if (CLASS_TYPE_P (f))
   13929              :                 {
   13930         3007 :                   CLASSTYPE_BEFRIENDING_CLASSES (f)
   13931         6014 :                     = tree_cons (NULL_TREE, type,
   13932         3007 :                                  CLASSTYPE_BEFRIENDING_CLASSES (f));
   13933         3051 :                   dump () && dump ("Class %N befriending %C:%N",
   13934            6 :                                    type, TREE_CODE (f), f);
   13935              :                 }
   13936              :             }
   13937              : 
   13938        44947 :           for (; friend_functions;
   13939         7447 :                friend_functions = TREE_CHAIN (friend_functions))
   13940         7447 :             for (tree friend_decls = TREE_VALUE (friend_functions);
   13941        16954 :                  friend_decls; friend_decls = TREE_CHAIN (friend_decls))
   13942              :               {
   13943         9507 :                 tree f = TREE_VALUE (friend_decls);
   13944         9507 :                 if (TREE_CODE (f) == TU_LOCAL_ENTITY)
   13945           36 :                   continue;
   13946              : 
   13947         9471 :                 DECL_BEFRIENDING_CLASSES (f)
   13948         9471 :                   = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
   13949         9537 :                 dump () && dump ("Class %N befriending %C:%N",
   13950           30 :                                  type, TREE_CODE (f), f);
   13951              :               }
   13952              :         }
   13953              : 
   13954        67132 :       if (TYPE_CONTAINS_VPTR_P (type))
   13955              :         /* Read and install the thunks.  */
   13956         3131 :         while (tree vfunc = tree_node ())
   13957              :           {
   13958          438 :             tree thunks = chained_decls ();
   13959          438 :             if (installing)
   13960          270 :               SET_DECL_THUNKS (vfunc, thunks);
   13961              :           }
   13962              : 
   13963        67132 :       vec_free (vtables);
   13964              :     }
   13965              : 
   13966              :   /* Propagate to all variants.  */
   13967        68515 :   if (installing)
   13968        38355 :     fixup_type_variants (type);
   13969              : 
   13970              :   /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
   13971              :      the fake base, we've not hooked it into the containing class's
   13972              :      data structure yet.  Fortunately it has a unique name.  */
   13973        38355 :   if (installing
   13974        38355 :       && DECL_NAME (defn) != as_base_identifier
   13975        37500 :       && (!CLASSTYPE_TEMPLATE_INFO (type)
   13976        31626 :           || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
   13977              :     /* Emit debug info.  It'd be nice to know if the interface TU
   13978              :        already emitted this.  */
   13979        20805 :     rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
   13980              : 
   13981        68515 :   vec_free (fields);
   13982              : 
   13983        68515 :   return !get_overrun ();
   13984              : }
   13985              : 
   13986              : void
   13987         9376 : trees_out::write_enum_def (tree decl)
   13988              : {
   13989         9376 :   tree type = TREE_TYPE (decl);
   13990              : 
   13991         9376 :   tree_node (TYPE_VALUES (type));
   13992              :   /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   13993              :      ENUMERAL_TYPE.  */
   13994         9376 : }
   13995              : 
   13996              : void
   13997         9376 : trees_out::mark_enum_def (tree decl)
   13998              : {
   13999         9376 :   tree type = TREE_TYPE (decl);
   14000              : 
   14001        48766 :   for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
   14002              :     {
   14003        39390 :       tree cst = TREE_VALUE (values);
   14004        39390 :       mark_by_value (cst);
   14005              :       /* We must mark the init to avoid circularity in tt_enum_int.  */
   14006        39390 :       if (tree init = DECL_INITIAL (cst))
   14007        38954 :         if (TREE_CODE (init) == INTEGER_CST)
   14008        38222 :           mark_by_value (init);
   14009              :     }
   14010         9376 : }
   14011              : 
   14012              : bool
   14013         3180 : trees_in::read_enum_def (tree defn, tree maybe_template)
   14014              : {
   14015         3180 :   tree type = TREE_TYPE (defn);
   14016         3180 :   tree values = tree_node ();
   14017              : 
   14018         3180 :   if (get_overrun ())
   14019              :     return false;
   14020              : 
   14021         3180 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
   14022         6360 :   bool installing = maybe_dup && !TYPE_VALUES (type);
   14023              : 
   14024         3180 :   if (installing)
   14025              :     {
   14026         1636 :       TYPE_VALUES (type) = values;
   14027              :       /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   14028              :          ENUMERAL_TYPE.  */
   14029              : 
   14030         2657 :       rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
   14031              :     }
   14032         1544 :   else if (maybe_dup)
   14033              :     {
   14034         1544 :       tree known = TYPE_VALUES (type);
   14035         8519 :       for (; known && values;
   14036         6975 :            known = TREE_CHAIN (known), values = TREE_CHAIN (values))
   14037              :         {
   14038         6984 :           tree known_decl = TREE_VALUE (known);
   14039         6984 :           tree new_decl = TREE_VALUE (values);
   14040              : 
   14041         6984 :           if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
   14042              :             break;
   14043              : 
   14044         6978 :           new_decl = maybe_duplicate (new_decl);
   14045              : 
   14046         6978 :           if (!cp_tree_equal (DECL_INITIAL (known_decl),
   14047         6978 :                               DECL_INITIAL (new_decl)))
   14048              :             break;
   14049              :         }
   14050              : 
   14051         1544 :       if (known || values)
   14052              :         {
   14053           12 :           auto_diagnostic_group d;
   14054           12 :           error_at (DECL_SOURCE_LOCATION (maybe_dup),
   14055              :                     "definition of %qD does not match", maybe_dup);
   14056           12 :           inform (DECL_SOURCE_LOCATION (defn),
   14057              :                   "existing definition %qD", defn);
   14058              : 
   14059           12 :           tree known_decl = NULL_TREE, new_decl = NULL_TREE;
   14060              : 
   14061           12 :           if (known)
   14062            9 :             known_decl = TREE_VALUE (known);
   14063           12 :           if (values)
   14064           12 :             new_decl = maybe_duplicate (TREE_VALUE (values));
   14065              : 
   14066           12 :           if (known_decl && new_decl)
   14067              :             {
   14068            9 :               inform (DECL_SOURCE_LOCATION (new_decl),
   14069              :                       "enumerator %qD does not match ...", new_decl);
   14070            9 :               inform (DECL_SOURCE_LOCATION (known_decl),
   14071              :                       "... this enumerator %qD", known_decl);
   14072              :             }
   14073            3 :           else if (known_decl || new_decl)
   14074              :             {
   14075            3 :               tree extra = known_decl ? known_decl : new_decl;
   14076            3 :               inform (DECL_SOURCE_LOCATION (extra),
   14077              :                       "additional enumerators beginning with %qD", extra);
   14078              :             }
   14079              :           else
   14080            0 :             inform (DECL_SOURCE_LOCATION (maybe_dup),
   14081              :                     "enumeration range differs");
   14082              : 
   14083              :           /* Mark it bad.  */
   14084           12 :           unmatched_duplicate (maybe_template);
   14085           12 :         }
   14086              :     }
   14087              : 
   14088              :   return true;
   14089              : }
   14090              : 
   14091              : /* Write out the body of DECL.  See above circularity note.  */
   14092              : 
   14093              : void
   14094       929039 : trees_out::write_definition (tree decl, bool refs_tu_local)
   14095              : {
   14096       929039 :   auto ovr = make_temp_override (writing_local_entities,
   14097       929039 :                                  writing_local_entities || refs_tu_local);
   14098              : 
   14099       929039 :   if (streaming_p ())
   14100              :     {
   14101       464429 :       assert_definition (decl);
   14102       464429 :       dump ()
   14103          952 :         && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
   14104              :     }
   14105              :   else
   14106       464610 :     dump (dumper::DEPEND)
   14107           96 :       && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
   14108              : 
   14109      1453042 :  again:
   14110      1453042 :   switch (TREE_CODE (decl))
   14111              :     {
   14112            0 :     default:
   14113            0 :       gcc_unreachable ();
   14114              : 
   14115       524003 :     case TEMPLATE_DECL:
   14116       524003 :       decl = DECL_TEMPLATE_RESULT (decl);
   14117       524003 :       goto again;
   14118              : 
   14119       579247 :     case FUNCTION_DECL:
   14120       579247 :       write_function_def (decl);
   14121       579247 :       break;
   14122              : 
   14123       210100 :     case TYPE_DECL:
   14124       210100 :       {
   14125       210100 :         tree type = TREE_TYPE (decl);
   14126       210100 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14127              :                     && TYPE_NAME (type) == decl);
   14128       210100 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14129         9376 :           write_enum_def (decl);
   14130              :         else
   14131       200724 :           write_class_def (decl);
   14132              :       }
   14133              :       break;
   14134              : 
   14135       139692 :     case VAR_DECL:
   14136       139692 :     case CONCEPT_DECL:
   14137       139692 :       write_var_def (decl);
   14138       139692 :       break;
   14139              :     }
   14140       929039 : }
   14141              : 
   14142              : /* Mark a declaration for by-value walking.  If DO_DEFN is true, mark
   14143              :    its body too.  */
   14144              : 
   14145              : void
   14146      3902217 : trees_out::mark_declaration (tree decl, bool do_defn)
   14147              : {
   14148      3902217 :   mark_by_value (decl);
   14149              : 
   14150      3902217 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14151      1292187 :     decl = DECL_TEMPLATE_RESULT (decl);
   14152              : 
   14153      3902217 :   if (!do_defn)
   14154              :     return;
   14155              : 
   14156       929061 :   switch (TREE_CODE (decl))
   14157              :     {
   14158            0 :     default:
   14159            0 :       gcc_unreachable ();
   14160              : 
   14161              :     case FUNCTION_DECL:
   14162              :       mark_function_def (decl);
   14163              :       break;
   14164              : 
   14165       210128 :     case TYPE_DECL:
   14166       210128 :       {
   14167       210128 :         tree type = TREE_TYPE (decl);
   14168       210128 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14169              :                     && TYPE_NAME (type) == decl);
   14170       210128 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14171         9376 :           mark_enum_def (decl);
   14172              :         else
   14173       200752 :           mark_class_def (decl);
   14174              :       }
   14175              :       break;
   14176              : 
   14177              :     case VAR_DECL:
   14178              :     case CONCEPT_DECL:
   14179              :       mark_var_def (decl);
   14180              :       break;
   14181              :     }
   14182              : }
   14183              : 
   14184              : /* Read in the body of DECL.  See above circularity note.  */
   14185              : 
   14186              : bool
   14187       344736 : trees_in::read_definition (tree decl)
   14188              : {
   14189       346076 :   dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
   14190              : 
   14191              :   tree maybe_template = decl;
   14192              : 
   14193       344736 :  again:
   14194       550689 :   switch (TREE_CODE (decl))
   14195              :     {
   14196              :     default:
   14197              :       break;
   14198              : 
   14199       205953 :     case TEMPLATE_DECL:
   14200       205953 :       decl = DECL_TEMPLATE_RESULT (decl);
   14201       205953 :       goto again;
   14202              : 
   14203       230754 :     case FUNCTION_DECL:
   14204       230754 :       return read_function_def (decl, maybe_template);
   14205              : 
   14206        71695 :     case TYPE_DECL:
   14207        71695 :       {
   14208        71695 :         tree type = TREE_TYPE (decl);
   14209        71695 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14210              :                     && TYPE_NAME (type) == decl);
   14211        71695 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14212         3180 :           return read_enum_def (decl, maybe_template);
   14213              :         else
   14214        68515 :           return read_class_def (decl, maybe_template);
   14215              :       }
   14216        42287 :       break;
   14217              : 
   14218        42287 :     case VAR_DECL:
   14219        42287 :     case CONCEPT_DECL:
   14220        42287 :       return read_var_def (decl, maybe_template);
   14221              :     }
   14222              : 
   14223              :   return false;
   14224              : }
   14225              : 
   14226              : /* Lookup an maybe insert a slot for depset for KEY.  */
   14227              : 
   14228              : depset **
   14229     19417688 : depset::hash::entity_slot (tree entity, bool insert)
   14230              : {
   14231     19417688 :   traits::compare_type key (entity, NULL);
   14232     29316393 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14233              :                                        insert ? INSERT : NO_INSERT);
   14234              : 
   14235     19417688 :   return slot;
   14236              : }
   14237              : 
   14238              : depset **
   14239       225328 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
   14240              : {
   14241       225328 :   traits::compare_type key (ctx, name);
   14242       292790 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14243              :                                        insert ? INSERT : NO_INSERT);
   14244              : 
   14245       225328 :   return slot;
   14246              : }
   14247              : 
   14248              : depset *
   14249      9440079 : depset::hash::find_dependency (tree decl)
   14250              : {
   14251      9440079 :   depset **slot = entity_slot (decl, false);
   14252              : 
   14253      9440079 :   return slot ? *slot : NULL;
   14254              : }
   14255              : 
   14256              : depset *
   14257        67462 : depset::hash::find_binding (tree ctx, tree name)
   14258              : {
   14259        67462 :   depset **slot = binding_slot (ctx, name, false);
   14260              : 
   14261        67462 :   return slot ? *slot : NULL;
   14262              : }
   14263              : 
   14264              : static bool is_tu_local_entity (tree decl, bool explain = false);
   14265              : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
   14266              : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
   14267              : 
   14268              : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
   14269              :    If EXPLAIN is true, emit an informative note about why DECL is TU-local.  */
   14270              : 
   14271              : static bool
   14272      4768385 : is_tu_local_entity (tree decl, bool explain/*=false*/)
   14273              : {
   14274      4768385 :   gcc_checking_assert (DECL_P (decl));
   14275      4768385 :   location_t loc = DECL_SOURCE_LOCATION (decl);
   14276      4768385 :   tree type = TREE_TYPE (decl);
   14277              : 
   14278              :   /* Only types, functions, variables, and template (specialisations)
   14279              :      can be TU-local.  */
   14280      4768385 :   if (TREE_CODE (decl) != TYPE_DECL
   14281              :       && TREE_CODE (decl) != FUNCTION_DECL
   14282              :       && TREE_CODE (decl) != VAR_DECL
   14283              :       && TREE_CODE (decl) != TEMPLATE_DECL)
   14284              :     return false;
   14285              : 
   14286              :   /* An explicit type alias is not an entity; we don't want to stream
   14287              :      such aliases if they refer to TU-local entities, so propagate this
   14288              :      from the original type. The built-in declarations of 'int' and such
   14289              :      are never TU-local.  */
   14290      4764673 :   if (TREE_CODE (decl) == TYPE_DECL
   14291      1605906 :       && !DECL_SELF_REFERENCE_P (decl)
   14292      6305347 :       && !DECL_IMPLICIT_TYPEDEF_P (decl))
   14293              :     {
   14294       829401 :       tree orig = DECL_ORIGINAL_TYPE (decl);
   14295       829401 :       if (orig && TYPE_NAME (orig))
   14296              :         {
   14297       173664 :           if (explain)
   14298           11 :             inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
   14299       173664 :           return is_tu_local_entity (TYPE_NAME (orig), explain);
   14300              :         }
   14301              :       else
   14302              :         return false;
   14303              :     }
   14304              : 
   14305              :   /* Check specializations first for slightly better explanations.  */
   14306      3935272 :   int use_tpl = -1;
   14307      3935272 :   tree ti = node_template_info (decl, use_tpl);
   14308      4820191 :   if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
   14309              :     {
   14310              :       /* A specialization of a TU-local template.  */
   14311       884760 :       tree tmpl = TI_TEMPLATE (ti);
   14312       884760 :       if (is_tu_local_entity (tmpl))
   14313              :         {
   14314           72 :           if (explain)
   14315              :             {
   14316           18 :               inform (loc, "%qD is a specialization of TU-local template %qD",
   14317              :                       decl, tmpl);
   14318           18 :               is_tu_local_entity (tmpl, /*explain=*/true);
   14319              :             }
   14320           72 :           return true;
   14321              :         }
   14322              : 
   14323              :       /* A specialization of a template with any TU-local template argument.  */
   14324       884688 :       if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
   14325              :         return true;
   14326              : 
   14327              :       /* FIXME A specialization of a template whose (possibly instantiated)
   14328              :          declaration is an exposure.  This should always be covered by the
   14329              :          above cases??  */
   14330              :     }
   14331              : 
   14332              :   /* A type, function, variable, or template with internal linkage.  */
   14333      3935168 :   linkage_kind kind = decl_linkage (decl);
   14334      3935168 :   if (kind == lk_internal
   14335              :       /* But although weakrefs are marked static, don't consider them
   14336              :          to be TU-local.  */
   14337      3935168 :       && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
   14338              :     {
   14339          849 :       if (explain)
   14340          168 :         inform (loc, "%qD declared with internal linkage", decl);
   14341          849 :       return true;
   14342              :     }
   14343              : 
   14344              :   /* Does not have a name with linkage and is declared, or introduced by a
   14345              :      lambda-expression, within the definition of a TU-local entity.  */
   14346      3934319 :   if (kind == lk_none)
   14347              :     {
   14348       395292 :       tree ctx = CP_DECL_CONTEXT (decl);
   14349       502436 :       if (LAMBDA_TYPE_P (type))
   14350        75796 :         if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
   14351       395292 :           ctx = extra;
   14352              : 
   14353       395292 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   14354              :         {
   14355           31 :           if (!TREE_PUBLIC (ctx))
   14356              :             {
   14357            0 :               if (explain)
   14358            0 :                 inform (loc, "%qD has no linkage and is declared in an "
   14359              :                         "anonymous namespace", decl);
   14360            0 :               return true;
   14361              :             }
   14362              :         }
   14363       395261 :       else if (TYPE_P (ctx))
   14364              :         {
   14365        45251 :           tree ctx_decl = TYPE_MAIN_DECL (ctx);
   14366        45251 :           if (is_tu_local_entity (ctx_decl))
   14367              :             {
   14368            6 :               if (explain)
   14369              :                 {
   14370            0 :                   inform (loc, "%qD has no linkage and is declared within "
   14371              :                           "TU-local entity %qT", decl, ctx);
   14372            0 :                   is_tu_local_entity (ctx_decl, /*explain=*/true);
   14373              :                 }
   14374            6 :               return true;
   14375              :             }
   14376              :         }
   14377       350010 :       else if (is_tu_local_entity (ctx))
   14378              :         {
   14379           33 :           if (explain)
   14380              :             {
   14381            6 :               inform (loc, "%qD has no linkage and is declared within "
   14382              :                       "TU-local entity %qD", decl, ctx);
   14383            6 :               is_tu_local_entity (ctx, /*explain=*/true);
   14384              :             }
   14385           33 :           return true;
   14386              :         }
   14387              :     }
   14388              : 
   14389              :   /* A type with no name that is defined outside a class-specifier, function
   14390              :      body, or initializer; or is introduced by a defining-type-specifier that
   14391              :      is used to declare only TU-local entities.
   14392              : 
   14393              :      We consider types with names for linkage purposes as having names, since
   14394              :      these aren't really TU-local.  */
   14395      3934280 :   tree inner = STRIP_TEMPLATE (decl);
   14396      1680498 :   if (inner
   14397      3934280 :       && TREE_CODE (inner) == TYPE_DECL
   14398      2957139 :       && TYPE_ANON_P (type)
   14399        41358 :       && !DECL_SELF_REFERENCE_P (inner)
   14400              :       /* An enum with an enumerator name for linkage.  */
   14401      1717906 :       && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
   14402              :     {
   14403        35338 :       tree main_decl = TYPE_MAIN_DECL (type);
   14404        70166 :       if (LAMBDA_TYPE_P (type))
   14405              :         {
   14406              :           /* A lambda expression is, in practice, TU-local iff it has no
   14407              :              mangling scope.  This currently doesn't line up exactly with
   14408              :              the standard's definition due to some ABI issues, but it's
   14409              :              pretty close, and avoids other issues down the line.  */
   14410        69530 :           if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
   14411              :             {
   14412            4 :               if (explain)
   14413            2 :                 inform (loc, "%qT has no name and cannot be differentiated "
   14414              :                         "from similar lambdas in other TUs", type);
   14415            4 :               return true;
   14416              :             }
   14417              :         }
   14418         1146 :       else if (!DECL_CLASS_SCOPE_P (main_decl)
   14419          603 :                && !decl_function_context (main_decl))
   14420              :         {
   14421           30 :           if (explain)
   14422           12 :             inform (loc, "%qT has no name and is not defined within a class, "
   14423              :                     "function, or initializer", type);
   14424           30 :           return true;
   14425              :         }
   14426              : 
   14427              :       // FIXME introduced by a defining-type-specifier only declaring TU-local
   14428              :       // entities; does this refer to e.g. 'static struct {} a;"?  I can't
   14429              :       // think of any cases where this isn't covered by earlier cases.  */
   14430              :     }
   14431              : 
   14432              :   return false;
   14433              : }
   14434              : 
   14435              : /* Helper for is_tu_local_entity.  Returns true if one of the ARGS of
   14436              :    DECL is TU-local.  Emits an explanation if EXPLAIN is true.  */
   14437              : 
   14438              : static bool
   14439      1005059 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
   14440              : {
   14441      1005059 :   if (!args || TREE_CODE (args) != TREE_VEC)
   14442              :     return false;
   14443              : 
   14444      2716183 :   for (tree a : tree_vec_range (args))
   14445              :     {
   14446      1711156 :       if (TREE_CODE (a) == TREE_VEC)
   14447              :         {
   14448       120371 :           if (has_tu_local_tmpl_arg (decl, a, explain))
   14449           32 :             return true;
   14450              :         }
   14451              :       else if (!WILDCARD_TYPE_P (a))
   14452              :         {
   14453      1445407 :           if (DECL_P (a) && is_tu_local_entity (a))
   14454              :             {
   14455            0 :               if (explain)
   14456              :                 {
   14457            0 :                   inform (DECL_SOURCE_LOCATION (decl),
   14458              :                           "%qD has TU-local template argument %qD",
   14459              :                           decl, a);
   14460            0 :                   is_tu_local_entity (a, /*explain=*/true);
   14461              :                 }
   14462            0 :               return true;
   14463              :             }
   14464              : 
   14465      1445407 :           if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
   14466              :             {
   14467           17 :               if (explain)
   14468              :                 {
   14469            1 :                   inform (DECL_SOURCE_LOCATION (decl),
   14470              :                           "%qD has TU-local template argument %qT",
   14471              :                           decl, a);
   14472            1 :                   is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
   14473              :                 }
   14474           17 :               return true;
   14475              :             }
   14476              : 
   14477      1445390 :           if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
   14478              :             return true;
   14479              :         }
   14480              :     }
   14481              : 
   14482      1005027 :   return false;
   14483              : }
   14484              : 
   14485              : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
   14486              :    or object.  Emits an explanation if EXPLAIN is true.  */
   14487              : 
   14488              : static bool
   14489       105875 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
   14490              : {
   14491       105875 :   if (!expr)
   14492              :     return false;
   14493              : 
   14494       103967 :   tree e = expr;
   14495       103967 :   STRIP_ANY_LOCATION_WRAPPER (e);
   14496       103967 :   STRIP_NOPS (e);
   14497       103967 :   if (TREE_CODE (e) == TARGET_EXPR)
   14498            0 :     e = TARGET_EXPR_INITIAL (e);
   14499            0 :   if (!e)
   14500              :     return false;
   14501              : 
   14502              :   /* It is, or is a pointer to, a TU-local function or the object associated
   14503              :      with a TU-local variable.  */
   14504       103967 :   tree object = NULL_TREE;
   14505       103967 :   if (TREE_CODE (e) == ADDR_EXPR)
   14506         2794 :     object = TREE_OPERAND (e, 0);
   14507       101173 :   else if (TREE_CODE (e) == PTRMEM_CST)
   14508            0 :     object = PTRMEM_CST_MEMBER (e);
   14509       101173 :   else if (VAR_OR_FUNCTION_DECL_P (e))
   14510              :     object = e;
   14511              : 
   14512         2794 :   if (object
   14513         3524 :       && VAR_OR_FUNCTION_DECL_P (object)
   14514         3611 :       && is_tu_local_entity (object))
   14515              :     {
   14516           54 :       if (explain)
   14517              :         {
   14518              :           /* We've lost a lot of location information by the time we get here,
   14519              :              so let's just do our best effort.  */
   14520           18 :           auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
   14521           18 :           if (VAR_P (object))
   14522            9 :             inform (loc, "%qD refers to TU-local object %qD", decl, object);
   14523              :           else
   14524            9 :             inform (loc, "%qD refers to TU-local function %qD", decl, object);
   14525           18 :           is_tu_local_entity (object, true);
   14526              :         }
   14527           54 :       return true;
   14528              :     }
   14529              : 
   14530              :   /* It is an object of class or array type and any of its subobjects or
   14531              :      any of the objects or functions to which its non-static data members
   14532              :      of reference type refer is TU-local and is usable in constant
   14533              :      expressions.  */
   14534       103913 :   if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
   14535        52970 :     for (auto &f : CONSTRUCTOR_ELTS (e))
   14536        42317 :       if (is_tu_local_value (decl, f.value, explain))
   14537              :         return true;
   14538              : 
   14539              :   return false;
   14540              : }
   14541              : 
   14542              : /* Complains if DECL is a TU-local entity imported from a named module.
   14543              :    Returns TRUE if instantiation should fail.  */
   14544              : 
   14545              : bool
   14546   9065499634 : instantiating_tu_local_entity (tree decl)
   14547              : {
   14548   9065499634 :   if (!modules_p ())
   14549              :     return false;
   14550              : 
   14551     34270951 :   if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
   14552              :     {
   14553           92 :       auto_diagnostic_group d;
   14554           92 :       error ("instantiation exposes TU-local entity %qD",
   14555           92 :              TU_LOCAL_ENTITY_NAME (decl));
   14556           92 :       inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
   14557           92 :       return true;
   14558           92 :     }
   14559              : 
   14560              :   /* Currently, only TU-local variables and functions, or possibly
   14561              :      templates thereof, will be emitted from named modules.  */
   14562     34270859 :   tree inner = STRIP_TEMPLATE (decl);
   14563     34270859 :   if (!VAR_OR_FUNCTION_DECL_P (inner))
   14564              :     return false;
   14565              : 
   14566              :   /* From this point we will only be emitting warnings; if we're not
   14567              :      warning about this case then there's no need to check further.  */
   14568      1428512 :   if (!warn_expose_global_module_tu_local
   14569      2857024 :       || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   14570      1428512 :                               OPT_Wexpose_global_module_tu_local))
   14571        11128 :     return false;
   14572              : 
   14573      1417384 :   if (!is_tu_local_entity (decl))
   14574              :     return false;
   14575              : 
   14576           65 :   if (!DECL_LANG_SPECIFIC (inner)
   14577          120 :       || !DECL_MODULE_IMPORT_P (inner))
   14578              :     return false;
   14579              : 
   14580              :   /* Referencing TU-local entities from a header is generally OK.
   14581              :      We don't have an easy way to detect if this declaration came
   14582              :      from a header via a separate named module, but we can just
   14583              :      ignore that case for warning purposes.  */
   14584            9 :   unsigned index = import_entity_index (decl);
   14585            9 :   module_state *mod = import_entity_module (index);
   14586            9 :   if (mod->is_header ())
   14587              :     return false;
   14588              : 
   14589            9 :   auto_diagnostic_group d;
   14590            9 :   pedwarn (input_location, OPT_Wexpose_global_module_tu_local,
   14591              :            "instantiation exposes TU-local entity %qD", decl);
   14592            9 :   inform (DECL_SOURCE_LOCATION (decl), "declared here");
   14593              : 
   14594              :   /* We treat TU-local entities from the GMF as not actually being
   14595              :      TU-local as an extension, so allow instantiation to proceed.  */
   14596            9 :   return false;
   14597            9 : }
   14598              : 
   14599              : /* DECL is a newly discovered dependency.  Create the depset, if it
   14600              :    doesn't already exist.  Add it to the worklist if so.
   14601              : 
   14602              :    DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
   14603              :    a using decl.
   14604              : 
   14605              :    We do not have to worry about adding the same dependency more than
   14606              :    once.  First it's harmless, but secondly the TREE_VISITED marking
   14607              :    prevents us wanting to do it anyway.  */
   14608              : 
   14609              : depset *
   14610      8326145 : depset::hash::make_dependency (tree decl, entity_kind ek)
   14611              : {
   14612              :   /* Make sure we're being told consistent information.  */
   14613     15575378 :   gcc_checking_assert ((ek == EK_NAMESPACE)
   14614              :                        == (TREE_CODE (decl) == NAMESPACE_DECL
   14615              :                            && !DECL_NAMESPACE_ALIAS (decl)));
   14616      8326145 :   gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
   14617      8326145 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   14618              :                        && (TREE_CODE (decl) != USING_DECL
   14619              :                            || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
   14620      8326145 :   gcc_checking_assert (!is_key_order ());
   14621      8326145 :   if (ek == EK_USING)
   14622        39362 :     gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
   14623      8326145 :   if (ek == EK_TU_LOCAL)
   14624           93 :     gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
   14625              : 
   14626      8326145 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14627              :     /* The template should have copied these from its result decl.  */
   14628      3184039 :     gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
   14629              :                          == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
   14630              : 
   14631      8326145 :   depset **slot = entity_slot (decl, true);
   14632      8326145 :   depset *dep = *slot;
   14633      8326145 :   bool for_binding = ek == EK_FOR_BINDING;
   14634              : 
   14635      8326145 :   if (!dep)
   14636              :     {
   14637       725753 :       if ((DECL_IMPLICIT_TYPEDEF_P (decl)
   14638              :            /* ... not an enum, for instance.  */
   14639       360294 :            && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   14640       355297 :            && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
   14641       325277 :            && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   14642      2489618 :           || (VAR_P (decl)
   14643        96738 :               && DECL_LANG_SPECIFIC (decl)
   14644        96678 :               && DECL_USE_TEMPLATE (decl) == 2))
   14645              :         {
   14646              :           /* A partial or explicit specialization. Partial
   14647              :              specializations might not be in the hash table, because
   14648              :              there can be multiple differently-constrained variants.
   14649              : 
   14650              :              template<typename T> class silly;
   14651              :              template<typename T> requires true class silly {};
   14652              : 
   14653              :              We need to find them, insert their TEMPLATE_DECL in the
   14654              :              dep_hash, and then convert the dep we just found into a
   14655              :              redirect.  */
   14656              : 
   14657        47306 :           tree ti = get_template_info (decl);
   14658        47306 :           tree tmpl = TI_TEMPLATE (ti);
   14659        47306 :           tree partial = NULL_TREE;
   14660        47306 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   14661       170634 :                spec; spec = TREE_CHAIN (spec))
   14662       148777 :             if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
   14663              :               {
   14664              :                 partial = TREE_VALUE (spec);
   14665              :                 break;
   14666              :               }
   14667              : 
   14668        47306 :           if (partial)
   14669              :             {
   14670              :               /* Eagerly create an empty redirect.  The following
   14671              :                  make_dependency call could cause hash reallocation,
   14672              :                  and invalidate slot's value.  */
   14673        25449 :               depset *redirect = make_entity (decl, EK_REDIRECT);
   14674              : 
   14675              :               /* Redirects are never reached -- always snap to their target.  */
   14676        25449 :               redirect->set_flag_bit<DB_UNREACHED_BIT> ();
   14677              : 
   14678        25449 :               *slot = redirect;
   14679              : 
   14680        25449 :               depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
   14681        25449 :               gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
   14682              : 
   14683        25449 :               redirect->deps.safe_push (tmpl_dep);
   14684              : 
   14685        25449 :               return redirect;
   14686              :             }
   14687              :         }
   14688              : 
   14689      1777796 :       bool has_def = ek != EK_USING && has_definition (decl);
   14690      1738434 :       if (ek > EK_BINDING)
   14691       154121 :         ek = EK_DECL;
   14692              : 
   14693              :       /* The only OVERLOADS we should see are USING decls from
   14694              :          bindings.  */
   14695      1777796 :       *slot = dep = make_entity (decl, ek, has_def);
   14696              : 
   14697      1777796 :       if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
   14698              :         /* The template_result should otherwise not be in the
   14699              :            table, or be an empty redirect (created above).  */
   14700       458626 :         if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
   14701        25449 :           gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
   14702              :                                && !(*eslot)->deps.length ());
   14703              : 
   14704      1777796 :       if (ignore_exposure)
   14705        45522 :         dep->set_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14706              : 
   14707      1777796 :       if (ek != EK_USING)
   14708              :         {
   14709      1738434 :           tree not_tmpl = STRIP_TEMPLATE (decl);
   14710      1738434 :           bool imported_from_module_p = false;
   14711              : 
   14712      1738434 :           if (DECL_LANG_SPECIFIC (not_tmpl)
   14713      3322086 :               && DECL_MODULE_IMPORT_P (not_tmpl))
   14714              :             {
   14715              :               /* Store the module number and index in cluster/section,
   14716              :                  so we don't have to look them up again.  */
   14717        89172 :               unsigned index = import_entity_index (decl);
   14718        89172 :               module_state *from = import_entity_module (index);
   14719              :               /* Remap will be zero for imports from partitions, which
   14720              :                  we want to treat as-if declared in this TU.  */
   14721        89172 :               if (from->remap)
   14722              :                 {
   14723        88415 :                   dep->cluster = index - from->entity_lwm;
   14724        88415 :                   dep->section = from->remap;
   14725        88415 :                   dep->set_flag_bit<DB_IMPORTED_BIT> ();
   14726              : 
   14727        88415 :                   if (!from->is_header ())
   14728      1738434 :                     imported_from_module_p = true;
   14729              :                 }
   14730              :             }
   14731              : 
   14732              :           /* Check for TU-local entities.  This is unnecessary in header
   14733              :              units because we can export internal-linkage decls, and
   14734              :              no declarations are exposures.  Similarly, if the decl was
   14735              :              imported from a non-header module we know it cannot have
   14736              :              been TU-local.  */
   14737      1738434 :           if (!header_module_p () && !imported_from_module_p)
   14738              :             {
   14739       813883 :               if (is_tu_local_entity (decl))
   14740          305 :                 dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
   14741              : 
   14742       813883 :               if (VAR_P (decl)
   14743        42849 :                   && decl_maybe_constant_var_p (decl)
   14744       855476 :                   && is_tu_local_value (decl, DECL_INITIAL (decl)))
   14745              :                 {
   14746              :                   /* A potentially-constant variable initialized to a TU-local
   14747              :                      value is not usable in constant expressions within other
   14748              :                      translation units.  We can achieve this by simply not
   14749              :                      streaming the definition in such cases.  */
   14750           24 :                   dep->clear_flag_bit<DB_DEFN_BIT> ();
   14751              : 
   14752           24 :                   if (DECL_DECLARED_CONSTEXPR_P (decl)
   14753           39 :                       || DECL_INLINE_VAR_P (decl))
   14754              :                     /* A constexpr variable initialized to a TU-local value,
   14755              :                        or an inline value (PR c++/119996), is an exposure.
   14756              : 
   14757              :                        For simplicity, we don't support "non-strict" TU-local
   14758              :                        values: even if the TU-local entity we refer to in the
   14759              :                        initialiser is in the GMF, we still won't consider this
   14760              :                        valid in constant expressions in other TUs, and so
   14761              :                        complain accordingly.  */
   14762           15 :                     dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14763              :                 }
   14764              :             }
   14765              : 
   14766              :           /* A namespace-scope type may be declared in one module unit
   14767              :              and defined in another; make sure that we're found when
   14768              :              completing the class.  */
   14769      1738434 :           if (ek == EK_DECL
   14770       683883 :               && !dep->is_import ()
   14771       675328 :               && dep->has_defn ()
   14772       357301 :               && DECL_NAMESPACE_SCOPE_P (not_tmpl)
   14773       129881 :               && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
   14774              :               /* Anonymous types can't be forward-declared.  */
   14775      1772826 :               && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
   14776        33886 :             dep->set_flag_bit<DB_IS_PENDING_BIT> ();
   14777              : 
   14778              :           /* Namespace-scope functions can be found by ADL by template
   14779              :              instantiations in this module.  We need to create bindings
   14780              :              for them so that name lookup recognises they exist, if they
   14781              :              won't be discarded.  add_binding_entity is too early to do
   14782              :              this for GM functions, because if nobody ends up using them
   14783              :              we'll have leftover bindings laying around, and it's tricky
   14784              :              to delete them and any namespaces they've implicitly created
   14785              :              deps on.  The downside is this means we don't pick up on
   14786              :              using-decls, but by [module.global.frag] p3.6 we don't have
   14787              :              to.  */
   14788      1738434 :           if (ek == EK_DECL
   14789      1738434 :               && !for_binding
   14790       529774 :               && !dep->is_import ()
   14791       521225 :               && !dep->is_tu_local ()
   14792       521121 :               && DECL_NAMESPACE_SCOPE_P (decl)
   14793        60819 :               && DECL_DECLARES_FUNCTION_P (decl)
   14794              :               /* Compiler-generated functions won't participate in ADL.  */
   14795        47086 :               && !DECL_ARTIFICIAL (decl)
   14796              :               /* A hidden friend doesn't need a binding.  */
   14797      1778044 :               && !(DECL_LANG_SPECIFIC (not_tmpl)
   14798        39610 :                    && DECL_UNIQUE_FRIEND_P (not_tmpl)))
   14799              :             {
   14800              :               /* This will only affect GM functions.  */
   14801        48164 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
   14802              :                                    || !DECL_MODULE_PURVIEW_P (not_tmpl));
   14803              :               /* We shouldn't see any instantiations or specialisations.  */
   14804        24082 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   14805              :                                    || !DECL_USE_TEMPLATE (decl));
   14806              : 
   14807        24082 :               tree ns = CP_DECL_CONTEXT (decl);
   14808        24082 :               tree name = DECL_NAME (decl);
   14809        24082 :               depset *binding = find_binding (ns, name);
   14810        24082 :               if (!binding)
   14811              :                 {
   14812         7627 :                   binding = make_binding (ns, name);
   14813         7627 :                   add_namespace_context (binding, ns);
   14814              : 
   14815         7627 :                   depset **slot = binding_slot (ns, name, /*insert=*/true);
   14816         7627 :                   *slot = binding;
   14817              :                 }
   14818              : 
   14819        24082 :               binding->deps.safe_push (dep);
   14820        24082 :               dep->deps.safe_push (binding);
   14821        24117 :               dump (dumper::DEPEND)
   14822            9 :                 && dump ("Built ADL binding for %C:%N",
   14823            9 :                          TREE_CODE (decl), decl);
   14824              :             }
   14825              :         }
   14826              : 
   14827      1777796 :       if (!dep->is_import ())
   14828      1689381 :         worklist.safe_push (dep);
   14829              :     }
   14830      6522900 :   else if (!ignore_exposure)
   14831      5768574 :     dep->clear_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14832              : 
   14833      8300696 :   dump (dumper::DEPEND)
   14834        36784 :     && dump ("%s on %s %C:%N found",
   14835              :              ek == EK_REDIRECT ? "Redirect"
   14836        36784 :              : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
   14837              :              : "Dependency",
   14838        36784 :              dep->entity_kind_name (), TREE_CODE (decl), decl);
   14839              : 
   14840      8300696 :   return dep;
   14841              : }
   14842              : 
   14843              : /* Whether REF is an exposure of a member type of SOURCE.
   14844              : 
   14845              :    This comes up with exposures of class-scope lambdas, that we currently
   14846              :    treat as TU-local due to ABI reasons.  In such a case the type of the
   14847              :    lambda will be exposed in two places, first by the class type it is in
   14848              :    the TYPE_FIELDS list of, and second by the actual member declaring that
   14849              :    lambda.  We only want the second case to warn.  */
   14850              : 
   14851              : static bool
   14852          253 : is_exposure_of_member_type (depset *source, depset *ref)
   14853              : {
   14854          253 :   gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
   14855              :                        && ref->is_tu_local (/*strict=*/true));
   14856          253 :   tree source_entity = STRIP_TEMPLATE (source->get_entity ());
   14857          253 :   tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
   14858              : 
   14859          253 :   if (!source->is_tu_local (/*strict=*/true)
   14860          241 :       && source_entity
   14861          241 :       && ref_entity
   14862          241 :       && DECL_IMPLICIT_TYPEDEF_P (source_entity)
   14863            2 :       && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
   14864            2 :       && DECL_CLASS_SCOPE_P (ref_entity)
   14865          255 :       && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
   14866              :     {
   14867            4 :       gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
   14868              :       return true;
   14869              :     }
   14870              :   else
   14871              :     return false;
   14872              : }
   14873              : 
   14874              : /* DEP is a newly discovered dependency.  Append it to current's
   14875              :    depset.  */
   14876              : 
   14877              : void
   14878      6212536 : depset::hash::add_dependency (depset *dep)
   14879              : {
   14880      6212536 :   gcc_checking_assert (current && !is_key_order ());
   14881      6212536 :   current->deps.safe_push (dep);
   14882              : 
   14883      6212536 :   if (dep->is_tu_local (/*strict=*/true))
   14884              :     {
   14885          325 :       if (dep->is_tu_local ())
   14886          260 :         current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
   14887              :       else
   14888           65 :         current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
   14889              : 
   14890          325 :       if (!ignore_exposure && !is_exposure_of_member_type (current, dep))
   14891              :         {
   14892          133 :           if (dep->is_tu_local ())
   14893           91 :             current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14894              :           else
   14895           42 :             current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
   14896              :         }
   14897              :     }
   14898              : 
   14899      6212536 :   if (current->get_entity_kind () == EK_USING
   14900        39725 :       && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
   14901      6218119 :       && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
   14902              :     {
   14903              :       /* CURRENT is an unwrapped using-decl and DECL is an enum's
   14904              :          implicit typedef.  Is CURRENT a member of the enum?  */
   14905         4963 :       tree c_decl = OVL_FUNCTION (current->get_entity ());
   14906              : 
   14907         4963 :       if (TREE_CODE (c_decl) == CONST_DECL
   14908         9882 :           && (current->deps[0]->get_entity ()
   14909         4919 :               == CP_DECL_CONTEXT (dep->get_entity ())))
   14910              :         /* Make DECL depend on CURRENT.  */
   14911         4865 :         dep->deps.safe_push (current);
   14912              :     }
   14913              : 
   14914              :   /* If two dependencies recursively depend on each other existing within
   14915              :      their own merge keys, we must ensure that the first dep we saw while
   14916              :      walking is written first in this cluster.  See sort_cluster for more
   14917              :      details.  */
   14918      6212536 :   if (writing_merge_key)
   14919              :     {
   14920      1119206 :       if (!dep->is_maybe_recursive () && !current->is_maybe_recursive ())
   14921        59890 :         current->set_flag_bit<DB_ENTRY_BIT> ();
   14922      1119206 :       dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14923      1119206 :       current->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14924              :     }
   14925              : 
   14926      6212536 :   if (dep->is_unreached ())
   14927              :     {
   14928              :       /* The dependency is reachable now.  */
   14929       455826 :       reached_unreached = true;
   14930       455826 :       dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   14931       455826 :       dump (dumper::DEPEND)
   14932           30 :         && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
   14933           30 :                  TREE_CODE (dep->get_entity ()), dep->get_entity ());
   14934              :     }
   14935      6212536 : }
   14936              : 
   14937              : depset *
   14938      9181451 : depset::hash::add_dependency (tree decl, entity_kind ek)
   14939              : {
   14940      9181451 :   depset *dep;
   14941              : 
   14942      9181451 :   if (is_key_order ())
   14943              :     {
   14944      2908438 :       dep = find_dependency (decl);
   14945      2908438 :       if (dep)
   14946              :         {
   14947      1315526 :           current->deps.safe_push (dep);
   14948      1315526 :           dump (dumper::MERGE)
   14949          723 :             && dump ("Key dependency on %s %C:%N found",
   14950          723 :                      dep->entity_kind_name (), TREE_CODE (decl), decl);
   14951              :         }
   14952              :       else
   14953              :         {
   14954              :           /* It's not a mergeable decl, look for it in the original
   14955              :              table.  */
   14956      1592912 :           dep = chain->find_dependency (decl);
   14957      1592912 :           gcc_checking_assert (dep);
   14958              :         }
   14959              :     }
   14960              :   else
   14961              :     {
   14962      6273013 :       dep = make_dependency (decl, ek);
   14963      6273013 :       if (dep->get_entity_kind () != EK_REDIRECT)
   14964      6212536 :         add_dependency (dep);
   14965              :     }
   14966              : 
   14967      9181451 :   return dep;
   14968              : }
   14969              : 
   14970              : void
   14971       712515 : depset::hash::add_namespace_context (depset *dep, tree ns)
   14972              : {
   14973       712515 :   depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
   14974       712515 :   dep->deps.safe_push (ns_dep);
   14975              : 
   14976              :   /* Mark it as special if imported so we don't walk connect when
   14977              :      SCCing.  */
   14978       712515 :   if (!dep->is_binding () && ns_dep->is_import ())
   14979            0 :     dep->set_special ();
   14980       712515 : }
   14981              : 
   14982              : struct add_binding_data
   14983              : {
   14984              :   tree ns;
   14985              :   bitmap partitions;
   14986              :   depset *binding;
   14987              :   depset::hash *hash;
   14988              :   bool met_namespace;
   14989              : };
   14990              : 
   14991              : /* Return true if we are, or contain something that is exported.  */
   14992              : 
   14993              : bool
   14994      5881286 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
   14995              : {
   14996      5881286 :   auto data = static_cast <add_binding_data *> (data_);
   14997      5881286 :   decl = strip_using_decl (decl);
   14998              : 
   14999      5881286 :   if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
   15000              :     {
   15001      5872723 :       tree inner = decl;
   15002              : 
   15003      5872723 :       if (TREE_CODE (inner) == CONST_DECL
   15004         9661 :           && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
   15005              :           /* A using-decl could make a CONST_DECL purview for a non-purview
   15006              :              enumeration.  */
   15007      5882384 :           && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
   15008         9620 :         inner = TYPE_NAME (DECL_CONTEXT (inner));
   15009      5863103 :       else if (TREE_CODE (inner) == TEMPLATE_DECL)
   15010       141364 :         inner = DECL_TEMPLATE_RESULT (inner);
   15011              : 
   15012     11560560 :       if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
   15013     11385783 :           && !((flags & WMB_Using) && (flags & WMB_Purview)))
   15014              :         /* Ignore entities not within the module purview.  We'll need to
   15015              :            create bindings for any non-discarded function calls for ADL,
   15016              :            but it's simpler to handle that at the point of use rather
   15017              :            than trying to clear out bindings after the fact.  */
   15018              :         return false;
   15019              : 
   15020       198032 :       if ((flags & WMB_Hidden)
   15021         5513 :           && DECL_LANG_SPECIFIC (inner)
   15022       203545 :           && DECL_UNIQUE_FRIEND_P (inner))
   15023              :         /* Hidden friends will be found via ADL on the class type,
   15024              :            and so do not need to have bindings.  Anticipated builtin
   15025              :            functions and the hidden decl underlying a DECL_LOCAL_DECL_P
   15026              :            also don't need exporting, but we should create a binding
   15027              :            anyway so that we can have a common decl to match against.  */
   15028              :         return false;
   15029              : 
   15030       192608 :       bool internal_decl = false;
   15031       192608 :       if (!header_module_p () && is_tu_local_entity (decl)
   15032       192872 :           && !((flags & WMB_Using) && (flags & WMB_Export)))
   15033              :         {
   15034              :           /* A TU-local entity.  For ADL we still need to create bindings
   15035              :              for internal-linkage functions attached to a named module.  */
   15036          153 :           if (DECL_DECLARES_FUNCTION_P (inner)
   15037          105 :               && DECL_LANG_SPECIFIC (inner)
   15038          363 :               && DECL_MODULE_ATTACH_P (inner))
   15039              :             {
   15040           93 :               gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
   15041              :               internal_decl = true;
   15042              :             }
   15043              :           else
   15044              :             return false;
   15045              :         }
   15046              : 
   15047       192443 :       if ((TREE_CODE (decl) == VAR_DECL
   15048       192443 :            || TREE_CODE (decl) == TYPE_DECL)
   15049       192443 :           && DECL_TINFO_P (decl))
   15050              :         /* Ignore TINFO things.  */
   15051              :         return false;
   15052              : 
   15053       192443 :       if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
   15054              :         /* Ignore NTTP objects.  */
   15055              :         return false;
   15056              : 
   15057       192443 :       if (deduction_guide_p (decl))
   15058              :         {
   15059              :           /* Ignore deduction guides, bindings for them will be created within
   15060              :              find_dependencies for their class template.  But still build a dep
   15061              :              for them so that we don't discard them.  */
   15062         1616 :           data->hash->make_dependency (decl, EK_FOR_BINDING);
   15063         1616 :           return false;
   15064              :         }
   15065              : 
   15066       190827 :       if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
   15067              :         {
   15068              :           /* An unscoped enum constant implicitly brought into the containing
   15069              :              namespace.  We treat this like a using-decl.  */
   15070         4085 :           gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
   15071              : 
   15072         4085 :           flags = WMB_Flags (flags | WMB_Using);
   15073         4085 :           if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
   15074              :               /* A using-decl can make an enum constant exported for a
   15075              :                  non-exported enumeration.  */
   15076         4085 :               || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
   15077         3854 :             flags = WMB_Flags (flags | WMB_Export);
   15078              :         }
   15079              : 
   15080       190827 :       if (!data->binding)
   15081              :         /* No binding to check.  */;
   15082        41567 :       else if (flags & WMB_Using)
   15083              :         {
   15084              :           /* Look in the binding to see if we already have this
   15085              :              using.  */
   15086       160268 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15087              :             {
   15088       129477 :               depset *d = data->binding->deps[ix];
   15089       258954 :               if (d->get_entity_kind () == EK_USING
   15090       129477 :                   && OVL_FUNCTION (d->get_entity ()) == decl)
   15091              :                 {
   15092            3 :                   if (!(flags & WMB_Hidden))
   15093            3 :                     d->clear_hidden_binding ();
   15094            3 :                   OVL_PURVIEW_P (d->get_entity ()) = true;
   15095            3 :                   if (flags & WMB_Export)
   15096            3 :                     OVL_EXPORT_P (d->get_entity ()) = true;
   15097            3 :                   return bool (flags & WMB_Export);
   15098              :                 }
   15099              :             }
   15100              :         }
   15101        26170 :       else if (flags & WMB_Dups)
   15102              :         {
   15103              :           /* Look in the binding to see if we already have this decl.  */
   15104           78 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15105              :             {
   15106           39 :               depset *d = data->binding->deps[ix];
   15107           39 :               if (d->get_entity () == decl)
   15108              :                 {
   15109           33 :                   if (!(flags & WMB_Hidden))
   15110           33 :                     d->clear_hidden_binding ();
   15111           33 :                   return false;
   15112              :                 }
   15113              :             }
   15114              :         }
   15115              : 
   15116              :       /* We're adding something.  */
   15117       190791 :       if (!data->binding)
   15118              :         {
   15119       149260 :           data->binding = make_binding (data->ns, DECL_NAME (decl));
   15120       149260 :           data->hash->add_namespace_context (data->binding, data->ns);
   15121              : 
   15122       149260 :           depset **slot = data->hash->binding_slot (data->ns,
   15123       149260 :                                                     DECL_NAME (decl), true);
   15124       149260 :           gcc_checking_assert (!*slot);
   15125       149260 :           *slot = data->binding;
   15126              :         }
   15127              : 
   15128              :       /* Make sure nobody left a tree visited lying about.  */
   15129       190791 :       gcc_checking_assert (!TREE_VISITED (decl));
   15130              : 
   15131       190791 :       if (flags & WMB_Using)
   15132              :         {
   15133        39362 :           decl = ovl_make (decl, NULL_TREE);
   15134        39362 :           OVL_USING_P (decl) = true;
   15135        39362 :           OVL_PURVIEW_P (decl) = true;
   15136        39362 :           if (flags & WMB_Export)
   15137        38272 :             OVL_EXPORT_P (decl) = true;
   15138              :         }
   15139              : 
   15140       190791 :       entity_kind ek = EK_FOR_BINDING;
   15141       190791 :       if (internal_decl)
   15142              :         ek = EK_TU_LOCAL;
   15143       190698 :       else if (flags & WMB_Using)
   15144        39362 :         ek = EK_USING;
   15145              : 
   15146       190791 :       depset *dep = data->hash->make_dependency (decl, ek);
   15147       190791 :       if (flags & WMB_Hidden)
   15148           89 :         dep->set_hidden_binding ();
   15149       190791 :       data->binding->deps.safe_push (dep);
   15150              :       /* Binding and contents are mutually dependent.  */
   15151       190791 :       dep->deps.safe_push (data->binding);
   15152              : 
   15153       190791 :       return (flags & WMB_Using
   15154       190791 :               ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
   15155              :     }
   15156         8563 :   else if (!data->met_namespace)
   15157              :     {
   15158              :       /* Namespace, walk exactly once.  */
   15159         8554 :       data->met_namespace = true;
   15160         8554 :       if (data->hash->add_namespace_entities (decl, data->partitions))
   15161              :         {
   15162              :           /* It contains an exported thing, so it is exported.  */
   15163         1707 :           gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
   15164         1707 :           gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
   15165         1707 :           DECL_MODULE_EXPORT_P (decl) = true;
   15166              :         }
   15167              : 
   15168         8554 :       if (DECL_MODULE_PURVIEW_P (decl))
   15169              :         {
   15170         2075 :           data->hash->make_dependency (decl, depset::EK_NAMESPACE);
   15171              : 
   15172         2075 :           return DECL_MODULE_EXPORT_P (decl);
   15173              :         }
   15174              :     }
   15175              : 
   15176              :   return false;
   15177              : }
   15178              : 
   15179              : /* Recursively find all the namespace bindings of NS.  Add a depset
   15180              :    for every binding that contains an export or module-linkage entity.
   15181              :    Add a defining depset for every such decl that we need to write a
   15182              :    definition.  Such defining depsets depend on the binding depset.
   15183              :    Returns true if we contain something exported.  */
   15184              : 
   15185              : bool
   15186        11355 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   15187              : {
   15188        12567 :   dump () && dump ("Looking for writables in %N", ns);
   15189        11355 :   dump.indent ();
   15190              : 
   15191        11355 :   unsigned count = 0;
   15192        11355 :   add_binding_data data;
   15193        11355 :   data.ns = ns;
   15194        11355 :   data.partitions = partitions;
   15195        11355 :   data.hash = this;
   15196              : 
   15197     15306717 :   for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
   15198              :     {
   15199      7647681 :       data.binding = nullptr;
   15200      7647681 :       data.met_namespace = false;
   15201      7647681 :       if (walk_module_binding (binding, partitions, add_binding_entity, &data))
   15202       141475 :         count++;
   15203              :     }
   15204              : 
   15205              :   /* Seed any using-directives so that we emit the relevant namespaces.  */
   15206        11985 :   for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
   15207          216 :     if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
   15208              :       {
   15209              :         /* Unless it's a (TU-local) anonymous namespace.
   15210              : 
   15211              :            FIXME instead of checking here, they should be
   15212              :            is_tu_local_entity.  */
   15213          181 :         if (!TREE_PUBLIC (USING_DECL_DECLS (udir)))
   15214           73 :           continue;
   15215          108 :         make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
   15216          108 :         if (DECL_MODULE_EXPORT_P (udir))
   15217           79 :           count++;
   15218              :       }
   15219              : 
   15220        11355 :   if (count)
   15221         4026 :     dump () && dump ("Found %u entries", count);
   15222        11355 :   dump.outdent ();
   15223              : 
   15224        11355 :   return count != 0;
   15225              : }
   15226              : 
   15227              : void
   15228          215 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
   15229              : {
   15230        21643 :   for (unsigned ix = 0; ix != partial_classes->length (); ix++)
   15231              :     {
   15232        21428 :       tree inner = (*partial_classes)[ix];
   15233              : 
   15234        21428 :       depset *dep = make_dependency (inner, depset::EK_DECL);
   15235              : 
   15236        21428 :       if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15237              :         {
   15238        21428 :           dep = dep->deps[0];
   15239              :           /* We should have recorded the template as a partial
   15240              :              specialization.  */
   15241        21428 :           gcc_checking_assert (dep->get_entity_kind ()
   15242              :                                == depset::EK_PARTIAL);
   15243              : 
   15244              :           /* Only emit GM entities if reached.  */
   15245        21428 :           if (!DECL_LANG_SPECIFIC (inner)
   15246        33972 :               || !DECL_MODULE_PURVIEW_P (inner))
   15247         9757 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15248              :         }
   15249              :       else
   15250              :         {
   15251              :           /* It was an explicit specialization, not a partial one.
   15252              :              We should have already added this.  */
   15253            0 :           gcc_checking_assert (dep->get_entity_kind ()
   15254              :                                == depset::EK_SPECIALIZATION);
   15255            0 :           gcc_checking_assert (dep->is_special ());
   15256              :         }
   15257              :     }
   15258          215 : }
   15259              : 
   15260              : /* Add the members of imported classes that we defined in this TU.
   15261              :    This will also include lazily created implicit member function
   15262              :    declarations.  (All others will be definitions.)  */
   15263              : 
   15264              : void
   15265           12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
   15266              : {
   15267           24 :   for (unsigned ix = 0; ix != class_members->length (); ix++)
   15268              :     {
   15269           12 :       tree defn = (*class_members)[ix];
   15270           12 :       depset *dep = make_dependency (defn, EK_INNER_DECL);
   15271              : 
   15272           12 :       if (dep->get_entity_kind () == EK_REDIRECT)
   15273            0 :         dep = dep->deps[0];
   15274              : 
   15275              :       /* Only non-instantiations need marking as pendings.  */
   15276           24 :       if (dep->get_entity_kind () == EK_DECL)
   15277           12 :         dep->set_flag_bit <DB_IS_PENDING_BIT> ();
   15278              :     }
   15279           12 : }
   15280              : 
   15281              : /* Add any entities found via dependent ADL.  */
   15282              : 
   15283              : void
   15284      7852222 : depset::hash::add_dependent_adl_entities (tree expr)
   15285              : {
   15286      7852222 :   gcc_checking_assert (!is_key_order ());
   15287              : 
   15288              :   /* This is not needed for header units where everything is
   15289              :      visible to name lookup, nothing is discarded.  */
   15290      7852222 :   if (header_module_p ())
   15291      7816052 :     return;
   15292              : 
   15293      3268473 :   if (TREE_CODE (current->get_entity ()) != TEMPLATE_DECL)
   15294              :     return;
   15295              : 
   15296      1847588 :   dep_adl_info info;
   15297      1847588 :   auto_vec<tree, 3> args;
   15298      1847588 :   switch (TREE_CODE (expr))
   15299              :     {
   15300       196361 :     case CALL_EXPR:
   15301       196361 :       if (!KOENIG_LOOKUP_P (expr)
   15302       196361 :           || !type_dependent_expression_p_push (expr))
   15303       189491 :         return;
   15304         6870 :       info.name = CALL_EXPR_FN (expr);
   15305         6870 :       if (!info.name)
   15306              :         return;
   15307         6870 :       if (TREE_CODE (info.name) == TEMPLATE_ID_EXPR)
   15308         1138 :         info.name = TREE_OPERAND (info.name, 0);
   15309         6870 :       if (TREE_CODE (info.name) == TU_LOCAL_ENTITY)
   15310              :         return;
   15311        12057 :       if (!identifier_p (info.name))
   15312         6728 :         info.name = OVL_NAME (info.name);
   15313        21949 :       for (int ix = 0; ix < call_expr_nargs (expr); ix++)
   15314        15079 :         args.safe_push (CALL_EXPR_ARG (expr, ix));
   15315              :       break;
   15316              : 
   15317        14347 :     case LE_EXPR:
   15318        14347 :     case GE_EXPR:
   15319        14347 :     case LT_EXPR:
   15320        14347 :     case GT_EXPR:
   15321        14347 :       info.rewrite = SPACESHIP_EXPR;
   15322        14347 :       goto overloadable_expr;
   15323              : 
   15324         7668 :     case NE_EXPR:
   15325         7668 :       info.rewrite = EQ_EXPR;
   15326         7668 :       goto overloadable_expr;
   15327              : 
   15328        15760 :     case EQ_EXPR:
   15329              :       /* Not strictly a rewrite candidate, but we need to ensure
   15330              :          that lookup of a matching NE_EXPR can succeed if that
   15331              :          would inhibit a rewrite with reversed parameters.  */
   15332        15760 :       info.rewrite = NE_EXPR;
   15333        15760 :       goto overloadable_expr;
   15334              : 
   15335       116371 :     case COMPOUND_EXPR:
   15336       116371 :     case MEMBER_REF:
   15337       116371 :     case MULT_EXPR:
   15338       116371 :     case TRUNC_DIV_EXPR:
   15339       116371 :     case TRUNC_MOD_EXPR:
   15340       116371 :     case PLUS_EXPR:
   15341       116371 :     case MINUS_EXPR:
   15342       116371 :     case LSHIFT_EXPR:
   15343       116371 :     case RSHIFT_EXPR:
   15344       116371 :     case SPACESHIP_EXPR:
   15345       116371 :     case BIT_AND_EXPR:
   15346       116371 :     case BIT_XOR_EXPR:
   15347       116371 :     case BIT_IOR_EXPR:
   15348       116371 :     case TRUTH_ANDIF_EXPR:
   15349       116371 :     case TRUTH_ORIF_EXPR:
   15350       116371 :     overloadable_expr:
   15351       116371 :       if (!type_dependent_expression_p_push (expr))
   15352              :         return;
   15353        84998 :       info.name = ovl_op_identifier (TREE_CODE (expr));
   15354        84998 :       gcc_checking_assert (tree_operand_length (expr) == 2);
   15355        84998 :       args.safe_push (TREE_OPERAND (expr, 0));
   15356        84998 :       args.safe_push (TREE_OPERAND (expr, 1));
   15357        84998 :       break;
   15358              : 
   15359              :     default:
   15360              :       return;
   15361              :     }
   15362              : 
   15363              :   /* If all arguments are type-dependent we don't need to do
   15364              :      anything further, we won't find new entities.  */
   15365        91868 :   bool all_type_dependent = true;
   15366       410038 :   for (tree arg : args)
   15367       170604 :     if (!type_dependent_expression_p_push (arg))
   15368              :       {
   15369              :         all_type_dependent = false;
   15370              :         break;
   15371              :       }
   15372        91868 :   if (all_type_dependent)
   15373              :     return;
   15374              : 
   15375        36170 :   gcc_checking_assert (!info.args);
   15376        36170 :   info.args = make_tree_vector ();
   15377       183916 :   for (tree arg : args)
   15378        75406 :     vec_safe_push (info.args, arg);
   15379              : 
   15380              :   /* We need to defer name lookup until after walking, otherwise
   15381              :      we get confused by stray TREE_VISITEDs.  */
   15382        36170 :   dep_adl_entity_list.safe_push (info);
   15383      1847588 : }
   15384              : 
   15385              : /* We add the partial & explicit specializations, and the explicit
   15386              :    instantiations.  */
   15387              : 
   15388              : static void
   15389      1053368 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
   15390              : {
   15391      1053368 :   vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
   15392              : 
   15393      1053368 :   if (!decl_p)
   15394              :     {
   15395              :       /* We exclusively use decls to locate things.  Make sure there's
   15396              :          no mismatch between the two specialization tables we keep.
   15397              :          pt.cc optimizes instantiation lookup using a complicated
   15398              :          heuristic.  We don't attempt to replicate that algorithm, but
   15399              :          observe its behaviour and reproduce it upon read back.  */
   15400              : 
   15401       313205 :        gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
   15402              :                            || DECL_CLASS_TEMPLATE_P (entry->tmpl));
   15403              : 
   15404       313205 :        gcc_checking_assert (!match_mergeable_specialization (true, entry));
   15405              :     }
   15406       740163 :   else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
   15407       363818 :     gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
   15408              : 
   15409      1053368 :   data->safe_push (entry);
   15410      1053368 : }
   15411              : 
   15412              : /* Arbitrary stable comparison.  */
   15413              : 
   15414              : static int
   15415     63838795 : specialization_cmp (const void *a_, const void *b_)
   15416              : {
   15417     63838795 :   const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
   15418     63838795 :   const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
   15419              : 
   15420     63838795 :   if (ea == eb)
   15421              :     return 0;
   15422              : 
   15423     63838795 :   tree a = ea->spec;
   15424     63838795 :   tree b = eb->spec;
   15425     63838795 :   if (TYPE_P (a))
   15426              :     {
   15427     18019761 :       a = TYPE_NAME (a);
   15428     18019761 :       b = TYPE_NAME (b);
   15429              :     }
   15430              : 
   15431     63838795 :   if (a == b)
   15432              :     /* This can happen with friend specializations.  Just order by
   15433              :        entry address.  See note in depset_cmp.  */
   15434            0 :     return ea < eb ? -1 : +1;
   15435              : 
   15436     63838795 :   return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
   15437              : }
   15438              : 
   15439              : /* We add all kinds of specialializations.  Implicit specializations
   15440              :    should only streamed and walked if they are reachable from
   15441              :    elsewhere.  Hence the UNREACHED flag.  This is making the
   15442              :    assumption that it is cheaper to reinstantiate them on demand
   15443              :    elsewhere, rather than stream them in when we instantiate their
   15444              :    general template.  Also, if we do stream them, we can only do that
   15445              :    if they are not internal (which they can become if they themselves
   15446              :    touch an internal entity?).  */
   15447              : 
   15448              : void
   15449         5602 : depset::hash::add_specializations (bool decl_p)
   15450              : {
   15451         5602 :   vec<spec_entry *> data;
   15452         5602 :   data.create (100);
   15453         5602 :   walk_specializations (decl_p, specialization_add, &data);
   15454         5602 :   data.qsort (specialization_cmp);
   15455      1058970 :   while (data.length ())
   15456              :     {
   15457      1053368 :       spec_entry *entry = data.pop ();
   15458      1053368 :       tree spec = entry->spec;
   15459      1053368 :       int use_tpl = 0;
   15460      1053368 :       bool is_friend = false;
   15461              : 
   15462      1053368 :       if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
   15463              :         /* A friend of a template.  This is keyed to the
   15464              :            instantiation.  */
   15465              :         is_friend = true;
   15466              : 
   15467      1053368 :       if (decl_p)
   15468              :         {
   15469       740163 :           if (tree ti = DECL_TEMPLATE_INFO (spec))
   15470              :             {
   15471       740163 :               tree tmpl = TI_TEMPLATE (ti);
   15472              : 
   15473       740163 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15474       740163 :               if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15475              :                 {
   15476         5480 :                   spec = tmpl;
   15477         5480 :                   gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
   15478              :                 }
   15479       734683 :               else if (is_friend)
   15480              :                 {
   15481         4998 :                   if (TI_TEMPLATE (ti) != entry->tmpl
   15482         4998 :                       || !template_args_equal (TI_ARGS (ti), entry->tmpl))
   15483         4998 :                     goto template_friend;
   15484              :                 }
   15485              :             }
   15486              :           else
   15487              :             {
   15488            0 :             template_friend:;
   15489         4998 :               gcc_checking_assert (is_friend);
   15490              :               /* This is a friend of a template class, but not the one
   15491              :                  that generated entry->spec itself (i.e. it's an
   15492              :                  equivalent clone).  We do not need to record
   15493              :                  this.  */
   15494         4998 :               continue;
   15495              :             }
   15496              :         }
   15497              :       else
   15498              :         {
   15499       313205 :           if (TREE_CODE (spec) == ENUMERAL_TYPE)
   15500              :             {
   15501         1489 :               tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
   15502              : 
   15503         1489 :               if (TYPE_P (ctx))
   15504         1483 :                 use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
   15505              :               else
   15506            6 :                 use_tpl = DECL_USE_TEMPLATE (ctx);
   15507              :             }
   15508              :           else
   15509       311716 :             use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
   15510              : 
   15511       313205 :           tree ti = TYPE_TEMPLATE_INFO (spec);
   15512       313205 :           tree tmpl = TI_TEMPLATE (ti);
   15513              : 
   15514       313205 :           spec = TYPE_NAME (spec);
   15515       313205 :           if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15516              :             {
   15517         1585 :               spec = tmpl;
   15518         1585 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15519              :             }
   15520              :         }
   15521              : 
   15522      1048370 :       bool needs_reaching = false;
   15523      1048370 :       if (use_tpl == 1)
   15524              :         /* Implicit instantiations only walked if we reach them.  */
   15525              :         needs_reaching = true;
   15526        83847 :       else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
   15527       150793 :                || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
   15528              :         /* Likewise, GMF explicit or partial specializations.  */
   15529              :         needs_reaching = true;
   15530              : 
   15531              : #if false && CHECKING_P
   15532              :       /* The instantiation isn't always on
   15533              :          DECL_TEMPLATE_INSTANTIATIONS, */
   15534              :       // FIXME: we probably need to remember this information?
   15535              :       /* Verify the specialization is on the
   15536              :          DECL_TEMPLATE_INSTANTIATIONS of the template.  */
   15537              :       for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
   15538              :            cons; cons = TREE_CHAIN (cons))
   15539              :         if (TREE_VALUE (cons) == entry->spec)
   15540              :           {
   15541              :             gcc_assert (entry->args == TREE_PURPOSE (cons));
   15542              :             goto have_spec;
   15543              :           }
   15544              :       gcc_unreachable ();
   15545              :     have_spec:;
   15546              : #endif
   15547              : 
   15548              :       /* Make sure nobody left a tree visited lying about.  */
   15549      1048370 :       gcc_checking_assert (!TREE_VISITED (spec));
   15550      1048370 :       depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
   15551      1048370 :       if (dep->is_special ())
   15552            0 :         gcc_unreachable ();
   15553              :       else
   15554              :         {
   15555      1048370 :           if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15556        24529 :             dep = dep->deps[0];
   15557      1023841 :           else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
   15558              :             {
   15559      1023841 :               dep->set_special ();
   15560      1023841 :               dep->deps.safe_push (reinterpret_cast<depset *> (entry));
   15561      1023841 :               if (!decl_p)
   15562       292697 :                 dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
   15563              :             }
   15564              : 
   15565      1048370 :           if (needs_reaching)
   15566      1003213 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15567      1048370 :           if (is_friend)
   15568            0 :             dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
   15569              :         }
   15570              :     }
   15571         5602 :   data.release ();
   15572         5602 : }
   15573              : 
   15574              : /* Add a depset into the mergeable hash.  */
   15575              : 
   15576              : void
   15577      1192838 : depset::hash::add_mergeable (depset *mergeable)
   15578              : {
   15579      1192838 :   gcc_checking_assert (is_key_order ());
   15580      1192838 :   entity_kind ek = mergeable->get_entity_kind ();
   15581      1192838 :   tree decl = mergeable->get_entity ();
   15582      1192838 :   gcc_checking_assert (ek < EK_DIRECT_HWM);
   15583              : 
   15584      1192838 :   depset **slot = entity_slot (decl, true);
   15585      1192838 :   gcc_checking_assert (!*slot);
   15586      1192838 :   depset *dep = make_entity (decl, ek);
   15587      1192838 :   *slot = dep;
   15588              : 
   15589      1192838 :   worklist.safe_push (dep);
   15590              : 
   15591              :   /* So we can locate the mergeable depset this depset refers to,
   15592              :      mark the first dep.  */
   15593      1192838 :   dep->set_special ();
   15594      1192838 :   dep->deps.safe_push (mergeable);
   15595      1192838 : }
   15596              : 
   15597              : /* Find the innermost-namespace scope of DECL, and that
   15598              :    namespace-scope decl.  */
   15599              : 
   15600              : tree
   15601     38936330 : find_pending_key (tree decl, tree *decl_p = nullptr)
   15602              : {
   15603     38936330 :   tree ns = decl;
   15604     47131023 :   do
   15605              :     {
   15606     47131023 :       decl = ns;
   15607     47131023 :       ns = CP_DECL_CONTEXT (ns);
   15608     47131023 :       if (TYPE_P (ns))
   15609      5075096 :         ns = TYPE_NAME (ns);
   15610              :     }
   15611     47131023 :   while (TREE_CODE (ns) != NAMESPACE_DECL);
   15612              : 
   15613     38936330 :   if (decl_p)
   15614     38384305 :     *decl_p = decl;
   15615              : 
   15616     38936330 :   return ns;
   15617              : }
   15618              : 
   15619              : /* Creates bindings and dependencies for all deduction guides of
   15620              :    the given class template DECL as needed.  */
   15621              : 
   15622              : void
   15623        56108 : depset::hash::add_deduction_guides (tree decl)
   15624              : {
   15625              :   /* Alias templates never have deduction guides.  */
   15626        56108 :   if (DECL_ALIAS_TEMPLATE_P (decl))
   15627        55123 :     return;
   15628              : 
   15629              :   /* We don't need to do anything for class-scope deduction guides,
   15630              :      as they will be added as members anyway.  */
   15631        56108 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   15632              :     return;
   15633              : 
   15634        43380 :   tree ns = CP_DECL_CONTEXT (decl);
   15635        43380 :   tree name = dguide_name (decl);
   15636              : 
   15637              :   /* We always add all deduction guides with a given name at once,
   15638              :      so if there's already a binding there's nothing to do.  */
   15639        43380 :   if (find_binding (ns, name))
   15640              :     return;
   15641              : 
   15642        40373 :   tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
   15643              :                                        /*complain=*/false);
   15644        40373 :   if (guides == error_mark_node)
   15645              :     return;
   15646              : 
   15647          985 :   depset *binding = nullptr;
   15648         4743 :   for (tree t : lkp_range (guides))
   15649              :     {
   15650         2773 :       gcc_checking_assert (!TREE_VISITED (t));
   15651         2773 :       depset *dep = make_dependency (t, EK_FOR_BINDING);
   15652              : 
   15653              :       /* We don't want to create bindings for imported deduction guides, as
   15654              :          this would potentially cause name lookup to return duplicates.  */
   15655         2773 :       if (dep->is_import ())
   15656            6 :         continue;
   15657              : 
   15658         2767 :       if (!binding)
   15659              :         {
   15660              :           /* We have bindings to add.  */
   15661          979 :           binding = make_binding (ns, name);
   15662          979 :           add_namespace_context (binding, ns);
   15663              : 
   15664          979 :           depset **slot = binding_slot (ns, name, /*insert=*/true);
   15665          979 :           *slot = binding;
   15666              :         }
   15667              : 
   15668         2767 :       binding->deps.safe_push (dep);
   15669         2767 :       dep->deps.safe_push (binding);
   15670         2767 :       dump (dumper::DEPEND)
   15671            0 :         && dump ("Built binding for deduction guide %C:%N",
   15672            0 :                  TREE_CODE (decl), decl);
   15673              :     }
   15674              : }
   15675              : 
   15676              : /* Iteratively find dependencies.  During the walk we may find more
   15677              :    entries on the same binding that need walking.  */
   15678              : 
   15679              : void
   15680       316080 : depset::hash::find_dependencies (module_state *module)
   15681              : {
   15682       316080 :   trees_out walker (NULL, module, *this);
   15683       316080 :   vec<depset *> unreached;
   15684       632160 :   unreached.create (worklist.length ());
   15685              : 
   15686         1135 :   for (;;)
   15687              :     {
   15688       317215 :       reached_unreached = false;
   15689      5403418 :       while (worklist.length ())
   15690              :         {
   15691      5086203 :           depset *item = worklist.pop ();
   15692              : 
   15693      5086203 :           gcc_checking_assert (!item->is_binding ());
   15694      5086203 :           if (item->is_unreached ())
   15695      2655287 :             unreached.quick_push (item);
   15696              :           else
   15697              :             {
   15698      2430916 :               current = item;
   15699      2430916 :               tree decl = current->get_entity ();
   15700      2430916 :               dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
   15701      2432239 :                 && dump ("Dependencies of %s %C:%N",
   15702         1323 :                          is_key_order () ? "key-order"
   15703         1323 :                          : current->entity_kind_name (), TREE_CODE (decl), decl);
   15704      2430916 :               dump.indent ();
   15705      2430916 :               walker.begin ();
   15706      2430916 :               if (current->get_entity_kind () == EK_USING)
   15707        39362 :                 walker.tree_node (OVL_FUNCTION (decl));
   15708      2391554 :               else if (current->get_entity_kind () == EK_TU_LOCAL)
   15709              :                 /* We only stream its name and location.  */
   15710           93 :                 module->note_location (DECL_SOURCE_LOCATION (decl));
   15711      2391461 :               else if (TREE_VISITED (decl))
   15712              :                 /* A global tree.  */;
   15713      2388917 :               else if (current->get_entity_kind () == EK_NAMESPACE)
   15714              :                 {
   15715         2624 :                   module->note_location (DECL_SOURCE_LOCATION (decl));
   15716         2624 :                   add_namespace_context (current, CP_DECL_CONTEXT (decl));
   15717              :                 }
   15718              :               else
   15719              :                 {
   15720      2386293 :                   walker.mark_declaration (decl, current->has_defn ());
   15721              : 
   15722      2386293 :                   if (!is_key_order ()
   15723      2386293 :                       && item->is_pending_entity ())
   15724              :                     {
   15725       552025 :                       tree ns = find_pending_key (decl, nullptr);
   15726       552025 :                       add_namespace_context (item, ns);
   15727              :                     }
   15728              : 
   15729      2386293 :                   auto ovr = make_temp_override
   15730      2386293 :                     (ignore_exposure, item->is_ignored_exposure_context ());
   15731      2386293 :                   walker.decl_value (decl, current);
   15732      2386293 :                   if (current->has_defn ())
   15733       460384 :                     walker.write_definition (decl, current->refs_tu_local ());
   15734      2386293 :                 }
   15735      2430916 :               walker.end ();
   15736              : 
   15737              :               /* If we see either a class template or a deduction guide, make
   15738              :                  sure to add all visible deduction guides.  We need to check
   15739              :                  both in case they have been added in separate modules, or
   15740              :                  one is in the GMF and would have otherwise been discarded.  */
   15741      2430916 :               if (!is_key_order ()
   15742      2430916 :                   && DECL_CLASS_TEMPLATE_P (decl))
   15743        53335 :                 add_deduction_guides (decl);
   15744      2430916 :               if (!is_key_order ()
   15745      2430916 :                   && deduction_guide_p (decl))
   15746         2773 :                 add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
   15747              : 
   15748              :               /* Handle dependent ADL for [module.global.frag] p3.3.  */
   15749      2430916 :               if (!is_key_order () && !dep_adl_entity_list.is_empty ())
   15750              :                 {
   15751        19296 :                   processing_template_decl_sentinel ptds;
   15752        19296 :                   ++processing_template_decl;
   15753        55466 :                   for (auto &info : dep_adl_entity_list)
   15754              :                     {
   15755        36170 :                       tree lookup = lookup_arg_dependent (info.name, NULL_TREE,
   15756              :                                                           info.args, true);
   15757       109740 :                       for (tree fn : lkp_range (lookup))
   15758              :                         /* We don't need to add_dependency, just have
   15759              :                            make_dependency build an ADL binding.  */
   15760        37400 :                         make_dependency (fn, EK_DECL);
   15761              : 
   15762        36170 :                       if (info.rewrite)
   15763              :                         {
   15764         8435 :                           tree rewrite_name = ovl_op_identifier (info.rewrite);
   15765         8435 :                           lookup = lookup_arg_dependent (rewrite_name, NULL_TREE,
   15766              :                                                          info.args, true);
   15767        27465 :                           for (tree fn : lkp_range (lookup))
   15768        10595 :                             make_dependency (fn, EK_DECL);
   15769              :                         }
   15770        36170 :                       release_tree_vector (info.args);
   15771              :                     }
   15772        19296 :                   dep_adl_entity_list.truncate (0);
   15773        19296 :                 }
   15774              : 
   15775      2430916 :               if (!is_key_order ()
   15776      1238078 :                   && TREE_CODE (decl) == TEMPLATE_DECL
   15777      2861703 :                   && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   15778              :                 {
   15779              :                   /* Mark all the explicit & partial specializations as
   15780              :                      reachable.  We search both specialization lists as some
   15781              :                      constrained partial specializations for class types are
   15782              :                      only found in DECL_TEMPLATE_SPECIALIZATIONS.  */
   15783      1227802 :                   auto mark_reached = [this](tree spec)
   15784              :                     {
   15785       805322 :                       if (TYPE_P (spec))
   15786       233423 :                         spec = TYPE_NAME (spec);
   15787       805322 :                       int use_tpl;
   15788       805322 :                       node_template_info (spec, use_tpl);
   15789       805322 :                       if (use_tpl & 2)
   15790              :                         {
   15791        82861 :                           depset *spec_dep = find_dependency (spec);
   15792        82861 :                           if (spec_dep->get_entity_kind () == EK_REDIRECT)
   15793        18684 :                             spec_dep = spec_dep->deps[0];
   15794        82861 :                           if (spec_dep->is_unreached ())
   15795              :                             {
   15796        17015 :                               reached_unreached = true;
   15797        17015 :                               spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   15798        17015 :                               dump (dumper::DEPEND)
   15799            0 :                                 && dump ("Reaching unreached specialization"
   15800            0 :                                          " %C:%N", TREE_CODE (spec), spec);
   15801              :                             }
   15802              :                         }
   15803      1227802 :                     };
   15804              : 
   15805       422480 :                   for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
   15806      1208395 :                        cons; cons = TREE_CHAIN (cons))
   15807       785915 :                     mark_reached (TREE_VALUE (cons));
   15808       422480 :                   for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
   15809       441887 :                        cons; cons = TREE_CHAIN (cons))
   15810        19407 :                     mark_reached (TREE_VALUE (cons));
   15811              :                 }
   15812              : 
   15813      2430916 :               dump.outdent ();
   15814      2430916 :               current = NULL;
   15815              :             }
   15816              :         }
   15817              : 
   15818       317215 :       if (!reached_unreached)
   15819              :         break;
   15820              : 
   15821              :       /* It's possible the we reached the unreached before we
   15822              :          processed it in the above loop, so we'll be doing this an
   15823              :          extra time.  However, to avoid that we have to do some
   15824              :          bit shuffling that also involves a scan of the list.
   15825              :          Swings & roundabouts I guess.  */
   15826         1135 :       std::swap (worklist, unreached);
   15827              :     }
   15828              : 
   15829       316080 :   unreached.release ();
   15830       316080 : }
   15831              : 
   15832              : /* Compare two entries of a single binding.  TYPE_DECL before
   15833              :    non-exported before exported.  */
   15834              : 
   15835              : static int
   15836       924372 : binding_cmp (const void *a_, const void *b_)
   15837              : {
   15838       924372 :   depset *a = *(depset *const *)a_;
   15839       924372 :   depset *b = *(depset *const *)b_;
   15840              : 
   15841       924372 :   tree a_ent = a->get_entity ();
   15842       924372 :   tree b_ent = b->get_entity ();
   15843       924372 :   gcc_checking_assert (a_ent != b_ent
   15844              :                        && !a->is_binding ()
   15845              :                        && !b->is_binding ());
   15846              : 
   15847              :   /* Implicit typedefs come first.  */
   15848       924372 :   bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
   15849       924372 :   bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
   15850       924238 :   if (a_implicit || b_implicit)
   15851              :     {
   15852              :       /* A binding with two implicit type decls?  That's unpossible!  */
   15853          268 :       gcc_checking_assert (!(a_implicit && b_implicit));
   15854          402 :       return a_implicit ? -1 : +1;  /* Implicit first.  */
   15855              :     }
   15856              : 
   15857              :   /* TU-local before non-TU-local.  */
   15858       924104 :   bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
   15859       924104 :   bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
   15860       924104 :   if (a_internal != b_internal)
   15861            0 :     return a_internal ? -1 : +1;  /* Internal first.  */
   15862              : 
   15863              :   /* Hidden before non-hidden.  */
   15864       924104 :   bool a_hidden = a->is_hidden ();
   15865       924104 :   bool b_hidden = b->is_hidden ();
   15866       924104 :   if (a_hidden != b_hidden)
   15867            0 :     return a_hidden ? -1 : +1;
   15868              : 
   15869       924104 :   bool a_using = a->get_entity_kind () == depset::EK_USING;
   15870       924104 :   bool a_export;
   15871       924104 :   if (a_using)
   15872              :     {
   15873       292592 :       a_export = OVL_EXPORT_P (a_ent);
   15874       292592 :       a_ent = OVL_FUNCTION (a_ent);
   15875              :     }
   15876       631512 :   else if (TREE_CODE (a_ent) == CONST_DECL
   15877            0 :            && DECL_LANG_SPECIFIC (a_ent)
   15878       631512 :            && DECL_MODULE_EXPORT_P (a_ent))
   15879              :     a_export = true;
   15880              :   else
   15881       631512 :     a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
   15882              :                                      ? TYPE_NAME (TREE_TYPE (a_ent))
   15883              :                                      : STRIP_TEMPLATE (a_ent));
   15884              : 
   15885       924104 :   bool b_using = b->get_entity_kind () == depset::EK_USING;
   15886       924104 :   bool b_export;
   15887       924104 :   if (b_using)
   15888              :     {
   15889       306291 :       b_export = OVL_EXPORT_P (b_ent);
   15890       306291 :       b_ent = OVL_FUNCTION (b_ent);
   15891              :     }
   15892       617813 :   else if (TREE_CODE (b_ent) == CONST_DECL
   15893            0 :            && DECL_LANG_SPECIFIC (b_ent)
   15894       617813 :            && DECL_MODULE_EXPORT_P (b_ent))
   15895              :     b_export = true;
   15896              :   else
   15897       617813 :     b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
   15898              :                                      ? TYPE_NAME (TREE_TYPE (b_ent))
   15899              :                                      : STRIP_TEMPLATE (b_ent));
   15900              : 
   15901              :   /* Non-exports before exports.  */
   15902       924104 :   if (a_export != b_export)
   15903       218411 :     return a_export ? +1 : -1;
   15904              : 
   15905              :   /* At this point we don't care, but want a stable sort.  */
   15906              : 
   15907       784383 :   if (a_using != b_using)
   15908              :     /* using first.  */
   15909        24852 :     return a_using? -1 : +1;
   15910              : 
   15911       766496 :   return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
   15912              : }
   15913              : 
   15914              : /* True iff TMPL has an explicit instantiation definition.
   15915              : 
   15916              :    This is local to module.cc because register_specialization skips adding most
   15917              :    instantiations unless module_maybe_has_cmi_p.  */
   15918              : 
   15919              : static bool
   15920           76 : template_has_explicit_inst (tree tmpl)
   15921              : {
   15922           88 :   for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
   15923              :     {
   15924           24 :       tree spec = TREE_VALUE (t);
   15925           24 :       if (DECL_EXPLICIT_INSTANTIATION (spec)
   15926           24 :           && !DECL_REALLY_EXTERN (spec))
   15927              :         return true;
   15928              :     }
   15929              :   return false;
   15930              : }
   15931              : 
   15932              : /* Complain about DEP that exposes a TU-local entity.
   15933              : 
   15934              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15935              :    if we explained anything.  */
   15936              : 
   15937              : bool
   15938          127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
   15939              : {
   15940          127 :   tree decl = dep->get_entity ();
   15941              : 
   15942              :   /* Don't need to walk if we're not going to be emitting
   15943              :      any diagnostics anyway.  */
   15944          148 :   if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15945           21 :                                      OPT_Wexpose_global_module_tu_local))
   15946              :     return false;
   15947              : 
   15948          523 :   for (depset *rdep : dep->deps)
   15949          135 :     if (!rdep->is_binding () && rdep->is_tu_local (strict)
   15950          369 :         && !is_exposure_of_member_type (dep, rdep))
   15951              :       {
   15952              :         // FIXME:QOI Better location information?  We're
   15953              :         // losing, so it doesn't matter about efficiency.
   15954          118 :         tree exposed = rdep->get_entity ();
   15955          118 :         auto_diagnostic_group d;
   15956          118 :         if (strict)
   15957              :           {
   15958              :             /* Allow suppressing the warning from the point of declaration
   15959              :                of the otherwise-exposed decl, for cases we know that
   15960              :                exposures will never be 'bad'.  */
   15961           27 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
   15962           27 :                                     OPT_Wexpose_global_module_tu_local)
   15963           45 :                 && pedwarn (DECL_SOURCE_LOCATION (decl),
   15964           18 :                             OPT_Wexpose_global_module_tu_local,
   15965              :                             "%qD exposes TU-local entity %qD", decl, exposed))
   15966              :               {
   15967           18 :                 bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15968           18 :                 gcc_checking_assert (informed);
   15969              :                 return true;
   15970              :               }
   15971              :           }
   15972              :         else
   15973              :           {
   15974           91 :             error_at (DECL_SOURCE_LOCATION (decl),
   15975              :                       "%qD exposes TU-local entity %qD", decl, exposed);
   15976           91 :             bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15977           91 :             gcc_checking_assert (informed);
   15978           91 :             if (dep->is_tu_local (/*strict=*/true))
   15979            3 :               inform (DECL_SOURCE_LOCATION (decl),
   15980              :                       "%qD is also TU-local but has been exposed elsewhere",
   15981              :                       decl);
   15982           91 :             return true;
   15983              :           }
   15984          118 :       }
   15985              : 
   15986              :   return false;
   15987              : }
   15988              : 
   15989              : /* Warn about a template DEP that references a TU-local entity.
   15990              : 
   15991              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15992              :    if we explained anything.  */
   15993              : 
   15994              : bool
   15995           94 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
   15996              : {
   15997           94 :   tree decl = dep->get_entity ();
   15998              : 
   15999              :   /* Don't bother walking if we know we won't be emitting anything.  */
   16000           94 :   if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   16001           94 :                            OPT_Wtemplate_names_tu_local)
   16002              :       /* Only warn strictly if users haven't silenced this warning here.  */
   16003          121 :       || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   16004           27 :                                          OPT_Wexpose_global_module_tu_local)))
   16005            0 :     return false;
   16006              : 
   16007              :   /* Friend decls in a class body are ignored, but this is harmless:
   16008              :      it should not impact any consumers.  */
   16009           94 :   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
   16010              :     return false;
   16011              : 
   16012              :   /* We should now only be warning about templates.  */
   16013           76 :   gcc_checking_assert
   16014              :     (TREE_CODE (decl) == TEMPLATE_DECL
   16015              :      && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
   16016              : 
   16017              :   /* Don't warn if we've seen any explicit instantiation definitions,
   16018              :      the intent might be for importers to only use those.  */
   16019           76 :   if (template_has_explicit_inst (decl))
   16020              :     return false;
   16021              : 
   16022          268 :   for (depset *rdep : dep->deps)
   16023          134 :     if (!rdep->is_binding () && rdep->is_tu_local (strict))
   16024              :       {
   16025           67 :         tree ref = rdep->get_entity ();
   16026           67 :         auto_diagnostic_group d;
   16027           67 :         if (strict)
   16028              :           {
   16029           15 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
   16030           15 :                                     OPT_Wexpose_global_module_tu_local)
   16031           21 :                 && warning_at (DECL_SOURCE_LOCATION (decl),
   16032            6 :                                OPT_Wtemplate_names_tu_local,
   16033              :                                "%qD refers to TU-local entity %qD, which may "
   16034              :                                "cause issues when instantiating in other TUs",
   16035              :                                decl, ref))
   16036              :               {
   16037            6 :                 is_tu_local_entity (ref, /*explain=*/true);
   16038            6 :                 return true;
   16039              :               }
   16040              :           }
   16041           52 :         else if (warning_at (DECL_SOURCE_LOCATION (decl),
   16042           52 :                              OPT_Wtemplate_names_tu_local,
   16043              :                              "%qD refers to TU-local entity %qD and cannot "
   16044              :                              "be instantiated in other TUs", decl, ref))
   16045              :           {
   16046           52 :             is_tu_local_entity (ref, /*explain=*/true);
   16047           52 :             return true;
   16048              :           }
   16049           67 :       }
   16050              : 
   16051              :   return false;
   16052              : }
   16053              : 
   16054              : /* Sort the bindings, issue errors about bad internal refs.  */
   16055              : 
   16056              : bool
   16057         2801 : depset::hash::finalize_dependencies ()
   16058              : {
   16059         2801 :   bool ok = true;
   16060      3925023 :   for (depset *dep : *this)
   16061              :     {
   16062      1961111 :       if (dep->is_binding ())
   16063              :         {
   16064              :           /* Keep the containing namespace dep first.  */
   16065       157866 :           gcc_checking_assert (dep->deps.length () > 1
   16066              :                                && (dep->deps[0]->get_entity_kind ()
   16067              :                                    == EK_NAMESPACE)
   16068              :                                && (dep->deps[0]->get_entity ()
   16069              :                                    == dep->get_entity ()));
   16070       157866 :           if (dep->deps.length () > 2)
   16071        15008 :             gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
   16072              :                        sizeof (dep->deps[1]), binding_cmp);
   16073              : 
   16074              :           /* Bindings shouldn't refer to imported entities.  */
   16075       157866 :           if (CHECKING_P)
   16076       849104 :             for (depset *entity : dep->deps)
   16077       375506 :               gcc_checking_assert (!entity->is_import ());
   16078       157866 :           continue;
   16079       157866 :         }
   16080              : 
   16081              :       /* Otherwise, we'll check for bad internal refs.
   16082              :          Don't complain about any references from TU-local entities.  */
   16083      1803245 :       if (dep->is_tu_local ())
   16084          264 :         continue;
   16085              : 
   16086              :       /* We already complained about usings of non-external entities in
   16087              :          check_can_export_using_decl, don't do it again here.  */
   16088      1802981 :       if (dep->get_entity_kind () == EK_USING)
   16089        39362 :         continue;
   16090              : 
   16091      1763619 :       if (dep->is_exposure ())
   16092              :         {
   16093          106 :           bool explained = diagnose_bad_internal_ref (dep);
   16094              : 
   16095              :           /* A TU-local variable will always be considered an exposure,
   16096              :              so we don't have to worry about strict-only handling.  */
   16097          106 :           tree decl = dep->get_entity ();
   16098          106 :           if (!explained
   16099           15 :               && VAR_P (decl)
   16100          121 :               && (DECL_DECLARED_CONSTEXPR_P (decl)
   16101            6 :                   || DECL_INLINE_VAR_P (decl)))
   16102              :             {
   16103           15 :               auto_diagnostic_group d;
   16104           15 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   16105            9 :                 error_at (DECL_SOURCE_LOCATION (decl),
   16106              :                           "%qD is declared %<constexpr%> and is initialized to "
   16107              :                           "a TU-local value", decl);
   16108              :               else
   16109              :                 {
   16110              :                   /* This can only occur with references.  */
   16111            6 :                   gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
   16112            6 :                   error_at (DECL_SOURCE_LOCATION (decl),
   16113              :                             "%qD is a reference declared %<inline%> and is "
   16114              :                             "constant-initialized to a TU-local value", decl);
   16115              :                 }
   16116           15 :               bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
   16117              :                                                  /*explain=*/true);
   16118           15 :               gcc_checking_assert (informed);
   16119           15 :               explained = true;
   16120           15 :             }
   16121              : 
   16122              :           /* We should have emitted an error above, unless the warning was
   16123              :              silenced.  */
   16124          106 :           gcc_checking_assert (explained);
   16125          106 :           ok = false;
   16126          106 :           continue;
   16127          106 :         }
   16128              : 
   16129              :       /* In all other cases, we're just warning (rather than erroring).
   16130              :          We don't want to do too much warning, so let's just bail after
   16131              :          the first warning we successfully emit.  */
   16132      1763531 :       if (warn_expose_global_module_tu_local
   16133      1763513 :           && !dep->is_tu_local (/*strict=*/true)
   16134      1763475 :           && dep->is_exposure (/*strict=*/true)
   16135      1763534 :           && diagnose_bad_internal_ref (dep, /*strict=*/true))
   16136           18 :         continue;
   16137              : 
   16138      1763547 :       if (warn_template_names_tu_local
   16139       271626 :           && dep->refs_tu_local ()
   16140      1763562 :           && diagnose_template_names_tu_local (dep))
   16141           52 :         continue;
   16142              : 
   16143      1763443 :       if (warn_template_names_tu_local
   16144       271574 :           && warn_expose_global_module_tu_local
   16145       271574 :           && !dep->is_tu_local (/*strict=*/true)
   16146       271550 :           && dep->refs_tu_local (/*strict=*/true)
   16147           30 :           && !dep->is_exposure (/*strict=*/true)
   16148      1763470 :           && diagnose_template_names_tu_local (dep, /*strict=*/true))
   16149              :         continue;
   16150              :     }
   16151              : 
   16152         2801 :   return ok;
   16153              : }
   16154              : 
   16155              : /* Core of TARJAN's algorithm to find Strongly Connected Components
   16156              :    within a graph.  See https://en.wikipedia.org/wiki/
   16157              :    Tarjan%27s_strongly_connected_components_algorithm for details.
   16158              : 
   16159              :    We use depset::section as lowlink.  Completed nodes have
   16160              :    depset::cluster containing the cluster number, with the top
   16161              :    bit set.
   16162              : 
   16163              :    A useful property is that the output vector is a reverse
   16164              :    topological sort of the resulting DAG.  In our case that means
   16165              :    dependent SCCs are found before their dependers.  We make use of
   16166              :    that property.  */
   16167              : 
   16168              : void
   16169      2587765 : depset::tarjan::connect (depset *v)
   16170              : {
   16171      2587765 :   gcc_checking_assert (v->is_binding ()
   16172              :                        || !(v->is_tu_local ()
   16173              :                             || v->is_unreached ()
   16174              :                             || v->is_import ()));
   16175              : 
   16176      2587765 :   v->cluster = v->section = ++index;
   16177      2587765 :   stack.safe_push (v);
   16178              : 
   16179              :   /* Walk all our dependencies, ignore a first marked slot  */
   16180     22523430 :   for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
   16181              :     {
   16182      8679132 :       depset *dep = v->deps[ix];
   16183              : 
   16184      8679132 :       if (dep->is_binding ()
   16185     17140929 :           || !(dep->is_import () || dep->is_tu_local ()))
   16186              :         {
   16187      8666757 :           unsigned lwm = dep->cluster;
   16188              : 
   16189      8666757 :           if (!dep->cluster)
   16190              :             {
   16191              :               /* A new node.  Connect it.  */
   16192      1446084 :               connect (dep);
   16193      1446084 :               lwm = dep->section;
   16194              :             }
   16195              : 
   16196      8666757 :           if (dep->section && v->section > lwm)
   16197      1359834 :             v->section = lwm;
   16198              :         }
   16199              :     }
   16200              : 
   16201      2587765 :   if (v->section == v->cluster)
   16202              :     {
   16203              :       /* Root of a new SCC.  Push all the members onto the result list. */
   16204              :       unsigned num = v->cluster;
   16205      2587765 :       depset *p;
   16206      2587765 :       do
   16207              :         {
   16208      2587765 :           p = stack.pop ();
   16209      2587765 :           p->cluster = num;
   16210      2587765 :           p->section = 0;
   16211      2587765 :           result.quick_push (p);
   16212              :         }
   16213      2587765 :       while (p != v);
   16214              :     }
   16215      2587765 : }
   16216              : 
   16217              : /* Compare two depsets.  The specific ordering is unimportant, we're
   16218              :    just trying to get consistency.  */
   16219              : 
   16220              : static int
   16221    123934865 : depset_cmp (const void *a_, const void *b_)
   16222              : {
   16223    123934865 :   depset *a = *(depset *const *)a_;
   16224    123934865 :   depset *b = *(depset *const *)b_;
   16225              : 
   16226    123934865 :   depset::entity_kind a_kind = a->get_entity_kind ();
   16227    123934865 :   depset::entity_kind b_kind = b->get_entity_kind ();
   16228              : 
   16229    123934865 :   if  (a_kind != b_kind)
   16230              :     /* Different entity kinds, order by that.  */
   16231      6282972 :     return a_kind < b_kind ? -1 : +1;
   16232              : 
   16233    119502099 :   tree a_decl = a->get_entity ();
   16234    119502099 :   tree b_decl = b->get_entity ();
   16235    119502099 :   if (a_kind == depset::EK_USING)
   16236              :     {
   16237              :       /* If one is a using, the other must be too.  */
   16238      2254335 :       a_decl = OVL_FUNCTION (a_decl);
   16239      2254335 :       b_decl = OVL_FUNCTION (b_decl);
   16240              :     }
   16241              : 
   16242    119502099 :   if (a_decl != b_decl)
   16243              :     /* Different entities, order by their UID.  */
   16244    111741681 :     return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
   16245              : 
   16246      7760418 :   if (a_kind == depset::EK_BINDING)
   16247              :     {
   16248              :       /* Both are bindings.  Order by identifier hash.  */
   16249      7757147 :       gcc_checking_assert (a->get_name () != b->get_name ());
   16250      7757147 :       hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
   16251      7757147 :       hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
   16252     11558307 :       return (ah == bh ? 0 : ah < bh ? -1 : +1);
   16253              :     }
   16254              : 
   16255              :   /* They are the same decl.  This can happen with two using decls
   16256              :      pointing to the same target.  The best we can aim for is
   16257              :      consistently telling qsort how to order them.  Hopefully we'll
   16258              :      never have to debug a case that depends on this.  Oh, who am I
   16259              :      kidding?  Good luck.  */
   16260         3271 :   gcc_checking_assert (a_kind == depset::EK_USING);
   16261              : 
   16262              :   /* Order by depset address.  Not the best, but it is something.  */
   16263         3271 :   return a < b ? -1 : +1;
   16264              : }
   16265              : 
   16266              : /* Sort the clusters in SCC such that those that depend on one another
   16267              :    are placed later.   */
   16268              : 
   16269              : // FIXME: I am not convinced this is needed and, if needed,
   16270              : // sufficient.  We emit the decls in this order but that emission
   16271              : // could walk into later decls (from the body of the decl, or default
   16272              : // arg-like things).  Why doesn't that walk do the right thing?  And
   16273              : // if it DTRT why do we need to sort here -- won't things naturally
   16274              : // work?  I think part of the issue is that when we're going to refer
   16275              : // to an entity by name, and that entity is in the same cluster as us,
   16276              : // we need to actually walk that entity, if we've not already walked
   16277              : // it.
   16278              : static void
   16279       313279 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   16280              : {
   16281       313279 :   depset::hash table (size, original);
   16282              : 
   16283       313279 :   dump.indent ();
   16284              : 
   16285              :   /* Place bindings last, usings before that.  It's not strictly
   16286              :      necessary, but it does make things neater.  Says Mr OCD.  */
   16287              :   unsigned bind_lwm = size;
   16288              :   unsigned use_lwm = size;
   16289      1703085 :   for (unsigned ix = 0; ix != use_lwm;)
   16290              :     {
   16291      1389806 :       depset *dep = scc[ix];
   16292      1389806 :       switch (dep->get_entity_kind ())
   16293              :         {
   16294       157609 :         case depset::EK_BINDING:
   16295              :           /* Move to end.  No increment.  Notice this could be moving
   16296              :              a using decl, which we'll then move again.  */
   16297       157609 :           if (--bind_lwm != ix)
   16298              :             {
   16299        90418 :               scc[ix] = scc[bind_lwm];
   16300        90418 :               scc[bind_lwm] = dep;
   16301              :             }
   16302       157609 :           if (use_lwm > bind_lwm)
   16303              :             {
   16304       125544 :               use_lwm--;
   16305       125544 :               break;
   16306              :             }
   16307              :           /* We must have copied a using or TU-local, so move it too.  */
   16308        32065 :           dep = scc[ix];
   16309        32065 :           gcc_checking_assert
   16310              :             (dep->get_entity_kind () == depset::EK_USING
   16311              :              || dep->get_entity_kind () == depset::EK_TU_LOCAL);
   16312              :           /* FALLTHROUGH  */
   16313              : 
   16314        71424 :         case depset::EK_USING:
   16315        71424 :         case depset::EK_TU_LOCAL:
   16316        71424 :           if (--use_lwm != ix)
   16317              :             {
   16318        53849 :               scc[ix] = scc[use_lwm];
   16319        53849 :               scc[use_lwm] = dep;
   16320              :             }
   16321              :           break;
   16322              : 
   16323      1192838 :         case depset::EK_DECL:
   16324      1192838 :         case depset::EK_SPECIALIZATION:
   16325      1192838 :         case depset::EK_PARTIAL:
   16326      1192838 :           table.add_mergeable (dep);
   16327      1192838 :           ix++;
   16328      1192838 :           break;
   16329              : 
   16330            0 :         default:
   16331            0 :           gcc_unreachable ();
   16332              :         }
   16333              :     }
   16334              : 
   16335       313279 :   gcc_checking_assert (use_lwm <= bind_lwm);
   16336       313567 :   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
   16337              : 
   16338       313279 :   table.find_dependencies (nullptr);
   16339              : 
   16340       313279 :   auto_vec<depset *> order = table.connect ();
   16341       626558 :   gcc_checking_assert (order.length () == use_lwm);
   16342              : 
   16343              :   /* Now rewrite entries [0,lwm), in the dependency order we
   16344              :      discovered.  Usually each entity is in its own cluster.  Rarely,
   16345              :      we can get multi-entity clusters, in which case all but one must
   16346              :      only be reached from within the cluster.  This happens for
   16347              :      something like:
   16348              : 
   16349              :      template<typename T>
   16350              :      auto Foo (const T &arg) -> TPL<decltype (arg)>;
   16351              : 
   16352              :      The instantiation of TPL will be in the specialization table, and
   16353              :      refer to Foo via arg.  But we can only get to that specialization
   16354              :      from Foo's declaration, so we only need to treat Foo as mergeable
   16355              :      (We'll do structural comparison of TPL<decltype (arg)>).
   16356              : 
   16357              :      We approximate finding the single cluster entry dep by checking for
   16358              :      entities recursively depending on a dep first seen when streaming
   16359              :      its own merge key; the first dep we see in such a cluster should be
   16360              :      the first one streamed.  */
   16361              :   unsigned entry_pos = ~0u;
   16362              :   unsigned cluster = ~0u;
   16363      3012234 :   for (unsigned ix = 0; ix != order.length (); ix++)
   16364              :     {
   16365      1192838 :       gcc_checking_assert (order[ix]->is_special ());
   16366      1192838 :       bool tight = order[ix]->cluster == cluster;
   16367      1192838 :       depset *dep = order[ix]->deps[0];
   16368      1193831 :       dump (dumper::MERGE)
   16369         1983 :         && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
   16370          993 :                  tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
   16371      1192838 :       scc[ix] = dep;
   16372      1192838 :       if (tight)
   16373              :         {
   16374          126 :           gcc_checking_assert (dep->is_maybe_recursive ());
   16375          126 :           if (dep->is_entry ())
   16376              :             {
   16377              :               /* There should only be one entry dep in a cluster.  */
   16378            9 :               gcc_checking_assert (!scc[entry_pos]->is_entry ());
   16379            9 :               gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
   16380            9 :               scc[ix] = scc[entry_pos];
   16381            9 :               scc[entry_pos] = dep;
   16382              :             }
   16383              :         }
   16384              :       else
   16385              :         entry_pos = ix;
   16386      1192838 :       cluster = order[ix]->cluster;
   16387              :     }
   16388              : 
   16389       313567 :   dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
   16390       313279 :   dump.outdent ();
   16391       313279 : }
   16392              : 
   16393              : /* Reduce graph to SCCS clusters.  SCCS will be populated with the
   16394              :    depsets in dependency order.  Each depset's CLUSTER field contains
   16395              :    its cluster number.  Each SCC has a unique cluster number, and are
   16396              :    contiguous in SCCS. Cluster numbers are otherwise arbitrary.  */
   16397              : 
   16398              : vec<depset *>
   16399       316051 : depset::hash::connect ()
   16400              : {
   16401       316051 :   tarjan connector (size ());
   16402       316051 :   vec<depset *> deps;
   16403       316051 :   deps.create (size ());
   16404      6622103 :   for (depset *item : *this)
   16405              :     {
   16406      3153026 :       entity_kind kind = item->get_entity_kind ();
   16407      2995417 :       if (kind == EK_BINDING
   16408      2995417 :           || !(kind == EK_REDIRECT
   16409      2969968 :                || item->is_tu_local ()
   16410      2969837 :                || item->is_unreached ()
   16411      2439074 :                || item->is_import ()))
   16412      2587765 :         deps.quick_push (item);
   16413              :     }
   16414              : 
   16415              :   /* Iteration over the hash table is an unspecified ordering.  While
   16416              :      that has advantages, it causes 2 problems.  Firstly repeatable
   16417              :      builds are tricky.  Secondly creating testcases that check
   16418              :      dependencies are correct by making sure a bad ordering would
   16419              :      happen if that was wrong.  */
   16420      1773783 :   deps.qsort (depset_cmp);
   16421              : 
   16422      2903816 :   while (deps.length ())
   16423              :     {
   16424      2587765 :       depset *v = deps.pop ();
   16425      2587765 :       dump (dumper::CLUSTER) &&
   16426         1800 :         (v->is_binding ()
   16427          210 :          ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
   16428         1590 :          : dump ("Connecting %s %s %C:%N",
   16429         1590 :                  is_key_order () ? "key-order"
   16430          870 :                  : !v->has_defn () ? "declaration" : "definition",
   16431         1590 :                  v->entity_kind_name (), TREE_CODE (v->get_entity ()),
   16432              :                  v->get_entity ()));
   16433      2587765 :       if (!v->cluster)
   16434      1141681 :         connector.connect (v);
   16435              :     }
   16436              : 
   16437       316051 :   deps.release ();
   16438       632102 :   return connector.result;
   16439       316051 : }
   16440              : 
   16441              : /* Initialize location spans.  */
   16442              : 
   16443              : void
   16444         4950 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
   16445              : {
   16446         4950 :   gcc_checking_assert (!init_p ());
   16447         4950 :   spans = new vec<span> ();
   16448         4950 :   spans->reserve (20);
   16449              : 
   16450         4950 :   span interval;
   16451         4950 :   interval.ordinary.first = 0;
   16452         4950 :   interval.macro.second = MAX_LOCATION_T + 1;
   16453         4950 :   interval.ordinary_delta = interval.macro_delta = 0;
   16454              : 
   16455              :   /* A span for reserved fixed locs.  */
   16456         4950 :   interval.ordinary.second
   16457         4950 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   16458         4950 :   interval.macro.first = interval.macro.second;
   16459         4950 :   dump (dumper::LOCATION)
   16460           42 :     && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16461              :              interval.ordinary.first, interval.ordinary.second,
   16462              :              interval.macro.first, interval.macro.second);
   16463         4950 :   spans->quick_push (interval);
   16464              : 
   16465              :   /* A span for command line & forced headers.  */
   16466         4950 :   interval.ordinary.first = interval.ordinary.second;
   16467         4950 :   interval.macro.second = interval.macro.first;
   16468         4950 :   if (map)
   16469              :     {
   16470         4944 :       interval.ordinary.second = map->start_location;
   16471         4944 :       interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
   16472              :     }
   16473         4950 :   dump (dumper::LOCATION)
   16474           21 :     && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16475              :              interval.ordinary.first, interval.ordinary.second,
   16476              :              interval.macro.first, interval.macro.second);
   16477         4950 :   spans->quick_push (interval);
   16478              : 
   16479              :   /* Start an interval for the main file.  */
   16480         4950 :   interval.ordinary.first = interval.ordinary.second;
   16481         4950 :   interval.macro.second = interval.macro.first;
   16482         4950 :   dump (dumper::LOCATION)
   16483           21 :     && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
   16484              :              interval.ordinary.first, interval.macro.second);
   16485         4950 :   spans->quick_push (interval);
   16486         4950 : }
   16487              : 
   16488              : /* Reopen the span, if we want the about-to-be-inserted set of maps to
   16489              :    be propagated in our own location table.  I.e. we are the primary
   16490              :    interface and we're importing a partition.  */
   16491              : 
   16492              : bool
   16493         3087 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
   16494              : {
   16495         3087 :   bool opened = (module_interface_p () && !module_partition_p ()
   16496         3557 :                  && import->is_partition ());
   16497          178 :   if (opened)
   16498          178 :     open (hwm);
   16499         3087 :   return opened;
   16500              : }
   16501              : 
   16502              : /* Open a new linemap interval.  The just-created ordinary map is the
   16503              :    first map of the interval.  */
   16504              : 
   16505              : void
   16506         1083 : loc_spans::open (location_t hwm)
   16507              : {
   16508         1083 :   span interval;
   16509         1083 :   interval.ordinary.first = interval.ordinary.second = hwm;
   16510         2166 :   interval.macro.first = interval.macro.second
   16511         1083 :     = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16512         1083 :   interval.ordinary_delta = interval.macro_delta = 0;
   16513         1083 :   dump (dumper::LOCATION)
   16514            0 :     && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
   16515            0 :              spans->length (), interval.ordinary.first,
   16516              :              interval.macro.second);
   16517         1083 :   if (spans->length ())
   16518              :     {
   16519              :       /* No overlapping!  */
   16520         1083 :       auto &last = spans->last ();
   16521         1083 :       gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
   16522         1083 :       gcc_checking_assert (interval.macro.second <= last.macro.first);
   16523              :     }
   16524         1083 :   spans->safe_push (interval);
   16525         1083 : }
   16526              : 
   16527              : /* Close out the current linemap interval.  The last maps are within
   16528              :    the interval.  */
   16529              : 
   16530              : void
   16531         6030 : loc_spans::close ()
   16532              : {
   16533         6030 :   span &interval = spans->last ();
   16534              : 
   16535         6030 :   interval.ordinary.second
   16536         6030 :     = ((line_table->highest_location
   16537         6030 :         + (loc_one << line_table->default_range_bits))
   16538         6030 :        & ~((loc_one << line_table->default_range_bits) - 1));
   16539         6030 :   interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16540         6030 :   dump (dumper::LOCATION)
   16541           21 :     && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
   16542           21 :              spans->length () - 1,
   16543              :              interval.ordinary.first,interval.ordinary.second,
   16544              :              interval.macro.first, interval.macro.second);
   16545         6030 : }
   16546              : 
   16547              : /* Given an ordinary location LOC, return the lmap_interval it resides
   16548              :    in.  NULL if it is not in an interval.  */
   16549              : 
   16550              : const loc_spans::span *
   16551     38794063 : loc_spans::ordinary (location_t loc)
   16552              : {
   16553     38794063 :   unsigned len = spans->length ();
   16554     77461967 :   unsigned pos = 0;
   16555     77471936 :   while (len)
   16556              :     {
   16557     77457017 :       unsigned half = len / 2;
   16558     77457017 :       const span &probe = (*spans)[pos + half];
   16559     77457017 :       if (loc < probe.ordinary.first)
   16560              :         len = half;
   16561     77447048 :       else if (loc < probe.ordinary.second)
   16562              :         return &probe;
   16563              :       else
   16564              :         {
   16565     38667904 :           pos += half + 1;
   16566     38667904 :           len = len - (half + 1);
   16567              :         }
   16568              :     }
   16569              :   return NULL;
   16570              : }
   16571              : 
   16572              : /* Likewise, given a macro location LOC, return the lmap interval it
   16573              :    resides in.   */
   16574              : 
   16575              : const loc_spans::span *
   16576      3032558 : loc_spans::macro (location_t loc)
   16577              : {
   16578      3032558 :   unsigned len = spans->length ();
   16579      6062070 :   unsigned pos = 0;
   16580      6062088 :   while (len)
   16581              :     {
   16582      6062058 :       unsigned half = len / 2;
   16583      6062058 :       const span &probe = (*spans)[pos + half];
   16584      6062058 :       if (loc >= probe.macro.second)
   16585              :         len = half;
   16586      6062040 :       else if (loc >= probe.macro.first)
   16587              :         return &probe;
   16588              :       else
   16589              :         {
   16590      3029512 :           pos += half + 1;
   16591      3029512 :           len = len - (half + 1);
   16592              :         }
   16593              :     }
   16594              :   return NULL;
   16595              : }
   16596              : 
   16597              : /* Return the ordinary location closest to FROM.  */
   16598              : 
   16599              : static location_t
   16600         6921 : ordinary_loc_of (line_maps *lmaps, location_t from)
   16601              : {
   16602        13845 :   while (!IS_ORDINARY_LOC (from))
   16603              :     {
   16604            3 :       if (IS_ADHOC_LOC (from))
   16605            3 :         from = get_location_from_adhoc_loc (lmaps, from);
   16606            3 :       if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
   16607              :         {
   16608              :           /* Find the ordinary location nearest FROM.  */
   16609            0 :           const line_map *map = linemap_lookup (lmaps, from);
   16610            0 :           const line_map_macro *mac_map = linemap_check_macro (map);
   16611            0 :           from = mac_map->get_expansion_point_location ();
   16612              :         }
   16613              :     }
   16614         6921 :   return from;
   16615              : }
   16616              : 
   16617              : static module_state **
   16618        12432 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
   16619              : {
   16620        12432 :   module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
   16621        12432 :   hashval_t hv = module_state_hash::hash (ct);
   16622              : 
   16623        12432 :   return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
   16624              : }
   16625              : 
   16626              : static module_state *
   16627       147454 : get_primary (module_state *parent)
   16628              : {
   16629       151507 :   while (parent->is_partition ())
   16630          670 :     parent = parent->parent;
   16631              : 
   16632       150837 :   if (!parent->name)
   16633              :     // Implementation unit has null name
   16634        91358 :     parent = parent->parent;
   16635              : 
   16636       146291 :   return parent;
   16637              : }
   16638              : 
   16639              : /* Find or create module NAME & PARENT in the hash table.  */
   16640              : 
   16641              : module_state *
   16642        12432 : get_module (tree name, module_state *parent, bool partition)
   16643              : {
   16644              :   /* We might be given an empty NAME if preprocessing fails to handle
   16645              :      a header-name token.  */
   16646        12432 :   if (name && TREE_CODE (name) == STRING_CST
   16647        15319 :       && TREE_STRING_LENGTH (name) == 0)
   16648              :     return nullptr;
   16649              : 
   16650        12432 :   if (partition)
   16651              :     {
   16652         1105 :       if (!parent)
   16653          235 :         parent = get_primary (this_module ());
   16654              : 
   16655         1105 :       if (!parent->is_partition () && !parent->flatname)
   16656          259 :         parent->set_flatname ();
   16657              :     }
   16658              : 
   16659        12432 :   module_state **slot = get_module_slot (name, parent, partition, true);
   16660        12432 :   module_state *state = *slot;
   16661        12432 :   if (!state)
   16662              :     {
   16663         6729 :       state = (new (ggc_alloc<module_state> ())
   16664         6729 :                module_state (name, parent, partition));
   16665         6729 :       *slot = state;
   16666              :     }
   16667              :   return state;
   16668              : }
   16669              : 
   16670              : /* Process string name PTR into a module_state.  */
   16671              : 
   16672              : static module_state *
   16673          457 : get_module (const char *ptr)
   16674              : {
   16675              :   /* On DOS based file systems, there is an ambiguity with A:B which can be
   16676              :      interpreted as a module Module:Partition or Drive:PATH.  Interpret strings
   16677              :      which clearly starts as pathnames as header-names and everything else is
   16678              :      treated as a (possibly malformed) named moduled.  */
   16679          457 :   if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
   16680              : #if HAVE_DOS_BASED_FILE_SYSTEM
   16681              :       || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
   16682              : #endif
   16683              :       || false)
   16684              :     /* A header name.  */
   16685          112 :     return get_module (build_string (strlen (ptr), ptr));
   16686              : 
   16687              :   bool partition = false;
   16688              :   module_state *mod = NULL;
   16689              : 
   16690         1357 :   for (const char *probe = ptr;; probe++)
   16691         1702 :     if (!*probe || *probe == '.' || *probe == ':')
   16692              :       {
   16693          435 :         if (probe == ptr)
   16694              :           return NULL;
   16695              : 
   16696          435 :         mod = get_module (get_identifier_with_length (ptr, probe - ptr),
   16697              :                           mod, partition);
   16698          435 :         ptr = probe;
   16699          435 :         if (*ptr == ':')
   16700              :           {
   16701           87 :             if (partition)
   16702              :               return NULL;
   16703              :             partition = true;
   16704              :           }
   16705              : 
   16706          435 :         if (!*ptr++)
   16707              :           break;
   16708              :       }
   16709         1267 :     else if (!(ISALPHA (*probe) || *probe == '_'
   16710           18 :                || (probe != ptr && ISDIGIT (*probe))))
   16711              :       return NULL;
   16712              : 
   16713              :   return mod;
   16714              : }
   16715              : 
   16716              : /* Create a new mapper connecting to OPTION.  */
   16717              : 
   16718              : module_client *
   16719         4950 : make_mapper (location_t loc, class mkdeps *deps)
   16720              : {
   16721         4950 :   timevar_start (TV_MODULE_MAPPER);
   16722         4950 :   const char *option = module_mapper_name;
   16723         4950 :   if (!option)
   16724         4902 :     option = getenv ("CXX_MODULE_MAPPER");
   16725              : 
   16726         9900 :   mapper = module_client::open_module_client
   16727         4950 :     (loc, option, deps, &set_cmi_repo,
   16728         4950 :      (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
   16729         4950 :      && save_decoded_options[0].arg != progname
   16730              :      ? save_decoded_options[0].arg : nullptr);
   16731              : 
   16732         4950 :   timevar_stop (TV_MODULE_MAPPER);
   16733              : 
   16734         4950 :   return mapper;
   16735              : }
   16736              : 
   16737              : static unsigned lazy_snum;
   16738              : 
   16739              : static bool
   16740        12248 : recursive_lazy (unsigned snum = ~0u)
   16741              : {
   16742        12248 :   if (lazy_snum)
   16743              :     {
   16744            0 :       error_at (input_location, "recursive lazy load");
   16745            0 :       return true;
   16746              :     }
   16747              : 
   16748        12248 :   lazy_snum = snum;
   16749        12248 :   return false;
   16750              : }
   16751              : 
   16752              : /* If THIS has an interface dependency on itself, report an error and
   16753              :    return false.  */
   16754              : 
   16755              : bool
   16756         2933 : module_state::check_circular_import (location_t from)
   16757              : {
   16758         2933 :   if (this == this_module ())
   16759              :     {
   16760              :       /* Cannot import the current module.  */
   16761            9 :       auto_diagnostic_group d;
   16762            9 :       error_at (from, "module %qs depends on itself", get_flatname ());
   16763            9 :       if (!header_module_p ())
   16764            6 :         inform (loc, "module %qs declared here", get_flatname ());
   16765            9 :       return false;
   16766            9 :     }
   16767              :   return true;
   16768              : }
   16769              : 
   16770              : /* Module name substitutions.  */
   16771              : static vec<module_state *,va_heap> substs;
   16772              : 
   16773              : void
   16774         9160 : module_state::mangle (bool include_partition)
   16775              : {
   16776         9160 :   if (subst)
   16777          425 :     mangle_module_substitution (subst);
   16778              :   else
   16779              :     {
   16780         8735 :       if (parent)
   16781          882 :         parent->mangle (include_partition);
   16782         8735 :       if (include_partition || !is_partition ())
   16783              :         {
   16784              :           // Partitions are significant for global initializer
   16785              :           // functions
   16786         8529 :           bool partition = is_partition () && !parent->is_partition ();
   16787         8529 :           subst = mangle_module_component (name, partition);
   16788         8529 :           substs.safe_push (this);
   16789              :         }
   16790              :     }
   16791         9160 : }
   16792              : 
   16793              : void
   16794         8278 : mangle_module (int mod, bool include_partition)
   16795              : {
   16796         8278 :   module_state *imp = (*modules)[mod];
   16797              : 
   16798         8278 :   gcc_checking_assert (!imp->is_header ());
   16799              : 
   16800         8278 :   if (!imp->name)
   16801              :     /* Set when importing the primary module interface.  */
   16802          223 :     imp = imp->parent;
   16803              : 
   16804              :   /* Ensure this is actually a module unit.  */
   16805          223 :   gcc_checking_assert (imp);
   16806              : 
   16807         8278 :   imp->mangle (include_partition);
   16808         8278 : }
   16809              : 
   16810              : /* Clean up substitutions.  */
   16811              : void
   16812         7807 : mangle_module_fini ()
   16813              : {
   16814        16336 :   while (substs.length ())
   16815         8529 :     substs.pop ()->subst = 0;
   16816         7807 : }
   16817              : 
   16818              : /* Announce WHAT about the module.  */
   16819              : 
   16820              : void
   16821        12759 : module_state::announce (const char *what) const
   16822              : {
   16823        12759 :   if (noisy_p ())
   16824              :     {
   16825            0 :       fprintf (stderr, " %s:%s", what, get_flatname ());
   16826            0 :       fflush (stderr);
   16827              :     }
   16828        12759 : }
   16829              : 
   16830              : /* A human-readable README section.  The contents of this section to
   16831              :    not contribute to the CRC, so the contents can change per
   16832              :    compilation.  That allows us to embed CWD, hostname, build time and
   16833              :    what not.  It is a STRTAB that may be extracted with:
   16834              :      readelf -pgnu.c++.README $(module).gcm */
   16835              : 
   16836              : void
   16837         2772 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
   16838              : {
   16839         2772 :   bytes_out readme (to);
   16840              : 
   16841         2772 :   readme.begin (false);
   16842              : 
   16843         2772 :   readme.printf ("GNU C++ %s",
   16844         2772 :                  is_header () ? "header unit"
   16845         1869 :                  : !is_partition () ? "primary interface"
   16846          205 :                  : is_interface () ? "interface partition"
   16847              :                  : "internal partition");
   16848              : 
   16849              :   /* Compiler's version.  */
   16850         2772 :   readme.printf ("compiler: %s", version_string);
   16851              : 
   16852              :   /* Module format version.  */
   16853         2772 :   verstr_t string;
   16854         2772 :   version2string (MODULE_VERSION, string);
   16855         2772 :   readme.printf ("version: %s", string);
   16856              : 
   16857              :   /* Module information.  */
   16858         2772 :   readme.printf ("module: %s", get_flatname ());
   16859         2772 :   readme.printf ("source: %s", main_input_filename);
   16860         2772 :   readme.printf ("dialect: %s", dialect);
   16861         2772 :   if (extensions)
   16862           30 :     readme.printf ("extensions: %s%s%s",
   16863              :                    extensions & SE_OPENMP ? "-fopenmp"
   16864            6 :                    : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
   16865              :                    (extensions & SE_OPENACC)
   16866            3 :                    && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
   16867              :                    ? " " : "",
   16868           12 :                    extensions & SE_OPENACC ? "-fopenacc" : "");
   16869              : 
   16870              :   /* The following fields could be expected to change between
   16871              :      otherwise identical compilations.  Consider a distributed build
   16872              :      system.  We should have a way of overriding that.  */
   16873         2772 :   if (char *cwd = getcwd (NULL, 0))
   16874              :     {
   16875         2772 :       readme.printf ("cwd: %s", cwd);
   16876         2772 :       free (cwd);
   16877              :     }
   16878         5544 :   readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
   16879              : #if NETWORKING
   16880              :   {
   16881              :     char hostname[64];
   16882              :     if (!gethostname (hostname, sizeof (hostname)))
   16883              :       readme.printf ("host: %s", hostname);
   16884              :   }
   16885              : #endif
   16886         2772 :   {
   16887              :     /* This of course will change!  */
   16888         2772 :     time_t stampy;
   16889         2772 :     auto kind = cpp_get_date (reader, &stampy);
   16890         2772 :     if (kind != CPP_time_kind::UNKNOWN)
   16891              :       {
   16892         2772 :         struct tm *time;
   16893              : 
   16894         2772 :         time = gmtime (&stampy);
   16895         2772 :         readme.print_time ("build", time, "UTC");
   16896              : 
   16897         2772 :         if (kind == CPP_time_kind::DYNAMIC)
   16898              :           {
   16899         2772 :             time = localtime (&stampy);
   16900         2772 :             readme.print_time ("local", time,
   16901              : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way?  */
   16902              :                                time->tm_zone
   16903              : #else
   16904              :                                ""
   16905              : #endif
   16906              :                                );
   16907              :           }
   16908              :       }
   16909              :   }
   16910              : 
   16911              :   /* Its direct imports.  */
   16912         3452 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16913              :     {
   16914          680 :       module_state *state = (*modules)[ix];
   16915              : 
   16916          680 :       if (state->is_direct ())
   16917          999 :         readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
   16918              :                        state->get_flatname (), state->filename);
   16919              :     }
   16920              : 
   16921         2772 :   readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
   16922         2772 : }
   16923              : 
   16924              : /* Sort environment var names in reverse order.  */
   16925              : 
   16926              : static int
   16927            0 : env_var_cmp (const void *a_, const void *b_)
   16928              : {
   16929            0 :   const unsigned char *a = *(const unsigned char *const *)a_;
   16930            0 :   const unsigned char *b = *(const unsigned char *const *)b_;
   16931              : 
   16932            0 :   for (unsigned ix = 0; ; ix++)
   16933              :     {
   16934            0 :       bool a_end = !a[ix] || a[ix] == '=';
   16935            0 :       if (a[ix] == b[ix])
   16936              :         {
   16937            0 :           if (a_end)
   16938              :             break;
   16939              :         }
   16940              :       else
   16941              :         {
   16942            0 :           bool b_end = !b[ix] || b[ix] == '=';
   16943              : 
   16944            0 :           if (!a_end && !b_end)
   16945            0 :             return a[ix] < b[ix] ? +1 : -1;
   16946            0 :           if (a_end && b_end)
   16947              :             break;
   16948            0 :           return a_end ? +1 : -1;
   16949              :         }
   16950            0 :     }
   16951              : 
   16952              :   return 0;
   16953              : }
   16954              : 
   16955              : /* Write the environment. It is a STRTAB that may be extracted with:
   16956              :      readelf -pgnu.c++.ENV $(module).gcm */
   16957              : 
   16958              : void
   16959            0 : module_state::write_env (elf_out *to)
   16960              : {
   16961            0 :   vec<const char *> vars;
   16962            0 :   vars.create (20);
   16963              : 
   16964            0 :   extern char **environ;
   16965            0 :   while (const char *var = environ[vars.length ()])
   16966            0 :     vars.safe_push (var);
   16967            0 :   vars.qsort (env_var_cmp);
   16968              : 
   16969            0 :   bytes_out env (to);
   16970            0 :   env.begin (false);
   16971            0 :   while (vars.length ())
   16972            0 :     env.printf ("%s", vars.pop ());
   16973            0 :   env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
   16974              : 
   16975            0 :   vars.release ();
   16976            0 : }
   16977              : 
   16978              : /* Write the direct or indirect imports.
   16979              :    u:N
   16980              :    {
   16981              :      u:index
   16982              :      s:name
   16983              :      u32:crc
   16984              :      s:filename (direct)
   16985              :      u:exported (direct)
   16986              :    } imports[N]
   16987              :  */
   16988              : 
   16989              : void
   16990          918 : module_state::write_imports (bytes_out &sec, bool direct)
   16991              : {
   16992          918 :   unsigned count = 0;
   16993              : 
   16994         1940 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16995              :     {
   16996         1022 :       module_state *imp = (*modules)[ix];
   16997              : 
   16998         1022 :       if (imp->remap && imp->is_direct () == direct)
   16999          490 :         count++;
   17000              :     }
   17001              : 
   17002          918 :   gcc_assert (!direct || count);
   17003              : 
   17004          918 :   sec.u (count);
   17005         1940 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   17006              :     {
   17007         1022 :       module_state *imp = (*modules)[ix];
   17008              : 
   17009         1022 :       if (imp->remap && imp->is_direct () == direct)
   17010              :         {
   17011          652 :           dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
   17012              :                            !direct ? "indirect "
   17013           81 :                            : imp->exported_p ? "exported " : "",
   17014              :                            ix, imp->remap, imp, imp->crc);
   17015          490 :           sec.u (imp->remap);
   17016          490 :           sec.str (imp->get_flatname ());
   17017          490 :           sec.u32 (imp->crc);
   17018          490 :           if (direct)
   17019              :             {
   17020          481 :               write_location (sec, imp->imported_from ());
   17021          481 :               sec.str (imp->filename);
   17022          481 :               int exportedness = 0;
   17023          481 :               if (imp->exported_p)
   17024              :                 exportedness = +1;
   17025          287 :               else if (!imp->is_purview_direct ())
   17026           13 :                 exportedness = -1;
   17027          481 :               sec.i (exportedness);
   17028              :             }
   17029              :         }
   17030              :     }
   17031          918 : }
   17032              : 
   17033              : /* READER, LMAPS  != NULL == direct imports,
   17034              :    == NUL == indirect imports.  */
   17035              : 
   17036              : unsigned
   17037          780 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
   17038              : {
   17039          780 :   unsigned count = sec.u ();
   17040          780 :   unsigned loaded = 0;
   17041              : 
   17042         1972 :   while (count--)
   17043              :     {
   17044          412 :       unsigned ix = sec.u ();
   17045          412 :       if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
   17046              :         {
   17047            0 :           sec.set_overrun ();
   17048            0 :           break;
   17049              :         }
   17050              : 
   17051          412 :       const char *name = sec.str (NULL);
   17052          412 :       module_state *imp = get_module (name);
   17053          412 :       unsigned crc = sec.u32 ();
   17054          412 :       int exportedness = 0;
   17055              : 
   17056              :       /* If the import is a partition, it must be the same primary
   17057              :          module as this TU.  */
   17058          412 :       if (imp && imp->is_partition () &&
   17059              :           (!named_module_p ()
   17060          144 :            || (get_primary (this_module ()) != get_primary (imp))))
   17061              :         imp = NULL;
   17062              : 
   17063          412 :       if (!imp)
   17064            0 :         sec.set_overrun ();
   17065          412 :       if (sec.get_overrun ())
   17066              :         break;
   17067              : 
   17068          412 :       if (lmaps)
   17069              :         {
   17070              :           /* A direct import, maybe load it.  */
   17071          408 :           location_t floc = read_location (sec);
   17072          408 :           const char *fname = sec.str (NULL);
   17073          408 :           exportedness = sec.i ();
   17074              : 
   17075          408 :           if (sec.get_overrun ())
   17076              :             break;
   17077              : 
   17078          408 :           if (!imp->check_circular_import (floc))
   17079            3 :             continue;
   17080              : 
   17081          405 :           if (imp->loadedness == ML_NONE)
   17082              :             {
   17083          318 :               imp->loc = floc;
   17084          318 :               imp->crc = crc;
   17085          318 :               if (!imp->get_flatname ())
   17086          275 :                 imp->set_flatname ();
   17087              : 
   17088          318 :               unsigned n = dump.push (imp);
   17089              : 
   17090          318 :               if (!imp->filename && fname)
   17091          275 :                 imp->filename = xstrdup (fname);
   17092              : 
   17093          318 :               if (imp->is_partition ())
   17094           33 :                 dump () && dump ("Importing elided partition %M", imp);
   17095              : 
   17096          318 :               if (!imp->do_import (reader, false))
   17097            3 :                 imp = NULL;
   17098          318 :               dump.pop (n);
   17099          318 :               if (!imp)
   17100            3 :                 continue;
   17101              :             }
   17102              : 
   17103          402 :           if (is_partition ())
   17104              :             {
   17105           69 :               if (!imp->is_direct () && !imp->is_partition_direct ())
   17106              :                 {
   17107           30 :                   imp->directness = MD_PARTITION_DIRECT;
   17108           30 :                   linemap_module_reparent (line_table, imp->loc, floc);
   17109              :                 }
   17110           69 :               if (exportedness > 0)
   17111            6 :                 imp->exported_p = true;
   17112              :             }
   17113              :         }
   17114              :       else
   17115              :         {
   17116              :           /* An indirect import, find it, it should already be here.  */
   17117            4 :           if (imp->loadedness == ML_NONE)
   17118              :             {
   17119            0 :               error_at (loc, "indirect import %qs is not already loaded", name);
   17120            0 :               continue;
   17121              :             }
   17122              :         }
   17123              : 
   17124          406 :       if (imp->crc != crc)
   17125            0 :         error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
   17126              : 
   17127          406 :       (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
   17128              : 
   17129          406 :       if (lmaps && exportedness >= 0)
   17130          388 :         set_import (imp, bool (exportedness));
   17131          586 :       dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
   17132           90 :                        : exportedness > 0 ? "exported "
   17133           51 :                        : exportedness < 0 ? "gmf" : "", ix, imp,
   17134              :                        imp->mod);
   17135          406 :       loaded++;
   17136              :     }
   17137              : 
   17138          780 :   return loaded;
   17139              : }
   17140              : 
   17141              : /* Write the import table to MOD_SNAME_PFX.imp.  */
   17142              : 
   17143              : void
   17144          459 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
   17145              : {
   17146          537 :   dump () && dump ("Writing imports");
   17147          459 :   dump.indent ();
   17148              : 
   17149          459 :   bytes_out sec (to);
   17150          459 :   sec.begin ();
   17151              : 
   17152          459 :   write_imports (sec, true);
   17153          459 :   write_imports (sec, false);
   17154              : 
   17155          459 :   sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
   17156          459 :   dump.outdent ();
   17157          459 : }
   17158              : 
   17159              : bool
   17160          390 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
   17161              : {
   17162          390 :   bytes_in sec;
   17163              : 
   17164          390 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
   17165              :     return false;
   17166              : 
   17167          477 :   dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
   17168          390 :   dump.indent ();
   17169              : 
   17170              :   /* Read the imports.  */
   17171          390 :   unsigned direct = read_imports (sec, reader, lmaps);
   17172          390 :   unsigned indirect = read_imports (sec, NULL, NULL);
   17173          390 :   if (direct + indirect + 1 != slurp->remap->length ())
   17174            6 :     from ()->set_error (elf::E_BAD_IMPORT);
   17175              : 
   17176          390 :   dump.outdent ();
   17177          390 :   if (!sec.end (from ()))
   17178              :     return false;
   17179              :   return true;
   17180          390 : }
   17181              : 
   17182              : /* We're the primary module interface, but have partitions.  Document
   17183              :    them so that non-partition module implementation units know which
   17184              :    have already been loaded.  */
   17185              : 
   17186              : void
   17187          136 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
   17188              : {
   17189          160 :   dump () && dump ("Writing %u elided partitions", count);
   17190          136 :   dump.indent ();
   17191              : 
   17192          136 :   bytes_out sec (to);
   17193          136 :   sec.begin ();
   17194              : 
   17195          350 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   17196              :     {
   17197          214 :       module_state *imp = (*modules)[ix];
   17198          214 :       if (imp->is_partition ())
   17199              :         {
   17200          229 :           dump () && dump ("Writing elided partition %M (crc=%x)",
   17201              :                            imp, imp->crc);
   17202          190 :           sec.str (imp->get_flatname ());
   17203          190 :           sec.u32 (imp->crc);
   17204          371 :           write_location (sec, imp->is_direct ()
   17205          181 :                           ? imp->imported_from () : UNKNOWN_LOCATION);
   17206          190 :           sec.str (imp->filename);
   17207              :         }
   17208              :     }
   17209              : 
   17210          136 :   sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
   17211          136 :   dump.outdent ();
   17212          136 : }
   17213              : 
   17214              : bool
   17215           27 : module_state::read_partitions (unsigned count)
   17216              : {
   17217           27 :   bytes_in sec;
   17218           27 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
   17219              :     return false;
   17220              : 
   17221           33 :   dump () && dump ("Reading %u elided partitions", count);
   17222           27 :   dump.indent ();
   17223              : 
   17224           66 :   while (count--)
   17225              :     {
   17226           39 :       const char *name = sec.str (NULL);
   17227           39 :       unsigned crc = sec.u32 ();
   17228           39 :       location_t floc = read_location (sec);
   17229           39 :       const char *fname = sec.str (NULL);
   17230              : 
   17231           39 :       if (sec.get_overrun ())
   17232              :         break;
   17233              : 
   17234           48 :       dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
   17235              : 
   17236           39 :       module_state *imp = get_module (name);
   17237           39 :       if (!imp  /* Partition should be ...  */
   17238           39 :           || !imp->is_partition () /* a partition ...  */
   17239           39 :           || imp->loadedness != ML_NONE  /* that is not yet loaded ...  */
   17240           78 :           || get_primary (imp) != this) /* whose primary is this.  */
   17241              :         {
   17242            0 :           sec.set_overrun ();
   17243            0 :           break;
   17244              :         }
   17245              : 
   17246           39 :       if (!imp->has_location ())
   17247           30 :         imp->loc = floc;
   17248           39 :       imp->crc = crc;
   17249           39 :       if (!imp->filename && fname[0])
   17250           30 :         imp->filename = xstrdup (fname);
   17251              :     }
   17252              : 
   17253           27 :   dump.outdent ();
   17254           27 :   if (!sec.end (from ()))
   17255              :     return false;
   17256              :   return true;
   17257           27 : }
   17258              : 
   17259              : /* Data for config reading and writing.  */
   17260              : struct module_state_config {
   17261              :   const char *dialect_str = get_dialect ();
   17262              :   line_map_uint_t ordinary_locs = 0;
   17263              :   line_map_uint_t macro_locs = 0;
   17264              :   unsigned num_imports = 0;
   17265              :   unsigned num_partitions = 0;
   17266              :   unsigned num_entities = 0;
   17267              :   unsigned loc_range_bits = 0;
   17268              :   unsigned active_init = 0;
   17269              : 
   17270       100480 :   static void release ()
   17271              :   {
   17272       100480 :     XDELETEVEC (dialect);
   17273       100480 :     dialect = NULL;
   17274              :   }
   17275              : 
   17276              : private:
   17277              :   static const char *get_dialect ();
   17278              :   static char *dialect;
   17279              : };
   17280              : 
   17281              : char *module_state_config::dialect;
   17282              : 
   17283              : /* Generate a string of the significant compilation options.
   17284              :    Generally assume the user knows what they're doing, in the same way
   17285              :    that object files can be mixed.  */
   17286              : 
   17287              : const char *
   17288         5990 : module_state_config::get_dialect ()
   17289              : {
   17290         5990 :   if (!dialect)
   17291         9470 :     dialect = concat (get_cxx_dialect_name (cxx_dialect),
   17292              :                       /* C++ implies these, only show if disabled.  */
   17293         4735 :                       flag_exceptions ? "" : "/no-exceptions",
   17294         4735 :                       flag_rtti ? "" : "/no-rtti",
   17295         4735 :                       flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
   17296              :                       /* C++ 20 implies concepts and coroutines.  */
   17297         1480 :                       cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
   17298         4735 :                       (cxx_dialect < cxx20 && flag_coroutines
   17299              :                        ? "/coroutines" : ""),
   17300         4735 :                       flag_module_implicit_inline ? "/implicit-inline" : "",
   17301         4735 :                       flag_contracts ? "/contracts" : "",
   17302         4735 :                       flag_reflection ? "/reflection" : "",
   17303              :                       NULL);
   17304              : 
   17305         5990 :   return dialect;
   17306              : }
   17307              : 
   17308              : /* Contents of a cluster.  */
   17309              : enum cluster_tag {
   17310              :   ct_decl,      /* A decl.  */
   17311              :   ct_defn,      /* A definition.  */
   17312              :   ct_bind,      /* A binding.  */
   17313              :   ct_hwm
   17314              : };
   17315              : 
   17316              : /* Binding modifiers.  */
   17317              : enum ct_bind_flags
   17318              : {
   17319              :   cbf_export = 0x1,     /* An exported decl.  */
   17320              :   cbf_hidden = 0x2,     /* A hidden (friend) decl.  */
   17321              :   cbf_using = 0x4,      /* A using decl.  */
   17322              :   cbf_internal = 0x8,   /* A TU-local decl.  */
   17323              : };
   17324              : 
   17325              : /* DEP belongs to a different cluster, seed it to prevent
   17326              :    unfortunately timed duplicate import.  */
   17327              : // FIXME: QOI For inter-cluster references we could just only pick
   17328              : // one entity from an earlier cluster.  Even better track
   17329              : // dependencies between earlier clusters
   17330              : 
   17331              : void
   17332      8701409 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
   17333              : {
   17334      8701409 :   if (dep->is_tu_local ())
   17335              :     /* We only stream placeholders for TU-local entities anyway.  */;
   17336      8701200 :   else if (dep->is_import () || dep->cluster < index_hwm)
   17337              :     {
   17338      3847429 :       tree ent = dep->get_entity ();
   17339      3847429 :       if (!TREE_VISITED (ent))
   17340              :         {
   17341      1640577 :           sec.tree_node (ent);
   17342      1641027 :           dump (dumper::CLUSTER)
   17343          450 :             && dump ("Seeded %s %N",
   17344          450 :                      dep->is_import () ? "import" : "intercluster", ent);
   17345              :         }
   17346              :     }
   17347      8701409 : }
   17348              : 
   17349              : /* Write the cluster of depsets in SCC[0-SIZE).
   17350              :    dep->section -> section number
   17351              :    dep->cluster -> entity number
   17352              :  */
   17353              : 
   17354              : unsigned
   17355       313279 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
   17356              :                              depset::hash &table, unsigned *counts,
   17357              :                              unsigned *crc_ptr)
   17358              : {
   17359       314813 :   dump () && dump ("Writing section:%u %u depsets", table.section, size);
   17360       313279 :   dump.indent ();
   17361              : 
   17362       313279 :   trees_out sec (to, this, table, table.section);
   17363       313279 :   sec.begin ();
   17364       313279 :   unsigned index_lwm = counts[MSC_entities];
   17365              : 
   17366              :   /* Determine entity numbers, mark for writing.   */
   17367       313588 :   dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
   17368      1703085 :   for (unsigned ix = 0; ix != size; ix++)
   17369              :     {
   17370      1389806 :       depset *b = scc[ix];
   17371              : 
   17372      1389806 :       switch (b->get_entity_kind ())
   17373              :         {
   17374            0 :         default:
   17375            0 :           gcc_unreachable ();
   17376              : 
   17377       157609 :         case depset::EK_BINDING:
   17378       157609 :           {
   17379       157609 :             dump (dumper::CLUSTER)
   17380          210 :               && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
   17381              :                        b->get_entity (), b->get_name ());
   17382       157609 :             depset *ns_dep = b->deps[0];
   17383       157609 :             gcc_checking_assert (ns_dep->get_entity_kind ()
   17384              :                                  == depset::EK_NAMESPACE
   17385              :                                  && ns_dep->get_entity () == b->get_entity ());
   17386       374992 :             for (unsigned jx = b->deps.length (); --jx;)
   17387              :               {
   17388       217383 :                 depset *dep = b->deps[jx];
   17389              :                 // We could be declaring something that is also a
   17390              :                 // (merged) import
   17391       256790 :                 gcc_checking_assert (dep->is_import ()
   17392              :                                      || TREE_VISITED (dep->get_entity ())
   17393              :                                      || (dep->get_entity_kind ()
   17394              :                                          == depset::EK_USING)
   17395              :                                      || (dep->get_entity_kind ()
   17396              :                                          == depset::EK_TU_LOCAL));
   17397              :               }
   17398              :           }
   17399              :           break;
   17400              : 
   17401      1192838 :         case depset::EK_DECL:
   17402      1192838 :         case depset::EK_SPECIALIZATION:
   17403      1192838 :         case depset::EK_PARTIAL:
   17404      1192838 :           b->cluster = counts[MSC_entities]++;
   17405      1192838 :           sec.mark_declaration (b->get_entity (), b->has_defn ());
   17406              :           /* FALLTHROUGH  */
   17407              : 
   17408      1232197 :         case depset::EK_USING:
   17409      1232197 :         case depset::EK_TU_LOCAL:
   17410      2464394 :           gcc_checking_assert (!b->is_import ()
   17411              :                                && !b->is_unreached ());
   17412      1392980 :           dump (dumper::CLUSTER)
   17413         1161 :             && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
   17414          729 :                      b->has_defn () ? "definition" : "declaration",
   17415              :                      b->get_entity ());
   17416              :           break;
   17417              :         }
   17418              :     }
   17419       313588 :   dump (dumper::CLUSTER) && (dump.outdent (), true);
   17420              : 
   17421              :   /* Ensure every out-of-cluster decl is referenced before we start
   17422              :      streaming.  We must do both imports *and* earlier clusters,
   17423              :      because the latter could reach into the former and cause a
   17424              :      duplicate loop.   */
   17425       313279 :   sec.set_importing (+1);
   17426      1703085 :   for (unsigned ix = 0; ix != size; ix++)
   17427              :     {
   17428      1389806 :       depset *b = scc[ix];
   17429     17493763 :       for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
   17430              :         {
   17431      7361000 :           depset *dep = b->deps[jx];
   17432              : 
   17433      7361000 :           if (dep->is_binding ())
   17434              :             {
   17435      1775079 :               for (unsigned ix = dep->deps.length (); --ix;)
   17436              :                 {
   17437      1340409 :                   depset *bind = dep->deps[ix];
   17438      1340409 :                   if (bind->get_entity_kind () == depset::EK_USING)
   17439       467090 :                     bind = bind->deps[1];
   17440              : 
   17441      1340409 :                   intercluster_seed (sec, index_lwm, bind);
   17442              :                 }
   17443              :               /* Also check the namespace itself.  */
   17444       217335 :               dep = dep->deps[0];
   17445              :             }
   17446              : 
   17447      7361000 :           intercluster_seed (sec, index_lwm, dep);
   17448              :         }
   17449              :     }
   17450       313279 :   sec.tree_node (NULL_TREE);
   17451              :   /* We're done importing now.  */
   17452       313279 :   sec.set_importing (-1);
   17453              : 
   17454              :   /* Write non-definitions.  */
   17455      1703085 :   for (unsigned ix = 0; ix != size; ix++)
   17456              :     {
   17457      1389806 :       depset *b = scc[ix];
   17458      1389806 :       tree decl = b->get_entity ();
   17459      1389806 :       switch (b->get_entity_kind ())
   17460              :         {
   17461            0 :         default:
   17462            0 :           gcc_unreachable ();
   17463       157609 :           break;
   17464              : 
   17465       157609 :         case depset::EK_BINDING:
   17466       157609 :           {
   17467       157609 :             gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
   17468       158807 :             dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
   17469              :                              decl, b->get_name ());
   17470       157609 :             sec.u (ct_bind);
   17471       157609 :             sec.tree_node (decl);
   17472       157609 :             sec.tree_node (b->get_name ());
   17473              : 
   17474              :             /* Write in reverse order, so reading will see the exports
   17475              :                first, thus building the overload chain will be
   17476              :                optimized.  */
   17477       532601 :             for (unsigned jx = b->deps.length (); --jx;)
   17478              :               {
   17479       217383 :                 depset *dep = b->deps[jx];
   17480       217383 :                 tree bound = dep->get_entity ();
   17481       217383 :                 unsigned flags = 0;
   17482       217383 :                 if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
   17483              :                   flags |= cbf_internal;
   17484       217335 :                 else if (dep->get_entity_kind () == depset::EK_USING)
   17485              :                   {
   17486        39359 :                     tree ovl = bound;
   17487        39359 :                     bound = OVL_FUNCTION (bound);
   17488        39359 :                     if (!(TREE_CODE (bound) == CONST_DECL
   17489         4916 :                           && UNSCOPED_ENUM_P (TREE_TYPE (bound))
   17490         4118 :                           && decl == TYPE_NAME (TREE_TYPE (bound))))
   17491              :                       /* An unscoped enumerator in its enumeration's
   17492              :                          scope is not a using.  */
   17493              :                       flags |= cbf_using;
   17494        39359 :                     if (OVL_EXPORT_P (ovl))
   17495        38272 :                       flags |= cbf_export;
   17496              :                   }
   17497              :                 else
   17498              :                   {
   17499              :                     /* An implicit typedef must be at one.  */
   17500       177976 :                     gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
   17501       177976 :                     if (dep->is_hidden ())
   17502              :                       flags |= cbf_hidden;
   17503       177887 :                     else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
   17504       141748 :                       flags |= cbf_export;
   17505              :                   }
   17506              : 
   17507       217383 :                 gcc_checking_assert (DECL_P (bound));
   17508              : 
   17509       217383 :                 sec.i (flags);
   17510       217383 :                 if (flags & cbf_internal)
   17511              :                   {
   17512           48 :                     sec.tree_node (name_for_tu_local_decl (bound));
   17513           48 :                     write_location (sec, DECL_SOURCE_LOCATION (bound));
   17514              :                   }
   17515              :                 else
   17516       217335 :                   sec.tree_node (bound);
   17517              :               }
   17518              : 
   17519              :             /* Terminate the list.  */
   17520       157609 :             sec.i (-1);
   17521              :           }
   17522       157609 :           break;
   17523              : 
   17524        39359 :         case depset::EK_USING:
   17525        39359 :         case depset::EK_TU_LOCAL:
   17526        39386 :           dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
   17527           27 :                            TREE_CODE (decl), decl);
   17528              :           break;
   17529              : 
   17530      1192838 :         case depset::EK_SPECIALIZATION:
   17531      1192838 :         case depset::EK_PARTIAL:
   17532      1192838 :         case depset::EK_DECL:
   17533      1195985 :           dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
   17534              :                            b->entity_kind_name (), b->cluster,
   17535         3147 :                            TREE_CODE (decl), decl);
   17536              : 
   17537      1192838 :           sec.u (ct_decl);
   17538      1192838 :           sec.tree_node (decl);
   17539              : 
   17540      1392953 :           dump () && dump ("Wrote declaration entity:%u %C:%N",
   17541         3147 :                            b->cluster, TREE_CODE (decl), decl);
   17542              :           break;
   17543              :         }
   17544              :     }
   17545              : 
   17546              :   depset *namer = NULL;
   17547              : 
   17548              :   /* Write out definitions  */
   17549      1703085 :   for (unsigned ix = 0; ix != size; ix++)
   17550              :     {
   17551      1389806 :       depset *b = scc[ix];
   17552      1389806 :       tree decl = b->get_entity ();
   17553      1389806 :       switch (b->get_entity_kind ())
   17554              :         {
   17555              :         default:
   17556              :           break;
   17557              : 
   17558      1192838 :         case depset::EK_SPECIALIZATION:
   17559      1192838 :         case depset::EK_PARTIAL:
   17560      1192838 :         case depset::EK_DECL:
   17561      1192838 :           if (!namer)
   17562       297826 :             namer = b;
   17563              : 
   17564      1192838 :           if (b->has_defn ())
   17565              :             {
   17566       460203 :               sec.u (ct_defn);
   17567       460203 :               sec.tree_node (decl);
   17568       461146 :               dump () && dump ("Writing definition %N", decl);
   17569       460203 :               sec.write_definition (decl, b->refs_tu_local ());
   17570              : 
   17571       460203 :               if (!namer->has_defn ())
   17572      1389806 :                 namer = b;
   17573              :             }
   17574              :           break;
   17575              :         }
   17576              :     }
   17577              : 
   17578              :   /* We don't find the section by name.  Use depset's decl's name for
   17579              :      human friendliness.  */
   17580       313279 :   unsigned name = 0;
   17581       313279 :   tree naming_decl = NULL_TREE;
   17582       313279 :   if (namer)
   17583              :     {
   17584       297826 :       naming_decl = namer->get_entity ();
   17585       297826 :       if (namer->get_entity_kind () == depset::EK_USING)
   17586              :         /* This unfortunately names the section from the target of the
   17587              :            using decl.  But the name is only a guide, so Do Not Care.  */
   17588            0 :         naming_decl = OVL_FUNCTION (naming_decl);
   17589       297826 :       if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
   17590              :         /* Lose any anonymousness.  */
   17591       104956 :         naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
   17592       297826 :       name = to->qualified_name (naming_decl, namer->has_defn ());
   17593              :     }
   17594              : 
   17595       313279 :   unsigned bytes = sec.pos;
   17596       313279 :   unsigned snum = sec.end (to, name, crc_ptr);
   17597              : 
   17598      1703085 :   for (unsigned ix = size; ix--;)
   17599      1389806 :     gcc_checking_assert (scc[ix]->section == snum);
   17600              : 
   17601       313279 :   dump.outdent ();
   17602       314813 :   dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
   17603              : 
   17604       313279 :   return bytes;
   17605       313279 : }
   17606              : 
   17607              : /* Read a cluster from section SNUM.  */
   17608              : 
   17609              : bool
   17610       212243 : module_state::read_cluster (unsigned snum)
   17611              : {
   17612       212243 :   trees_in sec (this);
   17613              : 
   17614       212243 :   if (!sec.begin (loc, from (), snum))
   17615              :     return false;
   17616              : 
   17617       213561 :   dump () && dump ("Reading section:%u", snum);
   17618       212243 :   dump.indent ();
   17619              : 
   17620              :   /* We care about structural equality.  */
   17621       212243 :   comparing_dependent_aliases++;
   17622              : 
   17623              :   /* First seed the imports.  */
   17624      1312105 :   while (tree import = sec.tree_node ())
   17625      2412153 :     dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
   17626              : 
   17627      1559516 :   while (!sec.get_overrun () && sec.more_p ())
   17628              :     {
   17629      1347273 :       unsigned ct = sec.u ();
   17630      1347273 :       switch (ct)
   17631              :         {
   17632            0 :         default:
   17633            0 :           sec.set_overrun ();
   17634            0 :           break;
   17635              : 
   17636       110068 :         case ct_bind:
   17637              :           /* A set of namespace bindings.  */
   17638       110068 :           {
   17639       110068 :             tree ns = sec.tree_node ();
   17640       110068 :             tree name = sec.tree_node ();
   17641       110068 :             tree decls = NULL_TREE;
   17642       110068 :             tree visible = NULL_TREE;
   17643       110068 :             tree internal = NULL_TREE;
   17644       110068 :             tree type = NULL_TREE;
   17645       110068 :             bool dedup = false;
   17646       110068 :             bool global_p = is_header ();
   17647              : 
   17648              :             /* We rely on the bindings being in the reverse order of
   17649              :                the resulting overload set.  */
   17650       258466 :             for (;;)
   17651              :               {
   17652       258466 :                 int flags = sec.i ();
   17653       258466 :                 if (flags < 0)
   17654              :                   break;
   17655              : 
   17656       148398 :                 if ((flags & cbf_hidden)
   17657           65 :                     && (flags & (cbf_using | cbf_export)))
   17658            0 :                   sec.set_overrun ();
   17659       148398 :                 if ((flags & cbf_internal)
   17660           15 :                     && flags != cbf_internal)
   17661            0 :                   sec.set_overrun ();
   17662              : 
   17663            0 :                 if (flags & cbf_internal)
   17664              :                   {
   17665           15 :                     tree name = sec.tree_node ();
   17666           15 :                     location_t loc = read_location (sec);
   17667           15 :                     if (sec.get_overrun ())
   17668              :                       break;
   17669              : 
   17670           15 :                     tree decl = make_node (TU_LOCAL_ENTITY);
   17671           15 :                     TU_LOCAL_ENTITY_NAME (decl) = name;
   17672           15 :                     TU_LOCAL_ENTITY_LOCATION (decl) = loc;
   17673           15 :                     internal = tree_cons (NULL_TREE, decl, internal);
   17674           15 :                     continue;
   17675           15 :                   }
   17676              : 
   17677       148383 :                 tree decl = sec.tree_node ();
   17678       148383 :                 if (sec.get_overrun ())
   17679              :                   break;
   17680              : 
   17681       148383 :                 if (!global_p)
   17682              :                   {
   17683              :                     /* Check if the decl could require GM merging.  */
   17684         8923 :                     tree orig = get_originating_module_decl (decl);
   17685         8923 :                     tree inner = STRIP_TEMPLATE (orig);
   17686         8923 :                     if (!DECL_LANG_SPECIFIC (inner)
   17687        17846 :                         || !DECL_MODULE_ATTACH_P (inner))
   17688              :                       global_p = true;
   17689              :                   }
   17690              : 
   17691       148383 :                 if (decls && TREE_CODE (decl) == TYPE_DECL)
   17692              :                   {
   17693              :                     /* Stat hack.  */
   17694           54 :                     if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
   17695            0 :                       sec.set_overrun ();
   17696              : 
   17697           54 :                     if (flags & cbf_using)
   17698              :                       {
   17699            3 :                         type = build_lang_decl_loc (UNKNOWN_LOCATION,
   17700              :                                                     USING_DECL,
   17701            3 :                                                     DECL_NAME (decl),
   17702              :                                                     NULL_TREE);
   17703            3 :                         USING_DECL_DECLS (type) = decl;
   17704            3 :                         USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
   17705            3 :                         DECL_CONTEXT (type) = ns;
   17706              : 
   17707            3 :                         DECL_MODULE_PURVIEW_P (type) = true;
   17708            3 :                         if (flags & cbf_export)
   17709            3 :                           DECL_MODULE_EXPORT_P (type) = true;
   17710              :                       }
   17711              :                     else
   17712              :                       type = decl;
   17713              :                   }
   17714              :                 else
   17715              :                   {
   17716       148329 :                     if ((flags & cbf_using) &&
   17717        17243 :                         !DECL_DECLARES_FUNCTION_P (decl))
   17718              :                       {
   17719              :                         /* We should only see a single non-function using-decl
   17720              :                            for a binding; more than that would clash.  */
   17721         4810 :                         if (decls)
   17722            0 :                           sec.set_overrun ();
   17723              : 
   17724              :                         /* FIXME: Propagate the location of the using-decl
   17725              :                            for use in diagnostics.  */
   17726         4810 :                         decls = build_lang_decl_loc (UNKNOWN_LOCATION,
   17727              :                                                      USING_DECL,
   17728         4810 :                                                      DECL_NAME (decl),
   17729              :                                                      NULL_TREE);
   17730         4810 :                         USING_DECL_DECLS (decls) = decl;
   17731              :                         /* We don't currently record the actual scope of the
   17732              :                            using-declaration, but this approximation should
   17733              :                            generally be good enough.  */
   17734         4810 :                         USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
   17735         4810 :                         DECL_CONTEXT (decls) = ns;
   17736              : 
   17737         4810 :                         DECL_MODULE_PURVIEW_P (decls) = true;
   17738         4810 :                         if (flags & cbf_export)
   17739         4798 :                           DECL_MODULE_EXPORT_P (decls) = true;
   17740              :                       }
   17741       143519 :                     else if (decls
   17742       105243 :                              || (flags & (cbf_hidden | cbf_using))
   17743       242420 :                              || DECL_FUNCTION_TEMPLATE_P (decl))
   17744              :                       {
   17745        60617 :                         decls = ovl_make (decl, decls);
   17746        60617 :                         if (flags & cbf_using)
   17747              :                           {
   17748        12433 :                             dedup = true;
   17749        12433 :                             OVL_USING_P (decls) = true;
   17750        12433 :                             OVL_PURVIEW_P (decls) = true;
   17751        12433 :                             if (flags & cbf_export)
   17752        12418 :                               OVL_EXPORT_P (decls) = true;
   17753              :                           }
   17754              : 
   17755        60617 :                         if (flags & cbf_hidden)
   17756           65 :                           OVL_HIDDEN_P (decls) = true;
   17757        60552 :                         else if (dedup)
   17758        15519 :                           OVL_DEDUP_P (decls) = true;
   17759              :                       }
   17760              :                     else
   17761              :                       decls = decl;
   17762              : 
   17763       148317 :                     if (flags & cbf_export
   17764       148329 :                         || (!(flags & cbf_hidden)
   17765         8884 :                             && (is_module () || is_partition ())))
   17766              :                       visible = decls;
   17767              :                   }
   17768              :               }
   17769              : 
   17770       110068 :             if (!decls && !internal)
   17771            0 :               sec.set_overrun ();
   17772              : 
   17773       110068 :             if (sec.get_overrun ())
   17774              :               break; /* Bail.  */
   17775              : 
   17776       110980 :             dump () && dump ("Binding of %P", ns, name);
   17777       110068 :             if (!set_module_binding (ns, name, mod, global_p,
   17778       110068 :                                      is_module () || is_partition (),
   17779              :                                      decls, type, visible, internal))
   17780            0 :               sec.set_overrun ();
   17781              :           }
   17782              :           break;
   17783              : 
   17784       892472 :         case ct_decl:
   17785              :           /* A decl.  */
   17786       892472 :           {
   17787       892472 :             tree decl = sec.tree_node ();
   17788      2455796 :             dump () && dump ("Read declaration of %N", decl);
   17789              :           }
   17790              :           break;
   17791              : 
   17792       344733 :         case ct_defn:
   17793       344733 :           {
   17794       344733 :             tree decl = sec.tree_node ();
   17795       346073 :             dump () && dump ("Reading definition of %N", decl);
   17796       344733 :             sec.read_definition (decl);
   17797              :           }
   17798       344733 :           break;
   17799              :         }
   17800              :     }
   17801              : 
   17802              :   /* When lazy loading is in effect, we can be in the middle of
   17803              :      parsing or instantiating a function.  Save it away.
   17804              :      push_function_context does too much work.   */
   17805       212243 :   tree old_cfd = current_function_decl;
   17806       212243 :   struct function *old_cfun = cfun;
   17807       401451 :   for (const post_process_data& pdata : sec.post_process ())
   17808              :     {
   17809       144058 :       tree decl = pdata.decl;
   17810              : 
   17811       144058 :       bool abstract = false;
   17812       144058 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   17813              :         {
   17814        90142 :           abstract = true;
   17815        90142 :           decl = DECL_TEMPLATE_RESULT (decl);
   17816              :         }
   17817              : 
   17818       144058 :       current_function_decl = decl;
   17819       144058 :       allocate_struct_function (decl, abstract);
   17820       144058 :       cfun->language = ggc_cleared_alloc<language_function> ();
   17821       144058 :       cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
   17822       144058 :       cfun->function_start_locus = pdata.start_locus;
   17823       144058 :       cfun->function_end_locus = pdata.end_locus;
   17824       144058 :       cfun->language->returns_value = pdata.returns_value;
   17825       144058 :       cfun->language->returns_null = pdata.returns_null;
   17826       144058 :       cfun->language->returns_abnormally = pdata.returns_abnormally;
   17827       144058 :       cfun->language->infinite_loop = pdata.infinite_loop;
   17828       144058 :       cfun->coroutine_component = DECL_COROUTINE_P (decl);
   17829              : 
   17830              :       /* Make sure we emit explicit instantiations.
   17831              :          FIXME do we want to do this in expand_or_defer_fn instead?  */
   17832       144058 :       if (DECL_EXPLICIT_INSTANTIATION (decl)
   17833       144058 :           && !DECL_EXTERNAL (decl))
   17834           27 :         setup_explicit_instantiation_definition_linkage (decl);
   17835              : 
   17836       144058 :       if (abstract)
   17837              :         ;
   17838        53916 :       else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
   17839         9076 :         vec_safe_push (post_load_decls, decl);
   17840              :       else
   17841              :         {
   17842        44840 :           bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
   17843              : #ifdef PCC_STATIC_STRUCT_RETURN
   17844              :           cfun->returns_pcc_struct = aggr;
   17845              : #endif
   17846        44840 :           cfun->returns_struct = aggr;
   17847        44840 :           expand_or_defer_fn (decl);
   17848              : 
   17849              :           /* If we first see this function after at_eof, it doesn't get
   17850              :              note_vague_linkage_fn from tentative_decl_linkage, so the loop in
   17851              :              c_parse_final_cleanups won't consider it.  But with DECL_COMDAT we
   17852              :              can just clear DECL_EXTERNAL and let cgraph decide.
   17853              :              FIXME handle this outside module.cc after GCC 15.  */
   17854         4888 :           if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   17855        46818 :               && DECL_NOT_REALLY_EXTERN (decl))
   17856         1935 :             DECL_EXTERNAL (decl) = false;
   17857              :         }
   17858              : 
   17859              :     }
   17860       212319 :   for (const tree& type : sec.post_process_type ())
   17861              :     {
   17862              :       /* Attempt to complete an array type now in case its element type
   17863              :          had a definition streamed later in the cluster.  */
   17864           36 :       gcc_checking_assert (TREE_CODE (type) == ARRAY_TYPE);
   17865           36 :       complete_type (type);
   17866              :     }
   17867       212243 :   set_cfun (old_cfun);
   17868       212243 :   current_function_decl = old_cfd;
   17869       212243 :   comparing_dependent_aliases--;
   17870              : 
   17871       212243 :   dump.outdent ();
   17872       213561 :   dump () && dump ("Read section:%u", snum);
   17873              : 
   17874       212243 :   loaded_clusters++;
   17875              : 
   17876       212243 :   if (!sec.end (from ()))
   17877              :     return false;
   17878              : 
   17879              :   return true;
   17880       212243 : }
   17881              : 
   17882              : void
   17883       160535 : module_state::write_namespace (bytes_out &sec, depset *dep)
   17884              : {
   17885       160535 :   unsigned ns_num = dep->cluster;
   17886       160535 :   unsigned ns_import = 0;
   17887              : 
   17888       160535 :   if (dep->is_import ())
   17889            0 :     ns_import = dep->section;
   17890       160535 :   else if (dep->get_entity () != global_namespace)
   17891       105302 :     ns_num++;
   17892              : 
   17893       160535 :   sec.u (ns_import);
   17894       160535 :   sec.u (ns_num);
   17895       160535 : }
   17896              : 
   17897              : tree
   17898       198842 : module_state::read_namespace (bytes_in &sec)
   17899              : {
   17900       198842 :   unsigned ns_import = sec.u ();
   17901       198842 :   unsigned ns_num = sec.u ();
   17902       198842 :   tree ns = NULL_TREE;
   17903              : 
   17904       198842 :   if (ns_import || ns_num)
   17905              :     {
   17906       127021 :       if (!ns_import)
   17907       127021 :         ns_num--;
   17908              : 
   17909       127021 :       if (unsigned origin = slurp->remap_module (ns_import))
   17910              :         {
   17911       127021 :           module_state *from = (*modules)[origin];
   17912       127021 :           if (ns_num < from->entity_num)
   17913              :             {
   17914       127021 :               binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
   17915              : 
   17916       127021 :               if (!slot.is_lazy ())
   17917       127021 :                 ns = slot;
   17918              :             }
   17919              :         }
   17920              :       else
   17921            0 :         sec.set_overrun ();
   17922              :     }
   17923              :   else
   17924        71821 :     ns = global_namespace;
   17925              : 
   17926       198842 :   return ns;
   17927              : }
   17928              : 
   17929              : /* SPACES is a sorted vector of namespaces.  Write out the namespaces
   17930              :    to MOD_SNAME_PFX.nms section.   */
   17931              : 
   17932              : void
   17933          590 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
   17934              :                                 unsigned num, unsigned *crc_p)
   17935              : {
   17936          648 :   dump () && dump ("Writing namespaces");
   17937          590 :   dump.indent ();
   17938              : 
   17939          590 :   bytes_out sec (to);
   17940          590 :   sec.begin ();
   17941              : 
   17942         3196 :   for (unsigned ix = 0; ix != num; ix++)
   17943              :     {
   17944         2606 :       depset *b = spaces[ix];
   17945         2606 :       tree ns = b->get_entity ();
   17946              : 
   17947              :       /* This could be an anonymous namespace even for a named module,
   17948              :          since we can still emit no-linkage decls.  */
   17949         2606 :       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
   17950              : 
   17951         2606 :       unsigned flags = 0;
   17952         2606 :       if (TREE_PUBLIC (ns))
   17953         2545 :         flags |= 1;
   17954         2606 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17955          490 :         flags |= 2;
   17956         2606 :       if (DECL_MODULE_PURVIEW_P (ns))
   17957         2060 :         flags |= 4;
   17958         2606 :       if (DECL_MODULE_EXPORT_P (ns))
   17959         1838 :         flags |= 8;
   17960         2606 :       if (TREE_DEPRECATED (ns))
   17961           17 :         flags |= 16;
   17962              : 
   17963         2746 :       dump () && dump ("Writing namespace:%u %N%s%s%s%s",
   17964              :                        b->cluster, ns,
   17965          140 :                        flags & 1 ? ", public" : "",
   17966          140 :                        flags & 2 ? ", inline" : "",
   17967          140 :                        flags & 4 ? ", purview" : "",
   17968          140 :                        flags & 8 ? ", export" : "",
   17969          140 :                        flags & 16 ? ", deprecated" : "");
   17970         2606 :       sec.u (b->cluster);
   17971         2606 :       sec.u (to->name (DECL_NAME (ns)));
   17972         2606 :       write_namespace (sec, b->deps[0]);
   17973              : 
   17974         2606 :       sec.u (flags);
   17975         2606 :       write_location (sec, DECL_SOURCE_LOCATION (ns));
   17976              : 
   17977         2606 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17978              :         {
   17979          490 :           if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
   17980              :             {
   17981          127 :               tree tags = TREE_VALUE (attr);
   17982          127 :               sec.u (list_length (tags));
   17983          257 :               for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
   17984          130 :                 sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
   17985              :             }
   17986              :           else
   17987          363 :             sec.u (0);
   17988              :         }
   17989              :     }
   17990              : 
   17991          590 :   sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
   17992          590 :   dump.outdent ();
   17993          590 : }
   17994              : 
   17995              : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace.  Fill in
   17996              :    SPACES from that data.  */
   17997              : 
   17998              : bool
   17999          609 : module_state::read_namespaces (unsigned num)
   18000              : {
   18001          609 :   bytes_in sec;
   18002              : 
   18003          609 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
   18004              :     return false;
   18005              : 
   18006          682 :   dump () && dump ("Reading namespaces");
   18007          609 :   dump.indent ();
   18008              : 
   18009         3495 :   for (unsigned ix = 0; ix != num; ix++)
   18010              :     {
   18011         2886 :       unsigned entity_index = sec.u ();
   18012         2886 :       unsigned name = sec.u ();
   18013              : 
   18014         2886 :       tree parent = read_namespace (sec);
   18015              : 
   18016              :       /* See comment in write_namespace about why not bits.  */
   18017         2886 :       unsigned flags = sec.u ();
   18018         2886 :       location_t src_loc = read_location (sec);
   18019         2886 :       unsigned tags_count = (flags & 2) ? sec.u () : 0;
   18020              : 
   18021         2886 :       if (entity_index >= entity_num
   18022         2886 :           || !parent
   18023         2886 :           || (flags & 0xc) == 0x8)
   18024            0 :         sec.set_overrun ();
   18025              : 
   18026              :       tree tags = NULL_TREE;
   18027         3030 :       while (tags_count--)
   18028              :         {
   18029          144 :           size_t len;
   18030          144 :           const char *str = sec.str (&len);
   18031          144 :           tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
   18032          144 :           tags = nreverse (tags);
   18033              :         }
   18034              : 
   18035         2886 :       if (sec.get_overrun ())
   18036              :         break;
   18037              : 
   18038         5708 :       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
   18039              : 
   18040         3106 :       dump () && dump ("Read namespace:%u %P%s%s%s%s",
   18041              :                        entity_index, parent, id,
   18042          110 :                        flags & 1 ? ", public" : "",
   18043              :                        flags & 2 ? ", inline" : "",
   18044          110 :                        flags & 4 ? ", purview" : "",
   18045          110 :                        flags & 8 ? ", export" : "",
   18046          110 :                        flags & 16 ? ", deprecated" : "");
   18047         2886 :       bool visible_p = ((flags & 8)
   18048         2886 :                         || ((flags & 1)
   18049          533 :                             && (flags & 4)
   18050          101 :                             && (is_partition () || is_module ())));
   18051         2886 :       tree inner = add_imported_namespace (parent, id, src_loc, mod,
   18052              :                                            bool (flags & 2), visible_p);
   18053         2886 :       if (!inner)
   18054              :         {
   18055            0 :           sec.set_overrun ();
   18056            0 :           break;
   18057              :         }
   18058              : 
   18059         2886 :       if (is_partition ())
   18060              :         {
   18061           54 :           if (flags & 4)
   18062           48 :             DECL_MODULE_PURVIEW_P (inner) = true;
   18063           54 :           if (flags & 8)
   18064           27 :             DECL_MODULE_EXPORT_P (inner) = true;
   18065              :         }
   18066              : 
   18067         2886 :       if (flags & 16)
   18068           19 :         TREE_DEPRECATED (inner) = true;
   18069              : 
   18070         2886 :       if (tags)
   18071          141 :         DECL_ATTRIBUTES (inner)
   18072          282 :           = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
   18073              : 
   18074              :       /* Install the namespace.  */
   18075         2886 :       (*entity_ary)[entity_lwm + entity_index] = inner;
   18076         2886 :       if (DECL_MODULE_IMPORT_P (inner))
   18077              :         {
   18078            0 :           bool existed;
   18079            0 :           unsigned *slot = &entity_map->get_or_insert
   18080            0 :             (DECL_UID (inner), &existed);
   18081            0 :           if (existed)
   18082              :             /* If it existed, it should match.  */
   18083            0 :             gcc_checking_assert (inner == (*entity_ary)[*slot]);
   18084              :           else
   18085            0 :             *slot = entity_lwm + entity_index;
   18086              :         }
   18087              :     }
   18088              : 
   18089          609 :   dump.outdent ();
   18090          609 :   if (!sec.end (from ()))
   18091              :     return false;
   18092              :   return true;
   18093          609 : }
   18094              : 
   18095              : unsigned
   18096          590 : module_state::write_using_directives (elf_out *to, depset::hash &table,
   18097              :                                       vec<depset *> spaces, unsigned *crc_p)
   18098              : {
   18099          648 :   dump () && dump ("Writing using-directives");
   18100          590 :   dump.indent ();
   18101              : 
   18102          590 :   bytes_out sec (to);
   18103          590 :   sec.begin ();
   18104              : 
   18105          590 :   unsigned num = 0;
   18106         3786 :   auto emit_one_ns = [&](depset *parent_dep)
   18107              :     {
   18108         3196 :       tree parent = parent_dep->get_entity ();
   18109         3733 :       for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
   18110              :         {
   18111          183 :           if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
   18112           17 :             continue;
   18113          166 :           bool exported = DECL_MODULE_EXPORT_P (udir);
   18114          166 :           tree target = USING_DECL_DECLS (udir);
   18115          166 :           depset *target_dep = table.find_dependency (target);
   18116              : 
   18117              :           /* An using-directive imported from a different module might not
   18118              :              have been walked earlier (PR c++/122915).  But importers will
   18119              :              be able to just refer to the decl in that module unless it was
   18120              :              a partition anyway, so we don't have anything to do here.  */
   18121          166 :           if (!target_dep)
   18122              :             {
   18123            6 :               gcc_checking_assert (DECL_MODULE_IMPORT_P (udir));
   18124            6 :               continue;
   18125              :             }
   18126              : 
   18127          184 :           dump () && dump ("Writing using-directive in %N for %N",
   18128              :                            parent, target);
   18129          160 :           sec.u (exported);
   18130          160 :           write_namespace (sec, parent_dep);
   18131          160 :           write_namespace (sec, target_dep);
   18132          160 :           ++num;
   18133              :         }
   18134         3786 :     };
   18135              : 
   18136          590 :   emit_one_ns (table.find_dependency (global_namespace));
   18137         4376 :   for (depset *parent_dep : spaces)
   18138         2606 :     emit_one_ns (parent_dep);
   18139              : 
   18140          590 :   sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
   18141          590 :   dump.outdent ();
   18142              : 
   18143          590 :   return num;
   18144          590 : }
   18145              : 
   18146              : bool
   18147          165 : module_state::read_using_directives (unsigned num)
   18148              : {
   18149          165 :   if (!bitmap_bit_p (this_module ()->imports, mod))
   18150              :     {
   18151           13 :       dump () && dump ("Ignoring using-directives because module %M "
   18152              :                        "is not visible in this TU", this);
   18153           10 :       return true;
   18154              :     }
   18155              : 
   18156          155 :   bytes_in sec;
   18157              : 
   18158          155 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
   18159              :     return false;
   18160              : 
   18161          167 :   dump () && dump ("Reading using-directives");
   18162          155 :   dump.indent ();
   18163              : 
   18164          338 :   for (unsigned ix = 0; ix != num; ++ix)
   18165              :     {
   18166          183 :       bool exported = sec.u ();
   18167          183 :       tree parent = read_namespace (sec);
   18168          183 :       tree target = read_namespace (sec);
   18169          183 :       if (sec.get_overrun ())
   18170              :         break;
   18171              : 
   18172          195 :       dump () && dump ("Read using-directive in %N for %N", parent, target);
   18173          183 :       if (exported || is_module () || is_partition ())
   18174          156 :         add_imported_using_namespace (parent, target);
   18175              :     }
   18176              : 
   18177          155 :   dump.outdent ();
   18178          155 :   if (!sec.end (from ()))
   18179              :     return false;
   18180              :   return true;
   18181          155 : }
   18182              : 
   18183              : /* Write the binding TABLE to MOD_SNAME_PFX.bnd   */
   18184              : 
   18185              : unsigned
   18186         2772 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
   18187              : {
   18188         3072 :   dump () && dump ("Writing binding table");
   18189         2772 :   dump.indent ();
   18190              : 
   18191         2772 :   unsigned num = 0;
   18192         2772 :   bytes_out sec (to);
   18193         2772 :   sec.begin ();
   18194              : 
   18195      2795398 :   for (unsigned ix = 0; ix != sccs.length (); ix++)
   18196              :     {
   18197      1394927 :       depset *b = sccs[ix];
   18198      1394927 :       if (b->is_binding ())
   18199              :         {
   18200       157609 :           tree ns = b->get_entity ();
   18201       158807 :           dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
   18202              :                            b->section);
   18203       157609 :           sec.u (to->name (b->get_name ()));
   18204       157609 :           write_namespace (sec, b->deps[0]);
   18205       157609 :           sec.u (b->section);
   18206       157609 :           num++;
   18207              :         }
   18208              :     }
   18209              : 
   18210         2772 :   sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
   18211         2772 :   dump.outdent ();
   18212              : 
   18213         2772 :   return num;
   18214         2772 : }
   18215              : 
   18216              : /* Read the binding table from MOD_SNAME_PFX.bind.  */
   18217              : 
   18218              : bool
   18219         2961 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
   18220              : {
   18221         2961 :   bytes_in sec;
   18222              : 
   18223         2961 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
   18224              :     return false;
   18225              : 
   18226         3493 :   dump () && dump ("Reading binding table");
   18227         2961 :   dump.indent ();
   18228       198551 :   for (; !sec.get_overrun () && num--;)
   18229              :     {
   18230       195590 :       const char *name = from ()->name (sec.u ());
   18231       195590 :       tree ns = read_namespace (sec);
   18232       195590 :       unsigned snum = sec.u ();
   18233              : 
   18234       195590 :       if (!ns || !name || (snum - lwm) >= (hwm - lwm))
   18235            0 :         sec.set_overrun ();
   18236       195590 :       if (!sec.get_overrun ())
   18237              :         {
   18238       195590 :           tree id = get_identifier (name);
   18239       197140 :           dump () && dump ("Bindings %P section:%u", ns, id, snum);
   18240       195590 :           if (mod && !import_module_binding (ns, id, mod, snum))
   18241              :             break;
   18242              :         }
   18243              :     }
   18244              : 
   18245         2961 :   dump.outdent ();
   18246         2961 :   if (!sec.end (from ()))
   18247              :     return false;
   18248              :   return true;
   18249         2961 : }
   18250              : 
   18251              : /* Write the entity table to MOD_SNAME_PFX.ent
   18252              : 
   18253              :    Each entry is a section number.  */
   18254              : 
   18255              : void
   18256         2509 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
   18257              :                               unsigned count, unsigned *crc_p)
   18258              : {
   18259         2756 :   dump () && dump ("Writing entities");
   18260         2509 :   dump.indent ();
   18261              : 
   18262         2509 :   bytes_out sec (to);
   18263         2509 :   sec.begin ();
   18264              : 
   18265         2509 :   unsigned current = 0;
   18266      1397415 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18267              :     {
   18268      1394906 :       depset *d = depsets[ix];
   18269              : 
   18270      2592859 :       switch (d->get_entity_kind ())
   18271              :         {
   18272              :         default:
   18273              :           break;
   18274              : 
   18275         5115 :         case depset::EK_NAMESPACE:
   18276         5115 :           if (!d->is_import () && d->get_entity () != global_namespace)
   18277              :             {
   18278         2606 :               gcc_checking_assert (d->cluster == current);
   18279         2606 :               current++;
   18280         2606 :               sec.u (0);
   18281              :             }
   18282              :           break;
   18283              : 
   18284      1192838 :         case depset::EK_DECL:
   18285      1192838 :         case depset::EK_SPECIALIZATION:
   18286      1192838 :         case depset::EK_PARTIAL:
   18287      2385676 :           gcc_checking_assert (!d->is_unreached ()
   18288              :                                && !d->is_import ()
   18289              :                                && d->cluster == current
   18290              :                                && d->section);
   18291      1192838 :           current++;
   18292      1192838 :           sec.u (d->section);
   18293      1192838 :           break;
   18294              :         }
   18295              :     }
   18296         2509 :   gcc_assert (count == current);
   18297         2509 :   sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
   18298         2509 :   dump.outdent ();
   18299         2509 : }
   18300              : 
   18301              : bool
   18302         2732 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
   18303              : {
   18304         2732 :   trees_in sec (this);
   18305              : 
   18306         2732 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
   18307              :     return false;
   18308              : 
   18309         3210 :   dump () && dump ("Reading entities");
   18310         2732 :   dump.indent ();
   18311              : 
   18312      1336189 :   for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
   18313              :     {
   18314      1333457 :       unsigned snum = sec.u ();
   18315      1333457 :       if (snum && (snum - lwm) >= (hwm - lwm))
   18316            0 :         sec.set_overrun ();
   18317      1333457 :       if (sec.get_overrun ())
   18318              :         break;
   18319              : 
   18320      1333457 :       if (snum)
   18321      1330571 :         slot->set_lazy (snum << 2);
   18322              :     }
   18323              : 
   18324         2732 :   dump.outdent ();
   18325         2732 :   if (!sec.end (from ()))
   18326              :     return false;
   18327              :   return true;
   18328         2732 : }
   18329              : 
   18330              : /* Write the pending table to MOD_SNAME_PFX.pnd
   18331              : 
   18332              :    The pending table holds information about clusters that need to be
   18333              :    loaded because they contain information about something that is not
   18334              :    found by namespace-scope lookup.
   18335              : 
   18336              :    The three cases are:
   18337              : 
   18338              :    (a) Template (maybe-partial) specializations that we have
   18339              :    instantiated or defined.  When an importer needs to instantiate
   18340              :    that template, they /must have/ the partial, explicit & extern
   18341              :    specializations available.  If they have the other specializations
   18342              :    available, they'll have less work to do.  Thus, when we're about to
   18343              :    instantiate FOO, we have to be able to ask 'are there any
   18344              :    specialization of FOO in our imports?'.
   18345              : 
   18346              :    (b) (Maybe-implicit) member functions definitions.  A class could
   18347              :    be defined in one header, and an inline member defined in a
   18348              :    different header (this occurs in the STL).  Similarly, like the
   18349              :    specialization case, an implicit member function could have been
   18350              :    'instantiated' in one module, and it'd be nice to not have to
   18351              :    reinstantiate it in another.
   18352              : 
   18353              :    (c) Classes completed elsewhere.  A class could be declared in one
   18354              :    header and defined in another.  We need to know to load the class
   18355              :    definition before looking in it.  It does highlight an issue --
   18356              :    there could be an intermediate import between the outermost containing
   18357              :    namespace-scope class and the innermost being-defined class.  This is
   18358              :    actually possible with all of these cases, so be aware -- we're not
   18359              :    just talking of one level of import to get to the innermost namespace.
   18360              : 
   18361              :    This gets complicated fast, it took me multiple attempts to even
   18362              :    get something remotely working.  Partially because I focussed on
   18363              :    optimizing what I think turns out to be a smaller problem, given
   18364              :    the known need to do the more general case *anyway*.  I document
   18365              :    the smaller problem, because it does appear to be the natural way
   18366              :    to do it.  It's trap!
   18367              : 
   18368              :    **** THE TRAP
   18369              : 
   18370              :    Let's refer to the primary template or the containing class as the
   18371              :    KEY.  And the specialization or member as the PENDING-ENTITY.  (To
   18372              :    avoid having to say those mouthfuls all the time.)
   18373              : 
   18374              :    In either case, we have an entity and we need some way of mapping
   18375              :    that to a set of entities that need to be loaded before we can
   18376              :    proceed with whatever processing of the entity we were going to do.
   18377              : 
   18378              :    We need to link the key to the pending-entity in some way.  Given a
   18379              :    key, tell me the pending-entities I need to have loaded.  However
   18380              :    we tie the key to the pending-entity must not rely on the key being
   18381              :    loaded -- that'd defeat the lazy loading scheme.
   18382              : 
   18383              :    As the key will be an import in we know its entity number (either
   18384              :    because we imported it, or we're writing it out too).  Thus we can
   18385              :    generate a map of key-indices to pending-entities.  The
   18386              :    pending-entity indices will be into our span of the entity table,
   18387              :    and thus allow them to be lazily loaded.  The key index will be
   18388              :    into another slot of the entity table.  Notice that this checking
   18389              :    could be expensive, we don't want to iterate over a bunch of
   18390              :    pending-entity indices (across multiple imports), every time we're
   18391              :    about do to the thing with the key.  We need to quickly determine
   18392              :    'definitely nothing needed'.
   18393              : 
   18394              :    That's almost good enough, except that key indices are not unique
   18395              :    in a couple of cases :( Specifically the Global Module or a module
   18396              :    partition can result in multiple modules assigning an entity index
   18397              :    for the key.  The decl-merging on loading will detect that so we
   18398              :    only have one Key loaded, and in the entity hash it'll indicate the
   18399              :    entity index of first load.  Which might be different to how we
   18400              :    know it.  Notice this is restricted to GM entities or this-module
   18401              :    entities.  Foreign imports cannot have this.
   18402              : 
   18403              :    We can simply resolve this in the direction of how this module
   18404              :    referred to the key to how the importer knows it.  Look in the
   18405              :    entity table slot that we nominate, maybe lazy load it, and then
   18406              :    lookup the resultant entity in the entity hash to learn how the
   18407              :    importer knows it.
   18408              : 
   18409              :    But we need to go in the other direction :( Given the key, find all
   18410              :    the index-aliases of that key.  We can partially solve that by
   18411              :    adding an alias hash table.  Whenever we load a merged decl, add or
   18412              :    augment a mapping from the entity (or its entity-index) to the
   18413              :    newly-discovered index.  Then when we look for pending entities of
   18414              :    a key, we also iterate over this aliases this mapping provides.
   18415              : 
   18416              :    But that requires the alias to be loaded.  And that's not
   18417              :    necessarily true.
   18418              : 
   18419              :    *** THE SIMPLER WAY
   18420              : 
   18421              :    The remaining fixed thing we have is the innermost namespace
   18422              :    containing the ultimate namespace-scope container of the key and
   18423              :    the name of that container (which might be the key itself).  I.e. a
   18424              :    namespace-decl/identifier/module tuple.  Let's call this the
   18425              :    top-key.  We'll discover that the module is not important here,
   18426              :    because of cross-module possibilities mentioned in case #c above.
   18427              :    We can't markup namespace-binding slots.  The best we can do is
   18428              :    mark the binding vector with 'there's something here', and have
   18429              :    another map from namespace/identifier pairs to a vector of pending
   18430              :    entity indices.
   18431              : 
   18432              :    Maintain a pending-entity map.  This is keyed by top-key, and
   18433              :    maps to a vector of pending-entity indices.  On the binding vector
   18434              :    have flags saying whether the pending-name-entity map has contents.
   18435              :    (We might want to further extend the key to be GM-vs-Partition and
   18436              :    specialization-vs-member, but let's not get ahead of ourselves.)
   18437              : 
   18438              :    For every key-like entity, find the outermost namespace-scope
   18439              :    name.  Use that to lookup in the pending-entity map and then make
   18440              :    sure the specified entities are loaded.
   18441              : 
   18442              :    An optimization might be to have a flag in each key-entity saying
   18443              :    that its top key might be in the entity table.  It's not clear to
   18444              :    me how to set that flag cheaply -- cheaper than just looking.
   18445              : 
   18446              :    FIXME: It'd be nice to have a bit in decls to tell us whether to
   18447              :    even try this.  We can have a 'already done' flag, that we set when
   18448              :    we've done KLASS's lazy pendings.  When we import a module that
   18449              :    registers pendings on the same top-key as KLASS we need to clear
   18450              :    the flag.  A recursive walk of the top-key clearing the bit will
   18451              :    suffice.  Plus we only need to recurse on classes that have the bit
   18452              :    set.  (That means we need to set the bit on parents of KLASS here,
   18453              :    don't forget.)  However, first: correctness, second: efficiency.  */
   18454              : 
   18455              : unsigned
   18456         2772 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
   18457              :                               depset::hash &table, unsigned *crc_p)
   18458              : {
   18459         3072 :   dump () && dump ("Writing pending-entities");
   18460         2772 :   dump.indent ();
   18461              : 
   18462         2772 :   trees_out sec (to, this, table);
   18463         2772 :   sec.begin ();
   18464              : 
   18465         2772 :   unsigned count = 0;
   18466         2772 :   tree cache_ns = NULL_TREE;
   18467         2772 :   tree cache_id = NULL_TREE;
   18468         2772 :   unsigned cache_section = ~0;
   18469      1397699 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18470              :     {
   18471      1394927 :       depset *d = depsets[ix];
   18472              : 
   18473      1394927 :       if (d->is_binding ())
   18474       842972 :         continue;
   18475              : 
   18476      1237318 :       if (d->is_import ())
   18477            0 :         continue;
   18478              : 
   18479      1237318 :       if (!d->is_pending_entity ())
   18480       685363 :         continue;
   18481              : 
   18482       551955 :       tree key_decl = nullptr;
   18483       551955 :       tree key_ns = find_pending_key (d->get_entity (), &key_decl);
   18484       551955 :       tree key_name = DECL_NAME (key_decl);
   18485              : 
   18486       551955 :       if (IDENTIFIER_ANON_P (key_name))
   18487              :         {
   18488            6 :           gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
   18489           12 :           if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
   18490            6 :             key_name = DECL_NAME (attached);
   18491              :           else
   18492              :             {
   18493              :               /* There's nothing to attach it to.  Must
   18494              :                  always reinstantiate.  */
   18495            0 :               dump ()
   18496            0 :                 && dump ("Unattached lambda %N[%u] section:%u",
   18497            0 :                          d->get_entity_kind () == depset::EK_DECL
   18498              :                          ? "Member" : "Specialization", d->get_entity (),
   18499              :                          d->cluster, d->section);
   18500            0 :               continue;
   18501              :             }
   18502              :         }
   18503              : 
   18504       551955 :       char const *also = "";
   18505       551955 :       if (d->section == cache_section
   18506       365855 :           && key_ns == cache_ns
   18507       365855 :           && key_name == cache_id)
   18508              :         /* Same section & key as previous, no need to repeat ourselves.  */
   18509              :         also = "also ";
   18510              :       else
   18511              :         {
   18512       249942 :           cache_ns = key_ns;
   18513       249942 :           cache_id = key_name;
   18514       249942 :           cache_section = d->section;
   18515       249942 :           gcc_checking_assert (table.find_dependency (cache_ns));
   18516       249942 :           sec.tree_node (cache_ns);
   18517       249942 :           sec.tree_node (cache_id);
   18518       249942 :           sec.u (d->cluster);
   18519       249942 :           count++;
   18520              :         }
   18521       553411 :       dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
   18522          728 :                        d->get_entity_kind () == depset::EK_DECL
   18523              :                        ? "member" : "specialization", d->get_entity (),
   18524              :                        d->cluster, cache_section, also, cache_ns, cache_id);
   18525              :       }
   18526         2772 :   sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
   18527         2772 :   dump.outdent ();
   18528              : 
   18529         2772 :   return count;
   18530         2772 : }
   18531              : 
   18532              : bool
   18533         1577 : module_state::read_pendings (unsigned count)
   18534              : {
   18535         1577 :   trees_in sec (this);
   18536              : 
   18537         1577 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
   18538              :     return false;
   18539              : 
   18540         1899 :   dump () && dump ("Reading %u pendings", count);
   18541         1577 :   dump.indent ();
   18542              : 
   18543       277985 :   for (unsigned ix = 0; ix != count; ix++)
   18544              :     {
   18545       276408 :       pending_key key;
   18546       276408 :       unsigned index;
   18547              : 
   18548       276408 :       key.ns = sec.tree_node ();
   18549       276408 :       key.id = sec.tree_node ();
   18550       276408 :       index = sec.u ();
   18551              : 
   18552       276408 :       if (!key.ns || !key.id
   18553       276408 :           || !(TREE_CODE (key.ns) == NAMESPACE_DECL
   18554       276408 :                && !DECL_NAMESPACE_ALIAS (key.ns))
   18555       276408 :           || !identifier_p (key.id)
   18556       552816 :           || index >= entity_num)
   18557            0 :         sec.set_overrun ();
   18558              : 
   18559       276408 :       if (sec.get_overrun ())
   18560              :         break;
   18561              : 
   18562       277217 :       dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
   18563              : 
   18564       276408 :       index += entity_lwm;
   18565       276408 :       auto &vec = pending_table->get_or_insert (key);
   18566       276408 :       vec.safe_push (index);
   18567              :     }
   18568              : 
   18569         1577 :   dump.outdent ();
   18570         1577 :   if (!sec.end (from ()))
   18571              :     return false;
   18572              :   return true;
   18573         1577 : }
   18574              : 
   18575              : /* Read & write locations.  */
   18576              : enum loc_kind {
   18577              :   LK_ORDINARY,
   18578              :   LK_MACRO,
   18579              :   LK_IMPORT_ORDINARY,
   18580              :   LK_IMPORT_MACRO,
   18581              :   LK_ADHOC,
   18582              :   LK_RESERVED,
   18583              : };
   18584              : 
   18585              : static const module_state *
   18586         7457 : module_for_ordinary_loc (location_t loc)
   18587              : {
   18588         7457 :   unsigned pos = 0;
   18589        14914 :   unsigned len = ool->length () - pos;
   18590              : 
   18591         7460 :   while (len)
   18592              :     {
   18593         7460 :       unsigned half = len / 2;
   18594         7460 :       module_state *probe = (*ool)[pos + half];
   18595         7460 :       if (loc < probe->ordinary_locs.first)
   18596              :         len = half;
   18597         7457 :       else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
   18598              :         return probe;
   18599              :       else
   18600              :         {
   18601            0 :           pos += half + 1;
   18602            0 :           len = len - (half + 1);
   18603              :         }
   18604              :     }
   18605              : 
   18606              :   return nullptr;
   18607              : }
   18608              : 
   18609              : static const module_state *
   18610           15 : module_for_macro_loc (location_t loc)
   18611              : {
   18612           15 :   unsigned pos = 1;
   18613           15 :   unsigned len = modules->length () - pos;
   18614              : 
   18615           15 :   while (len)
   18616              :     {
   18617           15 :       unsigned half = len / 2;
   18618           15 :       module_state *probe = (*modules)[pos + half];
   18619           15 :       if (loc < probe->macro_locs.first)
   18620              :         {
   18621            0 :           pos += half + 1;
   18622            0 :           len = len - (half + 1);
   18623              :         }
   18624           15 :       else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
   18625              :         len = half;
   18626              :       else
   18627              :         return probe;
   18628              :     }
   18629              : 
   18630              :   return NULL;
   18631              : }
   18632              : 
   18633              : location_t
   18634         1331 : module_state::imported_from () const
   18635              : {
   18636         1331 :   location_t from = loc;
   18637         1331 :   line_map_ordinary const *fmap
   18638         1331 :     = linemap_check_ordinary (linemap_lookup (line_table, from));
   18639              : 
   18640         1331 :   if (MAP_MODULE_P (fmap))
   18641         1331 :     from = linemap_included_from (fmap);
   18642              : 
   18643         1331 :   return from;
   18644              : }
   18645              : 
   18646              : /* Note that LOC will need writing.  This allows us to prune locations
   18647              :    that are not needed.  */
   18648              : 
   18649              : bool
   18650     26930819 : module_state::note_location (location_t loc)
   18651              : {
   18652     26930819 :   bool added = false;
   18653     26930819 :   if (!macro_loc_table && !ord_loc_table)
   18654              :     ;
   18655     26930819 :   else if (loc < RESERVED_LOCATION_COUNT)
   18656              :     ;
   18657     24431633 :   else if (IS_ADHOC_LOC (loc))
   18658              :     {
   18659      3046193 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18660      3046193 :       note_location (locus);
   18661      3046193 :       source_range range = get_range_from_loc (line_table, loc);
   18662      3046193 :       if (range.m_start != locus)
   18663      2939447 :         note_location (range.m_start);
   18664      3046193 :       note_location (range.m_finish);
   18665              :     }
   18666     21385440 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18667              :     {
   18668      1520638 :       if (spans.macro (loc))
   18669              :         {
   18670      1520623 :           const line_map *map = linemap_lookup (line_table, loc);
   18671      1520623 :           const line_map_macro *mac_map = linemap_check_macro (map);
   18672      1520623 :           hashval_t hv = macro_loc_traits::hash (mac_map);
   18673      1520623 :           macro_loc_info *slot
   18674      1520623 :             = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
   18675      1520623 :           if (!slot->src)
   18676              :             {
   18677       161050 :               slot->src = mac_map;
   18678       161050 :               slot->remap = 0;
   18679              :               // Expansion locations could themselves be from a
   18680              :               // macro, we need to note them all.
   18681       161050 :               note_location (mac_map->m_expansion);
   18682       161050 :               gcc_checking_assert (mac_map->n_tokens);
   18683       161050 :               location_t tloc = UNKNOWN_LOCATION;
   18684      6020276 :               for (unsigned ix = mac_map->n_tokens * 2; ix--;)
   18685      5859226 :                 if (mac_map->macro_locations[ix] != tloc)
   18686              :                   {
   18687      3093499 :                     tloc = mac_map->macro_locations[ix];
   18688      3093499 :                     note_location (tloc);
   18689              :                   }
   18690              :               added = true;
   18691              :             }
   18692              :         }
   18693              :     }
   18694     19864802 :   else if (IS_ORDINARY_LOC (loc))
   18695              :     {
   18696     19864802 :       if (spans.ordinary (loc))
   18697              :         {
   18698     19857340 :           const line_map *map = linemap_lookup (line_table, loc);
   18699     19857340 :           const line_map_ordinary *ord_map = linemap_check_ordinary (map);
   18700     19857340 :           ord_loc_info lkup;
   18701     19857340 :           lkup.src = ord_map;
   18702     19857340 :           lkup.span = loc_one << ord_map->m_column_and_range_bits;
   18703     19857340 :           lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
   18704     19857340 :           lkup.remap = 0;
   18705     19857340 :           ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
   18706     19857340 :                                 (lkup, ord_loc_traits::hash (lkup), INSERT));
   18707     19857340 :           if (!slot->src)
   18708              :             {
   18709      2293312 :               *slot = lkup;
   18710      2293312 :               added = true;
   18711              :             }
   18712              :         }
   18713              :     }
   18714              :   else
   18715            0 :     gcc_unreachable ();
   18716     26930819 :   return added;
   18717              : }
   18718              : 
   18719              : /* If we're not streaming, record that we need location LOC.
   18720              :    Otherwise stream it.  */
   18721              : 
   18722              : void
   18723     40336876 : module_state::write_location (bytes_out &sec, location_t loc)
   18724              : {
   18725     40336876 :   if (!sec.streaming_p ())
   18726              :     {
   18727     14358785 :       note_location (loc);
   18728     14358785 :       return;
   18729              :     }
   18730              : 
   18731     25978091 :   if (loc < RESERVED_LOCATION_COUNT)
   18732              :     {
   18733      2563798 :       dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
   18734      2563780 :       sec.loc (LK_RESERVED + loc);
   18735              :     }
   18736     23414311 :   else if (IS_ADHOC_LOC (loc))
   18737              :     {
   18738      2973133 :       dump (dumper::LOCATION) && dump ("Adhoc location");
   18739      2973130 :       sec.u (LK_ADHOC);
   18740      2973130 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18741      2973130 :       write_location (sec, locus);
   18742      2973130 :       source_range range = get_range_from_loc (line_table, loc);
   18743      2973130 :       if (range.m_start == locus)
   18744              :         /* Compress.  */
   18745       100245 :         range.m_start = UNKNOWN_LOCATION;
   18746      2973130 :       write_location (sec, range.m_start);
   18747      2973130 :       write_location (sec, range.m_finish);
   18748      2973130 :       unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
   18749      2973130 :       sec.u (discriminator);
   18750              :     }
   18751     20441181 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18752              :     {
   18753      1511920 :       const macro_loc_info *info = nullptr;
   18754      1511920 :       line_map_uint_t offset = 0;
   18755      1511920 :       if (unsigned hwm = macro_loc_remap->length ())
   18756              :         {
   18757      1511914 :           info = macro_loc_remap->begin ();
   18758     22313987 :           while (hwm != 1)
   18759              :             {
   18760     19290159 :               unsigned mid = hwm / 2;
   18761     19290159 :               if (MAP_START_LOCATION (info[mid].src) <= loc)
   18762              :                 {
   18763      9785423 :                   info += mid;
   18764      9785423 :                   hwm -= mid;
   18765              :                 }
   18766              :               else
   18767              :                 hwm = mid;
   18768              :             }
   18769      1511914 :           offset = loc - MAP_START_LOCATION (info->src);
   18770      1511914 :           if (offset > info->src->n_tokens)
   18771            9 :             info = nullptr;
   18772              :         }
   18773              : 
   18774      1511920 :       gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
   18775              : 
   18776      1511920 :       if (info)
   18777              :         {
   18778      1511905 :           offset += info->remap;
   18779      1511905 :           sec.u (LK_MACRO);
   18780      1511905 :           sec.loc (offset);
   18781      1511905 :           dump (dumper::LOCATION)
   18782            9 :             && dump ("Macro location %K output %K", loc, offset);
   18783              :         }
   18784           15 :       else if (const module_state *import = module_for_macro_loc (loc))
   18785              :         {
   18786           15 :           auto off = loc - import->macro_locs.first;
   18787           15 :           sec.u (LK_IMPORT_MACRO);
   18788           15 :           sec.u (import->remap);
   18789           15 :           sec.loc (off);
   18790           15 :           dump (dumper::LOCATION)
   18791            0 :             && dump ("Imported macro location %K output %u:%K",
   18792            0 :                      loc, import->remap, off);
   18793              :         }
   18794              :       else
   18795            0 :         gcc_unreachable ();
   18796              :     }
   18797     18929261 :   else if (IS_ORDINARY_LOC (loc))
   18798              :     {
   18799              :       /* If we ran out of locations for imported decls, this location could
   18800              :          be a module unit's location.  In that case, remap the location
   18801              :          to be where we imported the module from.  */
   18802     18929261 :       if (spans.locations_exhausted_p () || CHECKING_P)
   18803              :         {
   18804     18929261 :           const line_map_ordinary *map
   18805     18929261 :             = linemap_check_ordinary (linemap_lookup (line_table, loc));
   18806     18929261 :           if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
   18807              :             {
   18808            0 :               gcc_checking_assert (spans.locations_exhausted_p ());
   18809            0 :               write_location (sec, linemap_included_from (map));
   18810            0 :               return;
   18811              :             }
   18812              :         }
   18813              : 
   18814     18929261 :       const ord_loc_info *info = nullptr;
   18815     18929261 :       line_map_uint_t offset = 0;
   18816     18929261 :       if (line_map_uint_t hwm = ord_loc_remap->length ())
   18817              :         {
   18818     18929261 :           info = ord_loc_remap->begin ();
   18819    280724822 :           while (hwm != 1)
   18820              :             {
   18821    242866300 :               auto mid = hwm / 2;
   18822    242866300 :               if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
   18823              :                 {
   18824    126058445 :                   info += mid;
   18825    126058445 :                   hwm -= mid;
   18826              :                 }
   18827              :               else
   18828              :                 hwm = mid;
   18829              :             }
   18830     18929261 :           offset = loc - MAP_START_LOCATION (info->src) - info->offset;
   18831     18929261 :           if (offset > info->span)
   18832         7457 :             info = nullptr;
   18833              :         }
   18834              : 
   18835     18929261 :       gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
   18836              : 
   18837     18929261 :       if (info)
   18838              :         {
   18839     18921804 :           offset += info->remap;
   18840     18921804 :           sec.u (LK_ORDINARY);
   18841     18921804 :           sec.loc (offset);
   18842              : 
   18843     18921804 :           dump (dumper::LOCATION)
   18844           78 :             && dump ("Ordinary location %K output %K", loc, offset);
   18845              :         }
   18846         7457 :       else if (const module_state *import = module_for_ordinary_loc (loc))
   18847              :         {
   18848         7457 :           auto off = loc - import->ordinary_locs.first;
   18849         7457 :           sec.u (LK_IMPORT_ORDINARY);
   18850         7457 :           sec.u (import->remap);
   18851         7457 :           sec.loc (off);
   18852         7457 :           dump (dumper::LOCATION)
   18853            0 :             && dump ("Imported ordinary location %K output %u:%K",
   18854            0 :                      loc, import->remap, off);
   18855              :         }
   18856              :       else
   18857            0 :         gcc_unreachable ();
   18858              :     }
   18859              :   else
   18860            0 :     gcc_unreachable ();
   18861              : }
   18862              : 
   18863              : location_t
   18864     21791675 : module_state::read_location (bytes_in &sec) const
   18865              : {
   18866     21791675 :   location_t locus = UNKNOWN_LOCATION;
   18867     21791675 :   unsigned kind = sec.u ();
   18868     21791675 :   switch (kind)
   18869              :      {
   18870      2049733 :     default:
   18871      2049733 :       {
   18872      2049733 :         if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
   18873      2049733 :           locus = location_t (kind - LK_RESERVED);
   18874              :         else
   18875            0 :           sec.set_overrun ();
   18876      2049733 :         dump (dumper::LOCATION)
   18877            0 :           && dump ("Reserved location %K", locus);
   18878              :       }
   18879              :       break;
   18880              : 
   18881      2340145 :      case LK_ADHOC:
   18882      2340145 :       {
   18883      2340145 :         dump (dumper::LOCATION) && dump ("Adhoc location");
   18884      2340145 :         locus = read_location (sec);
   18885      2340145 :         source_range range;
   18886      2340145 :         range.m_start = read_location (sec);
   18887      2340145 :         if (range.m_start == UNKNOWN_LOCATION)
   18888        72615 :           range.m_start = locus;
   18889      2340145 :         range.m_finish = read_location (sec);
   18890      2340145 :         unsigned discriminator = sec.u ();
   18891      2340145 :         if (locus != loc && range.m_start != loc && range.m_finish != loc)
   18892      2340145 :           locus = line_table->get_or_create_combined_loc (locus, range,
   18893              :                                                           nullptr, discriminator);
   18894              :       }
   18895              :       break;
   18896              : 
   18897      1601188 :     case LK_MACRO:
   18898      1601188 :       {
   18899      1601188 :         auto off = sec.loc ();
   18900              : 
   18901      1601188 :         if (macro_locs.second)
   18902              :           {
   18903      1601188 :             if (off < macro_locs.second)
   18904      1601188 :               locus = off + macro_locs.first;
   18905              :             else
   18906            0 :               sec.set_overrun ();
   18907              :           }
   18908              :         else
   18909            0 :           locus = loc;
   18910      1601188 :         dump (dumper::LOCATION)
   18911            0 :           && dump ("Macro %K becoming %K", off, locus);
   18912              :       }
   18913              :       break;
   18914              : 
   18915     15796896 :     case LK_ORDINARY:
   18916     15796896 :       {
   18917     15796896 :         auto off = sec.loc ();
   18918     15796896 :         if (ordinary_locs.second)
   18919              :           {
   18920     15796896 :             if (off < ordinary_locs.second)
   18921     15796896 :               locus = off + ordinary_locs.first;
   18922              :             else
   18923            0 :               sec.set_overrun ();
   18924              :           }
   18925              :         else
   18926            0 :           locus = loc;
   18927              : 
   18928     15796896 :         dump (dumper::LOCATION)
   18929            0 :           && dump ("Ordinary location %K becoming %K", off, locus);
   18930              :       }
   18931              :       break;
   18932              : 
   18933         3713 :      case LK_IMPORT_MACRO:
   18934         3713 :      case LK_IMPORT_ORDINARY:
   18935         3713 :        {
   18936         3713 :          unsigned mod = sec.u ();
   18937         3713 :          location_t off = sec.loc ();
   18938         3713 :          const module_state *import = NULL;
   18939              : 
   18940         3713 :          if (!mod && !slurp->remap)
   18941              :            /* This is an early read of a partition location during the
   18942              :               read of our ordinary location map.  */
   18943              :            import = this;
   18944              :          else
   18945              :            {
   18946         3713 :              mod = slurp->remap_module (mod);
   18947         3713 :              if (!mod)
   18948            0 :                sec.set_overrun ();
   18949              :              else
   18950         3713 :                import = (*modules)[mod];
   18951              :            }
   18952              : 
   18953         3713 :          if (import)
   18954              :            {
   18955         3713 :              if (kind == LK_IMPORT_MACRO)
   18956              :                {
   18957           22 :                  if (!import->macro_locs.second)
   18958            0 :                    locus = import->loc;
   18959           22 :                  else if (off < import->macro_locs.second)
   18960           22 :                    locus = off + import->macro_locs.first;
   18961              :                  else
   18962            0 :                    sec.set_overrun ();
   18963              :                }
   18964              :              else
   18965              :                {
   18966         3691 :                  if (!import->ordinary_locs.second)
   18967            0 :                    locus = import->loc;
   18968         3691 :                  else if (off < import->ordinary_locs.second)
   18969         3691 :                    locus = import->ordinary_locs.first + off;
   18970              :                  else
   18971            0 :                    sec.set_overrun ();
   18972              :                }
   18973              :            }
   18974              :        }
   18975              :        break;
   18976              :     }
   18977              : 
   18978     21791675 :   return locus;
   18979              : }
   18980              : 
   18981              : /* Allocate hash tables to record needed locations.  */
   18982              : 
   18983              : void
   18984         2801 : module_state::write_init_maps ()
   18985              : {
   18986         2801 :   macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
   18987         2801 :   ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
   18988         2801 : }
   18989              : 
   18990              : /* Prepare the span adjustments.  We prune unneeded locations -- at
   18991              :    this point every needed location must have been seen by
   18992              :    note_location.  */
   18993              : 
   18994              : range_t
   18995         2772 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
   18996              : {
   18997         3072 :   dump () && dump ("Preparing locations");
   18998         2772 :   dump.indent ();
   18999              : 
   19000         3072 :   dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
   19001          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.first,
   19002          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.second,
   19003          300 :                    spans[loc_spans::SPAN_RESERVED].macro.first,
   19004          300 :                    spans[loc_spans::SPAN_RESERVED].macro.second);
   19005              : 
   19006         2772 :   range_t info {0, 0};
   19007              : 
   19008              :   // Sort the noted lines.
   19009         2772 :   vec_alloc (ord_loc_remap, ord_loc_table->size ());
   19010         2772 :   for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   19011      4561108 :        iter != end; ++iter)
   19012      2279168 :     ord_loc_remap->quick_push (*iter);
   19013         2772 :   ord_loc_remap->qsort (&ord_loc_info::compare);
   19014              : 
   19015              :   // Note included-from maps.
   19016         2772 :   bool added = false;
   19017         2772 :   const line_map_ordinary *current = nullptr;
   19018      2287484 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19019      2281940 :        iter != end; ++iter)
   19020      2279168 :     if (iter->src != current)
   19021              :       {
   19022        34216 :         current = iter->src;
   19023        13662 :         for (auto probe = current;
   19024        34216 :              auto from = linemap_included_from (probe);
   19025        13662 :              probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
   19026              :           {
   19027        31076 :             if (has_partitions)
   19028              :               {
   19029              :                 // Partition locations need to elide their module map
   19030              :                 // entry.
   19031          223 :                 probe
   19032          223 :                   = linemap_check_ordinary (linemap_lookup (line_table, from));
   19033          223 :                 if (MAP_MODULE_P (probe))
   19034          190 :                   from = linemap_included_from (probe);
   19035              :               }
   19036              : 
   19037        31076 :             if (!note_location (from))
   19038              :               break;
   19039        13662 :             added = true;
   19040        13662 :           }
   19041              :       }
   19042         2772 :   if (added)
   19043              :     {
   19044              :       // Reconstruct the line array as we added items to the hash table.
   19045          501 :       vec_free (ord_loc_remap);
   19046          501 :       vec_alloc (ord_loc_remap, ord_loc_table->size ());
   19047          501 :       for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   19048      4562159 :            iter != end; ++iter)
   19049      2280829 :         ord_loc_remap->quick_push (*iter);
   19050          501 :       ord_loc_remap->qsort (&ord_loc_info::compare);
   19051              :     }
   19052         2772 :   delete ord_loc_table;
   19053         2772 :   ord_loc_table = nullptr;
   19054              : 
   19055              :   // Merge (sufficiently) adjacent spans, and calculate remapping.
   19056         2772 :   constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
   19057         5544 :   auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19058         2772 :   auto dst = begin;
   19059         2772 :   line_map_uint_t offset = 0;
   19060         2772 :   unsigned range_bits = 0;
   19061         2772 :   ord_loc_info *base = nullptr;
   19062      2295602 :   for (auto iter = begin; iter != end; ++iter)
   19063              :     {
   19064      2292830 :       if (base && iter->src == base->src)
   19065              :         {
   19066      4294880 :           if (base->offset + base->span +
   19067      2263413 :               ((adjacency << base->src->m_column_and_range_bits)
   19068              :                // If there are few c&r bits, allow further separation.
   19069      2263413 :                | (adjacency << 4))
   19070      2263413 :               >= iter->offset)
   19071              :             {
   19072              :               // Merge.
   19073      2031467 :               offset -= base->span;
   19074      2031467 :               base->span = iter->offset + iter->span - base->offset;
   19075      2031467 :               offset += base->span;
   19076      2031467 :               continue;
   19077              :             }
   19078              :         }
   19079        29417 :       else if (range_bits < iter->src->m_range_bits)
   19080         2676 :         range_bits = iter->src->m_range_bits;
   19081              : 
   19082       261363 :       offset += ((loc_one << iter->src->m_range_bits) - 1);
   19083       261363 :       offset &= ~((loc_one << iter->src->m_range_bits) - 1);
   19084       261363 :       iter->remap = offset;
   19085       261363 :       offset += iter->span;
   19086       261363 :       base = dst;
   19087       261363 :       *dst++ = *iter;
   19088              :     }
   19089         2772 :   ord_loc_remap->truncate (dst - begin);
   19090              : 
   19091         2772 :   info.first = ord_loc_remap->length ();
   19092         2772 :   cfg->ordinary_locs = offset;
   19093         2772 :   cfg->loc_range_bits = range_bits;
   19094         3072 :   dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
   19095              :                    info.first,
   19096              :                    cfg->ordinary_locs,
   19097              :                    cfg->loc_range_bits);
   19098              : 
   19099              :   // Remap the macro locations.
   19100         2772 :   vec_alloc (macro_loc_remap, macro_loc_table->size ());
   19101         2772 :   for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
   19102       324872 :        iter != end; ++iter)
   19103       161050 :     macro_loc_remap->quick_push (*iter);
   19104         2772 :   delete macro_loc_table;
   19105         2772 :   macro_loc_table = nullptr;
   19106              : 
   19107         2772 :   macro_loc_remap->qsort (&macro_loc_info::compare);
   19108         2772 :   offset = 0;
   19109         8316 :   for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
   19110       163822 :        iter != end; ++iter)
   19111              :     {
   19112       161050 :       auto mac = iter->src;
   19113       161050 :       iter->remap = offset;
   19114       161050 :       offset += mac->n_tokens;
   19115              :     }
   19116         2772 :   info.second = macro_loc_remap->length ();
   19117         2772 :   cfg->macro_locs = offset;
   19118              : 
   19119         3072 :   dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
   19120              : 
   19121         2772 :   dump.outdent ();
   19122              : 
   19123              :   // If we have no ordinary locs, we must also have no macro locs.
   19124         2772 :   gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
   19125              : 
   19126         2772 :   return info;
   19127              : }
   19128              : 
   19129              : bool
   19130         3012 : module_state::read_prepare_maps (const module_state_config *cfg)
   19131              : {
   19132         3012 :   location_t ordinary = line_table->highest_location + 1;
   19133         3012 :   ordinary += cfg->ordinary_locs;
   19134              : 
   19135         3012 :   location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19136         3012 :   macro -= cfg->macro_locs;
   19137              : 
   19138         3012 :   if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
   19139         3012 :       && macro >= LINE_MAP_MAX_LOCATION)
   19140              :     /* OK, we have enough locations.  */
   19141              :     return true;
   19142              : 
   19143            0 :   ordinary_locs.first = ordinary_locs.second = 0;
   19144            0 :   macro_locs.first = macro_locs.second = 0;
   19145              : 
   19146            0 :   spans.report_location_exhaustion (loc);
   19147              : 
   19148              :   return false;
   19149              : }
   19150              : 
   19151              : /* Write & read the location maps. Not called if there are no
   19152              :    locations.   */
   19153              : 
   19154              : void
   19155         2676 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
   19156              :                                    bool has_partitions, unsigned *crc_p)
   19157              : {
   19158         2954 :   dump () && dump ("Writing ordinary location maps");
   19159         2676 :   dump.indent ();
   19160              : 
   19161         2676 :   vec<const char *> filenames;
   19162         2676 :   filenames.create (20);
   19163              : 
   19164              :   /* Determine the unique filenames.  */
   19165         2676 :   const line_map_ordinary *current = nullptr;
   19166       269391 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19167       264039 :        iter != end; ++iter)
   19168       261363 :     if (iter->src != current)
   19169              :       {
   19170        29417 :         current = iter->src;
   19171        29417 :         const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19172              : 
   19173              :         /* We should never find a module linemap in an interval.  */
   19174        29417 :         gcc_checking_assert (!MAP_MODULE_P (iter->src));
   19175              : 
   19176              :         /* We expect very few filenames, so just an array.
   19177              :            (Not true when headers are still in play :()  */
   19178      2154467 :         for (unsigned jx = filenames.length (); jx--;)
   19179              :           {
   19180      2109572 :             const char *name = filenames[jx];
   19181      2109572 :             if (0 == strcmp (name, fname))
   19182              :               {
   19183              :                 /* Reset the linemap's name, because for things like
   19184              :                    preprocessed input we could have multiple instances
   19185              :                    of the same name, and we'd rather not percolate
   19186              :                    that.  */
   19187        13939 :                 const_cast<line_map_ordinary *> (iter->src)->to_file = name;
   19188        13939 :                 fname = NULL;
   19189        13939 :                 break;
   19190              :               }
   19191              :           }
   19192        29417 :         if (fname)
   19193        15478 :           filenames.safe_push (fname);
   19194              :       }
   19195              : 
   19196         2676 :   bytes_out sec (to);
   19197         2676 :   sec.begin ();
   19198              : 
   19199              :   /* Write the filenames.  */
   19200         2676 :   unsigned len = filenames.length ();
   19201         2676 :   sec.u (len);
   19202         2954 :   dump () && dump ("%u source file names", len);
   19203        18154 :   for (unsigned ix = 0; ix != len; ix++)
   19204              :     {
   19205        15478 :       const char *fname = filenames[ix];
   19206        15493 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19207        15478 :       sec.str (fname);
   19208              :     }
   19209              : 
   19210         2676 :   sec.loc (info.first); /* Num maps.  */
   19211         2676 :   const ord_loc_info *base = nullptr;
   19212       269391 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19213       264039 :        iter != end; ++iter)
   19214              :     {
   19215       261363 :       dump (dumper::LOCATION)
   19216           36 :         && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
   19217           36 :                  (location_t) (iter - ord_loc_remap->begin ()),
   19218           18 :                  MAP_START_LOCATION (iter->src),
   19219              :                  iter->offset, iter->span, iter->remap,
   19220              :                  iter->span);
   19221              : 
   19222       261363 :       if (!base || iter->src != base->src)
   19223        29417 :         base = iter;
   19224       261363 :       sec.loc (iter->offset - base->offset);
   19225       261363 :       if (base == iter)
   19226              :         {
   19227        29417 :           sec.u (iter->src->sysp);
   19228        29417 :           sec.u (iter->src->m_range_bits);
   19229        29417 :           sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
   19230              : 
   19231        29417 :           const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19232      6559278 :           for (unsigned ix = 0; ix != filenames.length (); ix++)
   19233      3279639 :             if (filenames[ix] == fname)
   19234              :               {
   19235        29417 :                 sec.u (ix);
   19236        29417 :                 break;
   19237              :               }
   19238        29417 :           unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
   19239        29417 :           line += iter->offset >> iter->src->m_column_and_range_bits;
   19240        29417 :           sec.u (line);
   19241              :         }
   19242       261363 :       sec.loc (iter->remap);
   19243       261363 :       if (base == iter)
   19244              :         {
   19245              :           /* Write the included from location, which means reading it
   19246              :              while reading in the ordinary maps.  So we'd better not
   19247              :              be getting ahead of ourselves.  */
   19248        29417 :           location_t from = linemap_included_from (iter->src);
   19249        29417 :           gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
   19250        29417 :           if (from != UNKNOWN_LOCATION && has_partitions)
   19251              :             {
   19252              :               /* A partition's span will have a from pointing at a
   19253              :                  MODULE_INC.  Find that map's from.  */
   19254          217 :               line_map_ordinary const *fmap
   19255          217 :                 = linemap_check_ordinary (linemap_lookup (line_table, from));
   19256          217 :               if (MAP_MODULE_P (fmap))
   19257          184 :                 from = linemap_included_from (fmap);
   19258              :             }
   19259        29417 :           write_location (sec, from);
   19260              :         }
   19261              :     }
   19262              : 
   19263         2676 :   filenames.release ();
   19264              : 
   19265         2676 :   sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
   19266         2676 :   dump.outdent ();
   19267         2676 : }
   19268              : 
   19269              : /* Return the prefix to use for dumping a #pragma diagnostic change to DK.  */
   19270              : 
   19271              : static const char *
   19272         1027 : dk_string (enum diagnostics::kind dk)
   19273              : {
   19274         1027 :   gcc_assert (dk > diagnostics::kind::unspecified
   19275              :               && dk < diagnostics::kind::last_diagnostic_kind);
   19276         1027 :   if (dk == diagnostics::kind::ignored)
   19277              :     /* diagnostics/kinds.def has an empty string for ignored.  */
   19278              :     return "ignored: ";
   19279              :   else
   19280            0 :     return diagnostics::get_text_for_kind (dk);
   19281              : }
   19282              : 
   19283              : /* Dump one #pragma GCC diagnostic entry.  */
   19284              : 
   19285              : static bool
   19286         2099 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
   19287              : {
   19288         2099 :   if (dk == diagnostics::kind::pop)
   19289         1072 :     return dump (" Index %u: pop from %d", index, opt);
   19290              :   else
   19291         1027 :     return dump (" Index %u: %s%s", index, dk_string (dk),
   19292         2054 :                  cl_options[opt].opt_text);
   19293              : }
   19294              : 
   19295              : /* Write out any #pragma GCC diagnostic info to the .dgc section.  */
   19296              : 
   19297              : void
   19298         5448 : module_state::write_diagnostic_classification (elf_out *to,
   19299              :                                                diagnostics::context *dc,
   19300              :                                                unsigned *crc_p)
   19301              : {
   19302         5448 :   auto &changes = dc->get_classification_history ();
   19303              : 
   19304         5448 :   bytes_out sec (to);
   19305         5448 :   if (sec.streaming_p ())
   19306              :     {
   19307         2676 :       sec.begin ();
   19308         2954 :       dump () && dump ("Writing diagnostic change locations");
   19309         2676 :       dump.indent ();
   19310              :     }
   19311              : 
   19312         5448 :   unsigned len = changes.length ();
   19313              : 
   19314              :   /* We don't want to write out any entries that came from one of our imports.
   19315              :      But then we need to adjust the total, and change diagnostics::kind::pop
   19316              :      targets to match the index in our actual output.  So remember how many
   19317              :      lines we had skipped at each step, where -1 means this line itself
   19318              :      is skipped.  */
   19319         5448 :   int skips = 0;
   19320         5448 :   auto_vec<int> skips_at (len);
   19321         5448 :   skips_at.safe_grow (len);
   19322              : 
   19323        71158 :   for (unsigned i = 0; i < len; ++i)
   19324              :     {
   19325        65710 :       const auto &c = changes[i];
   19326        65710 :       skips_at[i] = skips;
   19327        65710 :       if (linemap_location_from_module_p (line_table, c.location))
   19328              :         {
   19329        14714 :           ++skips;
   19330        14714 :           skips_at[i] = -1;
   19331        14714 :           continue;
   19332              :         }
   19333              :     }
   19334              : 
   19335         5448 :   if (sec.streaming_p ())
   19336              :     {
   19337         2676 :       sec.u (len - skips);
   19338         2954 :       dump () && dump ("Diagnostic changes: %u", len - skips);
   19339              :     }
   19340              : 
   19341        71158 :   for (unsigned i = 0; i < len; ++i)
   19342              :     {
   19343        65710 :       if (skips_at[i] == -1)
   19344        14714 :         continue;
   19345              : 
   19346        50996 :       const auto &c = changes[i];
   19347        50996 :       write_location (sec, c.location);
   19348        50996 :       if (sec.streaming_p ())
   19349              :         {
   19350        25498 :           unsigned opt = c.option;
   19351        25498 :           if (c.kind == diagnostics::kind::pop)
   19352        13109 :             opt -= skips_at[opt];
   19353        25498 :           sec.u (opt);
   19354        25498 :           sec.u (static_cast<unsigned> (c.kind));
   19355        67742 :           dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
   19356              :         }
   19357              :     }
   19358              : 
   19359         5448 :   if (sec.streaming_p ())
   19360              :     {
   19361         2676 :       sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
   19362         2676 :       dump.outdent ();
   19363              :     }
   19364         5448 : }
   19365              : 
   19366              : /* Read any #pragma GCC diagnostic info from the .dgc section.  */
   19367              : 
   19368              : bool
   19369         2954 : module_state::read_diagnostic_classification (diagnostics::context *dc)
   19370              : {
   19371         2954 :   bytes_in sec;
   19372              : 
   19373         2954 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
   19374              :     return false;
   19375              : 
   19376         3474 :   dump () && dump ("Reading diagnostic change locations");
   19377         2954 :   dump.indent ();
   19378              : 
   19379         2954 :   unsigned len = sec.u ();
   19380         3474 :   dump () && dump ("Diagnostic changes: %u", len);
   19381              : 
   19382         2954 :   auto &changes = dc->get_classification_history ();
   19383         2954 :   int offset = changes.length ();
   19384         2954 :   changes.reserve (len + 1);
   19385        28351 :   for (unsigned i = 0; i < len; ++i)
   19386              :     {
   19387        25397 :       location_t loc = read_location (sec);
   19388        25397 :       int opt = sec.u ();
   19389        25397 :       enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
   19390        25397 :       if (kind == diagnostics::kind::pop)
   19391              :         /* For a pop, opt is the 'changes' index to return to.  */
   19392        13080 :         opt += offset;
   19393        25397 :       changes.quick_push ({ loc, opt, kind });
   19394        25464 :       dump () && dump_dc_change (changes.length () - 1, opt, kind);
   19395              :     }
   19396              : 
   19397              :   /* Did the import pop all its diagnostic changes?  */
   19398         2954 :   bool last_was_reset = (len == 0);
   19399         2954 :   if (len)
   19400          232 :     for (int i = changes.length () - 1; ; --i)
   19401              :       {
   19402        11337 :         gcc_checking_assert (i >= offset);
   19403              : 
   19404        11337 :         const auto &c = changes[i];
   19405        11337 :         if (c.kind != diagnostics::kind::pop)
   19406              :           break;
   19407        11328 :         else if (c.option == offset)
   19408              :           {
   19409              :             last_was_reset = true;
   19410              :             break;
   19411              :           }
   19412              :         else
   19413              :           /* As in update_effective_level_from_pragmas, the loop will decrement
   19414              :              i so we actually jump to c.option - 1.  */
   19415        11221 :           i = c.option;
   19416        11221 :       }
   19417         2954 :   if (!last_was_reset)
   19418              :     {
   19419              :       /* It didn't, so add a pop at its last location to avoid affecting later
   19420              :          imports.  */
   19421            9 :       location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
   19422            9 :       changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
   19423           15 :       dump () && dump (" Adding final pop from index %d", offset);
   19424              :     }
   19425              : 
   19426         2954 :   dump.outdent ();
   19427         2954 :   if (!sec.end (from ()))
   19428              :     return false;
   19429              : 
   19430              :   return true;
   19431         2954 : }
   19432              : 
   19433              : void
   19434          124 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
   19435              : {
   19436          136 :   dump () && dump ("Writing macro location maps");
   19437          124 :   dump.indent ();
   19438              : 
   19439          124 :   bytes_out sec (to);
   19440          124 :   sec.begin ();
   19441              : 
   19442          136 :   dump () && dump ("Macro maps:%K", info.second);
   19443          124 :   sec.loc (info.second);
   19444              : 
   19445          124 :   line_map_uint_t macro_num = 0;
   19446          248 :   for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
   19447       161174 :        iter-- != begin;)
   19448              :     {
   19449       161050 :       auto mac = iter->src;
   19450       161050 :       sec.loc (iter->remap);
   19451       161050 :       sec.u (mac->n_tokens);
   19452       161050 :       sec.cpp_node (mac->macro);
   19453       161050 :       write_location (sec, mac->m_expansion);
   19454       161050 :       const location_t *locs = mac->macro_locations;
   19455              :       /* There are lots of identical runs.  */
   19456       161050 :       location_t prev = UNKNOWN_LOCATION;
   19457       161050 :       unsigned count = 0;
   19458       161050 :       unsigned runs = 0;
   19459      6020276 :       for (unsigned jx = mac->n_tokens * 2; jx--;)
   19460              :         {
   19461      5859226 :           location_t tok_loc = locs[jx];
   19462      5859226 :           if (tok_loc == prev)
   19463              :             {
   19464      2765727 :               count++;
   19465      2765727 :               continue;
   19466              :             }
   19467      3093499 :           runs++;
   19468      3093499 :           sec.u (count);
   19469      3093499 :           count = 1;
   19470      3093499 :           prev = tok_loc;
   19471      3093499 :           write_location (sec, tok_loc);
   19472              :         }
   19473       161050 :       sec.u (count);
   19474       161050 :       dump (dumper::LOCATION)
   19475            9 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
   19476            9 :                  macro_num, identifier (mac->macro),
   19477              :                  runs, mac->n_tokens,
   19478              :                  MAP_START_LOCATION (mac),
   19479            9 :                  MAP_START_LOCATION (mac) + mac->n_tokens,
   19480              :                  iter->remap);
   19481       161050 :       macro_num++;
   19482              :     }
   19483          124 :   gcc_assert (macro_num == info.second);
   19484              : 
   19485          124 :   sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
   19486          124 :   dump.outdent ();
   19487          124 : }
   19488              : 
   19489              : bool
   19490         2954 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
   19491              :                                   unsigned range_bits)
   19492              : {
   19493         2954 :   bytes_in sec;
   19494              : 
   19495         2954 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
   19496              :     return false;
   19497         3474 :   dump () && dump ("Reading ordinary location maps");
   19498         2954 :   dump.indent ();
   19499              : 
   19500              :   /* Read the filename table.  */
   19501         2954 :   unsigned len = sec.u ();
   19502         3474 :   dump () && dump ("%u source file names", len);
   19503         2954 :   vec<const char *> filenames;
   19504         2954 :   filenames.create (len);
   19505        20065 :   for (unsigned ix = 0; ix != len; ix++)
   19506              :     {
   19507        17111 :       size_t l;
   19508        17111 :       const char *buf = sec.str (&l);
   19509        17111 :       char *fname = XNEWVEC (char, l + 1);
   19510        17111 :       memcpy (fname, buf, l + 1);
   19511        17111 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19512              :       /* We leak these names into the line-map table.  But it
   19513              :          doesn't own them.  */
   19514        17111 :       filenames.quick_push (fname);
   19515              :     }
   19516              : 
   19517         2954 :   line_map_uint_t num_ordinary = sec.loc ();
   19518         3474 :   dump () && dump ("Ordinary maps:%K, range_bits:%u",
   19519              :                    num_ordinary, range_bits);
   19520              : 
   19521         2954 :   location_t offset = line_table->highest_location + 1;
   19522         2954 :   offset += ((loc_one << range_bits) - 1);
   19523         2954 :   offset &= ~((loc_one << range_bits) - 1);
   19524         2954 :   ordinary_locs.first = offset;
   19525              : 
   19526         2954 :   bool propagated = spans.maybe_propagate (this, offset);
   19527         2954 :   line_map_ordinary *maps = static_cast<line_map_ordinary *>
   19528         2954 :     (line_map_new_raw (line_table, false, num_ordinary));
   19529              : 
   19530         2954 :   const line_map_ordinary *base = nullptr;
   19531       306540 :   for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
   19532              :     {
   19533       303586 :       line_map_ordinary *map = &maps[ix];
   19534              : 
   19535       303586 :       location_t offset = sec.loc ();
   19536       303586 :       if (!offset)
   19537              :         {
   19538        33497 :           map->reason = LC_RENAME;
   19539        33497 :           map->sysp = sec.u ();
   19540        33497 :           map->m_range_bits = sec.u ();
   19541        33497 :           map->m_column_and_range_bits = sec.u () + map->m_range_bits;
   19542        33497 :           unsigned fnum = sec.u ();
   19543        66994 :           map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
   19544        33497 :           map->to_line = sec.u ();
   19545        33497 :           base = map;
   19546              :         }
   19547              :       else
   19548              :         {
   19549       270089 :           *map = *base;
   19550       270089 :           map->to_line += offset >> map->m_column_and_range_bits;
   19551              :         }
   19552       303586 :       location_t remap = sec.loc ();
   19553       303586 :       map->start_location = remap + ordinary_locs.first;
   19554       303586 :       if (base == map)
   19555              :         {
   19556              :           /* Root the outermost map at our location.  */
   19557        33497 :           ordinary_locs.second = remap;
   19558        33497 :           location_t from = read_location (sec);
   19559        33497 :           map->included_from = from != UNKNOWN_LOCATION ? from : loc;
   19560              :         }
   19561              :     }
   19562              : 
   19563         2954 :   ordinary_locs.second = num_ord_locs;
   19564              :   /* highest_location is the one handed out, not the next one to
   19565              :      hand out.  */
   19566         2954 :   line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
   19567              : 
   19568         2954 :   if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
   19569              :     /* We shouldn't run out of locations, as we checked before
   19570              :        starting.  */
   19571            0 :     sec.set_overrun ();
   19572         3474 :   dump () && dump ("Ordinary location [%K,+%K)",
   19573              :                    ordinary_locs.first, ordinary_locs.second);
   19574              : 
   19575         2954 :   if (propagated)
   19576          175 :     spans.close ();
   19577              : 
   19578         2954 :   filenames.release ();
   19579              : 
   19580         2954 :   dump.outdent ();
   19581         2954 :   if (!sec.end (from ()))
   19582              :     return false;
   19583              : 
   19584              :   return true;
   19585         2954 : }
   19586              : 
   19587              : bool
   19588          133 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
   19589              : {
   19590          133 :   bytes_in sec;
   19591              : 
   19592          133 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
   19593              :     return false;
   19594          142 :   dump () && dump ("Reading macro location maps");
   19595          133 :   dump.indent ();
   19596              : 
   19597          133 :   line_map_uint_t num_macros = sec.loc ();
   19598          142 :   dump () && dump ("Macro maps:%K locs:%K",
   19599              :                    num_macros, num_macro_locs);
   19600              : 
   19601          266 :   bool propagated = spans.maybe_propagate (this,
   19602          133 :                                            line_table->highest_location + 1);
   19603              : 
   19604          133 :   location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19605          133 :   macro_locs.second = num_macro_locs;
   19606          133 :   macro_locs.first = offset - num_macro_locs;
   19607              : 
   19608          142 :   dump () && dump ("Macro loc delta %K", offset);
   19609          142 :   dump () && dump ("Macro locations [%K,%K)",
   19610              :                    macro_locs.first, macro_locs.second);
   19611              : 
   19612       204689 :   for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
   19613              :     {
   19614       204556 :       location_t offset = sec.loc ();
   19615       204556 :       unsigned n_tokens = sec.u ();
   19616       204556 :       cpp_hashnode *node = sec.cpp_node ();
   19617       204556 :       location_t exp_loc = read_location (sec);
   19618              : 
   19619       204556 :       const line_map_macro *macro
   19620       204556 :         = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
   19621       204556 :       if (!macro)
   19622              :         /* We shouldn't run out of locations, as we checked that we
   19623              :            had enough before starting.  */
   19624              :         break;
   19625       204556 :       gcc_checking_assert (MAP_START_LOCATION (macro)
   19626              :                            == offset + macro_locs.first);
   19627              : 
   19628       204556 :       location_t *locs = macro->macro_locations;
   19629       204556 :       location_t tok_loc = UNKNOWN_LOCATION;
   19630       204556 :       unsigned count = sec.u ();
   19631       204556 :       unsigned runs = 0;
   19632      7498132 :       for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
   19633              :         {
   19634     11154755 :           while (!count-- && !sec.get_overrun ())
   19635              :             {
   19636      3861179 :               runs++;
   19637      3861179 :               tok_loc = read_location (sec);
   19638      3861179 :               count = sec.u ();
   19639              :             }
   19640      7293576 :           locs[jx] = tok_loc;
   19641              :         }
   19642       204556 :       if (count)
   19643            0 :         sec.set_overrun ();
   19644       204586 :       dump (dumper::LOCATION)
   19645            0 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
   19646              :                  ix, identifier (node), runs, n_tokens,
   19647              :                  MAP_START_LOCATION (macro),
   19648            0 :                  MAP_START_LOCATION (macro) + n_tokens);
   19649              :     }
   19650              : 
   19651          142 :   dump () && dump ("Macro location lwm:%K", macro_locs.first);
   19652          133 :   if (propagated)
   19653            3 :     spans.close ();
   19654              : 
   19655          133 :   dump.outdent ();
   19656          133 :   if (!sec.end (from ()))
   19657              :     return false;
   19658              : 
   19659              :   return true;
   19660          133 : }
   19661              : 
   19662              : /* Serialize the definition of MACRO.  */
   19663              : 
   19664              : void
   19665        78430 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
   19666              : {
   19667        78430 :   sec.u (macro->count);
   19668              : 
   19669        78430 :   bytes_out::bits_out bits = sec.stream_bits ();
   19670        78430 :   bits.b (macro->fun_like);
   19671        78430 :   bits.b (macro->variadic);
   19672        78430 :   bits.b (macro->syshdr);
   19673        78430 :   bits.bflush ();
   19674              : 
   19675        78430 :   write_location (sec, macro->line);
   19676        78430 :   if (macro->fun_like)
   19677              :     {
   19678         9800 :       sec.u (macro->paramc);
   19679         9800 :       const cpp_hashnode *const *parms = macro->parm.params;
   19680        24668 :       for (unsigned ix = 0; ix != macro->paramc; ix++)
   19681        14868 :         sec.cpp_node (parms[ix]);
   19682              :     }
   19683              : 
   19684              :   unsigned len = 0;
   19685       250762 :   for (unsigned ix = 0; ix != macro->count; ix++)
   19686              :     {
   19687       172332 :       const cpp_token *token = &macro->exp.tokens[ix];
   19688       172332 :       write_location (sec, token->src_loc);
   19689       172332 :       sec.u (token->type);
   19690       172332 :       sec.u (token->flags);
   19691       172332 :       switch (cpp_token_val_index (token))
   19692              :         {
   19693            0 :         default:
   19694            0 :           gcc_unreachable ();
   19695              : 
   19696        13064 :         case CPP_TOKEN_FLD_ARG_NO:
   19697              :           /* An argument reference.  */
   19698        13064 :           sec.u (token->val.macro_arg.arg_no);
   19699        13064 :           sec.cpp_node (token->val.macro_arg.spelling);
   19700        13064 :           break;
   19701              : 
   19702        33596 :         case CPP_TOKEN_FLD_NODE:
   19703              :           /* An identifier.  */
   19704        33596 :           sec.cpp_node (token->val.node.node);
   19705        33596 :           if (token->val.node.spelling == token->val.node.node)
   19706              :             /* The spelling will usually be the same.  so optimize
   19707              :                that.  */
   19708        33596 :             sec.str (NULL, 0);
   19709              :           else
   19710            0 :             sec.cpp_node (token->val.node.spelling);
   19711              :           break;
   19712              : 
   19713              :         case CPP_TOKEN_FLD_NONE:
   19714              :           break;
   19715              : 
   19716        56074 :         case CPP_TOKEN_FLD_STR:
   19717              :           /* A string, number or comment.  Not always NUL terminated,
   19718              :              we stream out in a single concatenation with embedded
   19719              :              NULs as that's a safe default.  */
   19720        56074 :           len += token->val.str.len + 1;
   19721        56074 :           sec.u (token->val.str.len);
   19722        56074 :           break;
   19723              : 
   19724            0 :         case CPP_TOKEN_FLD_SOURCE:
   19725            0 :         case CPP_TOKEN_FLD_TOKEN_NO:
   19726            0 :         case CPP_TOKEN_FLD_PRAGMA:
   19727              :           /* These do not occur inside a macro itself.  */
   19728            0 :           gcc_unreachable ();
   19729              :         }
   19730              :     }
   19731              : 
   19732        78430 :   if (len)
   19733              :     {
   19734        52453 :       char *ptr = reinterpret_cast<char *> (sec.buf (len));
   19735        52453 :       len = 0;
   19736       155818 :       for (unsigned ix = 0; ix != macro->count; ix++)
   19737              :         {
   19738       103365 :           const cpp_token *token = &macro->exp.tokens[ix];
   19739       103365 :           if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19740              :             {
   19741        56074 :               memcpy (ptr + len, token->val.str.text,
   19742        56074 :                       token->val.str.len);
   19743        56074 :               len += token->val.str.len;
   19744        56074 :               ptr[len++] = 0;
   19745              :             }
   19746              :         }
   19747              :     }
   19748        78430 : }
   19749              : 
   19750              : /* Read a macro definition.  */
   19751              : 
   19752              : cpp_macro *
   19753          801 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
   19754              : {
   19755          801 :   unsigned count = sec.u ();
   19756              :   /* We rely on knowing cpp_reader's hash table is ident_hash, and
   19757              :      its subobject allocator is stringpool_ggc_alloc and that is just
   19758              :      a wrapper for ggc_alloc_atomic.  */
   19759          801 :   cpp_macro *macro
   19760         1602 :     = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
   19761          801 :                                      + sizeof (cpp_token) * (count - !!count));
   19762          801 :   memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
   19763              : 
   19764          801 :   macro->count = count;
   19765          801 :   macro->kind = cmk_macro;
   19766          801 :   macro->imported_p = true;
   19767              : 
   19768          801 :   bytes_in::bits_in bits = sec.stream_bits ();
   19769          801 :   macro->fun_like = bits.b ();
   19770          801 :   macro->variadic = bits.b ();
   19771          801 :   macro->syshdr = bits.b ();
   19772          801 :   bits.bflush ();
   19773              : 
   19774          801 :   macro->line = read_location (sec);
   19775              : 
   19776          801 :   if (macro->fun_like)
   19777              :     {
   19778           83 :       unsigned paramc = sec.u ();
   19779           83 :       cpp_hashnode **params
   19780           83 :         = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
   19781           83 :       macro->paramc = paramc;
   19782           83 :       macro->parm.params = params;
   19783          177 :       for (unsigned ix = 0; ix != paramc; ix++)
   19784           94 :         params[ix] = sec.cpp_node ();
   19785              :     }
   19786              : 
   19787              :   unsigned len = 0;
   19788         2220 :   for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19789              :     {
   19790         1419 :       cpp_token *token = &macro->exp.tokens[ix];
   19791         1419 :       token->src_loc = read_location (sec);
   19792         1419 :       token->type = cpp_ttype (sec.u ());
   19793         1419 :       token->flags = sec.u ();
   19794         1419 :       switch (cpp_token_val_index (token))
   19795              :         {
   19796            0 :         default:
   19797            0 :           sec.set_overrun ();
   19798            0 :           break;
   19799              : 
   19800           77 :         case CPP_TOKEN_FLD_ARG_NO:
   19801              :           /* An argument reference.  */
   19802           77 :           {
   19803           77 :             unsigned arg_no = sec.u ();
   19804           77 :             if (arg_no - 1 >= macro->paramc)
   19805            0 :               sec.set_overrun ();
   19806           77 :             token->val.macro_arg.arg_no = arg_no;
   19807           77 :             token->val.macro_arg.spelling = sec.cpp_node ();
   19808              :           }
   19809           77 :           break;
   19810              : 
   19811          273 :         case CPP_TOKEN_FLD_NODE:
   19812              :           /* An identifier.  */
   19813          273 :           token->val.node.node = sec.cpp_node ();
   19814          273 :           token->val.node.spelling = sec.cpp_node ();
   19815          273 :           if (!token->val.node.spelling)
   19816          273 :             token->val.node.spelling = token->val.node.node;
   19817              :           break;
   19818              : 
   19819              :         case CPP_TOKEN_FLD_NONE:
   19820              :           break;
   19821              : 
   19822          623 :         case CPP_TOKEN_FLD_STR:
   19823              :           /* A string, number or comment.  */
   19824          623 :           token->val.str.len = sec.u ();
   19825          623 :           len += token->val.str.len + 1;
   19826          623 :           break;
   19827              :         }
   19828              :     }
   19829              : 
   19830          801 :   if (len)
   19831          620 :     if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
   19832              :       {
   19833              :         /* There should be a final NUL.  */
   19834          620 :         if (ptr[len-1])
   19835            0 :           sec.set_overrun ();
   19836              :         /* cpp_alloc_token_string will add a final NUL.  */
   19837          620 :         const unsigned char *buf
   19838          620 :           = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
   19839          620 :         len = 0;
   19840         1563 :         for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19841              :           {
   19842          943 :             cpp_token *token = &macro->exp.tokens[ix];
   19843          943 :             if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19844              :               {
   19845          623 :                 token->val.str.text = buf + len;
   19846          623 :                 len += token->val.str.len;
   19847          623 :                 if (buf[len++])
   19848            0 :                   sec.set_overrun ();
   19849              :               }
   19850              :           }
   19851              :       }
   19852              : 
   19853          801 :   if (sec.get_overrun ())
   19854            0 :     return NULL;
   19855              :   return macro;
   19856          801 : }
   19857              : 
   19858              : /* Exported macro data.  */
   19859              : struct GTY(()) macro_export {
   19860              :   cpp_macro *def;
   19861              :   location_t undef_loc;
   19862              : 
   19863       110395 :   macro_export ()
   19864       110395 :     :def (NULL), undef_loc (UNKNOWN_LOCATION)
   19865              :   {
   19866              :   }
   19867              : };
   19868              : 
   19869              : /* Imported macro data.  */
   19870              : class macro_import {
   19871              : public:
   19872              :   struct slot {
   19873              : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
   19874              :     int offset;
   19875              : #endif
   19876              :     /* We need to ensure we don't use the LSB for representation, as
   19877              :        that's the union discriminator below.  */
   19878              :     unsigned bits;
   19879              : 
   19880              : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
   19881              :     int offset;
   19882              : #endif
   19883              : 
   19884              :   public:
   19885              :     enum Layout {
   19886              :       L_DEF = 1,
   19887              :       L_UNDEF = 2,
   19888              :       L_BOTH = 3,
   19889              :       L_MODULE_SHIFT = 2
   19890              :     };
   19891              : 
   19892              :   public:
   19893              :     /* Not a regular ctor, because we put it in a union, and that's
   19894              :        not allowed in C++ 98.  */
   19895       197038 :     static slot ctor (unsigned module, unsigned defness)
   19896              :     {
   19897       197038 :       gcc_checking_assert (defness);
   19898       197038 :       slot s;
   19899       197038 :       s.bits = defness | (module << L_MODULE_SHIFT);
   19900       197038 :       s.offset = -1;
   19901       197038 :       return s;
   19902              :     }
   19903              : 
   19904              :   public:
   19905       159857 :     unsigned get_defness () const
   19906              :     {
   19907       159857 :       return bits & L_BOTH;
   19908              :     }
   19909       113215 :     unsigned get_module () const
   19910              :     {
   19911       113215 :       return bits >> L_MODULE_SHIFT;
   19912              :     }
   19913           12 :     void become_undef ()
   19914              :     {
   19915           12 :       bits &= ~unsigned (L_DEF);
   19916           12 :       bits |= unsigned (L_UNDEF);
   19917              :     }
   19918              :   };
   19919              : 
   19920              : private:
   19921              :   typedef vec<slot, va_heap, vl_embed> ary_t;
   19922              :   union either {
   19923              :     /* Discriminated by bits 0|1 != 0.  The expected case is that
   19924              :        there will be exactly one slot per macro, hence the effort of
   19925              :        packing that.  */
   19926              :     ary_t *ary;
   19927              :     slot single;
   19928              :   } u;
   19929              : 
   19930              : public:
   19931       160830 :   macro_import ()
   19932       160830 :   {
   19933       160830 :     u.ary = NULL;
   19934              :   }
   19935              : 
   19936              : private:
   19937      8658373 :   bool single_p () const
   19938              :   {
   19939      8658373 :     return u.single.bits & slot::L_BOTH;
   19940              :   }
   19941      8819336 :   bool occupied_p () const
   19942              :   {
   19943      8819336 :     return u.ary != NULL;
   19944              :   }
   19945              : 
   19946              : public:
   19947         2361 :   unsigned length () const
   19948              :   {
   19949         2361 :     gcc_checking_assert (occupied_p ());
   19950         2361 :     return single_p () ? 1 : u.ary->length ();
   19951              :   }
   19952      8510216 :   slot &operator[] (unsigned ix)
   19953              :   {
   19954      8510216 :     gcc_checking_assert (occupied_p ());
   19955      8510216 :     if (single_p ())
   19956              :       {
   19957      8403747 :         gcc_checking_assert (!ix);
   19958      8403747 :         return u.single;
   19959              :       }
   19960              :     else
   19961       106469 :       return (*u.ary)[ix];
   19962              :   }
   19963              : 
   19964              : public:
   19965              :   slot &exported ();
   19966              :   slot &append (unsigned module, unsigned defness);
   19967              : };
   19968              : 
   19969              : /* O is a new import to append to the list for.  If we're an empty
   19970              :    set, initialize us.  */
   19971              : 
   19972              : macro_import::slot &
   19973       197038 : macro_import::append (unsigned module, unsigned defness)
   19974              : {
   19975       197038 :   if (!occupied_p ())
   19976              :     {
   19977       160830 :       u.single = slot::ctor (module, defness);
   19978       160830 :       return u.single;
   19979              :     }
   19980              :   else
   19981              :     {
   19982        36208 :       bool single = single_p ();
   19983        36208 :       ary_t *m = single ? NULL : u.ary;
   19984        36208 :       vec_safe_reserve (m, 1 + single);
   19985        36208 :       if (single)
   19986        36205 :         m->quick_push (u.single);
   19987        36208 :       u.ary = m;
   19988        36208 :       return *u.ary->quick_push (slot::ctor (module, defness));
   19989              :     }
   19990              : }
   19991              : 
   19992              : /* We're going to export something.  Make sure the first import slot
   19993              :    is us.  */
   19994              : 
   19995              : macro_import::slot &
   19996       109721 : macro_import::exported ()
   19997              : {
   19998       109721 :   if (occupied_p () && !(*this)[0].get_module ())
   19999              :     {
   20000          133 :       slot &res = (*this)[0];
   20001          133 :       res.bits |= slot::L_DEF;
   20002          133 :       return res;
   20003              :     }
   20004              : 
   20005       109588 :   slot *a = &append (0, slot::L_DEF);
   20006       109588 :   if (!single_p ())
   20007              :     {
   20008        31865 :       slot &f = (*this)[0];
   20009        31865 :       std::swap (f, *a);
   20010        31865 :       a = &f;
   20011              :     }
   20012              :   return *a;
   20013              : }
   20014              : 
   20015              : /* The import (&exported) macros.  cpp_hasnode's deferred field
   20016              :    indexes this array (offset by 1, so zero means 'not present'.  */
   20017              : 
   20018              : static vec<macro_import, va_heap, vl_embed> *macro_imports;
   20019              : 
   20020              : /* The exported macros.  A macro_import slot's zeroth element's offset
   20021              :    indexes this array.  If the zeroth slot is not for module zero,
   20022              :    there is no export.  */
   20023              : 
   20024              : static GTY(()) vec<macro_export, va_gc> *macro_exports;
   20025              : 
   20026              : /* The reachable set of header imports from this TU.  */
   20027              : 
   20028              : static GTY(()) bitmap headers;
   20029              : 
   20030              : /* Get the (possibly empty) macro imports for NODE.  */
   20031              : 
   20032              : static macro_import &
   20033       165306 : get_macro_imports (cpp_hashnode *node)
   20034              : {
   20035       165306 :   if (node->deferred)
   20036         4476 :     return (*macro_imports)[node->deferred - 1];
   20037              : 
   20038       160830 :   vec_safe_reserve (macro_imports, 1);
   20039       160830 :   node->deferred = macro_imports->length () + 1;
   20040       160830 :   return *vec_safe_push (macro_imports, macro_import ());
   20041              : }
   20042              : 
   20043              : /* Get the macro export for export EXP of NODE.  */
   20044              : 
   20045              : static macro_export &
   20046       109721 : get_macro_export (macro_import::slot &slot)
   20047              : {
   20048       109721 :   if (slot.offset >= 0)
   20049          133 :     return (*macro_exports)[slot.offset];
   20050              : 
   20051       109588 :   vec_safe_reserve (macro_exports, 1);
   20052       109588 :   slot.offset = macro_exports->length ();
   20053       109588 :   return *macro_exports->quick_push (macro_export ());
   20054              : }
   20055              : 
   20056              : /* If NODE is an exportable macro, add it to the export set.  */
   20057              : 
   20058              : static int
   20059      3970888 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
   20060              : {
   20061      3970888 :   bool exporting = false;
   20062              : 
   20063      3970888 :   if (cpp_user_macro_p (node))
   20064       511108 :     if (cpp_macro *macro = node->value.macro)
   20065              :       /* Ignore imported, builtins, command line and forced header macros.  */
   20066       510652 :       if (!macro->imported_p
   20067       510652 :           && !macro->lazy && macro->line >= spans.main_start ())
   20068              :         {
   20069        77856 :           gcc_checking_assert (macro->kind == cmk_macro);
   20070              :           /* I don't want to deal with this corner case, that I suspect is
   20071              :              a devil's advocate reading of the standard.  */
   20072        77856 :           gcc_checking_assert (!macro->extra_tokens);
   20073              : 
   20074        77856 :           macro_import::slot &slot = get_macro_imports (node).exported ();
   20075        77856 :           macro_export &exp = get_macro_export (slot);
   20076        77856 :           exp.def = macro;
   20077        77856 :           exporting = true;
   20078              :         }
   20079              : 
   20080      3893032 :   if (!exporting && node->deferred)
   20081              :     {
   20082          612 :       macro_import &imports = (*macro_imports)[node->deferred - 1];
   20083          612 :       macro_import::slot &slot = imports[0];
   20084          612 :       if (!slot.get_module ())
   20085              :         {
   20086          581 :           gcc_checking_assert (slot.get_defness ());
   20087              :           exporting = true;
   20088              :         }
   20089              :     }
   20090              : 
   20091        77856 :   if (exporting)
   20092        78437 :     static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
   20093              : 
   20094      3970888 :   return 1; /* Don't stop.  */
   20095              : }
   20096              : 
   20097              : /* Order cpp_hashnodes A_ and B_ by their exported macro locations.  */
   20098              : 
   20099              : static int
   20100      4103743 : macro_loc_cmp (const void *a_, const void *b_)
   20101              : {
   20102      4103743 :   const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
   20103      4103743 :   macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
   20104      4103743 :   const macro_export &export_a = (*macro_exports)[import_a[0].offset];
   20105      4103743 :   location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
   20106              : 
   20107      4103743 :   const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
   20108      4103743 :   macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
   20109      4103743 :   const macro_export &export_b = (*macro_exports)[import_b[0].offset];
   20110      4103743 :   location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
   20111              : 
   20112      4103743 :   if (loc_a < loc_b)
   20113              :     return +1;
   20114      2108514 :   else if (loc_a > loc_b)
   20115              :     return -1;
   20116              :   else
   20117            0 :     return 0;
   20118              : }
   20119              : 
   20120              : /* Gather the macro definitions and undefinitions that we will need to
   20121              :    write out.   */
   20122              : 
   20123              : vec<cpp_hashnode *> *
   20124          903 : module_state::prepare_macros (cpp_reader *reader)
   20125              : {
   20126          903 :   vec<cpp_hashnode *> *macros;
   20127          903 :   vec_alloc (macros, 100);
   20128              : 
   20129          903 :   cpp_forall_identifiers (reader, maybe_add_macro, macros);
   20130              : 
   20131          927 :   dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
   20132              : 
   20133          903 :   macros->qsort (macro_loc_cmp);
   20134              : 
   20135              :   // Note the locations.
   20136        80243 :   for (unsigned ix = macros->length (); ix--;)
   20137              :     {
   20138        78437 :       cpp_hashnode *node = (*macros)[ix];
   20139        78437 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20140        78437 :       macro_export &mac = (*macro_exports)[slot.offset];
   20141              : 
   20142        78437 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20143            1 :         continue;
   20144              : 
   20145        78436 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20146           12 :         note_location (mac.undef_loc);
   20147        78436 :       if (mac.def)
   20148              :         {
   20149        78430 :           note_location (mac.def->line);
   20150       250762 :           for (unsigned ix = 0; ix != mac.def->count; ix++)
   20151       172332 :             note_location (mac.def->exp.tokens[ix].src_loc);
   20152              :         }
   20153              :     }
   20154              : 
   20155          903 :   return macros;
   20156              : }
   20157              : 
   20158              : /* Write out the exported defines.  This is two sections, one
   20159              :    containing the definitions, the other a table of node names.  */
   20160              : 
   20161              : unsigned
   20162          903 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
   20163              :                             unsigned *crc_p)
   20164              : {
   20165          970 :   dump () && dump ("Writing macros");
   20166          903 :   dump.indent ();
   20167              : 
   20168              :   /* Write the defs */
   20169          903 :   bytes_out sec (to);
   20170          903 :   sec.begin ();
   20171              : 
   20172          903 :   unsigned count = 0;
   20173        80243 :   for (unsigned ix = macros->length (); ix--;)
   20174              :     {
   20175        78437 :       cpp_hashnode *node = (*macros)[ix];
   20176        78437 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20177        78437 :       gcc_assert (!slot.get_module () && slot.get_defness ());
   20178              : 
   20179        78437 :       macro_export &mac = (*macro_exports)[slot.offset];
   20180        78437 :       gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
   20181              :                   == (mac.undef_loc != UNKNOWN_LOCATION)
   20182              :                   && !!(slot.get_defness () & macro_import::slot::L_DEF)
   20183              :                   == (mac.def != NULL));
   20184              : 
   20185        78437 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20186              :         {
   20187            1 :           warning_at (mac.def->line, 0,
   20188              :                       "not exporting %<#define %E%> as it is a keyword",
   20189              :                       identifier (node));
   20190            1 :           slot.offset = 0;
   20191            1 :           continue;
   20192              :         }
   20193              : 
   20194        78436 :       count++;
   20195        78436 :       slot.offset = sec.pos;
   20196        78436 :       dump (dumper::MACRO)
   20197           24 :         && dump ("Writing macro %s%s%s %I at %u",
   20198           24 :                  slot.get_defness () & macro_import::slot::L_UNDEF
   20199              :                  ? "#undef" : "",
   20200           24 :                  slot.get_defness () == macro_import::slot::L_BOTH
   20201              :                  ? " & " : "",
   20202           24 :                  slot.get_defness () & macro_import::slot::L_DEF
   20203              :                  ? "#define" : "",
   20204              :                  identifier (node), slot.offset);
   20205        78436 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20206           12 :         write_location (sec, mac.undef_loc);
   20207        78436 :       if (mac.def)
   20208        78430 :         write_define (sec, mac.def);
   20209              :     }
   20210          903 :   if (count)
   20211              :     // We may have ended on a tokenless macro with a very short
   20212              :     // location, that will cause problems reading its bit flags.
   20213          145 :     sec.u (0);
   20214          903 :   sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
   20215              : 
   20216          903 :   if (count)
   20217              :     {
   20218              :       /* Write the table.  */
   20219          145 :       bytes_out sec (to);
   20220          145 :       sec.begin ();
   20221          145 :       sec.u (count);
   20222              : 
   20223        78726 :       for (unsigned ix = macros->length (); ix--;)
   20224              :         {
   20225        78436 :           const cpp_hashnode *node = (*macros)[ix];
   20226        78436 :           macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20227              : 
   20228        78436 :           if (slot.offset)
   20229              :             {
   20230        78436 :               sec.cpp_node (node);
   20231        78436 :               sec.u (slot.get_defness ());
   20232        78436 :               sec.u (slot.offset);
   20233              :             }
   20234              :         }
   20235          145 :       sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
   20236          145 :     }
   20237              : 
   20238          903 :   dump.outdent ();
   20239          903 :   return count;
   20240          903 : }
   20241              : 
   20242              : bool
   20243          935 : module_state::read_macros ()
   20244              : {
   20245              :   /* Get the def section.  */
   20246          935 :   if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
   20247              :     return false;
   20248              : 
   20249              :   /* Get the tbl section, if there are defs. */
   20250          935 :   if (slurp->macro_defs.more_p ()
   20251          935 :       && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
   20252              :     return false;
   20253              : 
   20254              :   return true;
   20255              : }
   20256              : 
   20257              : /* Install the macro name table.  */
   20258              : 
   20259              : void
   20260          941 : module_state::install_macros ()
   20261              : {
   20262          941 :   bytes_in &sec = slurp->macro_tbl;
   20263          941 :   if (!sec.size)
   20264              :     return;
   20265              : 
   20266          204 :   dump () && dump ("Reading macro table %M", this);
   20267          182 :   dump.indent ();
   20268              : 
   20269          182 :   unsigned count = sec.u ();
   20270          204 :   dump () && dump ("%u macros", count);
   20271        87632 :   while (count--)
   20272              :     {
   20273        87450 :       cpp_hashnode *node = sec.cpp_node ();
   20274        87450 :       macro_import &imp = get_macro_imports (node);
   20275        87450 :       unsigned flags = sec.u () & macro_import::slot::L_BOTH;
   20276        87450 :       if (!flags)
   20277            0 :         sec.set_overrun ();
   20278              : 
   20279        87450 :       if (sec.get_overrun ())
   20280              :         break;
   20281              : 
   20282        87450 :       macro_import::slot &slot = imp.append (mod, flags);
   20283        87450 :       slot.offset = sec.u ();
   20284              : 
   20285        87450 :       dump (dumper::MACRO)
   20286           84 :         && dump ("Read %s macro %s%s%s %I at %u",
   20287           30 :                  imp.length () > 1 ? "add" : "new",
   20288           27 :                  flags & macro_import::slot::L_UNDEF ? "#undef" : "",
   20289              :                  flags == macro_import::slot::L_BOTH ? " & " : "",
   20290           30 :                  flags & macro_import::slot::L_DEF ? "#define" : "",
   20291              :                  identifier (node), slot.offset);
   20292              : 
   20293              :       /* We'll leak an imported definition's TOKEN_FLD_STR's data
   20294              :          here.  But that only happens when we've had to resolve the
   20295              :          deferred macro before this import -- why are you doing
   20296              :          that?  */
   20297        87450 :       if (cpp_macro *cur = cpp_set_deferred_macro (node))
   20298        31853 :         if (!cur->imported_p)
   20299              :           {
   20300        31853 :             macro_import::slot &slot = imp.exported ();
   20301        31853 :             macro_export &exp = get_macro_export (slot);
   20302        31853 :             exp.def = cur;
   20303       119485 :             dump (dumper::MACRO)
   20304            0 :               && dump ("Saving current #define %I", identifier (node));
   20305              :           }
   20306              :     }
   20307              : 
   20308              :   /* We're now done with the table.  */
   20309          182 :   elf_in::release (slurp->from, sec);
   20310              : 
   20311          182 :   dump.outdent ();
   20312              : }
   20313              : 
   20314              : /* Import the transitive macros.  */
   20315              : 
   20316              : void
   20317          899 : module_state::import_macros ()
   20318              : {
   20319          899 :   bitmap_ior_into (headers, slurp->headers);
   20320              : 
   20321          899 :   bitmap_iterator bititer;
   20322          899 :   unsigned bitnum;
   20323         1840 :   EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
   20324          941 :     (*modules)[bitnum]->install_macros ();
   20325          899 : }
   20326              : 
   20327              : /* NODE is being undefined at LOC.  Record it in the export table, if
   20328              :    necessary.  */
   20329              : 
   20330              : void
   20331       323244 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
   20332              : {
   20333       323244 :   if (!node->deferred)
   20334              :     /* The macro is not imported, so our undef is irrelevant.  */
   20335              :     return;
   20336              : 
   20337           12 :   unsigned n = dump.push (NULL);
   20338              : 
   20339           12 :   macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
   20340           12 :   macro_export &exp = get_macro_export (slot);
   20341              : 
   20342           12 :   exp.undef_loc = loc;
   20343           12 :   slot.become_undef ();
   20344           12 :   exp.def = NULL;
   20345              : 
   20346           18 :   dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
   20347              : 
   20348           12 :   dump.pop (n);
   20349              : }
   20350              : 
   20351              : /* NODE is a deferred macro node.  Determine the definition and return
   20352              :    it, with NULL if undefined.  May issue diagnostics.
   20353              : 
   20354              :    This can leak memory, when merging declarations -- the string
   20355              :    contents (TOKEN_FLD_STR) of each definition are allocated in
   20356              :    unreclaimable cpp objstack.  Only one will win.  However, I do not
   20357              :    expect this to be common -- mostly macros have a single point of
   20358              :    definition.  Perhaps we could restore the objstack to its position
   20359              :    after the first imported definition (if that wins)?  The macros
   20360              :    themselves are GC'd.  */
   20361              : 
   20362              : cpp_macro *
   20363          777 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
   20364              :                               cpp_hashnode *node)
   20365              : {
   20366          777 :   macro_import &imports = (*macro_imports)[node->deferred - 1];
   20367              : 
   20368          777 :   unsigned n = dump.push (NULL);
   20369          783 :   dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
   20370              : 
   20371          777 :   bitmap visible (BITMAP_GGC_ALLOC ());
   20372              : 
   20373          777 :   if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
   20374            0 :         && !imports[0].get_module ()))
   20375              :     {
   20376              :       /* Calculate the set of visible header imports.  */
   20377          777 :       bitmap_copy (visible, headers);
   20378         1861 :       for (unsigned ix = imports.length (); ix--;)
   20379              :         {
   20380         1084 :           const macro_import::slot &slot = imports[ix];
   20381         1084 :           unsigned mod = slot.get_module ();
   20382         1084 :           if ((slot.get_defness () & macro_import::slot::L_UNDEF)
   20383         1084 :               && bitmap_bit_p (visible, mod))
   20384              :             {
   20385           12 :               bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
   20386           12 :               bitmap_and_compl_into (visible, arg);
   20387           12 :               bitmap_set_bit (visible, mod);
   20388              :             }
   20389              :         }
   20390              :     }
   20391          777 :   bitmap_set_bit (visible, 0);
   20392              : 
   20393              :   /* Now find the macros that are still visible.  */
   20394          777 :   bool failed = false;
   20395          777 :   cpp_macro *def = NULL;
   20396          777 :   vec<macro_export> defs;
   20397          777 :   defs.create (imports.length ());
   20398         1861 :   for (unsigned ix = imports.length (); ix--;)
   20399              :     {
   20400         1084 :       const macro_import::slot &slot = imports[ix];
   20401         1084 :       unsigned mod = slot.get_module ();
   20402         1084 :       if (bitmap_bit_p (visible, mod))
   20403              :         {
   20404         1072 :           macro_export *pushed = NULL;
   20405         1072 :           if (mod)
   20406              :             {
   20407          807 :               const module_state *imp = (*modules)[mod];
   20408          807 :               bytes_in &sec = imp->slurp->macro_defs;
   20409          807 :               if (!sec.get_overrun ())
   20410              :                 {
   20411          807 :                   dump (dumper::MACRO)
   20412            6 :                     && dump ("Reading macro %s%s%s %I module %M at %u",
   20413            6 :                              slot.get_defness () & macro_import::slot::L_UNDEF
   20414              :                              ? "#undef" : "",
   20415            6 :                              slot.get_defness () == macro_import::slot::L_BOTH
   20416              :                              ? " & " : "",
   20417            6 :                              slot.get_defness () & macro_import::slot::L_DEF
   20418              :                              ? "#define" : "",
   20419            6 :                              identifier (node), imp, slot.offset);
   20420          807 :                   sec.random_access (slot.offset);
   20421              : 
   20422          807 :                   macro_export exp;
   20423          807 :                   if (slot.get_defness () & macro_import::slot::L_UNDEF)
   20424           12 :                     exp.undef_loc = imp->read_location (sec);
   20425          807 :                   if (slot.get_defness () & macro_import::slot::L_DEF)
   20426          801 :                     exp.def = imp->read_define (sec, reader);
   20427          807 :                   if (sec.get_overrun ())
   20428            0 :                     error_at (loc, "macro definitions of %qE corrupted",
   20429            0 :                               imp->name);
   20430              :                   else
   20431          807 :                     pushed = defs.quick_push (exp);
   20432              :                 }
   20433              :             }
   20434              :           else
   20435          265 :             pushed = defs.quick_push ((*macro_exports)[slot.offset]);
   20436         1072 :           if (pushed && pushed->def)
   20437              :             {
   20438         1066 :               if (!def)
   20439              :                 def = pushed->def;
   20440          292 :               else if (cpp_compare_macros (def, pushed->def))
   20441         1084 :                 failed = true;
   20442              :             }
   20443              :         }
   20444              :     }
   20445              : 
   20446          777 :   if (failed)
   20447              :     {
   20448              :       /* If LOC is the first loc, this is the end of file check, which
   20449              :          is a warning.  */
   20450           15 :       auto_diagnostic_group d;
   20451           15 :       if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
   20452            9 :         warning_at (loc, OPT_Winvalid_imported_macros,
   20453              :                     "inconsistent imported macro definition %qE",
   20454              :                     identifier (node));
   20455              :       else
   20456            6 :         error_at (loc, "inconsistent imported macro definition %qE",
   20457              :                   identifier (node));
   20458           60 :       for (unsigned ix = defs.length (); ix--;)
   20459              :         {
   20460           30 :           macro_export &exp = defs[ix];
   20461           30 :           if (exp.undef_loc)
   20462            0 :             inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
   20463           30 :           if (exp.def)
   20464           30 :             inform (exp.def->line, "%<#define %s%>",
   20465              :                     cpp_macro_definition (reader, node, exp.def));
   20466              :         }
   20467           15 :       def = NULL;
   20468           15 :     }
   20469              : 
   20470          777 :   defs.release ();
   20471              : 
   20472          777 :   dump.pop (n);
   20473              : 
   20474          777 :   return def;
   20475              : }
   20476              : 
   20477              : /* Stream the static aggregates.  Sadly some headers (ahem:
   20478              :    iostream) contain static vars, and rely on them to run global
   20479              :    ctors.  */
   20480              : unsigned
   20481          903 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
   20482              : {
   20483          903 :   if (!static_aggregates && !tls_aggregates)
   20484              :     return 0;
   20485              : 
   20486           45 :   dump () && dump ("Writing initializers");
   20487           45 :   dump.indent ();
   20488              : 
   20489           45 :   static_aggregates = nreverse (static_aggregates);
   20490           45 :   tls_aggregates = nreverse (tls_aggregates);
   20491              : 
   20492           45 :   unsigned count = 0;
   20493           45 :   trees_out sec (to, this, table, ~0u);
   20494           45 :   sec.begin ();
   20495              : 
   20496           45 :   tree list = static_aggregates;
   20497          135 :   for (int passes = 0; passes != 2; passes++)
   20498              :     {
   20499          258 :       for (tree init = list; init; init = TREE_CHAIN (init))
   20500          168 :         if (TREE_LANG_FLAG_0 (init))
   20501              :           {
   20502          144 :             if (STATIC_INIT_DECOMP_BASE_P (init))
   20503              :               {
   20504              :                 /* Ensure that in the returned result chain if the
   20505              :                    STATIC_INIT_DECOMP_*BASE_P flags are set, there is
   20506              :                    always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
   20507              :                    followed by one or more STATIC_INIT_DECOMP_NONBASE_P.  */
   20508           21 :                 int phase = 0;
   20509           21 :                 tree last = NULL_TREE;
   20510           21 :                 for (tree init2 = TREE_CHAIN (init);
   20511          102 :                      init2; init2 = TREE_CHAIN (init2))
   20512              :                   {
   20513          123 :                     if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
   20514              :                       ;
   20515          102 :                     else if (phase == 0
   20516          123 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20517              :                       {
   20518          102 :                         phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
   20519              :                         last = init2;
   20520              :                       }
   20521           81 :                     else if (IN_RANGE (phase, 1, 2)
   20522          162 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20523              :                       {
   20524           60 :                         if (TREE_LANG_FLAG_0 (init2))
   20525           81 :                           phase = 2;
   20526              :                         last = init2;
   20527              :                       }
   20528              :                     else
   20529              :                       break;
   20530              :                   }
   20531           21 :                 if (phase == 2)
   20532              :                   {
   20533              :                     /* In that case, add markers about it so that the
   20534              :                        STATIC_INIT_DECOMP_BASE_P and
   20535              :                        STATIC_INIT_DECOMP_NONBASE_P flags can be restored.  */
   20536           21 :                     sec.tree_node (build_int_cst (integer_type_node,
   20537           21 :                                                   2 * passes + 1));
   20538           21 :                     phase = 1;
   20539          123 :                     for (tree init2 = init; init2 != TREE_CHAIN (last);
   20540          102 :                          init2 = TREE_CHAIN (init2))
   20541          102 :                       if (TREE_LANG_FLAG_0 (init2))
   20542              :                         {
   20543          102 :                           tree decl = TREE_VALUE (init2);
   20544          102 :                           if (phase == 1
   20545          102 :                               && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20546              :                             {
   20547           21 :                               sec.tree_node (build_int_cst (integer_type_node,
   20548           21 :                                                             2 * passes + 2));
   20549           21 :                               phase = 2;
   20550              :                             }
   20551          102 :                           dump ("Initializer:%u for %N", count, decl);
   20552          102 :                           sec.tree_node (decl);
   20553          102 :                           ++count;
   20554              :                         }
   20555           21 :                     sec.tree_node (integer_zero_node);
   20556           21 :                     init = last;
   20557           21 :                     continue;
   20558           21 :                   }
   20559              :               }
   20560              : 
   20561          123 :             tree decl = TREE_VALUE (init);
   20562              : 
   20563          123 :             dump ("Initializer:%u for %N", count, decl);
   20564          123 :             sec.tree_node (decl);
   20565          123 :             ++count;
   20566              :           }
   20567              : 
   20568           90 :       list = tls_aggregates;
   20569              :     }
   20570              : 
   20571           45 :   sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
   20572           45 :   dump.outdent ();
   20573              : 
   20574           45 :   return count;
   20575           45 : }
   20576              : 
   20577              : /* We have to defer some post-load processing until we've completed
   20578              :    reading, because they can cause more reading.  */
   20579              : 
   20580              : static void
   20581        12844 : post_load_processing ()
   20582              : {
   20583              :   /* We mustn't cause a GC, our caller should have arranged for that
   20584              :      not to happen.  */
   20585        12844 :   gcc_checking_assert (function_depth);
   20586              : 
   20587        12844 :   if (!post_load_decls)
   20588              :     return;
   20589              : 
   20590         8222 :   tree old_cfd = current_function_decl;
   20591         8222 :   struct function *old_cfun = cfun;
   20592        17307 :   while (post_load_decls->length ())
   20593              :     {
   20594         9085 :       tree decl = post_load_decls->pop ();
   20595              : 
   20596         9140 :       dump () && dump ("Post-load processing of %N", decl);
   20597              : 
   20598         9085 :       if (VAR_P (decl) && DECL_NTTP_OBJECT_P (decl))
   20599              :         {
   20600            9 :           if (!DECL_SIZE (decl))
   20601              :             {
   20602            3 :               push_to_top_level ();
   20603            3 :               cp_finish_decl (decl, DECL_INITIAL (decl), false, NULL_TREE, 0);
   20604            3 :               pop_from_top_level ();
   20605              :             }
   20606            9 :           continue;
   20607              :         }
   20608              : 
   20609         9076 :       gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
   20610         9076 :       expand_or_defer_fn (decl);
   20611              :       /* As in module_state::read_cluster.  */
   20612          986 :       if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   20613         9409 :           && DECL_NOT_REALLY_EXTERN (decl))
   20614          292 :         DECL_EXTERNAL (decl) = false;
   20615              :     }
   20616              : 
   20617         8222 :   set_cfun (old_cfun);
   20618         8222 :   current_function_decl = old_cfd;
   20619              : }
   20620              : 
   20621              : bool
   20622           45 : module_state::read_inits (unsigned count)
   20623              : {
   20624           45 :   trees_in sec (this);
   20625           45 :   if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
   20626              :     return false;
   20627           57 :   dump () && dump ("Reading %u initializers", count);
   20628           45 :   dump.indent ();
   20629              : 
   20630           45 :   lazy_snum = ~0u;
   20631           45 :   int decomp_phase = 0;
   20632           45 :   tree *aggrp = NULL;
   20633          270 :   for (unsigned ix = 0; ix != count; ix++)
   20634              :     {
   20635          225 :       tree last = NULL_TREE;
   20636          225 :       if (decomp_phase)
   20637          102 :         last = *aggrp;
   20638              :       /* Merely referencing the decl causes its initializer to be read
   20639              :          and added to the correct list.  */
   20640          225 :       tree decl = sec.tree_node ();
   20641              :       /* module_state::write_inits can add special INTEGER_CST markers in
   20642              :          between the decls.  1 means STATIC_INIT_DECOMP_BASE_P entries
   20643              :          follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
   20644              :          entries follow in static_aggregates, 3 means
   20645              :          STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
   20646              :          4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
   20647              :          0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence.  */
   20648          225 :       if (tree_fits_shwi_p (decl))
   20649              :         {
   20650           63 :           if (sec.get_overrun ())
   20651              :             break;
   20652           63 :           decomp_phase = tree_to_shwi (decl);
   20653           63 :           if (decomp_phase)
   20654              :             {
   20655           42 :               aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
   20656              :               last = *aggrp;
   20657              :             }
   20658           63 :           decl = sec.tree_node ();
   20659              :         }
   20660              : 
   20661          225 :       if (sec.get_overrun ())
   20662              :         break;
   20663          225 :       if (decl)
   20664          225 :         dump ("Initializer:%u for %N", ix, decl);
   20665          225 :       if (decomp_phase)
   20666              :         {
   20667          102 :           tree init = *aggrp;
   20668          102 :           gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
   20669          102 :           if ((decomp_phase & 1) != 0)
   20670           21 :             STATIC_INIT_DECOMP_BASE_P (init) = 1;
   20671              :           else
   20672           81 :             STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
   20673              :         }
   20674              :     }
   20675           45 :   if (decomp_phase && !sec.get_overrun ())
   20676              :     {
   20677            0 :       tree decl = sec.tree_node ();
   20678            0 :       gcc_assert (integer_zerop (decl));
   20679              :     }
   20680           45 :   lazy_snum = 0;
   20681           45 :   post_load_processing ();
   20682           45 :   dump.outdent ();
   20683           45 :   if (!sec.end (from ()))
   20684              :     return false;
   20685              :   return true;
   20686           45 : }
   20687              : 
   20688              : void
   20689         2772 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
   20690              :                             unsigned *crc_ptr)
   20691              : {
   20692         2772 :   bytes_out cfg (to);
   20693              : 
   20694         2772 :   cfg.begin ();
   20695              : 
   20696        27720 :   for (unsigned ix = MSC_HWM; ix--;)
   20697        24948 :     cfg.u (counts[ix]);
   20698              : 
   20699         2772 :   if (dump ())
   20700              :     {
   20701          300 :       dump ("Cluster sections are [%u,%u)",
   20702              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20703          300 :       dump ("Bindings %u", counts[MSC_bindings]);
   20704          300 :       dump ("Pendings %u", counts[MSC_pendings]);
   20705          300 :       dump ("Entities %u", counts[MSC_entities]);
   20706          300 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20707          300 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20708          300 :       dump ("Macros %u", counts[MSC_macros]);
   20709          300 :       dump ("Initializers %u", counts[MSC_inits]);
   20710              :     }
   20711              : 
   20712         2772 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
   20713         2772 : }
   20714              : 
   20715              : bool
   20716         2961 : module_state::read_counts (unsigned counts[MSC_HWM])
   20717              : {
   20718         2961 :   bytes_in cfg;
   20719              : 
   20720         2961 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
   20721              :     return false;
   20722              : 
   20723        29610 :   for (unsigned ix = MSC_HWM; ix--;)
   20724        26649 :     counts[ix] = cfg.u ();
   20725              : 
   20726         2961 :   if (dump ())
   20727              :     {
   20728          532 :       dump ("Declaration sections are [%u,%u)",
   20729              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20730          532 :       dump ("Bindings %u", counts[MSC_bindings]);
   20731          532 :       dump ("Pendings %u", counts[MSC_pendings]);
   20732          532 :       dump ("Entities %u", counts[MSC_entities]);
   20733          532 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20734          532 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20735          532 :       dump ("Macros %u", counts[MSC_macros]);
   20736          532 :       dump ("Initializers %u", counts[MSC_inits]);
   20737              :     }
   20738              : 
   20739         2961 :   return cfg.end (from ());
   20740         2961 : }
   20741              : 
   20742              : /* Tool configuration:  MOD_SNAME_PFX .config
   20743              : 
   20744              :    This is data that confirms current state (or fails).  */
   20745              : 
   20746              : void
   20747         2772 : module_state::write_config (elf_out *to, module_state_config &config,
   20748              :                             unsigned inner_crc)
   20749              : {
   20750         2772 :   bytes_out cfg (to);
   20751              : 
   20752         2772 :   cfg.begin ();
   20753              : 
   20754              :   /* Write version and inner crc as u32 values, for easier
   20755              :      debug inspection.  */
   20756         3072 :   dump () && dump ("Writing version=%V, inner_crc=%x",
   20757              :                    MODULE_VERSION, inner_crc);
   20758         2772 :   cfg.u32 (unsigned (MODULE_VERSION));
   20759         2772 :   cfg.u32 (inner_crc);
   20760              : 
   20761         2772 :   cfg.u (to->name (is_header () ? "" : get_flatname ()));
   20762              : 
   20763              :   /* Configuration. */
   20764         3072 :   dump () && dump ("Writing target='%s', host='%s'",
   20765              :                    TARGET_MACHINE, HOST_MACHINE);
   20766         2772 :   unsigned target = to->name (TARGET_MACHINE);
   20767         2772 :   unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
   20768              :                    ? target : to->name (HOST_MACHINE));
   20769         2772 :   cfg.u (target);
   20770         2772 :   cfg.u (host);
   20771              : 
   20772         2772 :   cfg.str (config.dialect_str);
   20773         2772 :   cfg.u (extensions);
   20774              : 
   20775              :   /* Global tree information.  We write the globals crc separately,
   20776              :      rather than mix it directly into the overall crc, as it is used
   20777              :      to ensure data match between instances of the compiler, not
   20778              :      integrity of the file.  */
   20779         3072 :   dump () && dump ("Writing globals=%u, crc=%x",
   20780              :                    fixed_trees->length (), global_crc);
   20781         2772 :   cfg.u (fixed_trees->length ());
   20782         2772 :   cfg.u32 (global_crc);
   20783              : 
   20784         2772 :   if (is_partition ())
   20785          205 :     cfg.u (is_interface ());
   20786              : 
   20787         2772 :   cfg.u (config.num_imports);
   20788         2772 :   cfg.u (config.num_partitions);
   20789         2772 :   cfg.u (config.num_entities);
   20790              : 
   20791         2772 :   cfg.loc (config.ordinary_locs);
   20792         2772 :   cfg.loc (config.macro_locs);
   20793         2772 :   cfg.u (config.loc_range_bits);
   20794              : 
   20795         2772 :   cfg.u (config.active_init);
   20796              : 
   20797              :   /* Now generate CRC, we'll have incorporated the inner CRC because
   20798              :      of its serialization above.  */
   20799         2772 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
   20800         3072 :   dump () && dump ("Writing CRC=%x", crc);
   20801         2772 : }
   20802              : 
   20803              : void
   20804           40 : module_state::note_cmi_name ()
   20805              : {
   20806           40 :   if (!cmi_noted_p && filename)
   20807              :     {
   20808           40 :       cmi_noted_p = true;
   20809           40 :       inform (loc, "compiled module file is %qs",
   20810              :               maybe_add_cmi_prefix (filename));
   20811              :     }
   20812           40 : }
   20813              : 
   20814              : bool
   20815         3071 : module_state::read_config (module_state_config &config, bool complain)
   20816              : {
   20817         3071 :   bytes_in cfg;
   20818              : 
   20819         3071 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
   20820              :     return false;
   20821              : 
   20822              :   /* Check version.  */
   20823         3071 :   unsigned my_ver = MODULE_VERSION;
   20824         3071 :   unsigned their_ver = cfg.u32 ();
   20825         3606 :   dump () && dump  (my_ver == their_ver ? "Version %V"
   20826              :                     : "Expecting %V found %V", my_ver, their_ver);
   20827         3071 :   if (their_ver != my_ver)
   20828              :     {
   20829              :       /* The compiler versions differ.  Close enough? */
   20830            0 :       verstr_t my_string, their_string;
   20831              : 
   20832            0 :       version2string (my_ver, my_string);
   20833            0 :       version2string (their_ver, their_string);
   20834              : 
   20835              :       /* Reject when either is non-experimental or when experimental
   20836              :          major versions differ.  */
   20837            0 :       auto_diagnostic_group d;
   20838            0 :       bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
   20839              :                         || !IS_EXPERIMENTAL (their_ver)
   20840            0 :                         || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
   20841              :                        /* The 'I know what I'm doing' switch.  */
   20842            0 :                        && !flag_module_version_ignore);
   20843            0 :       bool inform_p = true;
   20844            0 :       if (!complain)
   20845              :         inform_p = false;
   20846            0 :       else if (reject_p)
   20847              :         {
   20848            0 :           cfg.set_overrun ();
   20849            0 :           error_at (loc, "compiled module is %sversion %s",
   20850              :                     IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20851              :                     their_string);
   20852              :         }
   20853              :       else
   20854            0 :         inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
   20855              :                              IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20856              :                              their_string);
   20857              : 
   20858            0 :       if (inform_p)
   20859              :         {
   20860            0 :           inform (loc, "compiler is %sversion %s%s%s",
   20861              :                   IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
   20862              :                   my_string,
   20863            0 :                   reject_p ? "" : flag_module_version_ignore
   20864            0 :                   ? ", be it on your own head!" : ", close enough?",
   20865              :                   reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
   20866            0 :           note_cmi_name ();
   20867              :         }
   20868              : 
   20869            0 :       if (reject_p)
   20870            0 :         goto done;
   20871            0 :     }
   20872              : 
   20873              :   /*  We wrote the inner crc merely to merge it, so simply read it
   20874              :       back and forget it.  */
   20875         3071 :   cfg.u32 ();
   20876              : 
   20877              :   /* Check module name.  */
   20878         3071 :   {
   20879         3071 :     const char *their_name = from ()->name (cfg.u ());
   20880         3071 :     const char *our_name = "";
   20881              : 
   20882         3071 :     if (!is_header ())
   20883         2033 :       our_name = get_flatname ();
   20884              : 
   20885              :     /* Header units can be aliased, so name checking is
   20886              :        inappropriate.  */
   20887         3071 :     if (0 != strcmp (their_name, our_name))
   20888              :       {
   20889            0 :         error_at (loc,
   20890            0 :                   their_name[0] && our_name[0] ? G_("module %qs found")
   20891              :                   : their_name[0]
   20892              :                   ? G_("header module expected, module %qs found")
   20893              :                   : G_("module %qs expected, header module found"),
   20894            0 :                   their_name[0] ? their_name : our_name);
   20895            0 :         cfg.set_overrun ();
   20896            0 :         goto done;
   20897              :       }
   20898              :   }
   20899              : 
   20900              :   /* Check the CRC after the above sanity checks, so that the user is
   20901              :      clued in.  */
   20902         3071 :   {
   20903         3071 :     unsigned e_crc = crc;
   20904         3071 :     crc = cfg.get_crc ();
   20905         3606 :     dump () && dump ("Reading CRC=%x", crc);
   20906              :     /* When not complaining we haven't set directness yet, so ignore the
   20907              :        mismatch. */
   20908         3071 :     if (complain && !is_direct () && crc != e_crc)
   20909              :       {
   20910            3 :         error_at (loc, "module %qs CRC mismatch", get_flatname ());
   20911            3 :         cfg.set_overrun ();
   20912            3 :         goto done;
   20913              :       }
   20914              :   }
   20915              : 
   20916              :   /* Check target & host.  */
   20917         3068 :   {
   20918         3068 :     const char *their_target = from ()->name (cfg.u ());
   20919         3068 :     const char *their_host = from ()->name (cfg.u ());
   20920         3603 :     dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
   20921         3068 :     if (strcmp (their_target, TARGET_MACHINE)
   20922         3068 :         || strcmp (their_host, HOST_MACHINE))
   20923              :       {
   20924            0 :         error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
   20925              :                   their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
   20926            0 :         cfg.set_overrun ();
   20927            0 :         goto done;
   20928              :       }
   20929              :   }
   20930              : 
   20931              :   /* Check compilation dialect.  This must match.  */
   20932         3068 :   {
   20933         3068 :     const char *their_dialect = cfg.str ();
   20934         3068 :     if (strcmp (their_dialect, config.dialect_str))
   20935              :       {
   20936            1 :         if (complain)
   20937            1 :           error_at (loc, "language dialect differs %qs, expected %qs",
   20938              :                     their_dialect, config.dialect_str);
   20939            1 :         cfg.set_overrun ();
   20940            1 :         goto done;
   20941              :       }
   20942              :   }
   20943              : 
   20944              :   /* Check for extensions.  If they set any, we must have them set
   20945              :      too.  */
   20946         3067 :   {
   20947         3067 :     unsigned ext = cfg.u ();
   20948         3067 :     unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
   20949         3067 :     if (flag_openmp_simd)
   20950            3 :       allowed |= SE_OPENMP_SIMD;
   20951         3067 :     if (flag_openacc)
   20952            3 :       allowed |= SE_OPENACC;
   20953              : 
   20954         3067 :     if (unsigned bad = ext & ~allowed)
   20955              :       {
   20956            9 :         if (bad & SE_OPENMP)
   20957            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
   20958            6 :         else if (bad & SE_OPENMP_SIMD)
   20959            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
   20960              :                          "%<-fopenmp-simd%> to enable");
   20961            9 :         if (bad & SE_OPENACC)
   20962            3 :           error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
   20963              :                          "enable");
   20964            9 :         cfg.set_overrun ();
   20965            9 :         goto done;
   20966              :       }
   20967         3058 :     extensions = ext;
   20968              :   }
   20969              : 
   20970              :   /* Check global trees.  */
   20971         3058 :   {
   20972         3058 :     unsigned their_fixed_length = cfg.u ();
   20973         3058 :     unsigned their_fixed_crc = cfg.u32 ();
   20974         3593 :     dump () && dump ("Read globals=%u, crc=%x",
   20975              :                      their_fixed_length, their_fixed_crc);
   20976         3058 :     if (!flag_preprocess_only
   20977         3058 :         && (their_fixed_length != fixed_trees->length ()
   20978         3004 :             || their_fixed_crc != global_crc))
   20979              :       {
   20980            0 :         error_at (loc, "fixed tree mismatch");
   20981            0 :         cfg.set_overrun ();
   20982            0 :         goto done;
   20983              :       }
   20984              :   }
   20985              : 
   20986              :   /* All non-partitions are interfaces.  */
   20987         3058 :   interface_p = !is_partition () || cfg.u ();
   20988              : 
   20989         3058 :   config.num_imports = cfg.u ();
   20990         3058 :   config.num_partitions = cfg.u ();
   20991         3058 :   config.num_entities = cfg.u ();
   20992              : 
   20993         3058 :   config.ordinary_locs = cfg.loc ();
   20994         3058 :   config.macro_locs = cfg.loc ();
   20995         3058 :   config.loc_range_bits = cfg.u ();
   20996              : 
   20997         3058 :   config.active_init = cfg.u ();
   20998              : 
   20999         3071 :  done:
   21000         3071 :   return cfg.end (from ());
   21001         3071 : }
   21002              : 
   21003              : /* Comparator for ordering the Ordered Ordinary Location array.  */
   21004              : 
   21005              : static int
   21006          124 : ool_cmp (const void *a_, const void *b_)
   21007              : {
   21008          124 :   auto *a = *static_cast<const module_state *const *> (a_);
   21009          124 :   auto *b = *static_cast<const module_state *const *> (b_);
   21010          124 :   if (a == b)
   21011              :     return 0;
   21012          124 :   else if (a->ordinary_locs.first < b->ordinary_locs.first)
   21013              :     return -1;
   21014              :   else
   21015           52 :     return +1;
   21016              : }
   21017              : 
   21018              : /* Use ELROND format to record the following sections:
   21019              :      qualified-names        : binding value(s)
   21020              :      MOD_SNAME_PFX.README   : human readable, strings
   21021              :      MOD_SNAME_PFX.ENV      : environment strings, strings
   21022              :      MOD_SNAME_PFX.nms      : namespace hierarchy
   21023              :      MOD_SNAME_PFX.udi      : namespace using-directives
   21024              :      MOD_SNAME_PFX.bnd      : binding table
   21025              :      MOD_SNAME_PFX.spc      : specialization table
   21026              :      MOD_SNAME_PFX.imp      : import table
   21027              :      MOD_SNAME_PFX.ent      : entity table
   21028              :      MOD_SNAME_PFX.prt      : partitions table
   21029              :      MOD_SNAME_PFX.olm      : ordinary line maps
   21030              :      MOD_SNAME_PFX.mlm      : macro line maps
   21031              :      MOD_SNAME_PFX.def      : macro definitions
   21032              :      MOD_SNAME_PFX.mac      : macro index
   21033              :      MOD_SNAME_PFX.ini      : inits
   21034              :      MOD_SNAME_PFX.cnt      : counts
   21035              :      MOD_SNAME_PFX.cfg      : config data
   21036              : */
   21037              : 
   21038              : bool
   21039         2801 : module_state::write_begin (elf_out *to, cpp_reader *reader,
   21040              :                            module_state_config &config, unsigned &crc)
   21041              : {
   21042              :   /* Figure out remapped module numbers, which might elide
   21043              :      partitions.  */
   21044         2801 :   bitmap partitions = NULL;
   21045         2801 :   if (!is_header () && !is_partition ())
   21046         1693 :     partitions = BITMAP_GGC_ALLOC ();
   21047         2801 :   write_init_maps ();
   21048              : 
   21049         2801 :   unsigned mod_hwm = 1;
   21050         3488 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   21051              :     {
   21052          687 :       module_state *imp = (*modules)[ix];
   21053              : 
   21054              :       /* Promote any non-partition direct import from a partition, unless
   21055              :          we're a partition.  */
   21056          627 :       if (!is_partition () && !imp->is_partition ()
   21057         1124 :           && imp->is_partition_direct ())
   21058           12 :         imp->directness = MD_PURVIEW_DIRECT;
   21059              : 
   21060              :       /* Write any import that is not a partition, unless we're a
   21061              :          partition.  */
   21062          687 :       if (!partitions || !imp->is_partition ())
   21063          497 :         imp->remap = mod_hwm++;
   21064              :       else
   21065              :         {
   21066          229 :           dump () && dump ("Partition %M %u", imp, ix);
   21067          190 :           bitmap_set_bit (partitions, ix);
   21068          190 :           imp->remap = 0;
   21069              :           /* All interface partitions must be exported.  */
   21070          190 :           if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
   21071              :             {
   21072            3 :               error_at (imp->loc, "interface partition is not exported");
   21073            3 :               bitmap_set_bit (exports, imp->mod);
   21074              :             }
   21075              : 
   21076              :           /* All the partition entities should have been loaded when
   21077              :              loading the partition.  */
   21078              :           if (CHECKING_P)
   21079         1245 :             for (unsigned jx = 0; jx != imp->entity_num; jx++)
   21080              :               {
   21081         1055 :                 binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
   21082         1055 :                 gcc_checking_assert (!slot->is_lazy ());
   21083              :               }
   21084              :         }
   21085              : 
   21086          687 :       if (imp->is_direct () && (imp->remap || imp->is_partition ()))
   21087          669 :         note_location (imp->imported_from ());
   21088              :     }
   21089              : 
   21090         2801 :   if (partitions && bitmap_empty_p (partitions))
   21091              :     /* No partitions present.  */
   21092              :     partitions = nullptr;
   21093              : 
   21094              :   /* Find the set of decls we must write out.  */
   21095         2801 :   depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
   21096              :   /* Add the specializations before the writables, so that we can
   21097              :      detect injected friend specializations.  */
   21098         2801 :   table.add_specializations (true);
   21099         2801 :   table.add_specializations (false);
   21100         2801 :   if (partial_specializations)
   21101              :     {
   21102          215 :       table.add_partial_entities (partial_specializations);
   21103          215 :       partial_specializations = NULL;
   21104              :     }
   21105         2801 :   table.add_namespace_entities (global_namespace, partitions);
   21106         2801 :   if (class_members)
   21107              :     {
   21108           12 :       table.add_class_entities (class_members);
   21109           12 :       class_members = NULL;
   21110              :     }
   21111              : 
   21112              :   /* Now join everything up.  */
   21113         2801 :   table.find_dependencies (this);
   21114              : 
   21115         2801 :   if (!table.finalize_dependencies ())
   21116              :     return false;
   21117              : 
   21118              : #if CHECKING_P
   21119              :   /* We're done verifying at-most once reading, reset to verify
   21120              :      at-most once writing.  */
   21121         2772 :   note_defs = note_defs_table_t::create_ggc (1000);
   21122              : #endif
   21123              : 
   21124              :   /* Determine Strongly Connected Components.  This will also strip any
   21125              :      unnecessary dependencies on imported or TU-local entities.  */
   21126         2772 :   vec<depset *> sccs = table.connect ();
   21127              : 
   21128         2772 :   vec_alloc (ool, modules->length ());
   21129         3452 :   for (unsigned ix = modules->length (); --ix;)
   21130              :     {
   21131          680 :       auto *import = (*modules)[ix];
   21132          680 :       if (import->loadedness > ML_NONE
   21133          680 :           && !(partitions && bitmap_bit_p (partitions, import->mod)))
   21134          490 :         ool->quick_push (import);
   21135              :     }
   21136         2772 :   ool->qsort (ool_cmp);
   21137              : 
   21138         2772 :   write_diagnostic_classification (nullptr, global_dc, nullptr);
   21139              : 
   21140         2772 :   vec<cpp_hashnode *> *macros = nullptr;
   21141         2772 :   if (is_header ())
   21142          903 :     macros = prepare_macros (reader);
   21143              : 
   21144         2772 :   config.num_imports = mod_hwm;
   21145         2772 :   config.num_partitions = modules->length () - mod_hwm;
   21146         2772 :   auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
   21147         2772 :   unsigned counts[MSC_HWM];
   21148         2772 :   memset (counts, 0, sizeof (counts));
   21149              : 
   21150              :   /* depset::cluster is the cluster number,
   21151              :      depset::section is unspecified scratch value.
   21152              : 
   21153              :      The following loops make use of the tarjan property that
   21154              :      dependencies will be earlier in the SCCS array.  */
   21155              : 
   21156              :   /* This first loop determines the number of depsets in each SCC, and
   21157              :      also the number of namespaces we're dealing with.  During the
   21158              :      loop, the meaning of a couple of depset fields now change:
   21159              : 
   21160              :      depset::cluster -> size_of cluster, if first of cluster & !namespace
   21161              :      depset::section -> section number of cluster (if !namespace). */
   21162              : 
   21163         2772 :   unsigned n_spaces = 0;
   21164         2772 :   counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
   21165       321172 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21166              :     {
   21167       318400 :       depset **base = &sccs[ix];
   21168              : 
   21169       602315 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21170              :         {
   21171         5121 :           n_spaces++;
   21172         5121 :           size = 1;
   21173              :         }
   21174              :       else
   21175              :         {
   21176              :           /* Count the members in this cluster.  */
   21177      1389806 :           for (size = 1; ix + size < sccs.length (); size++)
   21178      1387371 :             if (base[size]->cluster != base[0]->cluster)
   21179              :               break;
   21180              : 
   21181      1703085 :           for (unsigned jx = 0; jx != size; jx++)
   21182              :             {
   21183              :               /* Set the section number.  */
   21184      1389806 :               base[jx]->cluster = ~(~0u >> 1); /* A bad value.  */
   21185      1389806 :               base[jx]->section = counts[MSC_sec_hwm];
   21186              :             }
   21187              : 
   21188              :           /* Save the size in the first member's cluster slot.  */
   21189       313279 :           base[0]->cluster = size;
   21190              : 
   21191       313279 :           counts[MSC_sec_hwm]++;
   21192              :         }
   21193              :     }
   21194              : 
   21195              :   /* Write the clusters.  Namespace decls are put in the spaces array.
   21196              :      The meaning of depset::cluster changes to provide the
   21197              :      unnamed-decl count of the depset's decl (and remains zero for
   21198              :      non-decls and non-unnamed).  */
   21199         2772 :   unsigned bytes = 0;
   21200         2772 :   vec<depset *> spaces;
   21201         2772 :   spaces.create (n_spaces);
   21202              : 
   21203       321172 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21204              :     {
   21205       318400 :       depset **base = &sccs[ix];
   21206              : 
   21207       318400 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21208              :         {
   21209         5121 :           tree decl = base[0]->get_entity ();
   21210         5121 :           if (decl == global_namespace)
   21211         2515 :             base[0]->cluster = 0;
   21212         2606 :           else if (!base[0]->is_import ())
   21213              :             {
   21214         2606 :               base[0]->cluster = counts[MSC_entities]++;
   21215         2606 :               spaces.quick_push (base[0]);
   21216         2606 :               counts[MSC_namespaces]++;
   21217         2606 :               if (CHECKING_P)
   21218              :                 {
   21219              :                   /* Add it to the entity map, such that we can tell it is
   21220              :                      part of us.  */
   21221         2606 :                   bool existed;
   21222         2606 :                   unsigned *slot = &entity_map->get_or_insert
   21223         2606 :                     (DECL_UID (decl), &existed);
   21224         2606 :                   if (existed)
   21225              :                     /* It must have come from a partition.  */
   21226            0 :                     gcc_checking_assert
   21227              :                       (import_entity_module (*slot)->is_partition ());
   21228         2606 :                   *slot = ~base[0]->cluster;
   21229              :                 }
   21230       321036 :               dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
   21231              :             }
   21232              :           size = 1;
   21233              :         }
   21234              :       else
   21235              :         {
   21236       313279 :           size = base[0]->cluster;
   21237              : 
   21238              :           /* Cluster is now used to number entities.  */
   21239       313279 :           base[0]->cluster = ~(~0u >> 1); /* A bad value.  */
   21240              : 
   21241       313279 :           sort_cluster (&table, base, size);
   21242              : 
   21243              :           /* Record the section for consistency checking during stream
   21244              :              out -- we don't want to start writing decls in different
   21245              :              sections.  */
   21246       313279 :           table.section = base[0]->section;
   21247       313279 :           bytes += write_cluster (to, base, size, table, counts, &crc);
   21248       313279 :           table.section = 0;
   21249              :         }
   21250              :     }
   21251              : 
   21252              :   /* depset::cluster - entity number (on entities)
   21253              :      depset::section - cluster number  */
   21254              :   /* We'd better have written as many sections and found as many
   21255              :      namespaces as we predicted.  */
   21256         5544 :   gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
   21257              :               && spaces.length () == counts[MSC_namespaces]);
   21258              : 
   21259              :   /* Write the entities.  None happens if we contain namespaces or
   21260              :      nothing. */
   21261         2772 :   config.num_entities = counts[MSC_entities];
   21262         2772 :   if (counts[MSC_entities])
   21263         2509 :     write_entities (to, sccs, counts[MSC_entities], &crc);
   21264              : 
   21265              :   /* Write the namespaces.  */
   21266         2772 :   if (counts[MSC_namespaces])
   21267          590 :     write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
   21268              : 
   21269              :   /* Write any using-directives.  */
   21270         2772 :   if (counts[MSC_namespaces])
   21271          590 :     counts[MSC_using_directives]
   21272          590 :       = write_using_directives (to, table, spaces, &crc);
   21273              : 
   21274              :   /* Write the bindings themselves.  */
   21275         2772 :   counts[MSC_bindings] = write_bindings (to, sccs, &crc);
   21276              : 
   21277              :   /* Write the unnamed.  */
   21278         2772 :   counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
   21279              : 
   21280              :   /* Write the import table.  */
   21281         2772 :   if (config.num_imports > 1)
   21282          459 :     write_imports (to, &crc);
   21283              : 
   21284              :   /* Write elided partition table.  */
   21285         2772 :   if (config.num_partitions)
   21286          136 :     write_partitions (to, config.num_partitions, &crc);
   21287              : 
   21288              :   /* Write the line maps.  */
   21289         2772 :   if (config.ordinary_locs)
   21290              :     {
   21291         2676 :       write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
   21292         2676 :       write_diagnostic_classification (to, global_dc, &crc);
   21293              :     }
   21294         2772 :   if (config.macro_locs)
   21295          124 :     write_macro_maps (to, map_info, &crc);
   21296              : 
   21297         2772 :   if (is_header ())
   21298              :     {
   21299          903 :       counts[MSC_macros] = write_macros (to, macros, &crc);
   21300          903 :       counts[MSC_inits] = write_inits (to, table, &crc);
   21301          903 :       vec_free (macros);
   21302              :     }
   21303              : 
   21304         2772 :   unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21305         2772 :   dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
   21306          300 :                    clusters, (bytes + clusters / 2) / (clusters + !clusters));
   21307         2772 :   trees_out::instrument ();
   21308              : 
   21309         2772 :   write_counts (to, counts, &crc);
   21310              : 
   21311         2772 :   spaces.release ();
   21312         2772 :   sccs.release ();
   21313              : 
   21314         2772 :   vec_free (macro_loc_remap);
   21315         2772 :   vec_free (ord_loc_remap);
   21316         2772 :   vec_free (ool);
   21317              : 
   21318              :   // FIXME:QOI:  Have a command line switch to control more detailed
   21319              :   // information (which might leak data you do not want to leak).
   21320              :   // Perhaps (some of) the write_readme contents should also be
   21321              :   // so-controlled.
   21322         2772 :   if (false)
   21323              :     write_env (to);
   21324              : 
   21325         2772 :   return true;
   21326         2801 : }
   21327              : 
   21328              : // Finish module writing after we've emitted all dynamic initializers.
   21329              : 
   21330              : void
   21331         2772 : module_state::write_end (elf_out *to, cpp_reader *reader,
   21332              :                          module_state_config &config, unsigned &crc)
   21333              : {
   21334              :   /* And finish up.  */
   21335         2772 :   write_config (to, config, crc);
   21336              : 
   21337              :   /* Human-readable info.  */
   21338         2772 :   write_readme (to, reader, config.dialect_str);
   21339              : 
   21340         3072 :   dump () && dump ("Wrote %u sections", to->get_section_limit ());
   21341         2772 : }
   21342              : 
   21343              : /* Initial read of a CMI.  Checks config, loads up imports and line
   21344              :    maps.  */
   21345              : 
   21346              : bool
   21347         3025 : module_state::read_initial (cpp_reader *reader)
   21348              : {
   21349         3025 :   module_state_config config;
   21350         3025 :   bool ok = true;
   21351              : 
   21352         3025 :   if (ok && !read_config (config))
   21353              :     ok = false;
   21354              : 
   21355         3012 :   bool have_locs = ok && read_prepare_maps (&config);
   21356              : 
   21357              :   /* Ordinary maps before the imports.  */
   21358         3012 :   if (!(have_locs && config.ordinary_locs))
   21359           71 :     ordinary_locs.first = line_table->highest_location + 1;
   21360         2954 :   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
   21361              :     ok = false;
   21362              : 
   21363              :   /* Allocate the REMAP vector.  */
   21364         3025 :   slurp->alloc_remap (config.num_imports);
   21365              : 
   21366         3025 :   if (ok)
   21367              :     {
   21368              :       /* Read the import table.  Decrement current to stop this CMI
   21369              :          from being evicted during the import. */
   21370         3012 :       slurp->current--;
   21371         3012 :       if (config.num_imports > 1 && !read_imports (reader, line_table))
   21372              :         ok = false;
   21373         3012 :       slurp->current++;
   21374              :     }
   21375              : 
   21376              :   /* Read the elided partition table, if we're the primary partition.  */
   21377         3012 :   if (ok && config.num_partitions && is_module ()
   21378         3039 :       && !read_partitions (config.num_partitions))
   21379              :     ok = false;
   21380              : 
   21381              :   /* Determine the module's number.  */
   21382         3025 :   gcc_checking_assert (mod == MODULE_UNKNOWN);
   21383         3025 :   gcc_checking_assert (this != this_module ());
   21384              : 
   21385         3025 :   {
   21386              :     /* Allocate space in the entities array now -- that array must be
   21387              :        monotonically in step with the modules array.  */
   21388         3025 :     entity_lwm = vec_safe_length (entity_ary);
   21389         3025 :     entity_num = config.num_entities;
   21390         3025 :     gcc_checking_assert (modules->length () == 1
   21391              :                          || modules->last ()->entity_lwm <= entity_lwm);
   21392         3025 :     vec_safe_reserve (entity_ary, config.num_entities);
   21393              : 
   21394         3025 :     binding_slot slot;
   21395         3025 :     slot.u.binding = NULL_TREE;
   21396      1336503 :     for (unsigned count = config.num_entities; count--;)
   21397      1333478 :       entity_ary->quick_push (slot);
   21398              :   }
   21399              : 
   21400              :   /* We'll run out of other resources before we run out of module
   21401              :      indices.  */
   21402         3025 :   mod = modules->length ();
   21403         3025 :   vec_safe_push (modules, this);
   21404              : 
   21405              :   /* We always import and export ourselves. */
   21406         3025 :   bitmap_set_bit (imports, mod);
   21407         3025 :   bitmap_set_bit (exports, mod);
   21408              : 
   21409         3025 :   if (ok)
   21410         3012 :     (*slurp->remap)[0] = mod << 1;
   21411         3557 :   dump () && dump ("Assigning %M module number %u", this, mod);
   21412              : 
   21413              :   /* We should not have been frozen during the importing done by
   21414              :      read_config.  */
   21415         3025 :   gcc_assert (!from ()->is_frozen ());
   21416              : 
   21417              :   /* Macro maps after the imports.  */
   21418         3025 :   if (!(ok && have_locs && config.macro_locs))
   21419         2892 :     macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   21420          133 :   else if (!read_macro_maps (config.macro_locs))
   21421              :     ok = false;
   21422              : 
   21423              :   /* Diagnostic classification streaming needs to come after reading
   21424              :      macro maps to handle _Pragmas in macros.  */
   21425         3012 :   if (ok && have_locs && config.ordinary_locs
   21426         5979 :       && !read_diagnostic_classification (global_dc))
   21427              :     ok = false;
   21428              : 
   21429              :   /* Note whether there's an active initializer.  */
   21430         3025 :   active_init_p = !is_header () && bool (config.active_init);
   21431              : 
   21432         3025 :   gcc_assert (slurp->current == ~0u);
   21433         3025 :   return ok;
   21434              : }
   21435              : 
   21436              : /* Read a preprocessor state.  */
   21437              : 
   21438              : bool
   21439          941 : module_state::read_preprocessor (bool outermost)
   21440              : {
   21441          941 :   gcc_checking_assert (is_header () && slurp
   21442              :                        && slurp->remap_module (0) == mod);
   21443              : 
   21444          941 :   if (loadedness == ML_PREPROCESSOR)
   21445            6 :     return !(from () && from ()->get_error ());
   21446              : 
   21447          935 :   bool ok = true;
   21448              : 
   21449              :   /* Read direct header imports.  */
   21450          935 :   unsigned len = slurp->remap->length ();
   21451          977 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21452              :     {
   21453           42 :       unsigned map = (*slurp->remap)[ix];
   21454           42 :       if (map & 1)
   21455              :         {
   21456           42 :           module_state *import = (*modules)[map >> 1];
   21457           42 :           if (import->is_header ())
   21458              :             {
   21459           42 :               ok = import->read_preprocessor (false);
   21460           42 :               bitmap_ior_into (slurp->headers, import->slurp->headers);
   21461              :             }
   21462              :         }
   21463              :     }
   21464              : 
   21465              :   /* Record as a direct header.  */
   21466          935 :   if (ok)
   21467          935 :     bitmap_set_bit (slurp->headers, mod);
   21468              : 
   21469          935 :   if (ok && !read_macros ())
   21470              :     ok = false;
   21471              : 
   21472          935 :   loadedness = ML_PREPROCESSOR;
   21473          935 :   announce ("macros");
   21474              : 
   21475          935 :   if (flag_preprocess_only)
   21476              :     /* We're done with the string table.  */
   21477           39 :     from ()->release ();
   21478              : 
   21479          935 :   return check_read (outermost, ok);
   21480              : }
   21481              : 
   21482              : /* Read language state.  */
   21483              : 
   21484              : bool
   21485         3048 : module_state::read_language (bool outermost)
   21486              : {
   21487         3048 :   gcc_checking_assert (!lazy_snum);
   21488              : 
   21489         3048 :   if (loadedness == ML_LANGUAGE)
   21490           87 :     return !(slurp && from () && from ()->get_error ());
   21491              : 
   21492         2961 :   gcc_checking_assert (slurp && slurp->current == ~0u
   21493              :                        && slurp->remap_module (0) == mod);
   21494              : 
   21495         2961 :   bool ok = true;
   21496              : 
   21497              :   /* Read direct imports.  */
   21498         2961 :   unsigned len = slurp->remap->length ();
   21499         3367 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21500              :     {
   21501          406 :       unsigned map = (*slurp->remap)[ix];
   21502          406 :       if (map & 1)
   21503              :         {
   21504          402 :           module_state *import = (*modules)[map >> 1];
   21505          402 :           if (!import->read_language (false))
   21506          406 :             ok = false;
   21507              :         }
   21508              :     }
   21509              : 
   21510         2961 :   unsigned counts[MSC_HWM];
   21511              : 
   21512         2961 :   if (ok && !read_counts (counts))
   21513              :     ok = false;
   21514              : 
   21515         2961 :   function_depth++; /* Prevent unexpected GCs.  */
   21516              : 
   21517         2961 :   if (ok && counts[MSC_entities] != entity_num)
   21518              :     ok = false;
   21519         2961 :   if (ok && counts[MSC_entities]
   21520         2732 :       && !read_entities (counts[MSC_entities],
   21521              :                          counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21522              :     ok = false;
   21523              : 
   21524              :   /* Read the namespace hierarchy.  */
   21525         2961 :   if (ok && counts[MSC_namespaces]
   21526         3570 :       && !read_namespaces (counts[MSC_namespaces]))
   21527              :     ok = false;
   21528              : 
   21529              :   /* Read any using-directives.  */
   21530         2961 :   if (ok && counts[MSC_using_directives]
   21531         3126 :       && !read_using_directives (counts[MSC_using_directives]))
   21532              :     ok = false;
   21533              : 
   21534         2961 :   if (ok && !read_bindings (counts[MSC_bindings],
   21535              :                             counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21536              :     ok = false;
   21537              : 
   21538              :   /* And unnamed.  */
   21539         2961 :   if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
   21540              :     ok = false;
   21541              : 
   21542         2961 :   if (ok)
   21543              :     {
   21544         2961 :       slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21545         2961 :       available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21546              :     }
   21547              : 
   21548         2961 :   if (!flag_module_lazy
   21549         2961 :       || (is_partition ()
   21550          244 :           && module_interface_p ()
   21551          217 :           && !module_partition_p ()))
   21552              :     {
   21553              :       /* Read the sections in forward order, so that dependencies are read
   21554              :          first.  See note about tarjan_connect.  */
   21555          551 :       ggc_collect ();
   21556              : 
   21557          551 :       lazy_snum = ~0u;
   21558              : 
   21559          551 :       unsigned hwm = counts[MSC_sec_hwm];
   21560       144570 :       for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
   21561       144019 :         if (!load_section (ix, NULL))
   21562              :           {
   21563              :             ok = false;
   21564              :             break;
   21565              :           }
   21566          551 :       lazy_snum = 0;
   21567          551 :       post_load_processing ();
   21568              : 
   21569          551 :       ggc_collect ();
   21570              : 
   21571          551 :       if (ok && CHECKING_P)
   21572       517192 :         for (unsigned ix = 0; ix != entity_num; ix++)
   21573       516641 :           gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
   21574              :     }
   21575              : 
   21576              :   // If the import is a header-unit, we need to register initializers
   21577              :   // of any static objects it contains (looking at you _Ioinit).
   21578              :   // Notice, the ordering of these initializers will be that of a
   21579              :   // dynamic initializer at this point in the current TU.  (Other
   21580              :   // instances of these objects in other TUs will be initialized as
   21581              :   // part of that TU's global initializers.)
   21582         2961 :   if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
   21583              :     ok = false;
   21584              : 
   21585         2961 :   function_depth--;
   21586              : 
   21587         3322 :   announce (flag_module_lazy ? "lazy" : "imported");
   21588         2961 :   loadedness = ML_LANGUAGE;
   21589              : 
   21590         2961 :   gcc_assert (slurp->current == ~0u);
   21591              : 
   21592              :   /* We're done with the string table.  */
   21593         2961 :   from ()->release ();
   21594              : 
   21595         2961 :   return check_read (outermost, ok);
   21596              : }
   21597              : 
   21598              : bool
   21599       212243 : module_state::maybe_defrost ()
   21600              : {
   21601       212243 :   bool ok = true;
   21602       212243 :   if (from ()->is_frozen ())
   21603              :     {
   21604            9 :       if (lazy_open >= lazy_limit)
   21605            3 :         freeze_an_elf ();
   21606           18 :       dump () && dump ("Defrosting '%s'", filename);
   21607            9 :       ok = from ()->defrost (maybe_add_cmi_prefix (filename));
   21608            9 :       lazy_open++;
   21609              :     }
   21610              : 
   21611       212243 :   return ok;
   21612              : }
   21613              : 
   21614              : /* Load section SNUM, dealing with laziness.  It doesn't matter if we
   21615              :    have multiple concurrent loads, because we do not use TREE_VISITED
   21616              :    when reading back in.  */
   21617              : 
   21618              : bool
   21619       212243 : module_state::load_section (unsigned snum, binding_slot *mslot)
   21620              : {
   21621       212243 :   if (from ()->get_error ())
   21622              :     return false;
   21623              : 
   21624       212243 :   if (snum >= slurp->current)
   21625            0 :     from ()->set_error (elf::E_BAD_LAZY);
   21626       212243 :   else if (maybe_defrost ())
   21627              :     {
   21628       212243 :       unsigned old_current = slurp->current;
   21629       212243 :       slurp->current = snum;
   21630       212243 :       slurp->lru = 0;  /* Do not swap out.  */
   21631       212243 :       slurp->remaining--;
   21632       212243 :       read_cluster (snum);
   21633       212243 :       slurp->lru = ++lazy_lru;
   21634       212243 :       slurp->current = old_current;
   21635              :     }
   21636              : 
   21637       212243 :   if (mslot && mslot->is_lazy ())
   21638              :     {
   21639              :       /* Oops, the section didn't set this slot.  */
   21640            0 :       from ()->set_error (elf::E_BAD_DATA);
   21641            0 :       *mslot = NULL_TREE;
   21642              :     }
   21643              : 
   21644       212243 :   bool ok = !from ()->get_error ();
   21645       212243 :   if (!ok)
   21646              :     {
   21647            0 :       error_at (loc, "failed to read compiled module cluster %u: %s",
   21648            0 :                 snum, from ()->get_error (filename));
   21649            0 :       note_cmi_name ();
   21650              :     }
   21651              : 
   21652       212243 :   maybe_completed_reading ();
   21653              : 
   21654       212243 :   return ok;
   21655              : }
   21656              : 
   21657              : void
   21658       219148 : module_state::maybe_completed_reading ()
   21659              : {
   21660       219148 :   if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
   21661              :     {
   21662         2442 :       lazy_open--;
   21663              :       /* We no longer need the macros, all tokenizing has been done.  */
   21664         2442 :       slurp->release_macros ();
   21665              : 
   21666         2442 :       from ()->end ();
   21667         2442 :       slurp->close ();
   21668         2442 :       slurped ();
   21669              :     }
   21670       219148 : }
   21671              : 
   21672              : /* After a reading operation, make sure things are still ok.  If not,
   21673              :    emit an error and clean up.  */
   21674              : 
   21675              : bool
   21676         6942 : module_state::check_read (bool outermost, bool ok)
   21677              : {
   21678         6942 :   gcc_checking_assert (!outermost || slurp->current == ~0u);
   21679              : 
   21680         6942 :   if (!ok)
   21681           34 :     from ()->set_error ();
   21682              : 
   21683         6942 :   if (int e = from ()->get_error ())
   21684              :     {
   21685           40 :       auto_diagnostic_group d;
   21686           40 :       error_at (loc, "failed to read compiled module: %s",
   21687           40 :                 from ()->get_error (filename));
   21688           40 :       note_cmi_name ();
   21689              : 
   21690           40 :       if (e == EMFILE
   21691           40 :           || e == ENFILE
   21692              : #if MAPPED_READING
   21693           40 :           || e == ENOMEM
   21694              : #endif
   21695              :           || false)
   21696            0 :         inform (loc, "consider using %<-fno-module-lazy%>,"
   21697              :                 " increasing %<-param-lazy-modules=%u%> value,"
   21698              :                 " or increasing the per-process file descriptor limit",
   21699              :                 param_lazy_modules);
   21700           40 :       else if (e == ENOENT)
   21701           21 :         inform (loc, "imports must be built before being imported");
   21702              : 
   21703           40 :       if (outermost)
   21704           37 :         fatal_error (loc, "returning to the gate for a mechanical issue");
   21705              : 
   21706            3 :       ok = false;
   21707            3 :     }
   21708              : 
   21709         6905 :   maybe_completed_reading ();
   21710              : 
   21711         6905 :   return ok;
   21712              : }
   21713              : 
   21714              : /* Return the IDENTIFIER_NODE naming module IX.  This is the name
   21715              :    including dots.  */
   21716              : 
   21717              : char const *
   21718          408 : module_name (unsigned ix, bool header_ok)
   21719              : {
   21720          408 :   if (modules)
   21721              :     {
   21722          408 :       module_state *imp = (*modules)[ix];
   21723              : 
   21724          408 :       if (ix && !imp->name)
   21725            0 :         imp = imp->parent;
   21726              : 
   21727          408 :       if (header_ok || !imp->is_header ())
   21728          408 :         return imp->get_flatname ();
   21729              :     }
   21730              : 
   21731              :   return NULL;
   21732              : }
   21733              : 
   21734              : /* Return the bitmap describing what modules are imported.  Remember,
   21735              :    we always import ourselves.  */
   21736              : 
   21737              : bitmap
   21738       129024 : get_import_bitmap ()
   21739              : {
   21740       129024 :   return this_module ()->imports;
   21741              : }
   21742              : 
   21743              : /* Get the original decl for an instantiation at TINST, or NULL_TREE
   21744              :    if we're not an instantiation.  */
   21745              : 
   21746              : static tree
   21747       207848 : orig_decl_for_instantiation (tinst_level *tinst)
   21748              : {
   21749       207848 :   if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
   21750              :     return NULL_TREE;
   21751              : 
   21752       101313 :   tree decl = tinst->tldcl;
   21753       101313 :   if (TREE_CODE (decl) == TREE_LIST)
   21754            0 :     decl = TREE_PURPOSE (decl);
   21755       101313 :   if (TYPE_P (decl))
   21756        16602 :     decl = TYPE_NAME (decl);
   21757              :   return decl;
   21758              : }
   21759              : 
   21760              : /* Return the visible imports and path of instantiation for an
   21761              :    instantiation at TINST.  If TINST is nullptr, we're not in an
   21762              :    instantiation, and thus will return the visible imports of the
   21763              :    current TU (and NULL *PATH_MAP_P).   We cache the information on
   21764              :    the tinst level itself.  */
   21765              : 
   21766              : static bitmap
   21767       152743 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
   21768              : {
   21769       152743 :   gcc_checking_assert (modules_p ());
   21770              : 
   21771       152743 :   tree decl = orig_decl_for_instantiation (tinst);
   21772       152743 :   if (!decl)
   21773              :     {
   21774        67880 :       gcc_assert (!tinst || !tinst->next);
   21775              :       /* Not inside an instantiation, just the regular case.  */
   21776        67880 :       *path_map_p = nullptr;
   21777        67880 :       return get_import_bitmap ();
   21778              :     }
   21779              : 
   21780        84863 :   if (!tinst->path)
   21781              :     {
   21782              :       /* Calculate.  */
   21783        25832 :       bitmap visible = path_of_instantiation (tinst->next, path_map_p);
   21784        25832 :       bitmap path_map = *path_map_p;
   21785              : 
   21786        25832 :       if (!path_map)
   21787              :         {
   21788         3384 :           path_map = BITMAP_GGC_ALLOC ();
   21789         3384 :           bitmap_set_bit (path_map, 0);
   21790              :         }
   21791              : 
   21792        25832 :       if (unsigned mod = get_originating_module (decl))
   21793         3867 :         if (!bitmap_bit_p (path_map, mod))
   21794              :           {
   21795              :             /* This is brand new information!  */
   21796          172 :             bitmap new_path = BITMAP_GGC_ALLOC ();
   21797          172 :             bitmap_copy (new_path, path_map);
   21798          172 :             bitmap_set_bit (new_path, mod);
   21799          172 :             path_map = new_path;
   21800              : 
   21801          172 :             bitmap imports = (*modules)[mod]->imports;
   21802          172 :             if (bitmap_intersect_compl_p (imports, visible))
   21803              :               {
   21804              :                 /* IMPORTS contains additional modules to VISIBLE.  */
   21805           33 :                 bitmap new_visible = BITMAP_GGC_ALLOC ();
   21806              : 
   21807           33 :                 bitmap_ior (new_visible, visible, imports);
   21808           33 :                 visible = new_visible;
   21809              :               }
   21810              :           }
   21811              : 
   21812        25832 :       tinst->path = path_map;
   21813        25832 :       tinst->visible = visible;
   21814              :     }
   21815              : 
   21816        84863 :   *path_map_p = tinst->path;
   21817        84863 :   return tinst->visible;
   21818              : }
   21819              : 
   21820              : /* Return the bitmap describing what modules are visible along the
   21821              :    path of instantiation.  If we're not an instantiation, this will be
   21822              :    the visible imports of the TU.  *PATH_MAP_P is filled in with the
   21823              :    modules owning the instantiation path -- we see the module-linkage
   21824              :    entities of those modules.  */
   21825              : 
   21826              : bitmap
   21827     33366629 : visible_instantiation_path (bitmap *path_map_p)
   21828              : {
   21829     33366629 :   if (!modules_p ())
   21830              :     return NULL;
   21831              : 
   21832       126911 :   return path_of_instantiation (current_instantiation (), path_map_p);
   21833              : }
   21834              : 
   21835              : /* Returns the bitmap describing what modules were visible from the
   21836              :    module that the current instantiation originated from.  If we're
   21837              :    not an instantiation, returns NULL.  *MODULE_P is filled in with
   21838              :    the originating module of the definition for this instantiation.  */
   21839              : 
   21840              : bitmap
   21841        55105 : visible_from_instantiation_origination (unsigned *module_p)
   21842              : {
   21843        55105 :   if (!modules_p ())
   21844              :     return NULL;
   21845              : 
   21846        55105 :   tree decl = orig_decl_for_instantiation (current_instantiation ());
   21847        55105 :   if (!decl)
   21848              :     return NULL;
   21849              : 
   21850        16450 :   *module_p = get_originating_module (decl);
   21851        16450 :   return (*modules)[*module_p]->imports;
   21852              : }
   21853              : 
   21854              : /* We've just directly imported IMPORT.  Update our import/export
   21855              :    bitmaps.  IS_EXPORT is true if we're reexporting the OTHER.  */
   21856              : 
   21857              : void
   21858         3115 : module_state::set_import (module_state const *import, bool is_export)
   21859              : {
   21860         3115 :   gcc_checking_assert (this != import);
   21861              : 
   21862              :   /* We see IMPORT's exports (which includes IMPORT).  If IMPORT is
   21863              :      the primary interface or a partition we'll see its imports.  */
   21864         3115 :   bitmap_ior_into (imports, import->is_module () || import->is_partition ()
   21865              :                    ? import->imports : import->exports);
   21866              : 
   21867         3115 :   if (is_export)
   21868              :     /* We'll export OTHER's exports.  */
   21869          486 :     bitmap_ior_into (exports, import->exports);
   21870         3115 : }
   21871              : 
   21872              : /* Return the declaring entity of DECL.  That is the decl determining
   21873              :    how to decorate DECL with module information.  Returns NULL_TREE if
   21874              :    it's the global module.  */
   21875              : 
   21876              : tree
   21877    112017881 : get_originating_module_decl (tree decl)
   21878              : {
   21879              :   /* An enumeration constant.  */
   21880    112017881 :   if (TREE_CODE (decl) == CONST_DECL
   21881         8378 :       && DECL_CONTEXT (decl)
   21882    112026259 :       && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
   21883         7601 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   21884    112010280 :   else if (TREE_CODE (decl) == FIELD_DECL
   21885    111977749 :            || TREE_CODE (decl) == USING_DECL
   21886    223967848 :            || CONST_DECL_USING_P (decl))
   21887              :     {
   21888        53489 :       decl = DECL_CONTEXT (decl);
   21889        53489 :       if (TREE_CODE (decl) != FUNCTION_DECL)
   21890        53489 :         decl = TYPE_NAME (decl);
   21891              :     }
   21892              : 
   21893    112017881 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
   21894              :                        || TREE_CODE (decl) == FUNCTION_DECL
   21895              :                        || TREE_CODE (decl) == TYPE_DECL
   21896              :                        || TREE_CODE (decl) == VAR_DECL
   21897              :                        || TREE_CODE (decl) == CONCEPT_DECL
   21898              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
   21899              : 
   21900    114595819 :   for (;;)
   21901              :     {
   21902              :       /* Uninstantiated template friends are owned by the befriending
   21903              :          class -- not their context.  */
   21904    113306850 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   21905    113306850 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   21906        11626 :         decl = TYPE_NAME (DECL_CHAIN (decl));
   21907              : 
   21908              :       /* An imported temploid friend is attached to the same module the
   21909              :          befriending class was.  */
   21910    113306850 :       if (imported_temploid_friends)
   21911      3821505 :         if (tree *slot = imported_temploid_friends->get (decl))
   21912          741 :           decl = *slot;
   21913              : 
   21914    113306850 :       int use;
   21915    113306850 :       if (tree ti = node_template_info (decl, use))
   21916              :         {
   21917       978716 :           decl = TI_TEMPLATE (ti);
   21918       978716 :           if (TREE_CODE (decl) != TEMPLATE_DECL)
   21919              :             {
   21920              :               /* A friend template specialization.  */
   21921           43 :               gcc_checking_assert (OVL_P (decl));
   21922           43 :               return global_namespace;
   21923              :             }
   21924              :         }
   21925              :       else
   21926              :         {
   21927    112328134 :           tree ctx = CP_DECL_CONTEXT (decl);
   21928    112328134 :           if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21929              :             break;
   21930              : 
   21931       310296 :           if (TYPE_P (ctx))
   21932              :             {
   21933       279069 :               ctx = TYPE_NAME (ctx);
   21934       279069 :               if (!ctx)
   21935              :                 {
   21936              :                   /* Some kind of internal type.  */
   21937            0 :                   gcc_checking_assert (DECL_ARTIFICIAL (decl));
   21938            0 :                   return global_namespace;
   21939              :                 }
   21940              :             }
   21941       310296 :           decl = ctx;
   21942              :         }
   21943      1288969 :     }
   21944              : 
   21945    112017838 :   return decl;
   21946              : }
   21947              : 
   21948              : /* If DECL is imported, return which module imported it, or 0 for the current
   21949              :    module.  Except that if GLOBAL_M1, return -1 for decls attached to the
   21950              :    global module.  */
   21951              : 
   21952              : int
   21953      1598056 : get_originating_module (tree decl, bool global_m1)
   21954              : {
   21955      1598056 :   tree owner = get_originating_module_decl (decl);
   21956      1598056 :   tree not_tmpl = STRIP_TEMPLATE (owner);
   21957              : 
   21958      1598056 :   if (!DECL_LANG_SPECIFIC (not_tmpl))
   21959       471056 :     return global_m1 ? -1 : 0;
   21960              : 
   21961      2202567 :   if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
   21962              :     return -1;
   21963              : 
   21964       118774 :   int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
   21965       118774 :   gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
   21966              :   return mod;
   21967              : }
   21968              : 
   21969              : /* DECL is imported, return which module imported it.
   21970              :    If FLEXIBLE, return -1 if not found, otherwise checking ICE.  */
   21971              : 
   21972              : unsigned
   21973        15862 : get_importing_module (tree decl, bool flexible)
   21974              : {
   21975        15862 :   unsigned index = import_entity_index (decl, flexible);
   21976        15862 :   if (index == ~(~0u >> 1))
   21977              :     return -1;
   21978        15862 :   module_state *module = import_entity_module (index);
   21979              : 
   21980        15862 :   return module->mod;
   21981              : }
   21982              : 
   21983              : /* Is it permissible to redeclare OLDDECL with NEWDECL.
   21984              : 
   21985              :    If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
   21986              :    the current scope's module and attachment.  */
   21987              : 
   21988              : bool
   21989       206699 : module_may_redeclare (tree olddecl, tree newdecl)
   21990              : {
   21991       206699 :   tree decl = olddecl;
   21992       279879 :   for (;;)
   21993              :     {
   21994       243289 :       tree ctx = CP_DECL_CONTEXT (decl);
   21995       243289 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21996              :         // Found the namespace-scope decl.
   21997              :         break;
   21998        38048 :       if (!CLASS_TYPE_P (ctx))
   21999              :         // We've met a non-class scope.  Such a thing is not
   22000              :         // reopenable, so we must be ok.
   22001              :         return true;
   22002        36590 :       decl = TYPE_NAME (ctx);
   22003        36590 :     }
   22004              : 
   22005       205241 :   int use_tpl = 0;
   22006       205241 :   if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
   22007              :     // Specializations of any kind can be redeclared anywhere.
   22008              :     // FIXME: Should we be checking this in more places on the scope chain?
   22009              :     return true;
   22010              : 
   22011       146102 :   module_state *old_mod = get_primary (this_module ());
   22012       146102 :   module_state *new_mod = old_mod;
   22013              : 
   22014       146102 :   tree old_origin = get_originating_module_decl (decl);
   22015       146102 :   tree old_inner = STRIP_TEMPLATE (old_origin);
   22016       146102 :   bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
   22017       222776 :                              && DECL_MODULE_ATTACH_P (old_inner));
   22018       222776 :   if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22019              :     {
   22020          846 :       unsigned index = import_entity_index (old_origin);
   22021          846 :       old_mod = get_primary (import_entity_module (index));
   22022              :     }
   22023              : 
   22024       146102 :   bool newdecl_attached_p = module_attach_p ();
   22025       146102 :   if (newdecl)
   22026              :     {
   22027        36353 :       tree new_origin = get_originating_module_decl (newdecl);
   22028        36353 :       tree new_inner = STRIP_TEMPLATE (new_origin);
   22029        36353 :       newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
   22030        70314 :                             && DECL_MODULE_ATTACH_P (new_inner));
   22031        70314 :       if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
   22032              :         {
   22033          175 :           unsigned index = import_entity_index (new_origin);
   22034          175 :           new_mod = get_primary (import_entity_module (index));
   22035              :         }
   22036              :     }
   22037              : 
   22038              :   /* Module attachment needs to match.  */
   22039       146102 :   if (olddecl_attached_p == newdecl_attached_p)
   22040              :     {
   22041       145958 :       if (!olddecl_attached_p)
   22042              :         /* Both are GM entities, OK.  */
   22043              :         return true;
   22044              : 
   22045         1440 :       if (new_mod == old_mod)
   22046              :         /* Both attached to same named module, OK.  */
   22047              :         return true;
   22048              :     }
   22049              : 
   22050              :   /* Attached to different modules, error.  */
   22051          171 :   decl = newdecl ? newdecl : olddecl;
   22052          171 :   location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
   22053          171 :   if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
   22054              :     {
   22055            3 :       if (newdecl_attached_p)
   22056            3 :         error_at (loc, "declaring %qD in module %qs conflicts with builtin "
   22057              :                   "in global module", decl, new_mod->get_flatname ());
   22058              :       else
   22059            0 :         error_at (loc, "declaration %qD conflicts with builtin", decl);
   22060              :     }
   22061          333 :   else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22062              :     {
   22063          153 :       auto_diagnostic_group d;
   22064          153 :       if (newdecl_attached_p)
   22065           75 :         error_at (loc, "redeclaring %qD in module %qs conflicts with import",
   22066              :                   decl, new_mod->get_flatname ());
   22067              :       else
   22068           78 :         error_at (loc, "redeclaring %qD in global module conflicts with import",
   22069              :                   decl);
   22070              : 
   22071          153 :       if (olddecl_attached_p)
   22072          105 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22073              :                 "import declared attached to module %qs",
   22074              :                 old_mod->get_flatname ());
   22075              :       else
   22076           48 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22077              :                 "import declared in global module");
   22078          153 :     }
   22079              :   else
   22080              :     {
   22081           15 :       auto_diagnostic_group d;
   22082           15 :       if (newdecl_attached_p)
   22083           15 :         error_at (loc, "conflicting declaration of %qD in module %qs",
   22084              :                   decl, new_mod->get_flatname ());
   22085              :       else
   22086            0 :         error_at (loc, "conflicting declaration of %qD in global module",
   22087              :                   decl);
   22088              : 
   22089           15 :       if (olddecl_attached_p)
   22090            0 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22091              :                 "previously declared in module %qs",
   22092              :                 old_mod->get_flatname ());
   22093              :       else
   22094           15 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22095              :                 "previously declared in global module");
   22096           15 :     }
   22097              :   return false;
   22098              : }
   22099              : 
   22100              : /* DECL is being created by this TU.  Record it came from here.  We
   22101              :    record module purview, so we can see if partial or explicit
   22102              :    specialization needs to be written out, even though its purviewness
   22103              :    comes from the most general template.  */
   22104              : 
   22105              : void
   22106    972834421 : set_instantiating_module (tree decl)
   22107              : {
   22108    972834421 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
   22109              :               || VAR_P (decl)
   22110              :               || TREE_CODE (decl) == TYPE_DECL
   22111              :               || TREE_CODE (decl) == CONCEPT_DECL
   22112              :               || TREE_CODE (decl) == TEMPLATE_DECL
   22113              :               || TREE_CODE (decl) == CONST_DECL
   22114              :               || (TREE_CODE (decl) == NAMESPACE_DECL
   22115              :                   && DECL_NAMESPACE_ALIAS (decl)));
   22116              : 
   22117    972834421 :   if (!modules_p ())
   22118              :     return;
   22119              : 
   22120      3784692 :   decl = STRIP_TEMPLATE (decl);
   22121              : 
   22122      3784692 :   if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
   22123       370958 :     retrofit_lang_decl (decl);
   22124              : 
   22125      3784692 :   if (DECL_LANG_SPECIFIC (decl))
   22126              :     {
   22127      2897077 :       DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
   22128              :       /* If this was imported, we'll still be in the entity_hash.  */
   22129      2897077 :       DECL_MODULE_IMPORT_P (decl) = false;
   22130              :     }
   22131              : }
   22132              : 
   22133              : /* If DECL is a class member, whose class is not defined in this TU
   22134              :    (it was imported), remember this decl.  */
   22135              : 
   22136              : void
   22137       152399 : set_defining_module (tree decl)
   22138              : {
   22139       152399 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   22140              :                        || !DECL_MODULE_IMPORT_P (decl));
   22141              : 
   22142       152399 :   if (module_maybe_has_cmi_p ())
   22143              :     {
   22144              :       /* We need to track all declarations within a module, not just those
   22145              :          in the module purview, because we don't necessarily know yet if
   22146              :          this module will require a CMI while in the global fragment.  */
   22147       102506 :       tree ctx = DECL_CONTEXT (decl);
   22148       102506 :       if (ctx
   22149       102506 :           && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
   22150        21472 :           && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
   22151       114077 :           && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
   22152              :         {
   22153              :           /* This entity's context is from an import.  We may need to
   22154              :              record this entity to make sure we emit it in the CMI.
   22155              :              Template specializations are in the template hash tables,
   22156              :              so we don't need to record them here as well.  */
   22157           39 :           int use_tpl = -1;
   22158           39 :           tree ti = node_template_info (decl, use_tpl);
   22159           39 :           if (use_tpl <= 0)
   22160              :             {
   22161           39 :               if (ti)
   22162              :                 {
   22163           21 :                   gcc_checking_assert (!use_tpl);
   22164              :                   /* Get to the TEMPLATE_DECL.  */
   22165           21 :                   decl = TI_TEMPLATE (ti);
   22166              :                 }
   22167              : 
   22168              :               /* Record it on the class_members list.  */
   22169           39 :               vec_safe_push (class_members, decl);
   22170              :             }
   22171              :         }
   22172              :     }
   22173       152399 : }
   22174              : 
   22175              : /* Also remember DECL if it's a newly declared class template partial
   22176              :    specialization, because these are not necessarily added to the
   22177              :    instantiation tables.  */
   22178              : 
   22179              : void
   22180      8347908 : set_defining_module_for_partial_spec (tree decl)
   22181              : {
   22182      8347908 :   if (module_maybe_has_cmi_p ()
   22183        25452 :       && DECL_IMPLICIT_TYPEDEF_P (decl)
   22184      8369339 :       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   22185        21431 :     vec_safe_push (partial_specializations, decl);
   22186      8347908 : }
   22187              : 
   22188              : /* Record that DECL is declared in this TU, and note attachment and
   22189              :    exporting for namespace-scope entities.  FRIEND_P is true if
   22190              :    this is a friend declaration.  */
   22191              : 
   22192              : void
   22193    313203774 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
   22194              : {
   22195    313203774 :   set_instantiating_module (decl);
   22196              : 
   22197              :   /* DECL_CONTEXT may not be set yet when we're called for
   22198              :      non-namespace-scope entities.  */
   22199    313203774 :   if (!DECL_CONTEXT (decl) || !DECL_NAMESPACE_SCOPE_P (decl))
   22200              :     return;
   22201              : 
   22202    116585284 :   gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
   22203              : 
   22204    116585284 :   if (module_attach_p ())
   22205              :     {
   22206         4305 :       retrofit_lang_decl (decl);
   22207         4305 :       DECL_MODULE_ATTACH_P (decl) = true;
   22208              :     }
   22209              : 
   22210              :   /* It is ill-formed to export a declaration with internal linkage.  However,
   22211              :      at the point this function is called we don't yet always know whether this
   22212              :      declaration has internal linkage; instead we defer this check for callers
   22213              :      to do once visibility has been determined.  */
   22214    116585284 :   if (module_exporting_p ())
   22215       158152 :     DECL_MODULE_EXPORT_P (decl) = true;
   22216              : }
   22217              : 
   22218              : /* Checks whether DECL within a module unit has valid linkage for its kind.
   22219              :    Must be called after visibility for DECL has been finalised.  */
   22220              : 
   22221              : void
   22222    393374626 : check_module_decl_linkage (tree decl)
   22223              : {
   22224    393374626 :   if (!module_has_cmi_p ())
   22225              :     return;
   22226              : 
   22227              :   /* A header unit shall not contain a definition of a non-inline function
   22228              :      or variable (not template) whose name has external linkage.  */
   22229       533210 :   if (header_module_p ()
   22230       481576 :       && !processing_template_decl
   22231       159566 :       && ((TREE_CODE (decl) == FUNCTION_DECL
   22232        94406 :            && !DECL_DECLARED_INLINE_P (decl))
   22233       123651 :           || (TREE_CODE (decl) == VAR_DECL
   22234        37274 :               && !DECL_INLINE_VAR_P (decl)))
   22235        45168 :       && decl_defined_p (decl)
   22236         4473 :       && !(DECL_LANG_SPECIFIC (decl)
   22237         4473 :            && DECL_TEMPLATE_INSTANTIATION (decl))
   22238       537683 :       && DECL_EXTERNAL_LINKAGE_P (decl))
   22239           60 :     error_at (DECL_SOURCE_LOCATION (decl),
   22240              :               "external linkage definition of %qD in header module must "
   22241              :               "be declared %<inline%>", decl);
   22242              : 
   22243              :   /* An internal-linkage declaration cannot be generally be exported.
   22244              :      But it's OK to export any declaration from a header unit, including
   22245              :      internal linkage declarations.  */
   22246       533210 :   if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
   22247              :     {
   22248              :       /* Let's additionally treat any exported declaration within an
   22249              :          internal namespace as exporting a declaration with internal
   22250              :          linkage, as this would also implicitly export the internal
   22251              :          linkage namespace.  */
   22252         1995 :       if (decl_anon_ns_mem_p (decl))
   22253              :         {
   22254           50 :           error_at (DECL_SOURCE_LOCATION (decl),
   22255              :                     "exporting declaration %qD declared in unnamed namespace",
   22256              :                     decl);
   22257           50 :           DECL_MODULE_EXPORT_P (decl) = false;
   22258              :         }
   22259         1945 :       else if (decl_linkage (decl) == lk_internal)
   22260              :         {
   22261           17 :           error_at (DECL_SOURCE_LOCATION (decl),
   22262              :                     "exporting declaration %qD with internal linkage", decl);
   22263           17 :           DECL_MODULE_EXPORT_P (decl) = false;
   22264              :         }
   22265              :     }
   22266              : }
   22267              : 
   22268              : /* Given a scope CTX, find the scope we want to attach the key to,
   22269              :    or NULL if no key scope is required.  */
   22270              : 
   22271              : static tree
   22272         2830 : adjust_key_scope (tree ctx)
   22273              : {
   22274              :   /* For members, key it to the containing type to handle deduplication
   22275              :      correctly.  For fields, this is necessary as FIELD_DECLs have no
   22276              :      dep and so would only be streamed after the lambda type, defeating
   22277              :      our ability to merge them.
   22278              : 
   22279              :      Other class-scope key decls might depend on the type of the lambda
   22280              :      but be within the same cluster; we need to ensure that we never
   22281              :      first see the key decl while streaming the lambda type as merging
   22282              :      would then fail when comparing the partially-streamed lambda type
   22283              :      of the key decl with the existing (PR c++/122310).
   22284              : 
   22285              :      Perhaps sort_cluster can be adjusted to handle this better, but
   22286              :      this is a simple workaround (and might down on the number of
   22287              :      entries in keyed_table as a bonus).  */
   22288         4711 :   while (!DECL_NAMESPACE_SCOPE_P (ctx))
   22289         3762 :     if (DECL_CLASS_SCOPE_P (ctx))
   22290         1767 :       ctx = TYPE_NAME (DECL_CONTEXT (ctx));
   22291              :     else
   22292          114 :       ctx = DECL_CONTEXT (ctx);
   22293              : 
   22294         2830 :   return ctx;
   22295              : }
   22296              : 
   22297              : /* DECL is keyed to CTX for odr purposes.  */
   22298              : 
   22299              : void
   22300      1884342 : maybe_key_decl (tree ctx, tree decl)
   22301              : {
   22302      1884342 :   if (!modules_p ())
   22303              :     return;
   22304              : 
   22305              :   /* We only need to deal here with decls attached to var, field,
   22306              :      parm, type, function, or concept decls.  */
   22307        12060 :   if (TREE_CODE (ctx) != VAR_DECL
   22308        12060 :       && TREE_CODE (ctx) != FIELD_DECL
   22309              :       && TREE_CODE (ctx) != PARM_DECL
   22310              :       && TREE_CODE (ctx) != TYPE_DECL
   22311              :       && TREE_CODE (ctx) != FUNCTION_DECL
   22312              :       && TREE_CODE (ctx) != CONCEPT_DECL)
   22313              :     return;
   22314              : 
   22315        24117 :   gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22316              :                        || TREE_CODE (ctx) == FUNCTION_DECL);
   22317              : 
   22318              :   /* We don't need to use the keyed map for functions with definitions,
   22319              :      as we can instead use the MK_local_type handling for streaming.  */
   22320        12060 :   if (TREE_CODE (ctx) == FUNCTION_DECL
   22321        12060 :       && (has_definition (ctx)
   22322              :           /* If we won't be streaming this definition there's also no
   22323              :              need to record the key, as it will not be useful for merging
   22324              :              (this function is non-inline and so a matching declaration
   22325              :              will always be an ODR violation anyway).  */
   22326          120 :           || !module_maybe_has_cmi_p ()))
   22327              :     return;
   22328              : 
   22329          662 :   ctx = adjust_key_scope (ctx);
   22330              : 
   22331          662 :   if (!keyed_table)
   22332          164 :     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22333              : 
   22334          662 :   auto &vec = keyed_table->get_or_insert (ctx);
   22335          662 :   if (!vec.length ())
   22336              :     {
   22337          610 :       retrofit_lang_decl (ctx);
   22338          610 :       DECL_MODULE_KEYED_DECLS_P (ctx) = true;
   22339              :     }
   22340          662 :   if (CHECKING_P)
   22341          824 :     for (tree t : vec)
   22342           58 :       gcc_checking_assert (t != decl);
   22343              : 
   22344          662 :   vec.safe_push (decl);
   22345              : }
   22346              : 
   22347              : /* Find the scope that the local type or lambda DECL is keyed to, if any.  */
   22348              : 
   22349              : static tree
   22350         2204 : get_keyed_decl_scope (tree decl)
   22351              : {
   22352         2204 :   gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   22353              : 
   22354         6612 :   tree scope = (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22355         4180 :                 ? LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))
   22356         2204 :                 : CP_DECL_CONTEXT (decl));
   22357         2204 :   if (!scope)
   22358              :     return NULL_TREE;
   22359              : 
   22360         2168 :   gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
   22361              :                        || TREE_CODE (scope) == FIELD_DECL
   22362              :                        || TREE_CODE (scope) == PARM_DECL
   22363              :                        || TREE_CODE (scope) == TYPE_DECL
   22364              :                        || (TREE_CODE (scope) == FUNCTION_DECL
   22365              :                            && !has_definition (scope))
   22366              :                        || TREE_CODE (scope) == CONCEPT_DECL);
   22367              : 
   22368         2168 :   scope = adjust_key_scope (scope);
   22369              : 
   22370         4336 :   gcc_checking_assert (scope
   22371              :                        && DECL_LANG_SPECIFIC (scope)
   22372              :                        && DECL_MODULE_KEYED_DECLS_P (scope));
   22373              :   return scope;
   22374              : }
   22375              : 
   22376              : /* DECL is an instantiated friend that should be attached to the same
   22377              :    module that ORIG is.  */
   22378              : 
   22379              : void
   22380      2710455 : propagate_defining_module (tree decl, tree orig)
   22381              : {
   22382      2710455 :   if (!modules_p ())
   22383              :     return;
   22384              : 
   22385         7422 :   tree not_tmpl = STRIP_TEMPLATE (orig);
   22386        14788 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
   22387              :     {
   22388          126 :       tree inner = STRIP_TEMPLATE (decl);
   22389          126 :       retrofit_lang_decl (inner);
   22390          126 :       DECL_MODULE_ATTACH_P (inner) = true;
   22391              :     }
   22392              : 
   22393        14788 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
   22394              :     {
   22395          387 :       bool exists = imported_temploid_friends->put (decl, orig);
   22396              : 
   22397              :       /* We should only be called if lookup for an existing decl
   22398              :          failed, in which case there shouldn't already be an entry
   22399              :          in the map.  */
   22400          387 :       gcc_assert (!exists);
   22401              :     }
   22402              : }
   22403              : 
   22404              : /* NEWDECL matched with OLDDECL, transfer defining module information
   22405              :    onto OLDDECL.  We've already validated attachment matches.  */
   22406              : 
   22407              : void
   22408     20070391 : transfer_defining_module (tree olddecl, tree newdecl)
   22409              : {
   22410     20070391 :   if (!modules_p ())
   22411              :     return;
   22412              : 
   22413        61300 :   tree old_inner = STRIP_TEMPLATE (olddecl);
   22414        61300 :   tree new_inner = STRIP_TEMPLATE (newdecl);
   22415              : 
   22416        61300 :   if (DECL_LANG_SPECIFIC (new_inner))
   22417              :     {
   22418        61160 :       gcc_checking_assert (DECL_LANG_SPECIFIC (old_inner));
   22419        61160 :       if (DECL_MODULE_PURVIEW_P (new_inner))
   22420        22767 :         DECL_MODULE_PURVIEW_P (old_inner) = true;
   22421        61160 :       if (!DECL_MODULE_IMPORT_P (new_inner))
   22422        61160 :         DECL_MODULE_IMPORT_P (old_inner) = false;
   22423              :     }
   22424              : 
   22425        61300 :   if (tree *p = imported_temploid_friends->get (newdecl))
   22426              :     {
   22427           70 :       tree orig = *p;
   22428           70 :       tree &slot = imported_temploid_friends->get_or_insert (olddecl);
   22429           70 :       if (!slot)
   22430           47 :         slot = orig;
   22431           23 :       else if (slot != orig)
   22432              :         /* This can happen when multiple classes declare the same
   22433              :            friend function (e.g. g++.dg/modules/tpl-friend-4);
   22434              :            make sure we at least attach to the same module.  */
   22435            3 :         gcc_checking_assert (get_originating_module (slot)
   22436              :                              == get_originating_module (orig));
   22437              :     }
   22438              : }
   22439              : 
   22440              : /* DECL is being freed, clear data we don't need anymore.  */
   22441              : 
   22442              : void
   22443        43835 : remove_defining_module (tree decl)
   22444              : {
   22445        43835 :   if (!modules_p ())
   22446              :     return;
   22447              : 
   22448        43835 :   if (imported_temploid_friends)
   22449        43835 :     imported_temploid_friends->remove (decl);
   22450              : }
   22451              : 
   22452              : /* Create the flat name string.  It is simplest to have it handy.  */
   22453              : 
   22454              : void
   22455         6419 : module_state::set_flatname ()
   22456              : {
   22457         6419 :   gcc_checking_assert (!flatname);
   22458         6419 :   if (parent)
   22459              :     {
   22460          695 :       auto_vec<tree,5> ids;
   22461          695 :       size_t len = 0;
   22462          695 :       char const *primary = NULL;
   22463          695 :       size_t pfx_len = 0;
   22464              : 
   22465          695 :       for (module_state *probe = this;
   22466         1693 :            probe;
   22467          998 :            probe = probe->parent)
   22468         1525 :         if (is_partition () && !probe->is_partition ())
   22469              :           {
   22470          527 :             primary = probe->get_flatname ();
   22471          527 :             pfx_len = strlen (primary);
   22472          527 :             break;
   22473              :           }
   22474              :         else
   22475              :           {
   22476          998 :             ids.safe_push (probe->name);
   22477          998 :             len += IDENTIFIER_LENGTH (probe->name) + 1;
   22478              :           }
   22479              : 
   22480          695 :       char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
   22481          695 :       flatname = flat;
   22482              : 
   22483          695 :       if (primary)
   22484              :         {
   22485          527 :           memcpy (flat, primary, pfx_len);
   22486          527 :           flat += pfx_len;
   22487          527 :           *flat++ = ':';
   22488              :         }
   22489              : 
   22490         1693 :       for (unsigned len = 0; ids.length ();)
   22491              :         {
   22492          998 :           if (len)
   22493          303 :             flat[len++] = '.';
   22494          998 :           tree elt = ids.pop ();
   22495          998 :           unsigned l = IDENTIFIER_LENGTH (elt);
   22496          998 :           memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
   22497          998 :           len += l;
   22498              :         }
   22499          695 :     }
   22500         5724 :   else if (is_header ())
   22501         1922 :     flatname = TREE_STRING_POINTER (name);
   22502              :   else
   22503         3802 :     flatname = IDENTIFIER_POINTER (name);
   22504         6419 : }
   22505              : 
   22506              : /* Open the GCM file and prepare to read.  Return whether that was
   22507              :    successful.  */
   22508              : 
   22509              : bool
   22510         3095 : module_state::open_slurp (cpp_reader *reader)
   22511              : {
   22512         3095 :   if (slurp)
   22513              :     return true;
   22514              : 
   22515         3049 :   if (lazy_open >= lazy_limit)
   22516            9 :     freeze_an_elf ();
   22517              : 
   22518         3049 :   int fd = -1;
   22519         3049 :   int e = ENOENT;
   22520         3049 :   if (filename)
   22521              :     {
   22522         3049 :       const char *file = maybe_add_cmi_prefix (filename);
   22523         3581 :       dump () && dump ("CMI is %s", file);
   22524         3049 :       if (note_module_cmi_yes || inform_cmi_p)
   22525           12 :         inform (loc, "reading CMI %qs", file);
   22526              :       /* Add the CMI file to the dependency tracking. */
   22527         3049 :       if (cpp_get_deps (reader))
   22528           15 :         deps_add_dep (cpp_get_deps (reader), file);
   22529         3049 :       fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
   22530         3049 :       e = errno;
   22531              :     }
   22532              : 
   22533         3049 :   gcc_checking_assert (!slurp);
   22534         6074 :   slurp = new slurping (new elf_in (fd, e));
   22535              : 
   22536         3049 :   bool ok = from ()->begin (loc);
   22537         3049 :   if (ok)
   22538              :     {
   22539         3025 :       lazy_open++;
   22540         3025 :       slurp->lru = ++lazy_lru;
   22541              :     }
   22542              :   return ok;
   22543              : }
   22544              : 
   22545              : /* Return whether importing this GCM would work without an error in
   22546              :    read_config.  */
   22547              : 
   22548              : bool
   22549           52 : module_state::check_importable (cpp_reader *reader)
   22550              : {
   22551           52 :   if (loadedness > ML_CONFIG)
   22552              :     return true;
   22553           49 :   if (!open_slurp (reader))
   22554              :     return false;
   22555           46 :   module_state_config config;
   22556           46 :   return read_config (config, /*complain*/false);
   22557              : }
   22558              : 
   22559              : /* Read the CMI file for a module.  */
   22560              : 
   22561              : bool
   22562         3046 : module_state::do_import (cpp_reader *reader, bool outermost)
   22563              : {
   22564         3046 :   gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
   22565              : 
   22566              :   /* If this TU is a partition of the module we're importing,
   22567              :      that module is the primary module interface.  */
   22568         3046 :   if (this_module ()->is_partition ()
   22569         3100 :       && this == get_primary (this_module ()))
   22570            9 :     module_p = true;
   22571              : 
   22572         3046 :   loc = linemap_module_loc (line_table, loc, get_flatname ());
   22573              : 
   22574         3046 :   bool ok = open_slurp (reader);
   22575         3046 :   if (!from ()->get_error ())
   22576              :     {
   22577         3025 :       announce ("importing");
   22578         3025 :       loadedness = ML_CONFIG;
   22579         3025 :       ok = read_initial (reader);
   22580              :     }
   22581              : 
   22582         3046 :   gcc_assert (slurp->current == ~0u);
   22583              : 
   22584         3046 :   return check_read (outermost, ok);
   22585              : }
   22586              : 
   22587              : /* Attempt to increase the file descriptor limit.  */
   22588              : 
   22589              : static bool
   22590         4956 : try_increase_lazy (unsigned want)
   22591              : {
   22592         4956 :   gcc_checking_assert (lazy_open >= lazy_limit);
   22593              : 
   22594              :   /* If we're increasing, saturate at hard limit.  */
   22595         4956 :   if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
   22596         4956 :     want = lazy_hard_limit;
   22597              : 
   22598              : #if HAVE_SETRLIMIT
   22599         4956 :   if ((!lazy_limit || !param_lazy_modules)
   22600         4944 :       && lazy_hard_limit
   22601         4944 :       && want <= lazy_hard_limit)
   22602              :     {
   22603         4944 :       struct rlimit rlimit;
   22604         4944 :       rlimit.rlim_cur = want + LAZY_HEADROOM;
   22605         4944 :       rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
   22606         4944 :       if (!setrlimit (RLIMIT_NOFILE, &rlimit))
   22607         4944 :         lazy_limit = want;
   22608              :     }
   22609              : #endif
   22610              : 
   22611         4956 :   return lazy_open < lazy_limit;
   22612              : }
   22613              : 
   22614              : /* Pick a victim module to freeze its reader.  */
   22615              : 
   22616              : void
   22617           12 : module_state::freeze_an_elf ()
   22618              : {
   22619           12 :   if (try_increase_lazy (lazy_open * 2))
   22620              :     return;
   22621              : 
   22622           12 :   module_state *victim = NULL;
   22623           12 :   for (unsigned ix = modules->length (); ix--;)
   22624              :     {
   22625           30 :       module_state *candidate = (*modules)[ix];
   22626           30 :       if (candidate && candidate->slurp && candidate->slurp->lru
   22627           60 :           && candidate->from ()->is_freezable ()
   22628           39 :           && (!victim || victim->slurp->lru > candidate->slurp->lru))
   22629              :         victim = candidate;
   22630              :     }
   22631              : 
   22632           12 :   if (victim)
   22633              :     {
   22634           18 :       dump () && dump ("Freezing '%s'", victim->filename);
   22635            9 :       if (victim->slurp->macro_defs.size)
   22636              :         /* Save the macro definitions to a buffer.  */
   22637            0 :         victim->from ()->preserve (victim->slurp->macro_defs);
   22638            9 :       if (victim->slurp->macro_tbl.size)
   22639              :         /* Save the macro definitions to a buffer.  */
   22640            0 :         victim->from ()->preserve (victim->slurp->macro_tbl);
   22641            9 :       victim->from ()->freeze ();
   22642            9 :       lazy_open--;
   22643              :     }
   22644              :   else
   22645            3 :     dump () && dump ("No module available for freezing");
   22646              : }
   22647              : 
   22648              : /* Load the lazy slot *MSLOT, INDEX'th slot of the module.  */
   22649              : 
   22650              : bool
   22651        62306 : module_state::lazy_load (unsigned index, binding_slot *mslot)
   22652              : {
   22653        62306 :   unsigned n = dump.push (this);
   22654              : 
   22655        62306 :   gcc_checking_assert (function_depth);
   22656              : 
   22657        62306 :   unsigned cookie = mslot->get_lazy ();
   22658        62306 :   unsigned snum = cookie >> 2;
   22659        62714 :   dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
   22660              : 
   22661        62306 :   bool ok = load_section (snum, mslot);
   22662              : 
   22663        62306 :   dump.pop (n);
   22664              : 
   22665        62306 :   return ok;
   22666              : }
   22667              : 
   22668              : /* Load MOD's binding for NS::ID into *MSLOT.  *MSLOT contains the
   22669              :    lazy cookie.  OUTER is true if this is the outermost lazy, (used
   22670              :    for diagnostics).  */
   22671              : 
   22672              : void
   22673         5918 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
   22674              : {
   22675         5918 :   int count = errorcount + warningcount;
   22676              : 
   22677         5918 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22678              : 
   22679              :   /* Make sure lazy loading from a template context behaves as if
   22680              :      from a non-template context.  */
   22681         5918 :   processing_template_decl_sentinel ptds;
   22682              : 
   22683              :   /* Stop GC happening, even in outermost loads (because our caller
   22684              :      could well be building up a lookup set).  */
   22685         5918 :   function_depth++;
   22686              : 
   22687         5918 :   gcc_checking_assert (mod);
   22688         5918 :   module_state *module = (*modules)[mod];
   22689         5918 :   unsigned n = dump.push (module);
   22690              : 
   22691         5918 :   unsigned snum = mslot->get_lazy ();
   22692         6273 :   dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
   22693              :                    module->name, snum);
   22694              : 
   22695         5918 :   bool ok = !recursive_lazy (snum);
   22696         5918 :   if (ok)
   22697              :     {
   22698         5918 :       ok = module->load_section (snum, mslot);
   22699         5918 :       lazy_snum = 0;
   22700         5918 :       post_load_processing ();
   22701              :     }
   22702              : 
   22703         5918 :   dump.pop (n);
   22704              : 
   22705         5918 :   function_depth--;
   22706              : 
   22707         5918 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22708              : 
   22709         5918 :   if (!ok)
   22710            0 :     fatal_error (input_location,
   22711            0 :                  module->is_header ()
   22712              :                  ? G_("failed to load binding %<%E%s%E%>")
   22713              :                  : G_("failed to load binding %<%E%s%E@%s%>"),
   22714            0 :                  ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22715              :                  module->get_flatname ());
   22716              : 
   22717         5918 :   if (count != errorcount + warningcount)
   22718           27 :     inform (input_location,
   22719           27 :             module->is_header ()
   22720              :             ? G_("during load of binding %<%E%s%E%>")
   22721              :             : G_("during load of binding %<%E%s%E@%s%>"),
   22722           27 :             ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22723              :             module->get_flatname ());
   22724         5918 : }
   22725              : 
   22726              : /* Load any pending entities keyed to NS and NAME.
   22727              :    Used to find pending types if we don't yet have a decl built.  */
   22728              : 
   22729              : void
   22730     37925392 : lazy_load_pendings (tree ns, tree name)
   22731              : {
   22732              :   /* Make sure lazy loading from a template context behaves as if
   22733              :      from a non-template context.  */
   22734     37925392 :   processing_template_decl_sentinel ptds;
   22735              : 
   22736     37925392 :   pending_key key;
   22737     37925392 :   key.ns = ns;
   22738     37925392 :   key.id = name;
   22739              : 
   22740     37925392 :   auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
   22741     37920444 :   if (!pending_vec)
   22742     37919062 :     return;
   22743              : 
   22744         6330 :   int count = errorcount + warningcount;
   22745              : 
   22746         6330 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22747         6330 :   bool ok = !recursive_lazy ();
   22748         6330 :   if (ok)
   22749              :     {
   22750         6330 :       function_depth++; /* Prevent GC */
   22751         6330 :       unsigned n = dump.push (NULL);
   22752         6834 :       dump () && dump ("Reading %u pending entities keyed to %P",
   22753              :                        pending_vec->length (), key.ns, key.id);
   22754         6330 :       for (unsigned ix = pending_vec->length (); ix--;)
   22755              :         {
   22756        71032 :           unsigned index = (*pending_vec)[ix];
   22757        71032 :           binding_slot *slot = &(*entity_ary)[index];
   22758              : 
   22759        71032 :           if (slot->is_lazy ())
   22760              :             {
   22761         6829 :               module_state *import = import_entity_module (index);
   22762         6829 :               if (!import->lazy_load (index - import->entity_lwm, slot))
   22763        71032 :                 ok = false;
   22764              :             }
   22765       141565 :           else if (dump ())
   22766              :             {
   22767          363 :               module_state *import = import_entity_module (index);
   22768          363 :               dump () && dump ("Entity %M[%u] already loaded",
   22769          363 :                                import, index - import->entity_lwm);
   22770              :             }
   22771              :         }
   22772              : 
   22773         6330 :       pending_table->remove (key);
   22774         6330 :       dump.pop (n);
   22775         6330 :       lazy_snum = 0;
   22776         6330 :       post_load_processing ();
   22777         6330 :       function_depth--;
   22778              :     }
   22779              : 
   22780         6330 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22781              : 
   22782         6330 :   if (!ok)
   22783            0 :     fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
   22784            0 :                  key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22785              : 
   22786         6330 :   if (count != errorcount + warningcount)
   22787            0 :     inform (input_location, "during load of pendings for %<%E%s%E%>",
   22788            0 :             key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22789     37925392 : }
   22790              : 
   22791              : /* Load any pending entities keyed to the top-key of DECL.  */
   22792              : 
   22793              : void
   22794     37832350 : lazy_load_pendings (tree decl)
   22795              : {
   22796     37832350 :   tree key_decl;
   22797     37832350 :   tree ns = find_pending_key (decl, &key_decl);
   22798     37832350 :   return lazy_load_pendings (ns, DECL_NAME (key_decl));
   22799              : }
   22800              : 
   22801              : static void
   22802         2764 : direct_import (module_state *import, cpp_reader *reader)
   22803              : {
   22804         2764 :   timevar_start (TV_MODULE_IMPORT);
   22805         2764 :   unsigned n = dump.push (import);
   22806              : 
   22807         2764 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22808         2764 :   if (import->loadedness == ML_NONE)
   22809         1819 :     if (!import->do_import (reader, true))
   22810            0 :       gcc_unreachable ();
   22811              : 
   22812         2727 :   this_module ()->set_import (import, import->exported_p);
   22813              : 
   22814         2727 :   if (import->loadedness < ML_LANGUAGE)
   22815              :     {
   22816         2646 :       if (!keyed_table)
   22817         2322 :         keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22818         2646 :       import->read_language (true);
   22819              :     }
   22820              : 
   22821         2727 :   dump.pop (n);
   22822         2727 :   timevar_stop (TV_MODULE_IMPORT);
   22823         2727 : }
   22824              : 
   22825              : /* Import module IMPORT.  */
   22826              : 
   22827              : void
   22828         2528 : import_module (module_state *import, location_t from_loc, bool exporting_p,
   22829              :                tree, cpp_reader *reader)
   22830              : {
   22831              :   /* A non-partition implementation unit has no name.  */
   22832         2528 :   if (!this_module ()->name && this_module ()->parent == import)
   22833              :     {
   22834            3 :       auto_diagnostic_group d;
   22835            3 :       error_at (from_loc, "import of %qs within its own implementation unit",
   22836              :                 import->get_flatname());
   22837            3 :       inform (import->loc, "module declared here");
   22838            3 :       return;
   22839            3 :     }
   22840              : 
   22841         2525 :   if (!import->check_circular_import (from_loc))
   22842              :     return;
   22843              : 
   22844         2519 :   if (!import->is_header () && current_lang_depth ())
   22845              :     /* Only header units should appear inside language
   22846              :        specifications.  The std doesn't specify this, but I think
   22847              :        that's an error in resolving US 033, because language linkage
   22848              :        is also our escape clause to getting things into the global
   22849              :        module, so we don't want to confuse things by having to think
   22850              :        about whether 'extern "C++" { import foo; }' puts foo's
   22851              :        contents into the global module all of a sudden.  */
   22852            6 :     warning (0, "import of named module %qs inside language-linkage block",
   22853              :              import->get_flatname ());
   22854              : 
   22855         2519 :   if (exporting_p || module_exporting_p ())
   22856          331 :     import->exported_p = true;
   22857              : 
   22858         2519 :   if (import->loadedness != ML_NONE)
   22859              :     {
   22860          942 :       from_loc = ordinary_loc_of (line_table, from_loc);
   22861          942 :       linemap_module_reparent (line_table, import->loc, from_loc);
   22862              :     }
   22863              : 
   22864         2519 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22865              : 
   22866         2519 :   direct_import (import, reader);
   22867              : }
   22868              : 
   22869              : /* Declare the name of the current module to be NAME.  EXPORTING_p is
   22870              :    true if this TU is the exporting module unit.  */
   22871              : 
   22872              : void
   22873         3176 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
   22874              :                 tree, cpp_reader *reader)
   22875              : {
   22876         3176 :   gcc_assert (global_namespace == current_scope ());
   22877              : 
   22878         3176 :   module_state *current = this_module ();
   22879         3176 :   if (module_purview_p () || module->loadedness > ML_CONFIG)
   22880              :     {
   22881            6 :       auto_diagnostic_group d;
   22882           12 :       error_at (from_loc, module_purview_p ()
   22883              :                 ? G_("module already declared")
   22884              :                 : G_("module already imported"));
   22885            6 :       if (module_purview_p ())
   22886            0 :         module = current;
   22887           12 :       inform (module->loc, module_purview_p ()
   22888              :               ? G_("module %qs declared here")
   22889              :               : G_("module %qs imported here"),
   22890              :               module->get_flatname ());
   22891            6 :       return;
   22892            6 :     }
   22893              : 
   22894         3170 :   gcc_checking_assert (module->is_module ());
   22895         3170 :   gcc_checking_assert (module->is_direct () && module->has_location ());
   22896              : 
   22897              :   /* Yer a module, 'arry.  */
   22898         3170 :   module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
   22899              : 
   22900              :   // Even in header units, we consider the decls to be purview
   22901         3170 :   module_kind |= MK_PURVIEW;
   22902              : 
   22903         3170 :   if (module->is_partition ())
   22904          214 :     module_kind |= MK_PARTITION;
   22905         3170 :   if (exporting_p)
   22906              :     {
   22907         2857 :       module->interface_p = true;
   22908         2857 :       module_kind |= MK_INTERFACE;
   22909              :     }
   22910              : 
   22911         3170 :   if (module_has_cmi_p ())
   22912              :     {
   22913              :       /* Copy the importing information we may have already done.  We
   22914              :          do not need to separate out the imports that only happen in
   22915              :          the GMF, in spite of what the literal wording of the std
   22916              :          might imply.  See p2191, the core list had a discussion
   22917              :          where the module implementors agreed that the GMF of a named
   22918              :          module is invisible to importers.  */
   22919         2925 :       module->imports = current->imports;
   22920              : 
   22921         2925 :       module->mod = 0;
   22922         2925 :       (*modules)[0] = module;
   22923              :     }
   22924              :   else
   22925              :     {
   22926          245 :       module->interface_p = true;
   22927          245 :       current->parent = module; /* So mangler knows module identity. */
   22928          245 :       direct_import (module, reader);
   22929              :     }
   22930              : }
   22931              : 
   22932              : /* Return true IFF we must emit a module global initializer function
   22933              :    (which will be called by importers' init code).  */
   22934              : 
   22935              : bool
   22936       108009 : module_global_init_needed ()
   22937              : {
   22938       108009 :   return module_has_cmi_p () && !header_module_p ();
   22939              : }
   22940              : 
   22941              : /* Calculate which, if any, import initializers need calling.  */
   22942              : 
   22943              : bool
   22944       100480 : module_determine_import_inits ()
   22945              : {
   22946       100480 :   if (!modules || header_module_p ())
   22947              :     return false;
   22948              : 
   22949              :   /* Prune active_init_p.  We need the same bitmap allocation
   22950              :      scheme as for the imports member.  */
   22951         3851 :   function_depth++; /* Disable GC.  */
   22952         3851 :   bitmap covered_imports (BITMAP_GGC_ALLOC ());
   22953              : 
   22954         3851 :   bool any = false;
   22955              : 
   22956              :   /* Because indirect imports are before their direct import, and
   22957              :      we're scanning the array backwards, we only need one pass!  */
   22958         6713 :   for (unsigned ix = modules->length (); --ix;)
   22959              :     {
   22960         2862 :       module_state *import = (*modules)[ix];
   22961              : 
   22962         2862 :       if (!import->active_init_p)
   22963              :         ;
   22964           64 :       else if (bitmap_bit_p (covered_imports, ix))
   22965            9 :         import->active_init_p = false;
   22966              :       else
   22967              :         {
   22968              :           /* Everything this imports is therefore handled by its
   22969              :              initializer, so doesn't need initializing by us.  */
   22970           55 :           bitmap_ior_into (covered_imports, import->imports);
   22971           55 :           any = true;
   22972              :         }
   22973              :     }
   22974         3851 :   function_depth--;
   22975              : 
   22976         3851 :   return any;
   22977              : }
   22978              : 
   22979              : /* Emit calls to each direct import's global initializer.  Including
   22980              :    direct imports of directly imported header units.  The initializers
   22981              :    of (static) entities in header units will be called by their
   22982              :    importing modules (for the instance contained within that), or by
   22983              :    the current TU (for the instances we've brought in).  Of course
   22984              :    such header unit behaviour is evil, but iostream went through that
   22985              :    door some time ago.  */
   22986              : 
   22987              : void
   22988           55 : module_add_import_initializers ()
   22989              : {
   22990           55 :   if (!modules || header_module_p ())
   22991            0 :     return;
   22992              : 
   22993           55 :   tree fntype = build_function_type (void_type_node, void_list_node);
   22994           55 :   releasing_vec args;  // There are no args
   22995              : 
   22996          125 :   for (unsigned ix = modules->length (); --ix;)
   22997              :     {
   22998           70 :       module_state *import = (*modules)[ix];
   22999           70 :       if (import->active_init_p)
   23000              :         {
   23001           55 :           tree name = mangle_module_global_init (ix);
   23002           55 :           tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
   23003              : 
   23004           55 :           DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
   23005           55 :           SET_DECL_ASSEMBLER_NAME (fndecl, name);
   23006           55 :           TREE_PUBLIC (fndecl) = true;
   23007           55 :           determine_visibility (fndecl);
   23008              : 
   23009           55 :           tree call = cp_build_function_call_vec (fndecl, &args,
   23010              :                                                   tf_warning_or_error);
   23011           55 :           finish_expr_stmt (call);
   23012              :         }
   23013              :     }
   23014           55 : }
   23015              : 
   23016              : /* NAME & LEN are a preprocessed header name, possibly including the
   23017              :    surrounding "" or <> characters.  Return the raw string name of the
   23018              :    module to which it refers.  This will be an absolute path, or begin
   23019              :    with ./, so it is immediately distinguishable from a (non-header
   23020              :    unit) module name.  If READER is non-null, ask the preprocessor to
   23021              :    locate the header to which it refers using the appropriate include
   23022              :    path.  Note that we do never do \ processing of the string, as that
   23023              :    matches the preprocessor's behaviour.  */
   23024              : 
   23025              : static const char *
   23026        25909 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
   23027              :                           const char *str, size_t &len_r)
   23028              : {
   23029        25909 :   size_t len = len_r;
   23030        25909 :   static char *buf = 0;
   23031        25909 :   static size_t alloc = 0;
   23032              : 
   23033        25909 :   if (!unquoted)
   23034              :     {
   23035            4 :       gcc_checking_assert (len >= 2
   23036              :                            && ((reader && str[0] == '<' && str[len-1] == '>')
   23037              :                                || (str[0] == '"' && str[len-1] == '"')));
   23038            4 :       str += 1;
   23039            4 :       len -= 2;
   23040              :     }
   23041              : 
   23042        25909 :   if (reader)
   23043              :     {
   23044            4 :       gcc_assert (!unquoted);
   23045              : 
   23046            4 :       if (len >= alloc)
   23047              :         {
   23048            4 :           alloc = len + 1;
   23049            4 :           buf = XRESIZEVEC (char, buf, alloc);
   23050              :         }
   23051            4 :       memcpy (buf, str, len);
   23052            4 :       buf[len] = 0;
   23053              : 
   23054            8 :       if (const char *hdr
   23055            4 :           = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
   23056              :         {
   23057            4 :           len = strlen (hdr);
   23058            4 :           str = hdr;
   23059              :         }
   23060              :       else
   23061            0 :         str = buf;
   23062              :     }
   23063              : 
   23064        25909 :   if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
   23065              :     {
   23066              :       /* Prepend './'  */
   23067            9 :       if (len + 3 > alloc)
   23068              :         {
   23069            9 :           alloc = len + 3;
   23070            9 :           buf = XRESIZEVEC (char, buf, alloc);
   23071              :         }
   23072              : 
   23073            9 :       buf[0] = '.';
   23074            9 :       buf[1] = DIR_SEPARATOR;
   23075            9 :       memmove (buf + 2, str, len);
   23076            9 :       len += 2;
   23077            9 :       buf[len] = 0;
   23078            9 :       str = buf;
   23079              :     }
   23080              : 
   23081        25909 :   len_r = len;
   23082        25909 :   return str;
   23083              : }
   23084              : 
   23085              : /* Set the CMI name from a cody packet.  Issue an error if
   23086              :    ill-formed.  */
   23087              : 
   23088         5822 : void module_state::set_filename (const Cody::Packet &packet)
   23089              : {
   23090         5822 :   if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23091              :     {
   23092              :       /* If we've seen this import before we better have the same CMI.  */
   23093         5819 :       const std::string &path = packet.GetString ();
   23094         5819 :       if (!filename)
   23095         5816 :         filename = xstrdup (packet.GetString ().c_str ());
   23096            3 :       else if (filename != path)
   23097            0 :         error_at (loc, "mismatching compiled module interface: "
   23098              :                   "had %qs, got %qs", filename, path.c_str ());
   23099              :     }
   23100              :   else
   23101              :     {
   23102            3 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23103            3 :       fatal_error (loc, "unknown compiled module interface: %s",
   23104            3 :                    packet.GetString ().c_str ());
   23105              :     }
   23106         5819 : }
   23107              : 
   23108              : /* The list of importable headers from C++ Table 24.  */
   23109              : 
   23110              : static const char *
   23111              : importable_headers[] =
   23112              :   {
   23113              :     "algorithm", "any", "array", "atomic",
   23114              :     "barrier", "bit", "bitset",
   23115              :     "charconv", "chrono", "compare", "complex", "concepts",
   23116              :     "condition_variable", "contracts", "coroutine",
   23117              :     "debugging", "deque",
   23118              :     "exception", "execution", "expected",
   23119              :     "filesystem", "flat_map", "flat_set", "format", "forward_list",
   23120              :     "fstream", "functional", "future",
   23121              :     "generator",
   23122              :     "hazard_pointer", "hive",
   23123              :     "initializer_list", "inplace_vector", "iomanip", "ios", "iosfwd",
   23124              :     "iostream", "istream", "iterator",
   23125              :     "latch", "limits", "linalg", "list", "locale",
   23126              :     "map", "mdspan", "memory", "memory_resource", "meta", "mutex",
   23127              :     "new", "numbers", "numeric",
   23128              :     "optional", "ostream",
   23129              :     "print",
   23130              :     "queue",
   23131              :     "random", "ranges", "ratio", "rcu", "regex",
   23132              :     "scoped_allocator", "semaphore", "set", "shared_mutex", "simd",
   23133              :     "source_location", "span", "spanstream", "sstream", "stack", "stacktrace",
   23134              :     "stdexcept", "stdfloat", "stop_token", "streambuf", "string",
   23135              :     "string_view", "syncstream", "system_error",
   23136              :     "text_encoding", "thread", "tuple", "type_traits", "typeindex", "typeinfo",
   23137              :     "unordered_map", "unordered_set",
   23138              :     "utility",
   23139              :     "valarray", "variant", "vector", "version"
   23140              :   };
   23141              : 
   23142              : /* True iff <name> is listed as an importable standard header.  */
   23143              : 
   23144              : static bool
   23145        23643 : is_importable_header (const char *name)
   23146              : {
   23147        23643 :   unsigned lo = 0;
   23148        23643 :   unsigned hi = ARRAY_SIZE (importable_headers);
   23149       179342 :   while (hi > lo)
   23150              :     {
   23151       158491 :       unsigned mid = (lo + hi)/2;
   23152       158491 :       int cmp = strcmp (name, importable_headers[mid]);
   23153       158491 :       if (cmp > 0)
   23154        36545 :         lo = mid + 1;
   23155       121946 :       else if (cmp < 0)
   23156              :         hi = mid;
   23157              :       else
   23158              :         return true;
   23159              :     }
   23160              :   return false;
   23161              : }
   23162              : 
   23163              : /* Figure out whether to treat HEADER as an include or an import.  */
   23164              : 
   23165              : static char *
   23166        24978 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
   23167              :                          _cpp_file *file, bool angle, const char **alternate)
   23168              : {
   23169        24978 :   if (!modules_p ())
   23170              :     {
   23171              :       /* Turn off.  */
   23172            0 :       cpp_get_callbacks (reader)->translate_include = NULL;
   23173            0 :       return nullptr;
   23174              :     }
   23175              : 
   23176        24978 :   const char *path = _cpp_get_file_path (file);
   23177              : 
   23178        24978 :   dump.push (NULL);
   23179              : 
   23180        26503 :   dump () && dump ("Checking include translation '%s'", path);
   23181        24978 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23182              : 
   23183        24978 :   size_t len = strlen (path);
   23184        24978 :   path = canonicalize_header_name (NULL, loc, true, path, len);
   23185        24978 :   auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
   23186              : 
   23187        24978 :   enum class xlate_kind {
   23188              :     unknown, text, import, invalid
   23189        24978 :   } translate = xlate_kind::unknown;
   23190              : 
   23191        24978 :   if (packet.GetCode () == Cody::Client::PC_BOOL)
   23192        24929 :     translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
   23193           49 :   else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23194              :     {
   23195              :       /* Record the CMI name for when we do the import.
   23196              :          We may already know about this import, but libcpp doesn't yet.  */
   23197           49 :       module_state *import = get_module (build_string (len, path));
   23198           49 :       import->set_filename (packet);
   23199           49 :       if (import->check_importable (reader))
   23200              :         translate = xlate_kind::import;
   23201              :       else
   23202            0 :         translate = xlate_kind::invalid;
   23203              :     }
   23204              :   else
   23205              :     {
   23206            0 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23207            0 :       error_at (loc, "cannot determine %<#include%> translation of %s: %s",
   23208            0 :                 path, packet.GetString ().c_str ());
   23209              :     }
   23210              : 
   23211        24978 :   bool note = (translate == xlate_kind::invalid);
   23212        24978 :   if (note_include_translate_yes && translate == xlate_kind::import)
   23213              :     note = true;
   23214        24973 :   else if (note_include_translate_no && translate == xlate_kind::unknown)
   23215              :     note = true;
   23216        24970 :   else if (note_includes)
   23217              :     /* We do not expect the note_includes vector to be large, so O(N)
   23218              :        iteration.  */
   23219          434 :     for (unsigned ix = note_includes->length (); !note && ix--;)
   23220          217 :       if (!strcmp ((*note_includes)[ix], path))
   23221            1 :         note = true;
   23222              : 
   23223              :   /* Maybe try importing a different header instead.  */
   23224        24978 :   if (alternate && translate == xlate_kind::unknown)
   23225              :     {
   23226        24531 :       const char *fname = _cpp_get_file_name (file);
   23227              :       /* Redirect importable <name> to <bits/stdc++.h>.  */
   23228              :       /* ??? Generalize to use a .json.  */
   23229        24531 :       expanded_location eloc = expand_location (loc);
   23230        27309 :       auto indir = [](const char *f, const char *d)
   23231              :       {
   23232         2778 :         if (!filename_ncmp (f, d, strlen (d))) return true;
   23233              :         /* Also check canonical paths (c++/123879).  */
   23234          929 :         auto cf = lrealpath (f); auto cd = lrealpath (d);
   23235          929 :         bool r = cf && cd && !filename_ncmp (cf, cd, strlen (cd));
   23236          929 :         free (cf); free (cd);
   23237          929 :         return r;
   23238              :       };
   23239        23643 :       if (angle && is_importable_header (fname)
   23240              :           /* Exclude <version> which often goes with import std.  */
   23241         2792 :           && strcmp (fname, "version") != 0
   23242              :           /* Don't redirect #includes between headers under the same include
   23243              :              path directory (i.e. between library headers); if the import
   23244              :              brings in the current file we then get redefinition errors.  */
   23245         2778 :           && !indir (eloc.file, _cpp_get_file_dir (file)->name)
   23246              :           /* ??? These are needed when running a toolchain from the build
   23247              :              directory, because libsupc++ headers aren't linked into
   23248              :              libstdc++-v3/include with the other headers.  */
   23249          851 :           && !strstr (eloc.file, "libstdc++-v3/include")
   23250        25015 :           && !strstr (eloc.file, "libsupc++"))
   23251          401 :         *alternate = "bits/stdc++.h";
   23252              :     }
   23253              : 
   23254        24978 :   if (note)
   23255           12 :     inform (loc, translate == xlate_kind::import
   23256              :             ? G_("include %qs translated to import")
   23257              :             : translate == xlate_kind::invalid
   23258            3 :             ? G_("import of %qs failed, falling back to include")
   23259              :             : G_("include %qs processed textually"), path);
   23260              : 
   23261        28025 :   dump () && dump (translate == xlate_kind::import
   23262              :                    ? "Translating include to import"
   23263              :                    : "Keeping include as include");
   23264        24978 :   dump.pop (0);
   23265              : 
   23266        24978 :   if (translate != xlate_kind::import)
   23267              :     return nullptr;
   23268              : 
   23269              :   /* Create the translation text.  */
   23270           49 :   loc = ordinary_loc_of (lmaps, loc);
   23271           49 :   const line_map_ordinary *map
   23272           49 :     = linemap_check_ordinary (linemap_lookup (lmaps, loc));
   23273           49 :   unsigned col = SOURCE_COLUMN (map, loc);
   23274           49 :   col -= (col != 0); /* Columns are 1-based.  */
   23275              : 
   23276           49 :   unsigned alloc = len + col + 60;
   23277           49 :   char *res = XNEWVEC (char, alloc);
   23278              : 
   23279           49 :   strcpy (res, "__import");
   23280           49 :   unsigned actual = 8;
   23281           49 :   if (col > actual)
   23282              :     {
   23283              :       /* Pad out so the filename appears at the same position.  */
   23284           46 :       memset (res + actual, ' ', col - actual);
   23285           46 :       actual = col;
   23286              :     }
   23287              :   /* No need to encode characters, that's not how header names are
   23288              :      handled.  */
   23289           49 :   actual += snprintf (res + actual, alloc - actual,
   23290              :                       "\"%s\" [[__translated]];\n", path);
   23291           49 :   gcc_checking_assert (actual < alloc);
   23292              : 
   23293              :   /* cpplib will delete the buffer.  */
   23294              :   return res;
   23295        24978 : }
   23296              : 
   23297              : static void
   23298          927 : begin_header_unit (cpp_reader *reader)
   23299              : {
   23300              :   /* Set the module header name from the main_input_filename.  */
   23301          927 :   const char *main = main_input_filename;
   23302          927 :   size_t len = strlen (main);
   23303          927 :   main = canonicalize_header_name (NULL, 0, true, main, len);
   23304          927 :   module_state *module = get_module (build_string (len, main));
   23305              : 
   23306          927 :   preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
   23307          927 : }
   23308              : 
   23309              : /* We've just properly entered the main source file.  I.e. after the
   23310              :    command line, builtins and forced headers.  Record the line map and
   23311              :    location of this map.  Note we may be called more than once.  The
   23312              :    first call sticks.  */
   23313              : 
   23314              : void
   23315       102430 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
   23316              :                        const line_map_ordinary *map)
   23317              : {
   23318       102430 :   gcc_checking_assert (lmaps == line_table);
   23319       102430 :   if (modules_p () && !spans.init_p ())
   23320              :     {
   23321         4944 :       unsigned n = dump.push (NULL);
   23322         4944 :       spans.init (lmaps, map);
   23323         4944 :       dump.pop (n);
   23324         4944 :       if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
   23325              :         {
   23326              :           /* Tell the preprocessor this is an include file.  */
   23327          918 :           cpp_retrofit_as_include (reader);
   23328          918 :           begin_header_unit (reader);
   23329              :         }
   23330              :     }
   23331       102430 : }
   23332              : 
   23333              : /* Process the pending_import queue, making sure we know the
   23334              :    filenames.   */
   23335              : 
   23336              : static void
   23337         5855 : name_pending_imports (cpp_reader *reader)
   23338              : {
   23339         5855 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23340              : 
   23341         5855 :   if (!vec_safe_length (pending_imports))
   23342              :     /* Not doing anything.  */
   23343              :     return;
   23344              : 
   23345         5008 :   timevar_start (TV_MODULE_MAPPER);
   23346              : 
   23347         5008 :   auto n = dump.push (NULL);
   23348         5630 :   dump () && dump ("Resolving direct import names");
   23349         5008 :   bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
   23350         5008 :                     || cpp_get_deps (reader));
   23351         5008 :   bool any = false;
   23352              : 
   23353        10965 :   for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23354              :     {
   23355         5957 :       module_state *module = (*pending_imports)[ix];
   23356         5957 :       gcc_checking_assert (module->is_direct ());
   23357         5957 :       if (!module->filename && !module->visited_p)
   23358              :         {
   23359         5845 :           bool export_p = (module->is_module ()
   23360         5845 :                            && (module->is_partition ()
   23361         3034 :                                || module->is_exported ()));
   23362              : 
   23363         5845 :           Cody::Flags flags = Cody::Flags::None;
   23364         5845 :           if (flag_preprocess_only
   23365         5845 :               && !(module->is_header () && !export_p))
   23366              :             {
   23367          141 :               if (!want_deps)
   23368           72 :                 continue;
   23369              :               flags = Cody::Flags::NameOnly;
   23370              :             }
   23371              : 
   23372         5773 :           if (!any)
   23373              :             {
   23374         4934 :               any = true;
   23375         4934 :               mapper->Cork ();
   23376              :             }
   23377         5773 :           if (export_p)
   23378         2979 :             mapper->ModuleExport (module->get_flatname (), flags);
   23379              :           else
   23380         2794 :             mapper->ModuleImport (module->get_flatname (), flags);
   23381         5773 :           module->visited_p = true;
   23382              :         }
   23383              :     }
   23384              : 
   23385         5008 :   if (any)
   23386              :     {
   23387         4934 :       auto response = mapper->Uncork ();
   23388         4934 :       auto r_iter = response.begin ();
   23389        10799 :       for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23390              :         {
   23391         5868 :           module_state *module = (*pending_imports)[ix];
   23392         5868 :           if (module->visited_p)
   23393              :             {
   23394         5773 :               module->visited_p = false;
   23395         5773 :               gcc_checking_assert (!module->filename);
   23396              : 
   23397         5773 :               module->set_filename (*r_iter);
   23398         5770 :               ++r_iter;
   23399              :             }
   23400              :         }
   23401         4931 :     }
   23402              : 
   23403         5005 :   dump.pop (n);
   23404              : 
   23405         5005 :   timevar_stop (TV_MODULE_MAPPER);
   23406              : }
   23407              : 
   23408              : /* We've just lexed a module-specific control line for MODULE.  Mark
   23409              :    the module as a direct import, and possibly load up its macro
   23410              :    state.  Returns the primary module, if this is a module
   23411              :    declaration.  */
   23412              : /* Perhaps we should offer a preprocessing mode where we read the
   23413              :    directives from the header unit, rather than require the header's
   23414              :    CMI.  */
   23415              : 
   23416              : module_state *
   23417         5977 : preprocess_module (module_state *module, location_t from_loc,
   23418              :                    bool in_purview, bool is_import, bool is_export,
   23419              :                    cpp_reader *reader)
   23420              : {
   23421         5977 :   if (!is_import)
   23422              :     {
   23423         3332 :       if (in_purview || module->loc)
   23424              :         {
   23425              :           /* We've already seen a module declaration.  If only preprocessing
   23426              :              then we won't complain in declare_module, so complain here.  */
   23427           42 :           if (flag_preprocess_only)
   23428            6 :             error_at (from_loc,
   23429              :                       in_purview
   23430              :                       ? G_("module already declared")
   23431              :                       : G_("module already imported"));
   23432              :           /* Always pretend this was an import to aid error recovery.  */
   23433              :           is_import = true;
   23434              :         }
   23435              :       else
   23436              :         {
   23437              :           /* Record it is the module.  */
   23438         3290 :           module->module_p = true;
   23439         3290 :           if (is_export)
   23440              :             {
   23441         2959 :               module->exported_p = true;
   23442         2959 :               module->interface_p = true;
   23443              :             }
   23444              :         }
   23445              :     }
   23446              : 
   23447         5977 :   if (module->directness < MD_DIRECT + in_purview)
   23448              :     {
   23449              :       /* Mark as a direct import.  */
   23450         5930 :       module->directness = module_directness (MD_DIRECT + in_purview);
   23451              : 
   23452              :       /* Set the location to be most informative for users.  */
   23453         5930 :       from_loc = ordinary_loc_of (line_table, from_loc);
   23454         5930 :       if (module->loadedness != ML_NONE)
   23455            6 :         linemap_module_reparent (line_table, module->loc, from_loc);
   23456              :       else
   23457              :         {
   23458              :           /* Don't overwrite the location if we're importing ourselves
   23459              :              after already having seen a module-declaration.  */
   23460         5924 :           if (!(is_import && module->is_module ()))
   23461         5894 :             module->loc = from_loc;
   23462         5924 :           if (!module->flatname)
   23463         5885 :             module->set_flatname ();
   23464              :         }
   23465              :     }
   23466              : 
   23467         5977 :   auto desired = ML_CONFIG;
   23468         5977 :   if (is_import
   23469         2687 :       && module->is_header ()
   23470         6902 :       && (!cpp_get_options (reader)->preprocessed
   23471            3 :           || cpp_get_options (reader)->directives_only))
   23472              :     /* We need preprocessor state now.  */
   23473              :     desired = ML_PREPROCESSOR;
   23474              : 
   23475         5977 :   if (!is_import || module->loadedness < desired)
   23476              :     {
   23477         5957 :       vec_safe_push (pending_imports, module);
   23478              : 
   23479         5957 :       if (desired == ML_PREPROCESSOR)
   23480              :         {
   23481          905 :           unsigned n = dump.push (NULL);
   23482              : 
   23483         1104 :           dump () && dump ("Reading %M preprocessor state", module);
   23484          905 :           name_pending_imports (reader);
   23485              : 
   23486              :           /* Preserve the state of the line-map.  */
   23487          905 :           auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
   23488              : 
   23489              :           /* We only need to close the span, if we're going to emit a
   23490              :              CMI.  But that's a little tricky -- our token scanner
   23491              :              needs to be smarter -- and this isn't much state.
   23492              :              Remember, we've not parsed anything at this point, so
   23493              :              our module state flags are inadequate.  */
   23494          905 :           spans.maybe_init ();
   23495          905 :           spans.close ();
   23496              : 
   23497          905 :           timevar_start (TV_MODULE_IMPORT);
   23498              : 
   23499              :           /* Load the config of each pending import -- we must assign
   23500              :              module numbers monotonically.  */
   23501         1999 :           for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23502              :             {
   23503         1094 :               auto *import = (*pending_imports)[ix];
   23504         1272 :               if (!(import->is_module ()
   23505          178 :                     && (import->is_partition () || import->is_exported ()))
   23506          922 :                   && import->loadedness == ML_NONE
   23507         2015 :                   && (!flag_preprocess_only
   23508           51 :                       || (import->is_header ()
   23509              :                           /* Allow a missing/unimportable GCM with -MG.
   23510              :                              FIXME We should also try falling back to #include
   23511              :                              before giving up entirely.  */
   23512           42 :                           && (!cpp_get_options (reader)->deps.missing_files
   23513            3 :                               || import->check_importable (reader)))))
   23514              :                 {
   23515          909 :                   unsigned n = dump.push (import);
   23516          909 :                   import->do_import (reader, true);
   23517          909 :                   dump.pop (n);
   23518              :                 }
   23519              :             }
   23520          905 :           vec_free (pending_imports);
   23521              : 
   23522              :           /* Restore the line-map state.  */
   23523          905 :           spans.open (linemap_module_restore (line_table, pre_hwm));
   23524              : 
   23525              :           /* Now read the preprocessor state of this particular
   23526              :              import.  */
   23527          905 :           if (module->loadedness == ML_CONFIG
   23528          905 :               && module->read_preprocessor (true))
   23529          899 :             module->import_macros ();
   23530              : 
   23531          905 :           timevar_stop (TV_MODULE_IMPORT);
   23532              : 
   23533          905 :           dump.pop (n);
   23534              :         }
   23535              :     }
   23536              : 
   23537         5977 :   return is_import ? NULL : get_primary (module);
   23538              : }
   23539              : 
   23540              : /* We've completed phase-4 translation.  Emit any dependency
   23541              :    information for the not-yet-loaded direct imports, and fill in
   23542              :    their file names.  We'll have already loaded up the direct header
   23543              :    unit wavefront.  */
   23544              : 
   23545              : void
   23546         4950 : preprocessed_module (cpp_reader *reader)
   23547              : {
   23548         4950 :   unsigned n = dump.push (NULL);
   23549              : 
   23550         5536 :   dump () && dump ("Completed phase-4 (tokenization) processing");
   23551              : 
   23552         4950 :   name_pending_imports (reader);
   23553         4947 :   vec_free (pending_imports);
   23554              : 
   23555         4947 :   spans.maybe_init ();
   23556         4947 :   spans.close ();
   23557              : 
   23558         4947 :   using iterator = hash_table<module_state_hash>::iterator;
   23559         4947 :   if (mkdeps *deps = cpp_get_deps (reader))
   23560              :     {
   23561              :       /* Walk the module hash, informing the dependency machinery.  */
   23562           57 :       iterator end = modules_hash->end ();
   23563          342 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23564              :         {
   23565          114 :           module_state *module = *iter;
   23566              : 
   23567          114 :           if (module->is_direct ())
   23568              :             {
   23569           90 :               if (module->is_module ()
   23570           90 :                   && (module->is_interface () || module->is_partition ()))
   23571           36 :                 deps_add_module_target (deps, module->get_flatname (),
   23572           36 :                                         maybe_add_cmi_prefix (module->filename),
   23573           36 :                                         module->is_header (),
   23574           36 :                                         module->is_exported ());
   23575              :               else
   23576           54 :                 deps_add_module_dep (deps, module->get_flatname ());
   23577              :             }
   23578              :         }
   23579              :     }
   23580              : 
   23581         4947 :   if (flag_header_unit && !flag_preprocess_only)
   23582              :     {
   23583              :       /* Find the main module -- remember, it's not yet in the module
   23584              :          array.  */
   23585          915 :       iterator end = modules_hash->end ();
   23586         1938 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23587              :         {
   23588          969 :           module_state *module = *iter;
   23589          969 :           if (module->is_module ())
   23590              :             {
   23591          915 :               declare_module (module, cpp_main_loc (reader), true, NULL, reader);
   23592          915 :               module_kind |= MK_EXPORTING;
   23593          915 :               break;
   23594              :             }
   23595              :         }
   23596              :     }
   23597              : 
   23598         4947 :   dump.pop (n);
   23599         4947 : }
   23600              : 
   23601              : /* VAL is a global tree, add it to the global vec if it is
   23602              :    interesting.  Add some of its targets, if they too are
   23603              :    interesting.  We do not add identifiers, as they can be re-found
   23604              :    via the identifier hash table.  There is a cost to the number of
   23605              :    global trees.  */
   23606              : 
   23607              : static int
   23608      3067446 : maybe_add_global (tree val, unsigned &crc)
   23609              : {
   23610      3067446 :   int v = 0;
   23611              : 
   23612      3067446 :   if (val && !(identifier_p (val) || TREE_VISITED (val)))
   23613              :     {
   23614       943898 :       TREE_VISITED (val) = true;
   23615       943898 :       crc = crc32_unsigned (crc, fixed_trees->length ());
   23616       943898 :       vec_safe_push (fixed_trees, val);
   23617       943898 :       v++;
   23618              : 
   23619       943898 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
   23620       943898 :         v += maybe_add_global (TREE_TYPE (val), crc);
   23621       943898 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
   23622       618748 :         v += maybe_add_global (TYPE_NAME (val), crc);
   23623              :     }
   23624              : 
   23625      3067446 :   return v;
   23626              : }
   23627              : 
   23628              : /* Initialize module state.  Create the hash table, determine the
   23629              :    global trees.  Create the module for current TU.  */
   23630              : 
   23631              : void
   23632         4950 : init_modules (cpp_reader *reader)
   23633              : {
   23634              :   /* PCH should not be reachable because of lang-specs, but the
   23635              :      user could have overridden that.  */
   23636         4950 :   if (pch_file)
   23637            0 :     fatal_error (input_location,
   23638              :                  "C++ modules are incompatible with precompiled headers");
   23639              : 
   23640         4950 :   if (cpp_get_options (reader)->traditional)
   23641            0 :     fatal_error (input_location,
   23642              :                  "C++ modules are incompatible with traditional preprocessing");
   23643              : 
   23644              :   /* :: is always exported.  */
   23645         4950 :   DECL_MODULE_EXPORT_P (global_namespace) = true;
   23646              : 
   23647         4950 :   modules_hash = hash_table<module_state_hash>::create_ggc (31);
   23648         4950 :   vec_safe_reserve (modules, 20);
   23649              : 
   23650              :   /* Create module for current TU.  */
   23651         4950 :   module_state *current
   23652         4950 :     = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
   23653         4950 :   current->mod = 0;
   23654         4950 :   bitmap_set_bit (current->imports, 0);
   23655         4950 :   modules->quick_push (current);
   23656              : 
   23657         4950 :   gcc_checking_assert (!fixed_trees);
   23658              : 
   23659         4950 :   headers = BITMAP_GGC_ALLOC ();
   23660              : 
   23661         4950 :   if (note_includes)
   23662              :     /* Canonicalize header names.  */
   23663            2 :     for (unsigned ix = 0; ix != note_includes->length (); ix++)
   23664              :       {
   23665            1 :         const char *hdr = (*note_includes)[ix];
   23666            1 :         size_t len = strlen (hdr);
   23667              : 
   23668            1 :         bool system = hdr[0] == '<';
   23669            1 :         bool user = hdr[0] == '"';
   23670            1 :         bool delimed = system || user;
   23671              : 
   23672            1 :         if (len <= (delimed ? 2 : 0)
   23673            1 :             || (delimed && hdr[len-1] != (system ? '>' : '"')))
   23674            0 :           error ("invalid header name %qs", hdr);
   23675              : 
   23676            1 :         hdr = canonicalize_header_name (delimed ? reader : NULL,
   23677              :                                         0, !delimed, hdr, len);
   23678            1 :         char *path = XNEWVEC (char, len + 1);
   23679            1 :         memcpy (path, hdr, len);
   23680            1 :         path[len] = 0;
   23681              : 
   23682            1 :         (*note_includes)[ix] = path;
   23683              :       }
   23684              : 
   23685         4950 :   if (note_cmis)
   23686              :     /* Canonicalize & mark module names.  */
   23687           12 :     for (unsigned ix = 0; ix != note_cmis->length (); ix++)
   23688              :       {
   23689            6 :         const char *name = (*note_cmis)[ix];
   23690            6 :         size_t len = strlen (name);
   23691              : 
   23692            6 :         bool is_system = name[0] == '<';
   23693            6 :         bool is_user = name[0] == '"';
   23694            6 :         bool is_pathname = false;
   23695            6 :         if (!(is_system || is_user))
   23696           12 :           for (unsigned ix = len; !is_pathname && ix--;)
   23697            9 :             is_pathname = IS_DIR_SEPARATOR (name[ix]);
   23698            6 :         if (is_system || is_user || is_pathname)
   23699              :           {
   23700            3 :             if (len <= (is_pathname ? 0 : 2)
   23701            3 :                 || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
   23702              :               {
   23703            0 :                 error ("invalid header name %qs", name);
   23704            0 :                 continue;
   23705              :               }
   23706              :             else
   23707            3 :               name = canonicalize_header_name (is_pathname ? nullptr : reader,
   23708              :                                                0, is_pathname, name, len);
   23709              :           }
   23710            6 :         if (auto module = get_module (name))
   23711            6 :           module->inform_cmi_p = 1;
   23712              :         else
   23713            0 :           error ("invalid module name %qs", name);
   23714              :       }
   23715              : 
   23716         4950 :   dump.push (NULL);
   23717              : 
   23718              :   /* Determine lazy handle bound.  */
   23719         4950 :   {
   23720         4950 :     unsigned limit = 1000;
   23721              : #if HAVE_GETRLIMIT
   23722         4950 :     struct rlimit rlimit;
   23723         4950 :     if (!getrlimit (RLIMIT_NOFILE, &rlimit))
   23724              :       {
   23725         4950 :         lazy_hard_limit = (rlimit.rlim_max < 1000000
   23726         4950 :                            ? unsigned (rlimit.rlim_max) : 1000000);
   23727         4950 :         lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
   23728         4950 :                            ? lazy_hard_limit - LAZY_HEADROOM : 0);
   23729         4950 :         if (rlimit.rlim_cur < limit)
   23730            0 :           limit = unsigned (rlimit.rlim_cur);
   23731              :       }
   23732              : #endif
   23733         4950 :     limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
   23734              : 
   23735         4950 :     if (unsigned parm = param_lazy_modules)
   23736              :       {
   23737         4950 :         if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
   23738            6 :           lazy_limit = parm;
   23739              :       }
   23740              :     else
   23741            0 :       lazy_limit = limit;
   23742              :   }
   23743              : 
   23744         4950 :   if (dump ())
   23745              :     {
   23746          586 :       verstr_t ver;
   23747          586 :       version2string (MODULE_VERSION, ver);
   23748          586 :       dump ("Source: %s", main_input_filename);
   23749          586 :       dump ("Compiler: %s", version_string);
   23750          586 :       dump ("Modules: %s", ver);
   23751          586 :       dump ("Checking: %s",
   23752              : #if CHECKING_P
   23753              :             "checking"
   23754              : #elif ENABLE_ASSERT_CHECKING
   23755              :             "asserting"
   23756              : #else
   23757              :             "release"
   23758              : #endif
   23759              :             );
   23760          586 :       dump ("Compiled by: "
   23761              : #ifdef __GNUC__
   23762              :             "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
   23763              : #ifdef __OPTIMIZE__
   23764              :             "optimizing"
   23765              : #else
   23766              :             "not optimizing"
   23767              : #endif
   23768              : #else
   23769              :             "not GCC"
   23770              : #endif
   23771              :             );
   23772          586 :       dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
   23773          586 :       dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
   23774          586 :       dump ("Lazy limit: %u", lazy_limit);
   23775          586 :       dump ("Lazy hard limit: %u", lazy_hard_limit);
   23776          586 :       dump ("");
   23777              :     }
   23778              : 
   23779              :   /* Construct the global tree array.  This is an array of unique
   23780              :      global trees (& types).  Do this now, rather than lazily, as
   23781              :      some global trees are lazily created and we don't want that to
   23782              :      mess with our syndrome of fixed trees.  */
   23783         4950 :   unsigned crc = 0;
   23784         4950 :   vec_alloc (fixed_trees, 250);
   23785              : 
   23786         5536 :   dump () && dump ("+Creating globals");
   23787              :   /* Insert the TRANSLATION_UNIT_DECL.  */
   23788         4950 :   TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
   23789         4950 :   fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
   23790        29700 :   for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
   23791              :     {
   23792        24750 :       const tree *ptr = global_tree_arys[jx].first;
   23793        24750 :       unsigned limit = global_tree_arys[jx].second;
   23794              : 
   23795      1514700 :       for (unsigned ix = 0; ix != limit; ix++, ptr++)
   23796              :         {
   23797      1489950 :           !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
   23798      1489950 :           unsigned v = maybe_add_global (*ptr, crc);
   23799      1666336 :           dump () && dump ("+%u", v);
   23800              :         }
   23801              :     }
   23802              :   /* OS- and machine-specific types are dynamically registered at
   23803              :      runtime, so cannot be part of global_tree_arys.  */
   23804         4950 :   registered_builtin_types && dump ("") && dump ("+\tB:");
   23805        19800 :   for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
   23806              :     {
   23807        14850 :       unsigned v = maybe_add_global (TREE_VALUE (t), crc);
   23808        16608 :       dump () && dump ("+%u", v);
   23809              :     }
   23810         4950 :   global_crc = crc32_unsigned (crc, fixed_trees->length ());
   23811         4950 :   dump ("") && dump ("Created %u unique globals, crc=%x",
   23812              :                      fixed_trees->length (), global_crc);
   23813       953798 :   for (unsigned ix = fixed_trees->length (); ix--;)
   23814       948848 :     TREE_VISITED ((*fixed_trees)[ix]) = false;
   23815              : 
   23816         4950 :   dump.pop (0);
   23817              : 
   23818         4950 :   if (!flag_module_lazy)
   23819              :     /* Get the mapper now, if we're not being lazy.  */
   23820          313 :     get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23821              : 
   23822         4950 :   if (!flag_preprocess_only)
   23823              :     {
   23824         4806 :       pending_table = new pending_map_t (EXPERIMENT (1, 400));
   23825         4806 :       entity_map = new entity_map_t (EXPERIMENT (1, 400));
   23826         4806 :       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
   23827         4806 :       imported_temploid_friends
   23828         4806 :         = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
   23829              :     }
   23830              : 
   23831              : #if CHECKING_P
   23832         4950 :   note_defs = note_defs_table_t::create_ggc (1000);
   23833              : #endif
   23834              : 
   23835         4950 :   if (flag_header_unit && cpp_get_options (reader)->preprocessed)
   23836            9 :     begin_header_unit (reader);
   23837              : 
   23838              :   /* Collect here to make sure things are tagged correctly (when
   23839              :      aggressively GC'd).  */
   23840         4950 :   ggc_collect ();
   23841         4950 : }
   23842              : 
   23843              : /* If NODE is a deferred macro, load it.  */
   23844              : 
   23845              : static int
   23846        83149 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
   23847              : {
   23848        83149 :   location_t main_loc
   23849        83149 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   23850              : 
   23851        83149 :   if (cpp_user_macro_p (node)
   23852        83149 :       && !node->value.macro)
   23853              :     {
   23854           72 :       cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
   23855           72 :       dump () && dump ("Loaded macro #%s %I",
   23856              :                        macro ? "define" : "undef", identifier (node));
   23857              :     }
   23858              : 
   23859        83149 :   return 1;
   23860              : }
   23861              : 
   23862              : /* At the end of tokenizing, we no longer need the macro tables of
   23863              :    imports.  But the user might have requested some checking.  */
   23864              : 
   23865              : void
   23866       100688 : maybe_check_all_macros (cpp_reader *reader)
   23867              : {
   23868       100688 :   if (!warn_imported_macros)
   23869              :     return;
   23870              : 
   23871              :   /* Force loading of any remaining deferred macros.  This will
   23872              :      produce diagnostics if they are ill-formed.  */
   23873           21 :   unsigned n = dump.push (NULL);
   23874           21 :   cpp_forall_identifiers (reader, load_macros, NULL);
   23875           21 :   dump.pop (n);
   23876              : }
   23877              : 
   23878              : // State propagated from finish_module_processing to fini_modules
   23879              : 
   23880              : struct module_processing_cookie
   23881              : {
   23882              :   elf_out out;
   23883              :   module_state_config config;
   23884              :   char *cmi_name;
   23885              :   char *tmp_name;
   23886              :   unsigned crc;
   23887              :   bool began;
   23888              : 
   23889         2919 :   module_processing_cookie (char *cmi, char *tmp, int fd, int e)
   23890         2919 :     : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
   23891              :   {
   23892              :   }
   23893         2919 :   ~module_processing_cookie ()
   23894              :   {
   23895         2919 :     XDELETEVEC (tmp_name);
   23896         2919 :     XDELETEVEC (cmi_name);
   23897         2919 :   }
   23898              : };
   23899              : 
   23900              : /* Write the CMI, if we're a module interface.  */
   23901              : 
   23902              : void *
   23903       100480 : finish_module_processing (cpp_reader *reader)
   23904              : {
   23905       100480 :   module_processing_cookie *cookie = nullptr;
   23906              : 
   23907       100480 :   if (header_module_p ())
   23908          915 :     module_kind &= ~MK_EXPORTING;
   23909              : 
   23910       100480 :   if (!modules || !this_module ()->name)
   23911              :     {
   23912        97558 :       if (flag_module_only)
   23913            6 :         warning (0, "%<-fmodule-only%> used for non-interface");
   23914              :     }
   23915         2922 :   else if (!flag_syntax_only)
   23916              :     {
   23917         2919 :       int fd = -1;
   23918         2919 :       int e = -1;
   23919              : 
   23920         2919 :       timevar_start (TV_MODULE_EXPORT);
   23921              : 
   23922              :       /* Force a valid but empty line map at the end.  This simplifies
   23923              :          the line table preparation and writing logic.  */
   23924         2919 :       linemap_add (line_table, LC_ENTER, false, "", 0);
   23925              : 
   23926              :       /* We write to a tmpname, and then atomically rename.  */
   23927         2919 :       char *cmi_name = NULL;
   23928         2919 :       char *tmp_name = NULL;
   23929         2919 :       module_state *state = this_module ();
   23930              : 
   23931         2919 :       unsigned n = dump.push (state);
   23932         2919 :       state->announce ("creating");
   23933         2919 :       if (state->filename)
   23934              :         {
   23935         2919 :           size_t len = 0;
   23936         2919 :           cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
   23937         2919 :           tmp_name = XNEWVEC (char, len + 3);
   23938         2919 :           memcpy (tmp_name, cmi_name, len);
   23939         2919 :           strcpy (&tmp_name[len], "~");
   23940              : 
   23941         2919 :           if (!errorcount)
   23942           99 :             for (unsigned again = 2; ; again--)
   23943              :               {
   23944         2906 :                 fd = open (tmp_name,
   23945              :                            O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
   23946              :                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   23947         2906 :                 e = errno;
   23948         2906 :                 if (fd >= 0 || !again || e != ENOENT)
   23949              :                   break;
   23950           99 :                 create_dirs (tmp_name);
   23951              :               }
   23952         2919 :           if (note_module_cmi_yes || state->inform_cmi_p)
   23953            3 :             inform (state->loc, "writing CMI %qs", cmi_name);
   23954         3219 :           dump () && dump ("CMI is %s", cmi_name);
   23955              :         }
   23956              : 
   23957         2919 :       cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
   23958              : 
   23959         2919 :       if (errorcount)
   23960              :         /* Don't write the module if we have reported errors.  */;
   23961         2807 :       else if (erroneous_templates
   23962         2807 :                && !erroneous_templates->is_empty ())
   23963              :         {
   23964              :           /* Don't write the module if it contains an erroneous template.
   23965              :              Also emit notes about where errors occurred in case
   23966              :              -Wno-template-body was passed.  */
   23967            6 :           auto_diagnostic_group d;
   23968            6 :           error_at (state->loc, "not writing module %qs due to errors "
   23969              :                     "in template bodies", state->get_flatname ());
   23970            6 :           if (!warn_template_body)
   23971            3 :             inform (state->loc, "enable %<-Wtemplate-body%> for more details");
   23972           12 :           for (auto e : *erroneous_templates)
   23973            6 :             inform (e.second, "first error in %qD appeared here", e.first);
   23974            6 :         }
   23975         2801 :       else if (cookie->out.begin ())
   23976              :         {
   23977              :           /* So crashes finger-point the module decl.  */
   23978         2801 :           iloc_sentinel ils = state->loc;
   23979         2801 :           if (state->write_begin (&cookie->out, reader, cookie->config,
   23980         2801 :                                   cookie->crc))
   23981         2772 :             cookie->began = true;
   23982         2801 :         }
   23983              : 
   23984         2919 :       dump.pop (n);
   23985         2919 :       timevar_stop (TV_MODULE_EXPORT);
   23986              : 
   23987         2919 :       ggc_collect ();
   23988              :     }
   23989              : 
   23990       100480 :   if (modules)
   23991              :     {
   23992         4766 :       unsigned n = dump.push (NULL);
   23993         5352 :       dump () && dump ("Imported %u modules", modules->length () - 1);
   23994         5352 :       dump () && dump ("Containing %u clusters", available_clusters);
   23995         4766 :       dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
   23996          586 :                        (loaded_clusters * 100 + available_clusters / 2) /
   23997          586 :                        (available_clusters + !available_clusters));
   23998         4766 :       dump.pop (n);
   23999              :     }
   24000              : 
   24001       100480 :   return cookie;
   24002              : }
   24003              : 
   24004              : // Do the final emission of a module.  At this point we know whether
   24005              : // the module static initializer is a NOP or not.
   24006              : 
   24007              : static void
   24008         2919 : late_finish_module (cpp_reader *reader,  module_processing_cookie *cookie,
   24009              :                     bool init_fn_non_empty)
   24010              : {
   24011         2919 :   timevar_start (TV_MODULE_EXPORT);
   24012              : 
   24013         2919 :   module_state *state = this_module ();
   24014         2919 :   unsigned n = dump.push (state);
   24015         2919 :   state->announce ("finishing");
   24016              : 
   24017         2919 :   cookie->config.active_init = init_fn_non_empty;
   24018         2919 :   if (cookie->began)
   24019         2772 :     state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
   24020              : 
   24021         2919 :   if (cookie->out.end () && cookie->cmi_name)
   24022              :     {
   24023              :       /* Some OS's do not replace NEWNAME if it already exists.
   24024              :          This'll have a race condition in erroneous concurrent
   24025              :          builds.  */
   24026         2807 :       unlink (cookie->cmi_name);
   24027         2807 :       if (rename (cookie->tmp_name, cookie->cmi_name))
   24028              :         {
   24029            0 :           dump () && dump ("Rename ('%s','%s') errno=%u",
   24030            0 :                            cookie->tmp_name, cookie->cmi_name, errno);
   24031            0 :           cookie->out.set_error (errno);
   24032              :         }
   24033              :     }
   24034              : 
   24035         2919 :   if (cookie->out.get_error () && cookie->began)
   24036              :     {
   24037            0 :       error_at (state->loc, "failed to write compiled module: %s",
   24038            0 :                 cookie->out.get_error (state->filename));
   24039            0 :       state->note_cmi_name ();
   24040              :     }
   24041              : 
   24042         2919 :   if (!errorcount)
   24043              :     {
   24044         2766 :       auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   24045         2766 :       mapper->ModuleCompiled (state->get_flatname ());
   24046              :     }
   24047          153 :   else if (cookie->cmi_name)
   24048              :     {
   24049              :       /* We failed, attempt to erase all evidence we even tried.  */
   24050          153 :       unlink (cookie->tmp_name);
   24051          153 :       unlink (cookie->cmi_name);
   24052              :     }
   24053              : 
   24054         2919 :   delete cookie;
   24055         2919 :   dump.pop (n);
   24056         2919 :   timevar_stop (TV_MODULE_EXPORT);
   24057         2919 : }
   24058              : 
   24059              : void
   24060       100480 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
   24061              : {
   24062       100480 :   if (cookie)
   24063         2919 :     late_finish_module (reader,
   24064              :                         static_cast<module_processing_cookie *> (cookie),
   24065              :                         has_inits);
   24066              : 
   24067              :   /* We're done with the macro tables now.  */
   24068       100480 :   vec_free (macro_exports);
   24069       100480 :   vec_free (macro_imports);
   24070       100480 :   headers = NULL;
   24071              : 
   24072              :   /* We're now done with everything but the module names.  */
   24073       100480 :   set_cmi_repo (NULL);
   24074       100480 :   if (mapper)
   24075              :     {
   24076         4766 :       timevar_start (TV_MODULE_MAPPER);
   24077         4766 :       module_client::close_module_client (0, mapper);
   24078         4766 :       mapper = nullptr;
   24079         4766 :       timevar_stop (TV_MODULE_MAPPER);
   24080              :     }
   24081       100480 :   module_state_config::release ();
   24082              : 
   24083              : #if CHECKING_P
   24084       100480 :   note_defs = NULL;
   24085              : #endif
   24086              : 
   24087       100480 :   if (modules)
   24088         7733 :     for (unsigned ix = modules->length (); --ix;)
   24089         2967 :       if (module_state *state = (*modules)[ix])
   24090         2967 :         state->release ();
   24091              : 
   24092              :   /* No need to lookup modules anymore.  */
   24093       100480 :   modules_hash = NULL;
   24094              : 
   24095              :   /* Or entity array.  We still need the entity map to find import numbers.  */
   24096       100480 :   vec_free (entity_ary);
   24097       100480 :   entity_ary = NULL;
   24098              : 
   24099              :   /* Or remember any pending entities.  */
   24100       105246 :   delete pending_table;
   24101       100480 :   pending_table = NULL;
   24102              : 
   24103              :   /* Or any keys -- Let it go!  */
   24104       102966 :   delete keyed_table;
   24105       100480 :   keyed_table = NULL;
   24106              : 
   24107              :   /* Allow a GC, we've possibly made much data unreachable.  */
   24108       100480 :   ggc_collect ();
   24109       100480 : }
   24110              : 
   24111              : /* If CODE is a module option, handle it & return true.  Otherwise
   24112              :    return false.  For unknown reasons I cannot get the option
   24113              :    generation machinery to set fmodule-mapper or -fmodule-header to
   24114              :    make a string type option variable.  */
   24115              : 
   24116              : bool
   24117      1978015 : handle_module_option (unsigned code, const char *str, int)
   24118              : {
   24119      1978015 :   auto hdr = CMS_header;
   24120              : 
   24121      1978015 :   switch (opt_code (code))
   24122              :     {
   24123           48 :     case OPT_fmodule_mapper_:
   24124           48 :       module_mapper_name = str;
   24125           48 :       return true;
   24126              : 
   24127           12 :     case OPT_fmodule_header_:
   24128           12 :       {
   24129           12 :         if (!strcmp (str, "user"))
   24130              :           hdr = CMS_user;
   24131           12 :         else if (!strcmp (str, "system"))
   24132              :           hdr = CMS_system;
   24133              :         else
   24134            0 :           error ("unknown header kind %qs", str);
   24135              :       }
   24136              :       /* Fallthrough.  */
   24137              : 
   24138          930 :     case OPT_fmodule_header:
   24139          930 :       flag_header_unit = hdr;
   24140          930 :       flag_modules = 1;
   24141          930 :       return true;
   24142              : 
   24143            1 :     case OPT_flang_info_include_translate_:
   24144            1 :       vec_safe_push (note_includes, str);
   24145            1 :       return true;
   24146              : 
   24147            6 :     case OPT_flang_info_module_cmi_:
   24148            6 :       vec_safe_push (note_cmis, str);
   24149            6 :       return true;
   24150              : 
   24151              :     default:
   24152              :       return false;
   24153              :     }
   24154              : }
   24155              : 
   24156              : /* Set preprocessor callbacks and options for modules.  */
   24157              : 
   24158              : void
   24159       102233 : module_preprocess_options (cpp_reader *reader)
   24160              : {
   24161       102233 :   gcc_checking_assert (!lang_hooks.preprocess_undef);
   24162       102233 :   if (modules_p ())
   24163              :     {
   24164         4950 :       auto *cb = cpp_get_callbacks (reader);
   24165              : 
   24166         4950 :       cb->translate_include = maybe_translate_include;
   24167         4950 :       cb->user_deferred_macro = module_state::deferred_macro;
   24168         4950 :       if (flag_header_unit)
   24169              :         {
   24170              :           /* If the preprocessor hook is already in use, that
   24171              :              implementation will call the undef langhook.  */
   24172          927 :           if (cb->undef)
   24173            0 :             lang_hooks.preprocess_undef = module_state::undef_macro;
   24174              :           else
   24175          927 :             cb->undef = module_state::undef_macro;
   24176              :         }
   24177         4950 :       auto *opt = cpp_get_options (reader);
   24178         4950 :       opt->module_directives = true;
   24179         4950 :       if (flag_no_output)
   24180           18 :         opt->directives_only = true;
   24181         4950 :       if (opt->main_search == CMS_none)
   24182         4945 :         opt->main_search = cpp_main_search (flag_header_unit);
   24183              :     }
   24184       102233 : }
   24185              : 
   24186              : #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.