LCOV - code coverage report
Current view: top level - gcc/cp - module.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 94.4 % 11132 10508
Test Date: 2026-10-24 16:24:59 Functions: 97.9 % 477 467
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       255365 : static inline cpp_hashnode *cpp_node (tree id)
     285              : {
     286       255365 :   return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
     287              : }
     288              : 
     289       157666 : static inline tree identifier (const cpp_hashnode *node)
     290              : {
     291              :   /* HT_NODE() expands to node->ident that HT_IDENT_TO_GCC_IDENT()
     292              :      then subtracts a nonzero constant, deriving a pointer to
     293              :      a different member than ident.  That's strictly undefined
     294              :      and detected by -Warray-bounds.  Suppress it.  See PR 101372.  */
     295       157666 : #pragma GCC diagnostic push
     296       157666 : #pragma GCC diagnostic ignored "-Warray-bounds"
     297       157666 :   return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
     298       157666 : #pragma GCC diagnostic pop
     299              : }
     300              : 
     301              : /* Id for dumping module information.  */
     302              : int module_dump_id;
     303              : 
     304              : /* We have a special module owner.  */
     305              : #define MODULE_UNKNOWN (~0U)    /* Not yet known.  */
     306              : 
     307              : /* Prefix for section names.  */
     308              : #define MOD_SNAME_PFX ".gnu.c++"
     309              : 
     310              : /* Format a version for user consumption.  */
     311              : 
     312              : typedef char verstr_t[32];
     313              : static void
     314         4236 : version2string (unsigned version, verstr_t &out)
     315              : {
     316         4236 :   unsigned major = MODULE_MAJOR (version);
     317         4236 :   unsigned minor = MODULE_MINOR (version);
     318              : 
     319         4236 :   if (IS_EXPERIMENTAL (version))
     320         4236 :     sprintf (out, "%04u/%02u/%02u-%02u:%02u%s",
     321         4236 :              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         4236 : }
     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         2965 :   class allocator {
     364              :   public:
     365              :     /* Tools tend to moan if the dtor's not virtual.  */
     366       104546 :     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       958042 :   data ()
     388       958042 :     :buffer (NULL), size (0), pos (0)
     389              :   {
     390              :   }
     391       972817 :   ~data ()
     392              :   {
     393              :     /* Make sure the derived and/or using class know what they're
     394              :        doing.  */
     395       972817 :     gcc_checking_assert (!buffer);
     396       972817 :   }
     397              : 
     398              : protected:
     399    709743363 :   char *use (unsigned count)
     400              :   {
     401    709743363 :     if (size < pos + count)
     402              :       return NULL;
     403    709743363 :     char *res = &buffer[pos];
     404    709743363 :     pos += count;
     405    419753542 :     return res;
     406              :   }
     407              : 
     408              :   unsigned calc_crc (unsigned) const;
     409              : 
     410              : public:
     411     55491828 :   void unuse (unsigned count)
     412              :   {
     413     55491828 :     pos -= count;
     414              :   }
     415              : 
     416              : public:
     417              :   static allocator simple_memory;
     418              : };
     419              : } // anon namespace
     420              : 
     421              : /* The simple data allocator.  */
     422              : data::allocator data::simple_memory;
     423              : 
     424              : /* Grow buffer to at least size NEEDED.  */
     425              : 
     426              : void
     427       896617 : data::allocator::grow (data &obj, unsigned needed, bool exact)
     428              : {
     429       896617 :   gcc_checking_assert (needed ? needed > obj.size : !obj.size);
     430       896617 :   if (!needed)
     431              :     /* Pick a default size.  */
     432       368667 :     needed = EXPERIMENT (100, 1000);
     433              : 
     434       896617 :   if (!exact)
     435       888085 :     needed *= 2;
     436       896617 :   obj.buffer = grow (obj.buffer, needed);
     437       896617 :   if (obj.buffer)
     438       896617 :     obj.size = needed;
     439              :   else
     440            0 :     obj.pos = obj.size = 0;
     441       896617 : }
     442              : 
     443              : /* Free a buffer.  */
     444              : 
     445              : void
     446       383492 : data::allocator::shrink (data &obj)
     447              : {
     448            0 :   shrink (obj.buffer);
     449       383492 :   obj.buffer = NULL;
     450       383492 :   obj.size = 0;
     451            0 : }
     452              : 
     453              : char *
     454        11977 : data::allocator::grow (char *ptr, unsigned needed)
     455              : {
     456        11977 :   return XRESIZEVAR (char, ptr, needed);
     457              : }
     458              : 
     459              : void
     460        14825 : data::allocator::shrink (char *ptr)
     461              : {
     462        14825 :   XDELETEVEC (ptr);
     463        14825 : }
     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       597956 : data::calc_crc (unsigned l) const
     470              : {
     471       597956 :   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       239758 :   bytes_in ()
     487       239758 :     : parent (), overrun (false)
     488              :   {
     489              :   }
     490       242679 :   ~bytes_in ()
     491              :   {
     492        16239 :   }
     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      1790177 :   bool more_p () const
     504              :   {
     505      1790177 :     return pos != size;
     506              :   }
     507              : 
     508              : public:
     509              :   /* Start reading at OFFSET.  */
     510          466 :   void random_access (unsigned offset)
     511              :   {
     512          466 :     if (offset > size)
     513            0 :       set_overrun ();
     514          466 :     pos = offset;
     515              :   }
     516              : 
     517              : public:
     518      1617627 :   void align (unsigned boundary)
     519              :   {
     520      1617627 :     if (unsigned pad = pos & (boundary - 1))
     521      3131608 :       read (boundary - pad);
     522              :   }
     523              : 
     524              : public:
     525    289989821 :   const char *read (unsigned count)
     526              :   {
     527      1513981 :     char *ptr = use (count);
     528    289989821 :     if (!ptr)
     529            0 :       set_overrun ();
     530    236018724 :     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       240897 :   unsigned get_crc () const
     537              :   {
     538       240897 :     return *(const unsigned *)&buffer[0];
     539              :   }
     540              : 
     541              : public:
     542              :   /* Manipulate the overrun flag.  */
     543    168526725 :   bool get_overrun () const
     544              :   {
     545    168526725 :     return overrun;
     546              :   }
     547           26 :   void set_overrun ()
     548              :   {
     549            0 :     overrun = true;
     550              :   }
     551              : 
     552              : public:
     553              :   unsigned u32 ();      /* Read uncompressed integer.  */
     554              : 
     555              : public:
     556              :   int c () ATTRIBUTE_UNUSED;            /* Read a char.  */
     557              :   int i ();             /* Read a signed int.  */
     558              :   unsigned u ();        /* Read an unsigned int.  */
     559              :   size_t z ();          /* Read a size_t.  */
     560              :   location_t loc ();    /* Read a location_t.  */
     561              :   HOST_WIDE_INT wi ();  /* Read a HOST_WIDE_INT.  */
     562              :   unsigned HOST_WIDE_INT wu (); /* Read an unsigned HOST_WIDE_INT.  */
     563              :   const char *str (size_t * = NULL); /* Read a string.  */
     564              :   const void *buf (size_t); /* Read a fixed-length buffer.  */
     565              :   cpp_hashnode *cpp_node (); /* Read a cpp node.  */
     566              : 
     567              :   struct bits_in;
     568              :   bits_in stream_bits ();
     569              : };
     570              : } // anon namespace
     571              : 
     572              : /* Verify the buffer's CRC is correct.  */
     573              : 
     574              : bool
     575       237792 : bytes_in::check_crc () const
     576              : {
     577       237792 :   if (size < 4)
     578              :     return false;
     579              : 
     580       237792 :   unsigned c_crc = calc_crc (size);
     581       237792 :   if (c_crc != get_crc ())
     582            0 :     return false;
     583              : 
     584              :   return true;
     585              : }
     586              : 
     587              : class elf_out;
     588              : 
     589              : /* Byte stream writer.  */
     590              : 
     591              : namespace {
     592              : class bytes_out : public data {
     593              :   typedef data parent;
     594              : 
     595              : public:
     596              :   allocator *memory;    /* Obtainer of memory.  */
     597              : 
     598              : public:
     599       712234 :   bytes_out (allocator *memory)
     600       712234 :     : parent (), memory (memory)
     601              :   {
     602              :   }
     603       712234 :   ~bytes_out ()
     604              :   {
     605       742948 :   }
     606              : 
     607              : public:
     608   1069632664 :   bool streaming_p () const
     609              :   {
     610   1069632664 :     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      2447183 :   void align (unsigned boundary)
     624              :   {
     625      2447183 :     if (unsigned pad = pos & (boundary - 1))
     626      2289035 :       write (boundary - pad);
     627      2447183 :   }
     628              : 
     629              : public:
     630    419753542 :   char *write (unsigned count, bool exact = false)
     631              :   {
     632    419753542 :     if (size < pos + count)
     633       513129 :       memory->grow (*this, pos + count, exact);
     634    419753542 :     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        20665 :   void str (const char *ptr)
     649              :   {
     650        20665 :     str (ptr, strlen (ptr));
     651        20665 :   }
     652       312901 :   void cpp_node (const cpp_hashnode *node)
     653              :   {
     654       312901 :     str ((const char *)NODE_NAME (node), NODE_LEN (node));
     655              :   }
     656              :   void str (const char *, size_t);  /* Write string of known length.  */
     657              :   void buf (const void *, size_t);  /* Write fixed length buffer.  */
     658              :   void *buf (size_t); /* Create a writable buffer */
     659              : 
     660              :   struct bits_out;
     661              :   bits_out stream_bits ();
     662              : 
     663              : public:
     664              :   /* Format a NUL-terminated raw string.  */
     665              :   void printf (const char *, ...) ATTRIBUTE_PRINTF_2;
     666              :   void print_time (const char *, const tm *, const char *);
     667              : 
     668              : public:
     669              :   /* Dump instrumentation.  */
     670              :   static void instrument ();
     671              : 
     672              : protected:
     673              :   /* Instrumentation.  */
     674              :   static unsigned spans[4];
     675              :   static unsigned lengths[4];
     676              : };
     677              : } // anon namespace
     678              : 
     679              : /* Finish bit packet.  Rewind the bytes not used.  */
     680              : 
     681              : static unsigned
     682     55434739 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
     683              : {
     684     55434739 :   gcc_assert (bit_pos);
     685     55434739 :   unsigned bytes = (bit_pos + 7) / 8;
     686     55434739 :   bits.unuse (4 - bytes);
     687     55434739 :   bit_pos = 0;
     688     55434739 :   bit_val = 0;
     689     55434739 :   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     16581759 :   bits_in (bytes_in& in)
     712     16581759 :     : in (in)
     713              :   { }
     714              : 
     715     16581759 :   ~bits_in ()
     716              :   {
     717     15573747 :     bflush ();
     718     16581759 :   }
     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     34335947 :   void bflush ()
     726              :   {
     727     34335947 :     if (bit_pos)
     728     18762200 :       bit_flush (in, bit_val, bit_pos);
     729     34335947 :   }
     730              : 
     731              :   /* Read one bit.  */
     732    567823379 :   bool b ()
     733              :   {
     734    567823379 :     if (!bit_pos)
     735     24829312 :       bit_val = in.u32 ();
     736    567823379 :     bool x = (bit_val >> bit_pos) & 1;
     737    567823379 :     bit_pos = (bit_pos + 1) % 32;
     738    567823379 :     return x;
     739              :   }
     740              : };
     741              : 
     742              : /* Factory function for bits_in.  */
     743              : 
     744              : bytes_in::bits_in
     745     16581759 : bytes_in::stream_bits ()
     746              : {
     747     16581759 :   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     24445620 :   bits_out (bytes_out& out)
     759     24445620 :     : out (out)
     760              :   { }
     761              : 
     762     24445620 :   ~bits_out ()
     763              :   {
     764        78790 :     bflush ();
     765              :   }
     766              : 
     767              :   bits_out(bits_out&&) = default;
     768              :   bits_out(const bits_out&) = delete;
     769              :   bits_out& operator=(const bits_out&) = delete;
     770              : 
     771              :   /* Completed a block of bools.  */
     772     50561591 :   void bflush ()
     773              :   {
     774     50561591 :     if (bit_pos)
     775              :       {
     776     27630003 :         out.u32 (bit_val);
     777     27630003 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     778              :       }
     779     50561591 :     out.spans[2]++;
     780     50561591 :     is_set = -1;
     781     50561591 :   }
     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    835874586 :   void b (bool x)
     788              :   {
     789    835874586 :     if (is_set != x)
     790              :       {
     791     87004243 :         is_set = x;
     792     87004243 :         out.spans[x]++;
     793              :       }
     794    835874586 :     out.lengths[x]++;
     795    835874586 :     bit_val |= unsigned (x) << bit_pos++;
     796    835874586 :     if (bit_pos == 32)
     797              :       {
     798      9042536 :         out.u32 (bit_val);
     799      9042536 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     800              :       }
     801    835874586 :   }
     802              : };
     803              : 
     804              : /* Factory function for bits_out.  */
     805              : 
     806              : bytes_out::bits_out
     807     24445620 : bytes_out::stream_bits ()
     808              : {
     809     24445620 :   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       362979 : bytes_out::set_crc (unsigned *crc_ptr)
     821              : {
     822       362979 :   if (crc_ptr)
     823              :     {
     824       360164 :       gcc_checking_assert (pos >= 4);
     825              : 
     826       360164 :       unsigned crc = calc_crc (pos);
     827       360164 :       unsigned accum = *crc_ptr;
     828              :       /* Only mix the existing *CRC_PTR if it is non-zero.  */
     829       360164 :       accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
     830       360164 :       *crc_ptr = accum;
     831              : 
     832              :       /* Buffer will be sufficiently aligned.  */
     833       360164 :       *(unsigned *)buffer = crc;
     834              :     }
     835       362979 : }
     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     36681662 : bytes_out::u32 (unsigned val)
     844              : {
     845     36681662 :   if (char *ptr = write (4))
     846              :     {
     847     36681662 :       ptr[0] = val;
     848     36681662 :       ptr[1] = val >> 8;
     849     36681662 :       ptr[2] = val >> 16;
     850     36681662 :       ptr[3] = val >> 24;
     851              :     }
     852     36681662 : }
     853              : 
     854              : unsigned
     855     24839062 : bytes_in::u32 ()
     856              : {
     857     24839062 :   unsigned val = 0;
     858     24839062 :   if (const char *ptr = read (4))
     859              :     {
     860     24839062 :       val |= (unsigned char)ptr[0];
     861     24839062 :       val |= (unsigned char)ptr[1] << 8;
     862     24839062 :       val |= (unsigned char)ptr[2] << 16;
     863     24839062 :       val |= (unsigned char)ptr[3] << 24;
     864              :     }
     865              : 
     866     24839062 :   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    147788717 : bytes_out::i (int v)
     892              : {
     893    147788717 :   if (char *ptr = write (1))
     894              :     {
     895    147788717 :       if (v <= 0x3f && v >= -0x40)
     896    114863562 :         *ptr = v & 0x7f;
     897              :       else
     898              :         {
     899     32925155 :           unsigned bytes = 0;
     900     32925155 :           int probe;
     901     32925155 :           if (v >= 0)
     902            0 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     903            0 :               bytes++;
     904              :           else
     905     49918604 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     906     16993449 :               bytes++;
     907     32925155 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     908     32925155 :           if ((ptr = write (++bytes)))
     909     82843759 :             for (; bytes--; v >>= 8)
     910     49918604 :               ptr[bytes] = v & 0xff;
     911              :         }
     912              :     }
     913    147788717 : }
     914              : 
     915              : int
     916    101723160 : bytes_in::i ()
     917              : {
     918    101723160 :   int v = 0;
     919    101723160 :   if (const char *ptr = read (1))
     920              :     {
     921    101723160 :       v = *ptr & 0xff;
     922    101723160 :       if (v & 0x80)
     923              :         {
     924     22890016 :           unsigned bytes = (v >> 4) & 0x7;
     925     22890016 :           v &= 0xf;
     926     22890016 :           if (v & 0x8)
     927     22890016 :             v |= -1 ^ 0x7;
     928              :           /* unsigned necessary due to left shifts of -ve values.  */
     929     22890016 :           unsigned uv = unsigned (v);
     930     22890016 :           if ((ptr = read (++bytes)))
     931     58630633 :             while (bytes--)
     932     35740617 :               uv = (uv << 8) | (*ptr++ & 0xff);
     933     22890016 :           v = int (uv);
     934              :         }
     935     78833144 :       else if (v & 0x40)
     936     10197457 :         v |= -1 ^ 0x3f;
     937              :     }
     938              : 
     939    101723160 :   return v;
     940              : }
     941              : 
     942              : void
     943    126393871 : bytes_out::u (unsigned v)
     944              : {
     945    126393871 :   if (char *ptr = write (1))
     946              :     {
     947    126393871 :       if (v <= 0x7f)
     948    109851347 :         *ptr = v;
     949              :       else
     950              :         {
     951     16542524 :           unsigned bytes = 0;
     952     16542524 :           unsigned probe;
     953     19729303 :           for (probe = v >> 8; probe > 0xf; probe >>= 8)
     954      3186779 :             bytes++;
     955     16542524 :           *ptr = 0x80 | bytes << 4 | probe;
     956     16542524 :           if ((ptr = write (++bytes)))
     957     36271827 :             for (; bytes--; v >>= 8)
     958     19729303 :               ptr[bytes] = v & 0xff;
     959              :         }
     960              :     }
     961    126393871 : }
     962              : 
     963              : unsigned
     964     87966610 : bytes_in::u ()
     965              : {
     966     87966610 :   unsigned v = 0;
     967              : 
     968     87966610 :   if (const char *ptr = read (1))
     969              :     {
     970     87966610 :       v = *ptr & 0xff;
     971     87966610 :       if (v & 0x80)
     972              :         {
     973     11646888 :           unsigned bytes = (v >> 4) & 0x7;
     974     11646888 :           v &= 0xf;
     975     11646888 :           if ((ptr = read (++bytes)))
     976     25553926 :             while (bytes--)
     977     13907038 :               v = (v << 8) | (*ptr++ & 0xff);
     978              :         }
     979              :     }
     980              : 
     981     87966610 :   return v;
     982              : }
     983              : 
     984              : void
     985     30193252 : bytes_out::wi (HOST_WIDE_INT v)
     986              : {
     987     30193252 :   if (char *ptr = write (1))
     988              :     {
     989     30193252 :       if (v <= 0x3f && v >= -0x40)
     990      5758198 :         *ptr = v & 0x7f;
     991              :       else
     992              :         {
     993     24435054 :           unsigned bytes = 0;
     994     24435054 :           HOST_WIDE_INT probe;
     995     24435054 :           if (v >= 0)
     996     89736213 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     997     65304658 :               bytes++;
     998              :           else
     999        11068 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
    1000         7569 :               bytes++;
    1001     24435054 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
    1002     24435054 :           if ((ptr = write (++bytes)))
    1003    114182335 :             for (; bytes--; v >>= 8)
    1004     89747281 :               ptr[bytes] = v & 0xff;
    1005              :         }
    1006              :     }
    1007     30193252 : }
    1008              : 
    1009              : HOST_WIDE_INT
    1010     19872265 : bytes_in::wi ()
    1011              : {
    1012     19872265 :   HOST_WIDE_INT v = 0;
    1013     19872265 :   if (const char *ptr = read (1))
    1014              :     {
    1015     19872265 :       v = *ptr & 0xff;
    1016     19872265 :       if (v & 0x80)
    1017              :         {
    1018     17920212 :           unsigned bytes = (v >> 4) & 0x7;
    1019     17920212 :           v &= 0xf;
    1020     17920212 :           if (v & 0x8)
    1021         2154 :             v |= -1 ^ 0x7;
    1022              :           /* unsigned necessary due to left shifts of -ve values.  */
    1023     17920212 :           unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
    1024     17920212 :           if ((ptr = read (++bytes)))
    1025     83039952 :             while (bytes--)
    1026     65119740 :               uv = (uv << 8) | (*ptr++ & 0xff);
    1027     17920212 :           v = (HOST_WIDE_INT) uv;
    1028              :         }
    1029      1952053 :       else if (v & 0x40)
    1030         9478 :         v |= -1 ^ 0x3f;
    1031              :     }
    1032              : 
    1033     19872265 :   return v;
    1034              : }
    1035              : 
    1036              : /* unsigned wide ints are just written as signed wide ints.  */
    1037              : 
    1038              : inline void
    1039     30192280 : bytes_out::wu (unsigned HOST_WIDE_INT v)
    1040              : {
    1041     30192280 :   wi ((HOST_WIDE_INT) v);
    1042              : }
    1043              : 
    1044              : inline unsigned HOST_WIDE_INT
    1045     19871667 : bytes_in::wu ()
    1046              : {
    1047     39131637 :   return (unsigned HOST_WIDE_INT) wi ();
    1048              : }
    1049              : 
    1050              : /* size_t written as unsigned or unsigned wide int.  */
    1051              : 
    1052              : inline void
    1053      2401197 : bytes_out::z (size_t s)
    1054              : {
    1055      2401197 :   if (sizeof (s) == sizeof (unsigned))
    1056              :     u (s);
    1057              :   else
    1058      4768789 :     wu (s);
    1059              : }
    1060              : 
    1061              : inline size_t
    1062      1599294 : bytes_in::z ()
    1063              : {
    1064      1599294 :   if (sizeof (size_t) == sizeof (unsigned))
    1065              :     return u ();
    1066              :   else
    1067      3198588 :     return wu ();
    1068              : }
    1069              : 
    1070              : /* location_t written as 32- or 64-bit as needed.  */
    1071              : 
    1072     26882692 : inline void bytes_out::loc (location_t l)
    1073              : {
    1074     26882692 :   if (sizeof (location_t) > sizeof (unsigned))
    1075     50941124 :     wu (l);
    1076              :   else
    1077              :     u (l);
    1078              : }
    1079              : 
    1080     17663738 : inline location_t bytes_in::loc ()
    1081              : {
    1082     17663738 :   if (sizeof (location_t) > sizeof (unsigned))
    1083     35324384 :     return wu ();
    1084              :   else
    1085              :     return u ();
    1086              : }
    1087              : 
    1088              : /* Buffer simply memcpied.  */
    1089              : void *
    1090      2447183 : bytes_out::buf (size_t len)
    1091              : {
    1092      2447183 :   align (sizeof (void *) * 2);
    1093      2447183 :   return write (len);
    1094              : }
    1095              : 
    1096              : void
    1097      2394434 : bytes_out::buf (const void *src, size_t len)
    1098              : {
    1099      2394434 :   if (void *ptr = buf (len))
    1100      2394434 :     memcpy (ptr, src, len);
    1101      2394434 : }
    1102              : 
    1103              : const void *
    1104      1617627 : bytes_in::buf (size_t len)
    1105              : {
    1106      1617627 :   align (sizeof (void *) * 2);
    1107      1617627 :   const char *ptr = read (len);
    1108              : 
    1109      1617627 :   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      2401179 : bytes_out::str (const char *string, size_t len)
    1117              : {
    1118      2367586 :   z (len);
    1119      2367586 :   if (len)
    1120              :     {
    1121      2367586 :       gcc_checking_assert (!string[len]);
    1122      2367586 :       buf (string, len + 1);
    1123              :     }
    1124      2367586 : }
    1125              : 
    1126              : const char *
    1127      1599288 : bytes_in::str (size_t *len_p)
    1128              : {
    1129      1599288 :   size_t len = z ();
    1130              : 
    1131              :   /* We're about to trust some user data.  */
    1132      1599288 :   if (overrun)
    1133            0 :     len = 0;
    1134      1599288 :   if (len_p)
    1135      1595285 :     *len_p = len;
    1136      1599288 :   const char *str = NULL;
    1137      1599288 :   if (len)
    1138              :     {
    1139      1599069 :       str = reinterpret_cast<const char *> (buf (len + 1));
    1140      1599069 :       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       255584 : bytes_in::cpp_node ()
    1151              : {
    1152       255584 :   size_t len;
    1153       255584 :   const char *s = str (&len);
    1154       255584 :   if (!len)
    1155              :     return NULL;
    1156       255365 :   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        28823 : bytes_out::printf (const char *format, ...)
    1164              : {
    1165        28823 :   va_list args;
    1166              :   /* Exercise buffer expansion.  */
    1167        28823 :   size_t len = EXPERIMENT (10, 500);
    1168              : 
    1169        57089 :   while (char *ptr = write (len))
    1170              :     {
    1171        57089 :       va_start (args, format);
    1172        57089 :       size_t actual = vsnprintf (ptr, len, format, args) + 1;
    1173        57089 :       va_end (args);
    1174        57089 :       if (actual <= len)
    1175              :         {
    1176        28823 :           unuse (len - actual);
    1177        28823 :           break;
    1178              :         }
    1179        28266 :       unuse (len);
    1180        28266 :       len = actual;
    1181        28266 :     }
    1182        28823 : }
    1183              : 
    1184              : void
    1185         5630 : bytes_out::print_time (const char *kind, const tm *time, const char *tz)
    1186              : {
    1187         5630 :   printf ("%stime: %4u/%02u/%02u %02u:%02u:%02u %s",
    1188         5630 :           kind, time->tm_year + 1900, time->tm_mon + 1, time->tm_mday,
    1189         5630 :           time->tm_hour, time->tm_min, time->tm_sec, tz);
    1190         5630 : }
    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         6050 :   elf (int fd, int e)
    1298        12100 :     :hdr (), sectab (), strtab (), fd (fd), err (fd >= 0 ? 0 : e)
    1299              :   {}
    1300         5968 :   ~elf ()
    1301              :   {
    1302         5968 :     gcc_checking_assert (fd < 0 && !hdr.buffer
    1303              :                          && !sectab.buffer && !strtab.buffer);
    1304         5968 :   }
    1305              : 
    1306              : public:
    1307              :   /* Return the error, if we have an error.  */
    1308       464931 :   int get_error () const
    1309              :   {
    1310       464931 :     return err;
    1311              :   }
    1312              :   /* Set the error, unless it's already been set.  */
    1313           53 :   void set_error (int e = E_BAD_DATA)
    1314              :   {
    1315           53 :     if (!err)
    1316           19 :       err = e;
    1317              :   }
    1318              :   /* Get an error string.  */
    1319              :   const char *get_error (const char *) const;
    1320              : 
    1321              : public:
    1322              :   /* Begin reading/writing file.  Return false on error.  */
    1323         5929 :   bool begin () const
    1324              :   {
    1325         5929 :     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         8452 : elf::end ()
    1358              : {
    1359              :   /* Close the stream and free the section table.  */
    1360         8452 :   if (fd >= 0 && close (fd))
    1361            0 :     set_error (errno);
    1362         8452 :   fd = -1;
    1363              : 
    1364         8452 :   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         3085 :   elf_in (int fd, int e)
    1381         6170 :     :parent (fd, e)
    1382              :   {
    1383              :   }
    1384         3003 :   ~elf_in ()
    1385              :   {
    1386         3003 :   }
    1387              : 
    1388              : public:
    1389       218942 :   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         1080 :   static void release (elf_in *self ATTRIBUTE_UNUSED, bytes_in &bytes)
    1416              :   {
    1417              : #if MAPPED_READING
    1418         1080 :     if (!(self && self->hdr.buffer && bytes.buffer >= self->hdr.buffer
    1419         1080 :           && bytes.buffer < self->hdr.buffer + self->hdr.pos))
    1420              : #endif
    1421            0 :       data::simple_memory.shrink (bytes.buffer);
    1422         1080 :     bytes.buffer = NULL;
    1423         1080 :     bytes.size = 0;
    1424         1080 :   }
    1425              : 
    1426              : public:
    1427       243914 :   static void grow (data &data, unsigned needed)
    1428              :   {
    1429       243914 :     gcc_checking_assert (!data.buffer);
    1430              : #if !MAPPED_READING
    1431              :     data.buffer = XNEWVEC (char, needed);
    1432              : #endif
    1433       243914 :     data.size = needed;
    1434       243914 :   }
    1435       250683 :   static void shrink (data &data)
    1436              :   {
    1437              : #if !MAPPED_READING
    1438              :     XDELETEVEC (data.buffer);
    1439              : #endif
    1440       250683 :     data.buffer = NULL;
    1441       250683 :     data.size = 0;
    1442              :   }
    1443              : 
    1444              : public:
    1445       240853 :   const section *get_section (unsigned s) const
    1446              :   {
    1447       240853 :     if (s * sizeof (section) < sectab.size)
    1448       240853 :       return reinterpret_cast<const section *>
    1449       240853 :         (&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       240853 :   bool read (data *d, const section *s)
    1464              :   {
    1465       240853 :     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         8523 :   void release ()
    1476              :   {
    1477         8523 :     shrink (strtab);
    1478              :   }
    1479              : 
    1480              : public:
    1481              :   bool begin (location_t);
    1482         5487 :   bool end ()
    1483              :   {
    1484         5487 :     release ();
    1485              : #if MAPPED_READING
    1486         5487 :     if (hdr.buffer)
    1487         3003 :       munmap (hdr.buffer, hdr.pos);
    1488         5487 :     hdr.buffer = NULL;
    1489              : #endif
    1490         5487 :     shrink (sectab);
    1491              : 
    1492         5487 :     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       324945 :   const char *name (unsigned offset)
    1499              :   {
    1500       649890 :     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         2965 :   elf_out (int fd, int e)
    1523         5815 :     :parent (fd, e), identtab (500), pos (0), began (false)
    1524              :   {
    1525              : #if MAPPED_WRITING
    1526         2965 :     offset = extent = 0;
    1527         2965 :     page_size = sysconf (_SC_PAGE_SIZE);
    1528         2965 :     if (page_size < SECTION_ALIGN)
    1529              :       /* Something really strange.  */
    1530            0 :       set_error (EINVAL);
    1531              : #endif
    1532         2965 :   }
    1533         2965 :   ~elf_out ()
    1534         2965 :   {
    1535         2965 :     data::simple_memory.shrink (hdr);
    1536         2965 :     data::simple_memory.shrink (sectab);
    1537         2965 :     data::simple_memory.shrink (strtab);
    1538         2965 :   }
    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         5930 :   unsigned get_section_limit () const
    1556              :   {
    1557         5930 :     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        21884 : bytes_in::begin (location_t loc, elf_in *source, const char *name)
    1595              : {
    1596        21884 :   unsigned snum = source->find (name);
    1597              : 
    1598        21884 :   return begin (loc, source, snum, name);
    1599              : }
    1600              : 
    1601              : /* Begin reading section numbered SNUM with NAME (may be NULL).  */
    1602              : 
    1603              : bool
    1604       237792 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
    1605              : {
    1606       237792 :   if (!source->read (this, source->find (snum))
    1607       237792 :       || !size || !check_crc ())
    1608              :     {
    1609            0 :       source->set_error (elf::E_BAD_DATA);
    1610            0 :       source->shrink (*this);
    1611            0 :       if (name)
    1612            0 :         error_at (loc, "section %qs is missing or corrupted", name);
    1613              :       else
    1614            0 :         error_at (loc, "section #%u is missing or corrupted", snum);
    1615              :       return false;
    1616              :     }
    1617       237792 :   pos = 4;
    1618       237792 :   return true;
    1619              : }
    1620              : 
    1621              : /* Finish reading a section.  */
    1622              : 
    1623              : bool
    1624       236673 : bytes_in::end (elf_in *src)
    1625              : {
    1626       236673 :   if (more_p ())
    1627           13 :     set_overrun ();
    1628       236673 :   if (overrun)
    1629           13 :     src->set_error ();
    1630              : 
    1631       236673 :   src->shrink (*this);
    1632              : 
    1633       236673 :   return !overrun;
    1634              : }
    1635              : 
    1636              : /* Begin writing buffer.  */
    1637              : 
    1638              : void
    1639       362979 : bytes_out::begin (bool need_crc)
    1640              : {
    1641            0 :   if (need_crc)
    1642            0 :     pos = 4;
    1643            0 :   memory->grow (*this, 0, false);
    1644            0 : }
    1645              : 
    1646              : /* Finish writing buffer.  Stream out to SINK as named section NAME.
    1647              :    Return section number or 0 on failure.  If CRC_PTR is true, crc
    1648              :    the data.  Otherwise it is a string section.  */
    1649              : 
    1650              : unsigned
    1651       362979 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    1652              : {
    1653       362979 :   lengths[3] += pos;
    1654       362979 :   spans[3]++;
    1655              : 
    1656       362979 :   set_crc (crc_ptr);
    1657       362979 :   unsigned sec_num = sink->add (*this, !crc_ptr, name);
    1658       362979 :   memory->shrink (*this);
    1659              : 
    1660       362979 :   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       243914 : elf_in::read (data *data, unsigned pos, unsigned length)
    1726              : {
    1727              : #if MAPPED_READING
    1728       243914 :   if (pos + length > hdr.pos)
    1729              :     {
    1730            0 :       set_error (EINVAL);
    1731              :       return NULL;
    1732              :     }
    1733              : #else
    1734              :   if (pos != ~0u && lseek (fd, pos, SEEK_SET) < 0)
    1735              :     {
    1736              :       set_error (errno);
    1737              :       return NULL;
    1738              :     }
    1739              : #endif
    1740       243914 :   grow (*data, length);
    1741              : #if MAPPED_READING
    1742       243914 :   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       243914 :   return data->buffer;
    1753              : }
    1754              : 
    1755              : /* Read section SNUM of TYPE.  Return section pointer or NULL on error.  */
    1756              : 
    1757              : const elf::section *
    1758       240853 : elf_in::find (unsigned snum, unsigned type)
    1759              : {
    1760       240853 :   const section *sec = get_section (snum);
    1761       240853 :   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        21929 : elf_in::find (const char *sname)
    1771              : {
    1772       140319 :   for (unsigned pos = sectab.size; pos -= sizeof (section); )
    1773              :     {
    1774       140319 :       const section *sec
    1775       140319 :         = reinterpret_cast<const section *> (&sectab.buffer[pos]);
    1776              : 
    1777       280638 :       if (0 == strcmp (sname, name (sec->name)))
    1778        21929 :         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         3085 : elf_in::begin (location_t loc)
    1789              : {
    1790         3085 :   if (!parent::begin ())
    1791              :     return false;
    1792              : 
    1793         3061 :   struct stat stat;
    1794         3061 :   unsigned size = 0;
    1795         3061 :   if (!fstat (fd, &stat))
    1796              :     {
    1797              : #if !defined (HOST_LACKS_INODE_NUMBERS)
    1798         3061 :       device = stat.st_dev;
    1799         3061 :       inode = stat.st_ino;
    1800              : #endif
    1801              :       /* Never generate files > 4GB, check we've not been given one.  */
    1802         3061 :       if (stat.st_size == unsigned (stat.st_size))
    1803         3061 :         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         3061 :   void *mapping = mmap (NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    1810         3061 :   if (mapping == MAP_FAILED)
    1811              :     {
    1812            0 :     fail:
    1813            0 :       set_error (errno);
    1814              :       return false;
    1815              :     }
    1816              :   /* We'll be hopping over this randomly.  Some systems declare the
    1817              :      first parm as char *, and other declare it as void *.  */
    1818         3061 :   if (madvise (reinterpret_cast <char *> (mapping), size, MADV_RANDOM))
    1819            0 :     goto fail;
    1820              : 
    1821         3061 :   hdr.buffer = (char *)mapping;
    1822              : #else
    1823              :   read (&hdr, 0, sizeof (header));
    1824              : #endif
    1825         3061 :   hdr.pos = size; /* Record size of the file.  */
    1826              : 
    1827         3061 :   const header *h = reinterpret_cast<const header *> (hdr.buffer);
    1828         3061 :   if (!h)
    1829              :     return false;
    1830              : 
    1831         3061 :   if (h->ident.magic[0] != 0x7f
    1832         3061 :       || h->ident.magic[1] != 'E'
    1833         3061 :       || h->ident.magic[2] != 'L'
    1834         3061 :       || 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         3061 :   if (h->ident.klass != MY_CLASS
    1845         3061 :       || h->ident.data != MY_ENDIAN
    1846         3061 :       || h->ident.version != EV_CURRENT
    1847         3061 :       || h->type != ET_NONE
    1848         3061 :       || h->machine != EM_NONE
    1849         3061 :       || h->ident.osabi != OSABI_NONE)
    1850              :     {
    1851            0 :       error_at (loc, "unexpected encapsulation format or type");
    1852            0 :       goto failed;
    1853              :     }
    1854              : 
    1855         3061 :   int e = -1;
    1856         3061 :   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         3061 :   unsigned strndx = h->shstrndx;
    1865         3061 :   unsigned shnum = h->shnum;
    1866         3061 :   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         3061 :   if (!shnum)
    1881            0 :     goto malformed;
    1882              : 
    1883         3061 :   if (!read (&sectab, h->shoff, shnum * sizeof (section)))
    1884            0 :     goto section_table_fail;
    1885              : 
    1886         3061 :   if (strndx == SHN_XINDEX)
    1887            0 :     strndx = get_section (0)->link;
    1888              : 
    1889         3061 :   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         3061 :   if (!(strtab.size && !strtab.buffer[0]
    1895         3061 :         && !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         3061 :   sectab.pos = h->shoff;
    1901         3061 :   strtab.pos = shnum * sizeof (section);
    1902              : #else
    1903              :   shrink (hdr);
    1904              : #endif
    1905              : 
    1906         3061 :   return true;
    1907              : }
    1908              : 
    1909              : /* Create a new mapping.  */
    1910              : 
    1911              : #if MAPPED_WRITING
    1912              : void
    1913         3733 : 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         7311 :   auto allocate = [](int fd, off_t offset, off_t length)
    1918              :     {
    1919              : #ifdef HAVE_POSIX_FALLOCATE
    1920         3578 :       int result = posix_fallocate (fd, offset, length);
    1921         3578 :       if (result != EINVAL && result != ENOTSUP && result != EOPNOTSUPP)
    1922         3578 :         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         3733 :   void *mapping = MAP_FAILED;
    1929         3733 :   if (extending && ext < 1024 * 1024)
    1930              :     {
    1931         3395 :       if (allocate (fd, offset, ext * 2))
    1932         3395 :         mapping = mmap (NULL, ext * 2, PROT_READ | PROT_WRITE,
    1933         3395 :                         MAP_SHARED, fd, offset);
    1934         3395 :       if (mapping != MAP_FAILED)
    1935              :         ext *= 2;
    1936              :     }
    1937              :   if (mapping == MAP_FAILED)
    1938              :     {
    1939          338 :       if (!extending || allocate (fd, offset, ext))
    1940          338 :         mapping = mmap (NULL, ext, PROT_READ | PROT_WRITE,
    1941          338 :                         MAP_SHARED, fd, offset);
    1942          338 :       if (mapping == MAP_FAILED)
    1943              :         {
    1944            0 :           set_error (errno);
    1945              :           mapping = NULL;
    1946              :           ext = 0;
    1947              :         }
    1948              :     }
    1949         3733 :   hdr.buffer = (char *)mapping;
    1950         3733 :   extent = ext;
    1951         3733 : }
    1952              : #endif
    1953              : 
    1954              : /* Flush out the current mapping.  */
    1955              : 
    1956              : #if MAPPED_WRITING
    1957              : void
    1958         3733 : elf_out::remove_mapping ()
    1959              : {
    1960         3733 :   if (hdr.buffer)
    1961              :     {
    1962              :       /* MS_ASYNC dtrt with the removed mapping, including a
    1963              :          subsequent overlapping remap.  */
    1964         3733 :       if (msync (hdr.buffer, extent, MS_ASYNC)
    1965         3733 :           || munmap (hdr.buffer, extent))
    1966              :         /* We're somewhat screwed at this point.  */
    1967            0 :         set_error (errno);
    1968              :     }
    1969              : 
    1970         3733 :   hdr.buffer = NULL;
    1971         3733 : }
    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       884640 : elf_out::grow (char *data, unsigned needed)
    1979              : {
    1980       884640 :   if (!data)
    1981              :     {
    1982              :       /* First allocation, check we're aligned.  */
    1983       368667 :       gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    1984              : #if MAPPED_WRITING
    1985       368667 :       data = hdr.buffer + (pos - offset);
    1986              : #endif
    1987              :     }
    1988              : 
    1989              : #if MAPPED_WRITING
    1990       884640 :   unsigned off = data - hdr.buffer;
    1991       884640 :   if (off + needed > extent)
    1992              :     {
    1993              :       /* We need to grow the mapping.  */
    1994          734 :       unsigned lwm = off & ~(page_size - 1);
    1995          734 :       unsigned hwm = (off + needed + page_size - 1) & ~(page_size - 1);
    1996              : 
    1997          734 :       gcc_checking_assert (hwm > extent);
    1998              : 
    1999          734 :       remove_mapping ();
    2000              : 
    2001          734 :       offset += lwm;
    2002          734 :       create_mapping (extent < hwm - lwm ? hwm - lwm : extent);
    2003              : 
    2004          734 :       data = hdr.buffer + (off - lwm);
    2005              :     }
    2006              : #else
    2007              :   data = allocator::grow (data, needed);
    2008              : #endif
    2009              : 
    2010       884640 :   return data;
    2011              : }
    2012              : 
    2013              : #if MAPPED_WRITING
    2014              : /* Shrinking is a NOP.  */
    2015              : void
    2016       368667 : elf_out::shrink (char *)
    2017              : {
    2018       368667 : }
    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      1662960 : elf_out::strtab_write (const char *s, unsigned l)
    2026              : {
    2027      1662960 :   if (strtab.pos + l > strtab.size)
    2028         1333 :     data::simple_memory.grow (strtab, strtab.pos + l, false);
    2029      1662960 :   memcpy (strtab.buffer + strtab.pos, s, l);
    2030      1662960 :   unsigned res = strtab.pos;
    2031      1662960 :   strtab.pos += l;
    2032      1662960 :   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       669622 : elf_out::strtab_write (tree decl, int inner)
    2040              : {
    2041       669622 :   tree ctx = CP_DECL_CONTEXT (decl);
    2042       669622 :   if (TYPE_P (ctx))
    2043         8255 :     ctx = TYPE_NAME (ctx);
    2044       669622 :   if (ctx != global_namespace)
    2045       345836 :     strtab_write (ctx, -1);
    2046              : 
    2047       669622 :   tree name = DECL_NAME (decl);
    2048       669622 :   if (!name)
    2049          339 :     name = DECL_ASSEMBLER_NAME_RAW (decl);
    2050       669622 :   strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
    2051              : 
    2052       669622 :   if (inner)
    2053       485728 :     strtab_write (&"::{}"[inner+1], 2);
    2054       669622 : }
    2055              : 
    2056              : /* Map IDENTIFIER IDENT to strtab offset.  Inserts into strtab if not
    2057              :    already there.  */
    2058              : 
    2059              : unsigned
    2060       166494 : elf_out::name (tree ident)
    2061              : {
    2062       166494 :   unsigned res = 0;
    2063       166494 :   if (ident)
    2064              :     {
    2065       166439 :       bool existed;
    2066       166439 :       int *slot = &identtab.get_or_insert (ident, &existed);
    2067       166439 :       if (!existed)
    2068       294870 :         *slot = strtab_write (IDENTIFIER_POINTER (ident),
    2069       147435 :                               IDENTIFIER_LENGTH (ident) + 1);
    2070       166439 :       res = *slot;
    2071              :     }
    2072       166494 :   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        36389 : elf_out::name (const char *literal)
    2080              : {
    2081        36389 :   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       323786 : elf_out::qualified_name (tree decl, bool is_defn)
    2089              : {
    2090       323786 :   gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
    2091       323786 :   unsigned result = strtab.pos;
    2092              : 
    2093       323786 :   strtab_write (decl, is_defn);
    2094       323786 :   strtab_write ("", 1);
    2095              : 
    2096       323786 :   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       368667 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
    2105              :               unsigned flags)
    2106              : {
    2107       368667 :   gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
    2108       368667 :   if (sectab.pos + sizeof (section) > sectab.size)
    2109         4956 :     data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
    2110       368667 :   section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
    2111       368667 :   memset (sec, 0, sizeof (section));
    2112       368667 :   sec->type = type;
    2113       368667 :   sec->flags = flags;
    2114       368667 :   sec->name = name;
    2115       368667 :   sec->offset = off;
    2116       368667 :   sec->size = size;
    2117       368667 :   if (flags & SHF_STRINGS)
    2118         5659 :     sec->entsize = 1;
    2119              : 
    2120       368667 :   unsigned res = sectab.pos;
    2121       368667 :   sectab.pos += sizeof (section);
    2122       368667 :   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        11376 : elf_out::write (const data &buffer)
    2130              : {
    2131              : #if MAPPED_WRITING
    2132              :   /* HDR is always mapped.  */
    2133        11376 :   if (&buffer != &hdr)
    2134              :     {
    2135         5688 :       bytes_out out (this);
    2136         5688 :       grow (out, buffer.pos, true);
    2137         5688 :       if (out.buffer)
    2138         5688 :         memcpy (out.buffer, buffer.buffer, buffer.pos);
    2139         5688 :       shrink (out);
    2140         5688 :     }
    2141              :   else
    2142              :     /* We should have been aligned during the first allocation.  */
    2143         5688 :     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        11376 :   unsigned res = pos;
    2152        11376 :   pos += buffer.pos;
    2153              : 
    2154        11376 :   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         9698 :       pos += padding;
    2164              :     }
    2165        11376 :   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       362979 : elf_out::write (const bytes_out &buf)
    2173              : {
    2174       362979 :   gcc_checking_assert (buf.memory == this);
    2175              :   /* A directly mapped buffer.  */
    2176       362979 :   gcc_checking_assert (buf.buffer - hdr.buffer >= 0
    2177              :                        && buf.buffer - hdr.buffer + buf.size <= extent);
    2178       362979 :   unsigned res = pos;
    2179       362979 :   pos += buf.pos;
    2180              : 
    2181              :   /* Align up.  We're not going to advance into the next page. */
    2182       362979 :   pos += -pos & (SECTION_ALIGN - 1);
    2183              : 
    2184       362979 :   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       362979 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
    2195              : {
    2196       362979 :   unsigned off = write (data);
    2197              : 
    2198       725958 :   return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
    2199       362979 :               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         2844 : elf_out::begin ()
    2207              : {
    2208         2844 :   if (!parent::begin ())
    2209              :     return false;
    2210              : 
    2211              :   /* Let the allocators pick a default.  */
    2212         2844 :   data::simple_memory.grow (strtab, 0, false);
    2213         2844 :   data::simple_memory.grow (sectab, 0, false);
    2214              : 
    2215              :   /* The string table starts with an empty string.  */
    2216         2844 :   name ("");
    2217              : 
    2218              :   /* Create the UNDEF section.  */
    2219         2844 :   add (SHT_NONE);
    2220              : 
    2221              : #if MAPPED_WRITING
    2222              :   /* Start a mapping.  */
    2223         2844 :   create_mapping (EXPERIMENT (page_size,
    2224              :                               (32767 + page_size) & ~(page_size - 1)));
    2225         2844 :   if (!hdr.buffer)
    2226              :     return false;
    2227              : #endif
    2228              : 
    2229              :   /* Write an empty header.  */
    2230         2844 :   grow (hdr, sizeof (header), true);
    2231         2844 :   header *h = reinterpret_cast<header *> (hdr.buffer);
    2232         2844 :   memset (h, 0, sizeof (header));
    2233         2844 :   hdr.pos = hdr.size;
    2234         2844 :   write (hdr);
    2235         2844 :   if (get_error ())
    2236              :     return false;
    2237         2844 :   began = true;
    2238         2844 :   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         2965 : elf_out::end ()
    2246              : {
    2247         2965 :   if (fd >= 0 && began)
    2248              :     {
    2249              :       /* Write the string table.  */
    2250         2844 :       unsigned strnam = name (".strtab");
    2251         2844 :       unsigned stroff = write (strtab);
    2252         2844 :       unsigned strndx = add (SHT_STRTAB, strnam, stroff, strtab.pos,
    2253              :                              SHF_STRINGS);
    2254              : 
    2255              :       /* Store escape values in section[0].  */
    2256         2844 :       if (strndx >= SHN_LORESERVE)
    2257              :         {
    2258            0 :           reinterpret_cast<section *> (sectab.buffer)->link = strndx;
    2259            0 :           strndx = SHN_XINDEX;
    2260              :         }
    2261         2844 :       unsigned shnum = sectab.pos / sizeof (section);
    2262         2844 :       if (shnum >= SHN_LORESERVE)
    2263              :         {
    2264            0 :           reinterpret_cast<section *> (sectab.buffer)->size = shnum;
    2265            0 :           shnum = SHN_XINDEX;
    2266              :         }
    2267              : 
    2268         2844 :       unsigned shoff = write (sectab);
    2269              : 
    2270              : #if MAPPED_WRITING
    2271         2844 :       if (offset)
    2272              :         {
    2273          155 :           remove_mapping ();
    2274          155 :           offset = 0;
    2275          155 :           create_mapping ((sizeof (header) + page_size - 1) & ~(page_size - 1),
    2276              :                           false);
    2277              :         }
    2278         2844 :       unsigned length = pos;
    2279              : #else
    2280              :       if (lseek (fd, 0, SEEK_SET) < 0)
    2281              :         set_error (errno);
    2282              : #endif
    2283              :       /* Write header.  */
    2284         2844 :       if (!get_error ())
    2285              :         {
    2286              :           /* Write the correct header now.  */
    2287         2844 :           header *h = reinterpret_cast<header *> (hdr.buffer);
    2288         2844 :           h->ident.magic[0] = 0x7f;
    2289         2844 :           h->ident.magic[1] = 'E';   /* Elrond */
    2290         2844 :           h->ident.magic[2] = 'L';   /* is an */
    2291         2844 :           h->ident.magic[3] = 'F';   /* elf.  */
    2292         2844 :           h->ident.klass = MY_CLASS;
    2293         2844 :           h->ident.data =  MY_ENDIAN;
    2294         2844 :           h->ident.version = EV_CURRENT;
    2295         2844 :           h->ident.osabi = OSABI_NONE;
    2296         2844 :           h->type = ET_NONE;
    2297         2844 :           h->machine = EM_NONE;
    2298         2844 :           h->version = EV_CURRENT;
    2299         2844 :           h->shoff = shoff;
    2300         2844 :           h->ehsize = sizeof (header);
    2301         2844 :           h->shentsize = sizeof (section);
    2302         2844 :           h->shnum = shnum;
    2303         2844 :           h->shstrndx = strndx;
    2304              : 
    2305         2844 :           pos = 0;
    2306         2844 :           write (hdr);
    2307              :         }
    2308              : 
    2309              : #if MAPPED_WRITING
    2310         2844 :       remove_mapping ();
    2311         2844 :       if (ftruncate (fd, length))
    2312            0 :         set_error (errno);
    2313              : #endif
    2314              :     }
    2315              : 
    2316         2965 :   data::simple_memory.shrink (sectab);
    2317         2965 :   data::simple_memory.shrink (strtab);
    2318              : 
    2319         2965 :   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      6647278 :   template<unsigned I> void set_flag_bit ()
    2426              :   {
    2427            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2428      6647278 :     discriminator |= 1u << I;
    2429      4216477 :   }
    2430      7115617 :   template<unsigned I> void clear_flag_bit ()
    2431              :   {
    2432            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2433      7115617 :     discriminator &= ~(1u << I);
    2434      7115617 :   }
    2435    685461150 :   template<unsigned I> bool get_flag_bit () const
    2436              :   {
    2437            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2438    831129907 :     return bool ((discriminator >> I) & 1);
    2439              :   }
    2440              : 
    2441              : public:
    2442    672545851 :   bool is_binding () const
    2443              :   {
    2444    157000851 :     return !get_flag_bit<DB_ZERO_BIT> ();
    2445              :   }
    2446    344027642 :   entity_kind get_entity_kind () const
    2447              :   {
    2448        41662 :     if (is_binding ())
    2449              :       return EK_BINDING;
    2450    260820128 :     return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
    2451              :   }
    2452              :   const char *entity_kind_name () const;
    2453              : 
    2454              : public:
    2455     10907453 :   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     10907453 :     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      2710584 :   bool is_pending_entity () const
    2466              :   {
    2467      4293036 :     return (get_entity_kind () == EK_SPECIALIZATION
    2468      1582452 :             || get_entity_kind () == EK_PARTIAL
    2469      4250558 :             || (get_entity_kind () == EK_DECL
    2470      1493059 :                 && 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     58585233 :   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     26017546 :     tree inner = STRIP_TEMPLATE (get_entity ());
    2487     58585233 :     return (get_flag_bit<DB_TU_LOCAL_BIT> ()
    2488     58585233 :             && (strict
    2489         3392 :                 || !VAR_OR_FUNCTION_DECL_P (inner)
    2490         2262 :                 || !NAMESPACE_SCOPE_P (inner)
    2491         2235 :                 || (DECL_LANG_SPECIFIC (inner)
    2492         2196 :                     && DECL_MODULE_PURVIEW_P (inner))));
    2493              :   }
    2494      1573386 :   bool refs_tu_local (bool strict = false) const
    2495              :   {
    2496       546183 :     return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
    2497       546183 :             || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
    2498              :   }
    2499      3820940 :   bool is_exposure (bool strict = false) const
    2500              :   {
    2501      1910411 :     return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
    2502      3820940 :             || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
    2503              :   }
    2504      2663669 :   bool is_ignored_exposure_context () const
    2505              :   {
    2506      2663669 :     return get_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
    2507              :   }
    2508              : 
    2509              : public:
    2510     35643528 :   bool is_import () const
    2511              :   {
    2512     10953280 :     return get_flag_bit<DB_IMPORTED_BIT> ();
    2513              :   }
    2514     21646413 :   bool is_unreached () const
    2515              :   {
    2516      1373140 :     return get_flag_bit<DB_UNREACHED_BIT> ();
    2517              :   }
    2518      2362687 :   bool is_hidden () const
    2519              :   {
    2520      2362687 :     return get_flag_bit<DB_HIDDEN_BIT> ();
    2521              :   }
    2522      1403621 :   bool is_maybe_recursive () const
    2523              :   {
    2524      1403621 :     return get_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
    2525              :   }
    2526         1136 :   bool is_entry () const
    2527              :   {
    2528         1136 :     return get_flag_bit<DB_ENTRY_BIT> ();
    2529              :   }
    2530      1692120 :   bool is_type_spec () const
    2531              :   {
    2532      1692120 :     return get_flag_bit<DB_TYPE_SPEC_BIT> ();
    2533              :   }
    2534      1692120 :   bool is_friend_spec () const
    2535              :   {
    2536      1692120 :     return get_flag_bit<DB_FRIEND_SPEC_BIT> ();
    2537              :   }
    2538              : 
    2539              : public:
    2540              :   /* We set these bit outside of depset.  */
    2541           89 :   void set_hidden_binding ()
    2542              :   {
    2543           89 :     set_flag_bit<DB_HIDDEN_BIT> ();
    2544              :   }
    2545           36 :   void clear_hidden_binding ()
    2546              :   {
    2547           36 :     clear_flag_bit<DB_HIDDEN_BIT> ();
    2548              :   }
    2549              : 
    2550              : public:
    2551     12915299 :   bool is_special () const
    2552              :   {
    2553     12915299 :     return get_flag_bit<DB_SPECIAL_BIT> ();
    2554              :   }
    2555      2430801 :   void set_special ()
    2556              :   {
    2557      2430801 :     set_flag_bit<DB_SPECIAL_BIT> ();
    2558              :   }
    2559              : 
    2560              : public:
    2561    217235280 :   tree get_entity () const
    2562              :   {
    2563     58585233 :     return entity;
    2564              :   }
    2565     27032604 :   tree get_name () const
    2566              :   {
    2567     27032604 :     gcc_checking_assert (is_binding ());
    2568     27032604 :     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     22125644 :     inline static hashval_t hash (const compare_type &p)
    2583              :     {
    2584     22125644 :       hashval_t h = pointer_hash<tree_node>::hash (p.first);
    2585     22125644 :       if (p.second)
    2586              :         {
    2587       244156 :           hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
    2588       244156 :           h = iterative_hash_hashval_t (h, nh);
    2589              :         }
    2590     22125644 :       return h;
    2591              :     }
    2592    135041319 :     inline static bool equal (const value_type b, const compare_type &p)
    2593              :     {
    2594    135041319 :       if (b->entity != p.first)
    2595              :         return false;
    2596              : 
    2597     16061240 :       if (p.second)
    2598        59450 :         return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
    2599              :       else
    2600     16001790 :         return !b->is_binding ();
    2601              :     }
    2602              : 
    2603              :     /* (re)hasher for a binding itself.  */
    2604    106278754 :     inline static hashval_t hash (const value_type b)
    2605              :     {
    2606    106278754 :       hashval_t h = pointer_hash<tree_node>::hash (b->entity);
    2607    106278754 :       if (b->is_binding ())
    2608              :         {
    2609     10577094 :           hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
    2610     10577094 :           h = iterative_hash_hashval_t (h, nh);
    2611              :         }
    2612    106278754 :       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      3447941 :     static void remove (value_type p)
    2625              :     {
    2626      3447941 :       delete (p);
    2627      3447941 :     }
    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       340752 :     hash (size_t size, hash *c = NULL)
    2663       340752 :       : parent (size), chain (c), current (NULL), section (0),
    2664       340752 :         reached_unreached (false), writing_merge_key (false),
    2665       340752 :         ignore_exposure (false)
    2666              :     {
    2667       340752 :       worklist.create (size);
    2668       340752 :       dep_adl_entity_list.create (16);
    2669       340752 :     }
    2670       340752 :     ~hash ()
    2671              :     {
    2672       340752 :       worklist.release ();
    2673       340752 :       dep_adl_entity_list.release ();
    2674       340752 :     }
    2675              : 
    2676              :   public:
    2677    162049434 :     bool is_key_order () const
    2678              :     {
    2679    162049434 :       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      1022627 :     temp_override<bool> ignore_exposure_if (bool cond)
    2687              :     {
    2688       806859 :       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       340723 :     tarjan (unsigned size)
    2737       340723 :       : index (0)
    2738              :     {
    2739       340723 :       result.create (size);
    2740       340723 :       stack.create (50);
    2741       340723 :     }
    2742       340723 :     ~tarjan ()
    2743              :     {
    2744       340723 :       gcc_assert (!stack.length ());
    2745       340723 :       stack.release ();
    2746       340723 :     }
    2747              : 
    2748              :   public:
    2749              :     void connect (depset *);
    2750              :   };
    2751              : };
    2752              : 
    2753              : inline
    2754      3447941 : depset::depset (tree entity)
    2755      3447941 :   :entity (entity), discriminator (0), cluster (0), section (0)
    2756              : {
    2757      3447941 :   deps.create (0);
    2758              : }
    2759              : 
    2760              : inline
    2761      3447941 : depset::~depset ()
    2762              : {
    2763      3447941 :   deps.release ();
    2764      3447941 : }
    2765              : 
    2766              : const char *
    2767        45312 : depset::entity_kind_name () const
    2768              : {
    2769              :   /* Same order as entity_kind.  */
    2770        45312 :   static const char *const names[] =
    2771              :     {"decl", "specialization", "partial", "using",
    2772              :      "namespace", "tu-local", "redirect", "binding"};
    2773        45312 :   static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
    2774              :                  "names must have an entry for every explicit entity_kind");
    2775        45312 :   entity_kind kind = get_entity_kind ();
    2776        45312 :   gcc_checking_assert (kind < ARRAY_SIZE (names));
    2777        45312 :   return names[kind];
    2778              : }
    2779              : 
    2780              : /* Create a depset for a namespace binding NS::NAME.  */
    2781              : 
    2782       164005 : depset *depset::make_binding (tree ns, tree name)
    2783              : {
    2784       164005 :   depset *binding = new depset (ns);
    2785              : 
    2786       164005 :   binding->discriminator = reinterpret_cast <uintptr_t> (name);
    2787              : 
    2788       164005 :   return binding;
    2789              : }
    2790              : 
    2791      3283936 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
    2792              : {
    2793      3283936 :   depset *r = new depset (entity);
    2794              : 
    2795      3283936 :   r->discriminator = ((1 << DB_ZERO_BIT)
    2796      3283936 :                       | (ek << DB_KIND_BIT)
    2797      3283936 :                       | is_defn << DB_DEFN_BIT);
    2798              : 
    2799      3283936 :   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     49885938 :   static hashval_t hash (const value_type &k)
    2816              :   {
    2817     49885938 :     hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
    2818     49885938 :     h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
    2819              : 
    2820     49885938 :     return h;
    2821              :   }
    2822     11557961 :   static bool equal (const value_type &k, const value_type &l)
    2823              :   {
    2824     11557961 :     return k.ns == l.ns && k.id == l.id;
    2825              :   }
    2826       222218 :   static void mark_empty (value_type &k)
    2827              :   {
    2828       222218 :     k.ns = k.id = NULL_TREE;
    2829              :   }
    2830         7891 :   static void mark_deleted (value_type &k)
    2831              :   {
    2832         7891 :     k.ns = NULL_TREE;
    2833         7891 :     gcc_checking_assert (k.id);
    2834         7891 :   }
    2835    395801005 :   static bool is_empty (const value_type &k)
    2836              :   {
    2837    395750498 :     return k.ns == NULL_TREE && k.id == NULL_TREE;
    2838              :   }
    2839     17461953 :   static bool is_deleted (const value_type &k)
    2840              :   {
    2841     17461953 :     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      3299594 :   merge_key ()
    2992      2667786 :     :ref_q (REF_QUAL_NONE), coro_disc (0), iobj_p (0), xobj_p (0), index (0),
    2993      3299594 :      ret (NULL_TREE), args (NULL_TREE),
    2994      3299594 :      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       132734 :   bool is_duplicate (tree decl)
    3121              :   {
    3122       265468 :     return find_duplicate (decl) != NULL;
    3123              :   }
    3124       169882 :   tree maybe_duplicate (tree decl)
    3125              :   {
    3126       169882 :     if (uintptr_t *dup = find_duplicate (decl))
    3127         8918 :       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       141758 :   void post_process (post_process_data data)
    3146              :   {
    3147       141758 :     post_decls.safe_push (data);
    3148              :   }
    3149              :   /* Register TYPE for postprocessing.  */
    3150           32 :   void post_process_type (tree type)
    3151              :   {
    3152           32 :     gcc_checking_assert (TYPE_P (type));
    3153           32 :     post_types.safe_push (type);
    3154           32 :   }
    3155              : 
    3156              : private:
    3157              :   void assert_definition (tree, bool installing);
    3158              : };
    3159              : } // anon namespace
    3160              : 
    3161       220280 : trees_in::trees_in (module_state *state)
    3162       220280 :   :parent (), state (state), unused (0)
    3163              : {
    3164       220280 :   duplicates = NULL;
    3165       220280 :   back_refs.create (500);
    3166       220280 :   post_decls.create (0);
    3167       220280 :   post_types.create (0);
    3168       220280 : }
    3169              : 
    3170       220280 : trees_in::~trees_in ()
    3171              : {
    3172       322269 :   delete (duplicates);
    3173       220280 :   back_refs.release ();
    3174       220280 :   post_decls.release ();
    3175       220280 :   post_types.release ();
    3176       220280 : }
    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    133196251 :   bool is_initial_scan () const
    3232              :   {
    3233    221347680 :     return !streaming_p () && !is_key_order ();
    3234              :   }
    3235    108511066 :   bool is_key_order () const
    3236              :   {
    3237     88151429 :     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       675816 :   void set_importing (int i ATTRIBUTE_UNUSED)
    3251              :   {
    3252              : #if CHECKING_P
    3253       675816 :     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       681520 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
    3357              :                       unsigned section)
    3358      1363040 :   :parent (mem), state (state), tree_map (500),
    3359       681520 :    dep_hash (&deps), ref_num (0), section (section),
    3360       681520 :    writing_local_entities (false), walking_bit_field_unit (false)
    3361              : {
    3362              : #if CHECKING_P
    3363       681520 :   importedness = 0;
    3364              : #endif
    3365       681520 : }
    3366              : 
    3367       681520 : trees_out::~trees_out ()
    3368              : {
    3369       681520 : }
    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         6120 :   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       101581 :   ~loc_spans ()
    3414              :   {
    3415       101581 :     delete spans;
    3416       101581 :   }
    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        16026 :   bool init_p () const
    3430              :   {
    3431        16026 :     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         5939 :   void maybe_init ()
    3439              :   {
    3440         5939 :     if (!init_p ())
    3441            6 :       init (line_table, nullptr);
    3442         5939 :   }
    3443              : 
    3444              : public:
    3445              :   enum {
    3446              :     SPAN_RESERVED = 0,  /* Reserved (fixed) locations.  */
    3447              :     SPAN_FIRST = 1,     /* LWM of locations to stream  */
    3448              :     SPAN_MAIN = 2       /* Main file and onwards.  */
    3449              :   };
    3450              : 
    3451              : public:
    3452       469638 :   location_t main_start () const
    3453              :   {
    3454       469638 :     return (*spans)[SPAN_MAIN].ordinary.first;
    3455              :   }
    3456              : 
    3457              : public:
    3458              :   void open (location_t);
    3459              :   void close ();
    3460              : 
    3461              : public:
    3462              :   /* Propagate imported linemaps to us, if needed.  */
    3463              :   bool maybe_propagate (module_state *import, location_t loc);
    3464              : 
    3465              : public:
    3466              :   /* Whether we can no longer represent new imported locations.  */
    3467            0 :   bool locations_exhausted_p () const
    3468              :   {
    3469            0 :     return locs_exhausted_p;
    3470              :   }
    3471            0 :   void report_location_exhaustion (location_t loc)
    3472              :   {
    3473            0 :     if (!locs_exhausted_p)
    3474              :       {
    3475              :         /* Just give the notice once.  */
    3476            0 :         locs_exhausted_p = true;
    3477            0 :         inform (loc, "unable to represent further imported source locations");
    3478              :       }
    3479              :   }
    3480              : 
    3481              : public:
    3482              :   const span *ordinary (location_t);
    3483              :   const span *macro (location_t);
    3484              : };
    3485              : 
    3486              : static loc_spans spans;
    3487              : 
    3488              : /* Information about ordinary locations we stream out.  */
    3489              : struct ord_loc_info
    3490              : {
    3491              :   const line_map_ordinary *src; // line map we're based on
    3492              :   line_map_uint_t offset;       // offset to this line
    3493              :   line_map_uint_t span;         // number of locs we span
    3494              :   line_map_uint_t remap;        // serialization
    3495              : 
    3496    346483772 :   static int compare (const void *a_, const void *b_)
    3497              :   {
    3498    346483772 :     auto *a = static_cast<const ord_loc_info *> (a_);
    3499    346483772 :     auto *b = static_cast<const ord_loc_info *> (b_);
    3500              : 
    3501    346483772 :     if (a->src != b->src)
    3502     50700432 :       return a->src < b->src ? -1 : +1;
    3503              : 
    3504              :     // Ensure no overlap
    3505    295783340 :     gcc_checking_assert (a->offset + a->span <= b->offset
    3506              :                          || b->offset + b->span <= a->offset);
    3507              : 
    3508    295783340 :     gcc_checking_assert (a->offset != b->offset);
    3509    295783340 :     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    142161722 :   static hashval_t hash (const value_type &v)
    3520              :   {
    3521    119449091 :     auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
    3522    142161722 :     return iterative_hash_hashval_t (v.offset, h);
    3523              :   }
    3524    157596222 :   static bool equal (const value_type &v, const compare_type p)
    3525              :   {
    3526    157596222 :     return v.src == p.src && v.offset == p.offset;
    3527              :   }
    3528              : 
    3529     10448772 :   static void mark_empty (value_type &v)
    3530              :   {
    3531     10448772 :     v.src = nullptr;
    3532              :   }
    3533    472787751 :   static bool is_empty (value_type &v)
    3534              :   {
    3535    472787751 :     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      2556207 :   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     10045740 :   static int compare (const void *a_, const void *b_)
    3555              :   {
    3556     10045740 :     auto *a = static_cast<const macro_loc_info *> (a_);
    3557     10045740 :     auto *b = static_cast<const macro_loc_info *> (b_);
    3558              : 
    3559     10045740 :     gcc_checking_assert (MAP_START_LOCATION (a->src)
    3560              :                          != MAP_START_LOCATION (b->src));
    3561     10045740 :     if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
    3562              :       return -1;
    3563              :     else
    3564      4844899 :       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     10727675 :   static hashval_t hash (compare_type p)
    3575              :   {
    3576     10727675 :     return pointer_hash<const line_map_macro>::hash (p);
    3577              :   }
    3578      9053948 :   static hashval_t hash (const value_type &v)
    3579              :   {
    3580      9053948 :     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       744179 :   static void mark_empty (value_type &v)
    3588              :   {
    3589       744179 :     v.src = nullptr;
    3590              :   }
    3591     34717627 :   static bool is_empty (value_type &v)
    3592              :   {
    3593     34717627 :     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       172577 :   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         5487 :   void close ()
    3646              :   {
    3647         5487 :     if (from)
    3648              :       {
    3649         3003 :         from->end ();
    3650         6006 :         delete from;
    3651         3003 :         from = NULL;
    3652              :       }
    3653         5487 :   }
    3654              : 
    3655              :  public:
    3656              :   void release_macros ();
    3657              : 
    3658              :  public:
    3659         3061 :   void alloc_remap (unsigned size)
    3660              :   {
    3661         3061 :     gcc_assert (!remap);
    3662         3061 :     vec_safe_reserve (remap, size);
    3663         9579 :     for (unsigned ix = size; ix--;)
    3664         3457 :       remap->quick_push (0);
    3665         3061 :   }
    3666      1252901 :   unsigned remap_module (unsigned owner)
    3667              :   {
    3668      1252901 :     if (owner < remap->length ())
    3669      1252901 :       return (*remap)[owner] >> 1;
    3670              :     return 0;
    3671              :   }
    3672              : 
    3673              :  public:
    3674              :   /* GC allocation.  But we must explicitly delete it.   */
    3675         3085 :   static void *operator new (size_t x)
    3676              :   {
    3677         6170 :     return ggc_alloc_atomic (x);
    3678              :   }
    3679         3003 :   static void operator delete (void *p)
    3680              :   {
    3681         3003 :     ggc_free (p);
    3682              :   }
    3683              : };
    3684              : 
    3685         3085 : slurping::slurping (elf_in *from)
    3686         3085 :   : remap (NULL), from (from),
    3687         3085 :     headers (BITMAP_GGC_ALLOC ()), macro_defs (), macro_tbl (),
    3688         3085 :     loc_deltas (0, 0),
    3689         3085 :     current (~0u), remaining (0), lru (0)
    3690              : {
    3691         3085 : }
    3692              : 
    3693         3003 : slurping::~slurping ()
    3694              : {
    3695         3003 :   vec_free (remap);
    3696         3003 :   remap = NULL;
    3697         3003 :   release_macros ();
    3698         3003 :   close ();
    3699         3003 : }
    3700              : 
    3701         5487 : void slurping::release_macros ()
    3702              : {
    3703         5487 :   if (macro_defs.size)
    3704          900 :     elf_in::release (from, macro_defs);
    3705         5487 :   if (macro_tbl.size)
    3706            0 :     elf_in::release (from, macro_tbl);
    3707         5487 : }
    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         3065 :   void release ()
    3820              :   {
    3821         3065 :     imports = exports = NULL;
    3822         3065 :     slurped ();
    3823              :   }
    3824         5549 :   void slurped ()
    3825              :   {
    3826         5549 :     delete slurp;
    3827         5549 :     slurp = NULL;
    3828         5549 :   }
    3829      1325567 :   elf_in *from () const
    3830              :   {
    3831      1325567 :     return slurp->from;
    3832              :   }
    3833              : 
    3834              :  public:
    3835              :   /* Kind of this module.  */
    3836       151760 :   bool is_module () const
    3837              :   {
    3838       151760 :     return module_p;
    3839              :   }
    3840      2605125 :   bool is_header () const
    3841              :   {
    3842      2605125 :     return header_p;
    3843              :   }
    3844          645 :   bool is_interface () const
    3845              :   {
    3846          645 :     return interface_p;
    3847              :   }
    3848       376181 :   bool is_partition () const
    3849              :   {
    3850       376181 :     return partition_p;
    3851              :   }
    3852              : 
    3853              :   /* How this module is used in the current TU.  */
    3854         3285 :   bool is_exported () const
    3855              :   {
    3856         3285 :     return exported_p;
    3857              :   }
    3858        21360 :   bool is_direct () const
    3859              :   {
    3860        21360 :     return directness >= MD_DIRECT;
    3861              :   }
    3862          286 :   bool is_purview_direct () const
    3863              :   {
    3864          286 :     return directness == MD_PURVIEW_DIRECT;
    3865              :   }
    3866          471 :   bool is_partition_direct () const
    3867              :   {
    3868          471 :     return directness == MD_PARTITION_DIRECT;
    3869              :   }
    3870              : 
    3871              :  public:
    3872              :   /* Is this a real module?  */
    3873        16590 :   bool has_location () const
    3874              :   {
    3875        16590 :     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        53846 :   const char *get_flatname () const
    4014              :   {
    4015        53846 :     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        11850 : module_state::module_state (tree name, module_state *parent, bool partition)
    4040        11850 :   : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
    4041        11850 :     parent (parent), name (name), slurp (NULL),
    4042        11850 :     flatname (NULL), filename (NULL),
    4043        11850 :     entity_lwm (~0u >> 1), entity_num (0),
    4044        11850 :     ordinary_locs (0, 0), macro_locs (0, 0),
    4045        11850 :     loc (UNKNOWN_LOCATION),
    4046        11850 :     crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
    4047              : {
    4048        11850 :   loadedness = ML_NONE;
    4049              : 
    4050        11850 :   module_p = header_p = interface_p = partition_p = false;
    4051              : 
    4052        11850 :   directness = MD_NONE;
    4053        11850 :   exported_p = false;
    4054              : 
    4055        11850 :   cmi_noted_p = false;
    4056        11850 :   active_init_p = false;
    4057              : 
    4058        11850 :   partition_p = partition;
    4059              : 
    4060        11850 :   inform_cmi_p = false;
    4061        11850 :   visited_p = false;
    4062              : 
    4063        11850 :   extensions = 0;
    4064        11850 :   if (name && TREE_CODE (name) == STRING_CST)
    4065              :     {
    4066         1931 :       header_p = true;
    4067              : 
    4068         1931 :       const char *string = TREE_STRING_POINTER (name);
    4069         1931 :       gcc_checking_assert (string[0] == '.'
    4070              :                            ? IS_DIR_SEPARATOR (string[1])
    4071              :                            : IS_ABSOLUTE_PATH (string));
    4072              :     }
    4073              : 
    4074        11850 :   gcc_checking_assert (!(parent && header_p));
    4075        11850 : }
    4076              : 
    4077           62 : module_state::~module_state ()
    4078              : {
    4079           62 :   release ();
    4080           62 : }
    4081              : 
    4082              : /* Hash module state.  */
    4083              : static hashval_t
    4084        17578 : module_name_hash (const_tree name)
    4085              : {
    4086        17578 :   if (TREE_CODE (name) == STRING_CST)
    4087         3543 :     return htab_hash_string (TREE_STRING_POINTER (name));
    4088              :   else
    4089        14035 :     return IDENTIFIER_HASH_VALUE (name);
    4090              : }
    4091              : 
    4092              : hashval_t
    4093         4981 : module_state_hash::hash (const value_type m)
    4094              : {
    4095         4981 :   hashval_t ph = pointer_hash<void>::hash
    4096         4981 :     (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
    4097         4981 :                                | m->is_partition ()));
    4098         4981 :   hashval_t nh = module_name_hash (m->name);
    4099         4981 :   return iterative_hash_hashval_t (ph, nh);
    4100              : }
    4101              : 
    4102              : /* Hash a name.  */
    4103              : hashval_t
    4104        12597 : module_state_hash::hash (const compare_type &c)
    4105              : {
    4106        12597 :   hashval_t ph = pointer_hash<void>::hash (reinterpret_cast<void *> (c.second));
    4107        12597 :   hashval_t nh = module_name_hash (c.first);
    4108              : 
    4109        12597 :   return iterative_hash_hashval_t (ph, nh);
    4110              : }
    4111              : 
    4112              : bool
    4113         8680 : module_state_hash::equal (const value_type existing,
    4114              :                           const compare_type &candidate)
    4115              : {
    4116         8680 :   uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
    4117         8680 :                   | existing->is_partition ());
    4118         8680 :   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         7135 :   if (existing->name == candidate.first)
    4124              :     return true;
    4125              : 
    4126              :   /* If neither are string csts, they can't be equal.  */
    4127         1486 :   if (TREE_CODE (candidate.first) != STRING_CST
    4128          491 :       || TREE_CODE (existing->name) != STRING_CST)
    4129              :     return false;
    4130              : 
    4131              :   /* String equality.  */
    4132          417 :   if (TREE_STRING_LENGTH (existing->name)
    4133          417 :       == TREE_STRING_LENGTH (candidate.first)
    4134          417 :       && !memcmp (TREE_STRING_POINTER (existing->name),
    4135          414 :                   TREE_STRING_POINTER (candidate.first),
    4136          414 :                   TREE_STRING_LENGTH (existing->name)))
    4137          131 :     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       391132 : this_module() {
    4200       391132 :   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        34093 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
    4243              : {
    4244        34093 :   auto *res = mapper;
    4245          320 :   if (!res)
    4246         5033 :     res = make_mapper (loc, deps);
    4247        34093 :   return res;
    4248              : }
    4249              : 
    4250              : /********************************************************************/
    4251              : static tree
    4252       507341 : get_clone_target (tree decl)
    4253              : {
    4254       507341 :   tree target;
    4255              : 
    4256       507341 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4257              :     {
    4258        67344 :       tree res_orig = DECL_CLONED_FUNCTION (DECL_TEMPLATE_RESULT (decl));
    4259              : 
    4260        67344 :       target = DECL_TI_TEMPLATE (res_orig);
    4261              :     }
    4262              :   else
    4263       439997 :     target = DECL_CLONED_FUNCTION (decl);
    4264              : 
    4265       507341 :   gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
    4266              : 
    4267       507341 :   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    146694331 : node_template_info (tree decl, int &use)
    4286              : {
    4287    146694331 :   tree ti = NULL_TREE;
    4288    146694331 :   int use_tpl = -1;
    4289    146694331 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    4290              :     {
    4291     26561933 :       tree type = TREE_TYPE (decl);
    4292              : 
    4293     26561933 :       ti = TYPE_TEMPLATE_INFO (type);
    4294     26561933 :       if (ti)
    4295              :         {
    4296      6368628 :           if (TYPE_LANG_SPECIFIC (type))
    4297      6356506 :             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        12122 :               tree ctx = CP_DECL_CONTEXT (decl);
    4306        12122 :               if (TYPE_P (ctx))
    4307        11883 :                 ctx = TYPE_NAME (ctx);
    4308        12122 :               node_template_info (ctx, use);
    4309        12122 :               use_tpl = use != 2 ? use : 0;
    4310              :             }
    4311              :         }
    4312              :     }
    4313    120132398 :   else if (DECL_LANG_SPECIFIC (decl)
    4314    120132398 :            && (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    102936631 :       use_tpl = DECL_USE_TEMPLATE (decl);
    4322    102936631 :       ti = DECL_TEMPLATE_INFO (decl);
    4323              :     }
    4324              : 
    4325    146694331 :   use = use_tpl;
    4326    146694331 :   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      2024351 : import_entity_index (tree decl, bool null_ok = false)
    4337              : {
    4338      2024351 :   if (unsigned *slot = entity_map->get (DECL_UID (decl)))
    4339      1973847 :     return *slot;
    4340              : 
    4341        50504 :   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       135699 : import_entity_module (unsigned index)
    4350              : {
    4351       135699 :   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       135639 :   unsigned pos = 1;
    4357       135639 :   unsigned len = modules->length () - pos;
    4358       302828 :   while (len)
    4359              :     {
    4360       167189 :       unsigned half = len / 2;
    4361       167189 :       module_state *probe = (*modules)[pos + half];
    4362       167189 :       if (index < probe->entity_lwm)
    4363              :         len = half;
    4364       136066 :       else if (index < probe->entity_lwm + probe->entity_num)
    4365              :         return probe;
    4366              :       else
    4367              :         {
    4368          427 :           pos += half + 1;
    4369          427 :           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    476457724 :   void indent ()
    4416              :   {
    4417          309 :     if (dumps)
    4418       733489 :       dumps->indent++;
    4419              :   }
    4420    476457724 :   void outdent ()
    4421              :   {
    4422    476457724 :     if (dumps)
    4423              :       {
    4424       733489 :         gcc_checking_assert (dumps->indent);
    4425       733489 :         dumps->indent--;
    4426              :       }
    4427    476457724 :   }
    4428              : 
    4429              : public:
    4430              :   /* Is dump enabled?.  */
    4431    283856131 :   bool operator () (int mask = 0)
    4432              :   {
    4433      5688279 :     if (!dumps || !dumps->stream)
    4434              :       return false;
    4435       513636 :     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       108730 : dumper::push (module_state *m)
    4450              : {
    4451       108730 :   FILE *stream = NULL;
    4452       108730 :   if (!dumps || !dumps->stack.length ())
    4453              :     {
    4454       107426 :       stream = dump_begin (module_dump_id, &flags);
    4455       107426 :       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       108687 : void dumper::pop (unsigned n)
    4494              : {
    4495       108687 :   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       537866 : dumper::impl::nested_name (tree t)
    4520              : {
    4521       537866 :   tree ti = NULL_TREE;
    4522       537866 :   int origin = -1;
    4523       537866 :   tree name = NULL_TREE;
    4524              : 
    4525       537866 :   if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
    4526            0 :     t = TU_LOCAL_ENTITY_NAME (t);
    4527              : 
    4528       537836 :   if (t && TREE_CODE (t) == TREE_BINFO)
    4529          384 :     t = BINFO_TYPE (t);
    4530              : 
    4531       537866 :   if (t && TYPE_P (t))
    4532       260899 :     t = TYPE_NAME (t);
    4533              : 
    4534       537824 :   if (t && DECL_P (t))
    4535              :     {
    4536       451525 :       if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
    4537              :         ;
    4538       419402 :       else if (tree ctx = DECL_CONTEXT (t))
    4539       329026 :         if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
    4540       329026 :             || nested_name (ctx))
    4541       329026 :           fputs ("::", stream);
    4542              : 
    4543       451525 :       int use_tpl;
    4544       451525 :       ti = node_template_info (t, use_tpl);
    4545       142087 :       if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    4546       593567 :           && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
    4547              :         t = TI_TEMPLATE (ti);
    4548       451525 :       tree not_tmpl = t;
    4549       451525 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    4550              :         {
    4551        23883 :           fputs ("template ", stream);
    4552        23883 :           not_tmpl = DECL_TEMPLATE_RESULT (t);
    4553              :         }
    4554              : 
    4555        23883 :       if (not_tmpl
    4556       451521 :           && DECL_P (not_tmpl)
    4557       451521 :           && DECL_LANG_SPECIFIC (not_tmpl)
    4558       271602 :           && 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       454752 :       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       451525 :   if (name)
    4581       501426 :     switch (TREE_CODE (name))
    4582              :       {
    4583        13685 :       default:
    4584        13685 :         fputs ("#unnamed#", stream);
    4585        13685 :         break;
    4586              : 
    4587       461999 :       case IDENTIFIER_NODE:
    4588       461999 :         fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
    4589       461999 :         break;
    4590              : 
    4591        25650 :       case INTEGER_CST:
    4592        25650 :         print_hex (wi::to_wide (name), stream);
    4593        25650 :         break;
    4594              : 
    4595           92 :       case STRING_CST:
    4596              :         /* If TREE_TYPE is NULL, this is a raw string.  */
    4597          184 :         fwrite (TREE_STRING_POINTER (name), 1,
    4598           92 :                 TREE_STRING_LENGTH (name) - (TREE_TYPE (name) != NULL_TREE),
    4599              :                 stream);
    4600           92 :         break;
    4601              :       }
    4602              :   else
    4603        36440 :     fputs ("#null#", stream);
    4604              : 
    4605       537866 :   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       537866 :   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       513489 :   else if (origin == -2)
    4623           45 :     fprintf (stream, "@???");
    4624              : 
    4625       537866 :   if (ti)
    4626              :     {
    4627       142087 :       tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
    4628       142087 :       fputs ("<", stream);
    4629       142087 :       if (args)
    4630       358748 :         for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
    4631              :           {
    4632       216661 :             if (ix)
    4633        74574 :               fputs (",", stream);
    4634       216661 :             nested_name (TREE_VEC_ELT (args, ix));
    4635              :           }
    4636       142087 :       fputs (">", stream);
    4637              :     }
    4638              : 
    4639       537866 :   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       450715 : dumper::operator () (const char *format, ...)
    4666              : {
    4667       450715 :   if (!(*this) ())
    4668              :     return false;
    4669              : 
    4670       385504 :   bool no_nl = format[0] == '+';
    4671       385504 :   format += no_nl;
    4672              : 
    4673       385504 :   if (dumps->bol)
    4674              :     {
    4675              :       /* Module import indent.  */
    4676       199156 :       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       199156 :       if (unsigned indent = dumps->indent)
    4686              :         {
    4687       106608 :           const char *prefix = "      ";
    4688       208200 :           fprintf (dumps->stream, (indent <= strlen (prefix)
    4689       101592 :                                    ? &prefix[strlen (prefix) - indent]
    4690              :                                    : "  .%d.  "), indent);
    4691              :         }
    4692       199156 :       dumps->bol = false;
    4693              :     }
    4694              : 
    4695       385504 :   va_list args;
    4696       385504 :   va_start (args, format);
    4697      1128325 :   while (const char *esc = strchr (format, '%'))
    4698              :     {
    4699       742821 :       fwrite (format, 1, (size_t)(esc - format), dumps->stream);
    4700       742821 :       format = ++esc;
    4701       742821 :       switch (*format++)
    4702              :         {
    4703            0 :         default:
    4704            0 :           gcc_unreachable ();
    4705              : 
    4706          586 :         case '%':
    4707          586 :           fputc ('%', dumps->stream);
    4708          586 :           break;
    4709              : 
    4710       113282 :         case 'C': /* Code */
    4711       113282 :           {
    4712       113282 :             tree_code code = (tree_code)va_arg (args, unsigned);
    4713       113282 :             fputs (get_tree_code_name (code), dumps->stream);
    4714              :           }
    4715       113282 :           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       126766 :         case 'N': /* Name.  */
    4746       126766 :           {
    4747       126766 :             tree t = va_arg (args, tree);
    4748       254021 :             while (t && TREE_CODE (t) == OVERLOAD)
    4749          489 :               t = OVL_FUNCTION (t);
    4750       126766 :             fputc ('\'', dumps->stream);
    4751       126766 :             dumps->nested_name (t);
    4752       126766 :             fputc ('\'', dumps->stream);
    4753              :           }
    4754       126766 :           break;
    4755              : 
    4756         7532 :         case 'P': /* Pair.  */
    4757         7532 :           {
    4758         7532 :             tree ctx = va_arg (args, tree);
    4759         7532 :             tree name = va_arg (args, tree);
    4760         7532 :             fputc ('\'', dumps->stream);
    4761         7532 :             dumps->nested_name (ctx);
    4762         7532 :             if (ctx && ctx != global_namespace)
    4763         1096 :               fputs ("::", dumps->stream);
    4764         7532 :             dumps->nested_name (name);
    4765         7532 :             fputc ('\'', dumps->stream);
    4766              :           }
    4767         7532 :           break;
    4768              : 
    4769          900 :         case 'R': /* Ratio */
    4770          900 :           {
    4771          900 :             unsigned a = va_arg (args, unsigned);
    4772          900 :             unsigned b = va_arg (args, unsigned);
    4773          900 :             fprintf (dumps->stream, "%.1f", (float) a / (b + !b));
    4774              :           }
    4775          900 :           break;
    4776              : 
    4777        34704 :         case 'S': /* Symbol name */
    4778        34704 :           {
    4779        34704 :             tree t = va_arg (args, tree);
    4780        34704 :             if (t && TYPE_P (t))
    4781        12631 :               t = TYPE_NAME (t);
    4782        32924 :             if (t && HAS_DECL_ASSEMBLER_NAME_P (t)
    4783        31750 :                 && DECL_ASSEMBLER_NAME_SET_P (t))
    4784              :               {
    4785          172 :                 fputc ('(', dumps->stream);
    4786          172 :                 fputs (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (t)),
    4787          172 :                        dumps->stream);
    4788          172 :                 fputc (')', dumps->stream);
    4789              :               }
    4790              :           }
    4791              :           break;
    4792              : 
    4793            0 :         case 'U': /* long unsigned.  */
    4794            0 :           {
    4795            0 :             unsigned long u = va_arg (args, unsigned long);
    4796            0 :             fprintf (dumps->stream, "%lu", u);
    4797              :           }
    4798            0 :           break;
    4799              : 
    4800          835 :         case 'V': /* Version.  */
    4801          835 :           {
    4802          835 :             unsigned v = va_arg (args, unsigned);
    4803          835 :             verstr_t string;
    4804              : 
    4805          835 :             version2string (v, string);
    4806          835 :             fputs (string, dumps->stream);
    4807              :           }
    4808          835 :           break;
    4809              : 
    4810            0 :         case 'c': /* Character.  */
    4811            0 :           {
    4812            0 :             int c = va_arg (args, int);
    4813            0 :             fputc (c, dumps->stream);
    4814              :           }
    4815            0 :           break;
    4816              : 
    4817        63208 :         case 'd': /* Decimal Int.  */
    4818        63208 :           {
    4819        63208 :             int d = va_arg (args, int);
    4820        63208 :             fprintf (dumps->stream, "%d", d);
    4821              :           }
    4822        63208 :           break;
    4823              : 
    4824            0 :         case 'p': /* Pointer. */
    4825            0 :           {
    4826            0 :             void *p = va_arg (args, void *);
    4827            0 :             fprintf (dumps->stream, "%p", p);
    4828              :           }
    4829            0 :           break;
    4830              : 
    4831       128856 :         case 's': /* String. */
    4832       128856 :           {
    4833       128856 :             const char *s = va_arg (args, char *);
    4834       128856 :             gcc_checking_assert (s);
    4835       128856 :             fputs (s, dumps->stream);
    4836              :           }
    4837       128856 :           break;
    4838              : 
    4839       250750 :         case 'u': /* Unsigned.  */
    4840       250750 :           {
    4841       250750 :             unsigned u = va_arg (args, unsigned);
    4842       250750 :             fprintf (dumps->stream, "%u", u);
    4843              :           }
    4844       250750 :           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       385504 :   fputs (format, dumps->stream);
    4855       385504 :   va_end (args);
    4856       385504 :   if (!no_nl)
    4857              :     {
    4858       199156 :       dumps->bol = true;
    4859       199156 :       fputc ('\n', dumps->stream);
    4860              :     }
    4861              :   return true;
    4862              : }
    4863              : 
    4864              : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
    4865              : {
    4866       259919 :   static int keep_cache_entry (tree t)
    4867              :   {
    4868       259919 :     if (!CHECKING_P)
    4869              :       /* GTY is unfortunately not clever enough to conditionalize
    4870              :          this.  */
    4871              :       gcc_unreachable ();
    4872              : 
    4873       259919 :     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       346301 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
    4895              :                              bool installing ATTRIBUTE_UNUSED)
    4896              : {
    4897              : #if CHECKING_P
    4898       346301 :   tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
    4899       346301 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    4900       346301 :   if (installing)
    4901              :     {
    4902              :       /* We must be inserting for the first time.  */
    4903       213567 :       gcc_assert (!*slot);
    4904       213567 :       *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       132734 :     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       346301 :   if (not_tmpl != decl)
    4921       202713 :     gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
    4922              : #endif
    4923       346301 : }
    4924              : 
    4925              : void
    4926       518191 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
    4927              : {
    4928              : #if CHECKING_P
    4929       518191 :   tree *slot = note_defs->find_slot (decl, INSERT);
    4930       518191 :   gcc_assert (!*slot);
    4931       518191 :   *slot = decl;
    4932       518191 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4933       294031 :     gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
    4934              : #endif
    4935       518191 : }
    4936              : 
    4937              : /********************************************************************/
    4938              : static bool
    4939        12927 : 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       104812 : set_cmi_repo (const char *r)
    4955              : {
    4956       104812 :   XDELETEVEC (cmi_repo);
    4957       104812 :   XDELETEVEC (cmi_path);
    4958       104812 :   cmi_path_alloc = 0;
    4959              : 
    4960       104812 :   cmi_repo = NULL;
    4961       104812 :   cmi_repo_length = 0;
    4962              : 
    4963       104812 :   if (!r || !r[0])
    4964              :     return;
    4965              : 
    4966         5030 :   size_t len = strlen (r);
    4967         5030 :   cmi_repo = XNEWVEC (char, len + 1);
    4968         5030 :   memcpy (cmi_repo, r, len + 1);
    4969              : 
    4970         5030 :   if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
    4971         5030 :     len--;
    4972         5030 :   if (len == 1 && cmi_repo[0] == '.')
    4973           27 :     len--;
    4974         5030 :   cmi_repo[len] = 0;
    4975         5030 :   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         6135 : maybe_add_cmi_prefix (const char *to, size_t *len_p = NULL)
    4983              : {
    4984         6135 :   size_t len = len_p || cmi_repo_length ? strlen (to) : 0;
    4985              : 
    4986         6135 :   if (cmi_repo_length && !IS_ABSOLUTE_PATH (to))
    4987              :     {
    4988         6108 :       if (cmi_path_alloc < cmi_repo_length + len + 2)
    4989              :         {
    4990         4906 :           XDELETEVEC (cmi_path);
    4991         4906 :           cmi_path_alloc = cmi_repo_length + len * 2 + 2;
    4992         4906 :           cmi_path = XNEWVEC (char, cmi_path_alloc);
    4993              : 
    4994         4906 :           memcpy (cmi_path, cmi_repo, cmi_repo_length);
    4995         4906 :           cmi_path[cmi_repo_length] = DIR_SEPARATOR;
    4996              :         }
    4997              : 
    4998         6108 :       memcpy (&cmi_path[cmi_repo_length + 1], to, len + 1);
    4999         6108 :       len += cmi_repo_length + 1;
    5000         6108 :       to = cmi_path;
    5001              :     }
    5002              : 
    5003         6135 :   if (len_p)
    5004         2965 :     *len_p = len;
    5005              : 
    5006         6135 :   return to;
    5007              : }
    5008              : 
    5009              : /* Try and create the directories of PATH.  */
    5010              : 
    5011              : static void
    5012           42 : create_dirs (char *path)
    5013              : {
    5014           42 :   char *base = path;
    5015              :   /* Skip past initial slashes of absolute path.  */
    5016           42 :   while (IS_DIR_SEPARATOR (*base))
    5017            0 :     base++;
    5018              : 
    5019              :   /* Try and create the missing directories.  */
    5020         2531 :   for (; *base; base++)
    5021         2489 :     if (IS_DIR_SEPARATOR (*base))
    5022              :       {
    5023          241 :         char sep = *base;
    5024          241 :         *base = 0;
    5025          241 :         int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
    5026          244 :         dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
    5027          241 :         *base = sep;
    5028          241 :         if (failed
    5029              :             /* Maybe racing with another creator (of a *different*
    5030              :                module).  */
    5031           46 :             && errno != EEXIST)
    5032              :           break;
    5033              :       }
    5034           42 : }
    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       115975 : friend_from_decl_list (tree frnd)
    5041              : {
    5042       115975 :   tree res = frnd;
    5043              : 
    5044       115975 :   if (TREE_CODE (frnd) != TEMPLATE_DECL)
    5045              :     {
    5046        73194 :       tree tmpl = NULL_TREE;
    5047        73194 :       if (TYPE_P (frnd))
    5048              :         {
    5049        11949 :           res = TYPE_NAME (frnd);
    5050        11760 :           if (CLASS_TYPE_P (frnd)
    5051        23709 :               && CLASSTYPE_TEMPLATE_INFO (frnd))
    5052        11751 :             tmpl = CLASSTYPE_TI_TEMPLATE (frnd);
    5053              :         }
    5054        61245 :       else if (DECL_TEMPLATE_INFO (frnd))
    5055              :         {
    5056        61245 :           tmpl = DECL_TI_TEMPLATE (frnd);
    5057        61245 :           if (TREE_CODE (tmpl) != TEMPLATE_DECL)
    5058              :             tmpl = NULL_TREE;
    5059              :         }
    5060              : 
    5061        83753 :       if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
    5062              :         res = tmpl;
    5063              :     }
    5064              : 
    5065       115975 :   return res;
    5066              : }
    5067              : 
    5068              : static tree
    5069        29336 : find_enum_member (tree ctx, tree name)
    5070              : {
    5071        29336 :   for (tree values = TYPE_VALUES (ctx);
    5072       483512 :        values; values = TREE_CHAIN (values))
    5073       475193 :     if (DECL_NAME (TREE_VALUE (values)) == name)
    5074              :       return TREE_VALUE (values);
    5075              : 
    5076              :   return NULL_TREE;
    5077              : }
    5078              : 
    5079              : /********************************************************************/
    5080              : /* Instrumentation gathered writing bytes.  */
    5081              : 
    5082              : void
    5083          300 : bytes_out::instrument ()
    5084              : {
    5085          300 :   dump ("Wrote %u bytes in %u blocks", lengths[3], spans[3]);
    5086          300 :   dump ("Wrote %u bits in %u bytes", lengths[0] + lengths[1], lengths[2]);
    5087         1200 :   for (unsigned ix = 0; ix < 2; ix++)
    5088          900 :     dump ("  %u %s spans of %R bits", spans[ix],
    5089              :           ix ? "one" : "zero", lengths[ix], spans[ix]);
    5090          300 :   dump ("  %u blocks with %R bits padding", spans[2],
    5091          300 :         lengths[2] * 8 - (lengths[0] + lengths[1]), spans[2]);
    5092          300 : }
    5093              : 
    5094              : /* Instrumentation gathered writing trees.  */
    5095              : void
    5096         2815 : trees_out::instrument ()
    5097              : {
    5098         2815 :   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         2815 : }
    5109              : 
    5110              : /* Setup and teardown for a tree walk.  */
    5111              : 
    5112              : void
    5113      3051495 : trees_out::begin ()
    5114              : {
    5115      3051495 :   gcc_assert (!streaming_p () || !tree_map.elements ());
    5116              : 
    5117      3051495 :   mark_trees ();
    5118      3051495 :   if (streaming_p ())
    5119       340768 :     parent::begin ();
    5120      3051495 : }
    5121              : 
    5122              : unsigned
    5123       340768 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    5124              : {
    5125       340768 :   gcc_checking_assert (streaming_p ());
    5126              : 
    5127       340768 :   unmark_trees ();
    5128       340768 :   return parent::end (sink, name, crc_ptr);
    5129              : }
    5130              : 
    5131              : void
    5132      2710727 : trees_out::end ()
    5133              : {
    5134      2710727 :   gcc_assert (!streaming_p ());
    5135              : 
    5136      2710727 :   unmark_trees ();
    5137              :   /* Do not parent::end -- we weren't streaming.  */
    5138      2710727 : }
    5139              : 
    5140              : void
    5141      3051495 : trees_out::mark_trees ()
    5142              : {
    5143      3051495 :   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      2384357 :       tree_map.~ptr_int_hash_map ();
    5149      2384357 :       new (&tree_map) ptr_int_hash_map (size);
    5150              :     }
    5151              : 
    5152              :   /* Install the fixed trees, with +ve references.  */
    5153      3051495 :   unsigned limit = fixed_trees->length ();
    5154    588030883 :   for (unsigned ix = 0; ix != limit; ix++)
    5155              :     {
    5156    584979388 :       tree val = (*fixed_trees)[ix];
    5157    584979388 :       bool existed = tree_map.put (val, ix + tag_fixed);
    5158    584979388 :       gcc_checking_assert (!TREE_VISITED (val) && !existed);
    5159    584979388 :       TREE_VISITED (val) = true;
    5160              :     }
    5161              : 
    5162      3051495 :   ref_num = 0;
    5163      3051495 : }
    5164              : 
    5165              : /* Unmark the trees we encountered  */
    5166              : 
    5167              : void
    5168      3051495 : trees_out::unmark_trees ()
    5169              : {
    5170      3051495 :   ptr_int_hash_map::iterator end (tree_map.end ());
    5171    701167940 :   for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
    5172              :     {
    5173    698116445 :       tree node = reinterpret_cast<tree> ((*iter).first);
    5174    698116445 :       int ref = (*iter).second;
    5175              :       /* We should have visited the node, and converted its mergeable
    5176              :          reference to a regular reference.  */
    5177    698116445 :       gcc_checking_assert (TREE_VISITED (node)
    5178              :                            && (ref <= tag_backref || ref >= tag_fixed));
    5179    698116445 :       TREE_VISITED (node) = false;
    5180              :     }
    5181      3051495 : }
    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      4741955 : trees_out::mark_by_value (tree decl)
    5189              : {
    5190      4741955 :   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      4741955 :   if (TREE_VISITED (decl))
    5196              :     /* Must already be forced or fixed.  */
    5197         4202 :     gcc_checking_assert (*tree_map.get (decl) >= tag_value);
    5198              :   else
    5199              :     {
    5200      4737753 :       bool existed = tree_map.put (decl, tag_value);
    5201      4737753 :       gcc_checking_assert (!existed);
    5202      4737753 :       TREE_VISITED (decl) = true;
    5203              :     }
    5204      4741955 : }
    5205              : 
    5206              : int
    5207    134929973 : trees_out::get_tag (tree t)
    5208              : {
    5209    134929973 :   gcc_checking_assert (TREE_VISITED (t));
    5210    134929973 :   return *tree_map.get (t);
    5211              : }
    5212              : 
    5213              : /* Insert T into the map, return its tag number.    */
    5214              : 
    5215              : int
    5216    113137057 : trees_out::insert (tree t, walk_kind walk)
    5217              : {
    5218    113137057 :   gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
    5219    113137057 :   int tag = --ref_num;
    5220    113137057 :   bool existed;
    5221    113137057 :   int &slot = tree_map.get_or_insert (t, &existed);
    5222    113137057 :   gcc_checking_assert (TREE_VISITED (t) == existed
    5223              :                        && (!existed
    5224              :                            || (walk == WK_value && slot == tag_value)));
    5225    113137057 :   TREE_VISITED (t) = true;
    5226    113137057 :   slot = tag;
    5227              : 
    5228    113137057 :   return tag;
    5229              : }
    5230              : 
    5231              : /* Insert T into the backreference array.  Return its back reference
    5232              :    number.  */
    5233              : 
    5234              : int
    5235     21708211 : trees_in::insert (tree t)
    5236              : {
    5237     21708211 :   gcc_checking_assert (t || get_overrun ());
    5238     21708211 :   back_refs.safe_push (t);
    5239     21708211 :   return -(int)back_refs.length ();
    5240              : }
    5241              : 
    5242              : /* A chained set of decls.  */
    5243              : 
    5244              : void
    5245       176838 : trees_out::chained_decls (tree decls)
    5246              : {
    5247       409664 :   for (; decls; decls = DECL_CHAIN (decls))
    5248       232826 :     tree_node (decls);
    5249       176838 :   tree_node (NULL_TREE);
    5250       176838 : }
    5251              : 
    5252              : tree
    5253        60760 : trees_in::chained_decls ()
    5254              : {
    5255        60760 :   tree decls = NULL_TREE;
    5256        60760 :   for (tree *chain = &decls;;)
    5257       145664 :     if (tree decl = tree_node ())
    5258              :       {
    5259        84904 :         if (!DECL_P (decl) || DECL_CHAIN (decl))
    5260              :           {
    5261            0 :             set_overrun ();
    5262            0 :             break;
    5263              :           }
    5264        84904 :         *chain = decl;
    5265        84904 :         chain = &DECL_CHAIN (decl);
    5266              :       }
    5267              :     else
    5268        84904 :       break;
    5269              : 
    5270        60760 :   return decls;
    5271              : }
    5272              : 
    5273              : /* A vector of decls following DECL_CHAIN.  */
    5274              : 
    5275              : void
    5276       438638 : trees_out::vec_chained_decls (tree decls)
    5277              : {
    5278       438638 :   if (streaming_p ())
    5279              :     {
    5280              :       unsigned len = 0;
    5281              : 
    5282      1406342 :       for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5283      1187069 :         len++;
    5284       219273 :       u (len);
    5285              :     }
    5286              : 
    5287      2813186 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5288              :     {
    5289       473353 :       if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5290      2392473 :           && 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      2374540 :         tree_node (decl);
    5296              :     }
    5297       438638 : }
    5298              : 
    5299              : vec<tree, va_heap> *
    5300       138580 : trees_in::vec_chained_decls ()
    5301              : {
    5302       138580 :   vec<tree, va_heap> *v = NULL;
    5303              : 
    5304       138580 :   if (unsigned len = u ())
    5305              :     {
    5306        72635 :       vec_alloc (v, len);
    5307              : 
    5308       933603 :       for (unsigned ix = 0; ix < len; ix++)
    5309              :         {
    5310       788333 :           tree decl = tree_node ();
    5311       788333 :           if (decl && !DECL_P (decl))
    5312              :             {
    5313            0 :               set_overrun ();
    5314            0 :               break;
    5315              :             }
    5316       788333 :           v->quick_push (decl);
    5317              :         }
    5318              : 
    5319        72635 :       if (get_overrun ())
    5320              :         {
    5321            0 :           vec_free (v);
    5322            0 :           v = NULL;
    5323              :         }
    5324              :     }
    5325              : 
    5326       138580 :   return v;
    5327              : }
    5328              : 
    5329              : /* A vector of trees.  */
    5330              : 
    5331              : void
    5332       308000 : trees_out::tree_vec (vec<tree, va_gc> *v)
    5333              : {
    5334       308000 :   unsigned len = vec_safe_length (v);
    5335       308000 :   if (streaming_p ())
    5336       153977 :     u (len);
    5337       394388 :   for (unsigned ix = 0; ix != len; ix++)
    5338        86388 :     tree_node ((*v)[ix]);
    5339       308000 : }
    5340              : 
    5341              : vec<tree, va_gc> *
    5342        97074 : trees_in::tree_vec ()
    5343              : {
    5344        97074 :   vec<tree, va_gc> *v = NULL;
    5345        97074 :   if (unsigned len = u ())
    5346              :     {
    5347        24584 :       vec_alloc (v, len);
    5348        76336 :       for (unsigned ix = 0; ix != len; ix++)
    5349        27168 :         v->quick_push (tree_node ());
    5350              :     }
    5351        97074 :   return v;
    5352              : }
    5353              : 
    5354              : /* A vector of tree pairs.  */
    5355              : 
    5356              : void
    5357         7836 : trees_out::tree_pair_vec (vec<tree_pair_s, va_gc> *v)
    5358              : {
    5359         7836 :   unsigned len = vec_safe_length (v);
    5360         7836 :   if (streaming_p ())
    5361         3918 :     u (len);
    5362         7836 :   if (len)
    5363        42614 :     for (unsigned ix = 0; ix != len; ix++)
    5364              :       {
    5365        34916 :         tree_pair_s const &s = (*v)[ix];
    5366        34916 :         tree_node (s.purpose);
    5367        34916 :         tree_node (s.value);
    5368              :       }
    5369         7836 : }
    5370              : 
    5371              : vec<tree_pair_s, va_gc> *
    5372         2699 : trees_in::tree_pair_vec ()
    5373              : {
    5374         2699 :   vec<tree_pair_s, va_gc> *v = NULL;
    5375         2699 :   if (unsigned len = u ())
    5376              :     {
    5377         2647 :       vec_alloc (v, len);
    5378        17362 :       for (unsigned ix = 0; ix != len; ix++)
    5379              :         {
    5380        12068 :           tree_pair_s s;
    5381        12068 :           s.purpose = tree_node ();
    5382        12068 :           s.value = tree_node ();
    5383        12068 :           v->quick_push (s);
    5384              :       }
    5385              :     }
    5386         2699 :   return v;
    5387              : }
    5388              : 
    5389              : void
    5390       467634 : trees_out::tree_list (tree list, bool has_purpose)
    5391              : {
    5392      2122651 :   for (; list; list = TREE_CHAIN (list))
    5393              :     {
    5394      1655017 :       gcc_checking_assert (TREE_VALUE (list));
    5395      1655017 :       tree_node (TREE_VALUE (list));
    5396      1655017 :       if (has_purpose)
    5397      1598319 :         tree_node (TREE_PURPOSE (list));
    5398              :     }
    5399       467634 :   tree_node (NULL_TREE);
    5400       467634 : }
    5401              : 
    5402              : tree
    5403       149193 : trees_in::tree_list (bool has_purpose)
    5404              : {
    5405       149193 :   tree res = NULL_TREE;
    5406              : 
    5407       701952 :   for (tree *chain = &res; tree value = tree_node ();
    5408      1105518 :        chain = &TREE_CHAIN (*chain))
    5409              :     {
    5410       552759 :       tree purpose = has_purpose ? tree_node () : NULL_TREE;
    5411       552759 :       *chain = build_tree_list (purpose, value);
    5412       552759 :     }
    5413              : 
    5414       149193 :   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     22813719 : trees_out::start (tree t, bool code_streamed)
    5468              : {
    5469     22813719 :   if (TYPE_P (t))
    5470              :     {
    5471       861295 :       enum tree_code code = TREE_CODE (t);
    5472       861295 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5473              :       /* All these types are TYPE_NON_COMMON.  */
    5474       861295 :       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     22813719 :   if (!code_streamed)
    5483     22057572 :     u (TREE_CODE (t));
    5484              : 
    5485     22813719 :   switch (TREE_CODE (t))
    5486              :     {
    5487     20438494 :     default:
    5488     20438494 :       if (VL_EXP_CLASS_P (t))
    5489       881337 :         u (VL_EXP_OPERAND_LENGTH (t));
    5490              :       break;
    5491              : 
    5492       904860 :     case INTEGER_CST:
    5493       904860 :       u (TREE_INT_CST_NUNITS (t));
    5494       904860 :       u (TREE_INT_CST_EXT_NUNITS (t));
    5495       904860 :       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        67794 :     case STRING_CST:
    5514        67794 :       str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
    5515        67794 :       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       150059 :     case TREE_BINFO:
    5547       150059 :       u (BINFO_N_BASE_BINFOS (t));
    5548       150059 :       break;
    5549              : 
    5550      1252437 :     case TREE_VEC:
    5551      1252437 :       u (TREE_VEC_LENGTH (t));
    5552      1252437 :       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     22813719 :       break;
    5565              :     }
    5566     22813719 : }
    5567              : 
    5568              : /* Start tree read.  Allocate the receiving node.  */
    5569              : 
    5570              : tree
    5571     15549518 : trees_in::start (unsigned code)
    5572              : {
    5573     15549518 :   tree t = NULL_TREE;
    5574              : 
    5575     15549518 :   if (!code)
    5576     14102793 :     code = u ();
    5577              : 
    5578     15549518 :   switch (code)
    5579              :     {
    5580     13950515 :     default:
    5581     13950515 :       if (code >= MAX_TREE_CODES)
    5582              :         {
    5583            0 :         fail:
    5584            0 :           set_overrun ();
    5585            0 :           return NULL_TREE;
    5586              :         }
    5587     13950515 :       else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
    5588              :         {
    5589       605416 :           unsigned ops = u ();
    5590       605416 :           t = build_vl_exp (tree_code (code), ops);
    5591              :         }
    5592              :       else
    5593     13345099 :         t = make_node (tree_code (code));
    5594              :       break;
    5595              : 
    5596       606309 :     case INTEGER_CST:
    5597       606309 :       {
    5598       606309 :         unsigned n = u ();
    5599       606309 :         unsigned e = u ();
    5600       606309 :         t = make_int_cst (n, e);
    5601              :       }
    5602       606309 :       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        50217 :     case STRING_CST:
    5627        50217 :       {
    5628        50217 :         size_t l;
    5629        50217 :         const char *chars = str (&l);
    5630        50217 :         t = build_string (l, chars);
    5631              :       }
    5632        50217 :       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        94375 :     case TREE_BINFO:
    5661        94375 :       t = make_tree_binfo (u ());
    5662        94375 :       break;
    5663              : 
    5664       848027 :     case TREE_VEC:
    5665       848027 :       t = make_tree_vec (u ());
    5666       848027 :       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       863625 : get_importer_interface (tree decl)
    5693              : {
    5694              :   /* Internal linkage entities must be emitted in each importer if
    5695              :      there is a definition available.  */
    5696       863625 :   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       400503 :   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       393699 :   if (DECL_LANG_SPECIFIC (decl)
    5710       393699 :       && DECL_EXPLICIT_INSTANTIATION (decl))
    5711        29084 :     return (header_module_p () && !DECL_EXTERNAL (decl)
    5712        29084 :             ? importer_interface::always_emit
    5713              :             : importer_interface::external);
    5714              : 
    5715              :   /* A gnu_inline function is never emitted in any TU.  */
    5716       364615 :   if (TREE_CODE (decl) == FUNCTION_DECL
    5717       270471 :       && DECL_DECLARED_INLINE_P (decl)
    5718       626286 :       && 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     22852798 : 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     22852798 :   tree_code code = TREE_CODE (t);
    5741              : 
    5742     22852798 :   WB (t->base.side_effects_flag);
    5743     22852798 :   WB (t->base.constant_flag);
    5744     22852798 :   WB (t->base.addressable_flag);
    5745     22852798 :   WB (t->base.volatile_flag);
    5746     22852798 :   WB (t->base.readonly_flag);
    5747              :   /* base.asm_written_flag is a property of the current TU's use of
    5748              :      this decl.  */
    5749     22852798 :   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     22852798 :   WB (t->base.nothrow_flag);
    5755     22852798 :   WB (t->base.static_flag);
    5756              :   /* This is TYPE_CACHED_VALUES_P for types.  */
    5757     22852798 :   WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5758     22852798 :   WB (t->base.private_flag);
    5759     22852798 :   WB (t->base.protected_flag);
    5760     22852798 :   WB (t->base.deprecated_flag);
    5761     22852798 :   WB (t->base.default_def_flag);
    5762              : 
    5763     22852798 :   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     19834914 :     default:
    5774     19834914 :       WB (t->base.u.bits.lang_flag_0);
    5775     19834914 :       bool flag_1 = t->base.u.bits.lang_flag_1;
    5776     19834914 :       if (!flag_1)
    5777              :         ;
    5778       650048 :       else if (code == TEMPLATE_INFO)
    5779              :         /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader.  */
    5780              :         flag_1 = false;
    5781       642916 :       else if (code == VAR_DECL)
    5782              :         {
    5783              :           /* This is DECL_INITIALIZED_P.  */
    5784       116570 :           if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    5785              :             /* We'll set this when reading the definition.  */
    5786     19834914 :             flag_1 = false;
    5787              :         }
    5788     19834914 :       WB (flag_1);
    5789     19834914 :       WB (t->base.u.bits.lang_flag_2);
    5790     19834914 :       WB (t->base.u.bits.lang_flag_3);
    5791     19834914 :       WB (t->base.u.bits.lang_flag_4);
    5792     19834914 :       WB (t->base.u.bits.lang_flag_5);
    5793     19834914 :       WB (t->base.u.bits.lang_flag_6);
    5794     19834914 :       WB (t->base.u.bits.saturating_flag);
    5795     19834914 :       WB (t->base.u.bits.unsigned_flag);
    5796     19834914 :       WB (t->base.u.bits.packed_flag);
    5797     19834914 :       WB (t->base.u.bits.user_align);
    5798     19834914 :       WB (t->base.u.bits.nameless_flag);
    5799     19834914 :       WB (t->base.u.bits.atomic_flag);
    5800     19834914 :       WB (t->base.u.bits.unavailable_flag);
    5801     19834914 :       break;
    5802              :     }
    5803              : 
    5804     19834914 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5805              :     {
    5806       900374 :       WB (t->type_common.no_force_blk_flag);
    5807       900374 :       WB (t->type_common.needs_constructing_flag);
    5808       900374 :       WB (t->type_common.transparent_aggr_flag);
    5809       900374 :       WB (t->type_common.restrict_flag);
    5810       900374 :       WB (t->type_common.string_flag);
    5811       900374 :       WB (t->type_common.lang_flag_0);
    5812       900374 :       WB (t->type_common.lang_flag_1);
    5813       900374 :       WB (t->type_common.lang_flag_2);
    5814       900374 :       WB (t->type_common.lang_flag_3);
    5815       900374 :       WB (t->type_common.lang_flag_4);
    5816       900374 :       WB (t->type_common.lang_flag_5);
    5817       900374 :       WB (t->type_common.lang_flag_6);
    5818       900374 :       WB (t->type_common.typeless_storage);
    5819              :     }
    5820              : 
    5821     19834914 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5822              :     return;
    5823              : 
    5824      4914373 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5825              :     {
    5826      4914373 :       WB (t->decl_common.nonlocal_flag);
    5827      4914373 :       WB (t->decl_common.virtual_flag);
    5828      4914373 :       WB (t->decl_common.ignored_flag);
    5829      4914373 :       WB (t->decl_common.abstract_flag);
    5830      4914373 :       WB (t->decl_common.artificial_flag);
    5831      4914373 :       WB (t->decl_common.preserve_flag);
    5832      4914373 :       WB (t->decl_common.debug_expr_is_from);
    5833      4914373 :       WB (t->decl_common.lang_flag_0);
    5834      4914373 :       WB (t->decl_common.lang_flag_1);
    5835      4914373 :       WB (t->decl_common.lang_flag_2);
    5836      4914373 :       WB (t->decl_common.lang_flag_3);
    5837      4914373 :       WB (t->decl_common.lang_flag_4);
    5838              : 
    5839      4914373 :       {
    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      4914373 :         bool interface_known = t->decl_common.lang_flag_5;
    5844      4914373 :         if (interface_known
    5845      4914373 :             && get_importer_interface (t) == importer_interface::unknown)
    5846              :           interface_known = false;
    5847      4914373 :         WB (interface_known);
    5848              :       }
    5849              : 
    5850      4914373 :       WB (t->decl_common.lang_flag_6);
    5851      4914373 :       WB (t->decl_common.lang_flag_7);
    5852      4914373 :       WB (t->decl_common.lang_flag_8);
    5853      4914373 :       WB (t->decl_common.decl_flag_0);
    5854              : 
    5855      4914373 :       {
    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      4914373 :         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      4914373 :         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      4913893 :         if (!is_external
    5869      4130690 :             && VAR_OR_FUNCTION_DECL_P (t)
    5870      5313754 :             && get_importer_interface (t) == importer_interface::external)
    5871              :           is_external = true;
    5872      4914373 :         WB (is_external);
    5873              :       }
    5874              : 
    5875      4914373 :       WB (t->decl_common.decl_flag_2);
    5876      4914373 :       WB (t->decl_common.decl_flag_3);
    5877      4914373 :       WB (t->decl_common.not_gimple_reg_flag);
    5878      4914373 :       WB (t->decl_common.decl_by_reference_flag);
    5879      4914373 :       WB (t->decl_common.decl_read_flag);
    5880      4914373 :       WB (t->decl_common.decl_nonshareable_flag);
    5881      4914373 :       WB (t->decl_common.decl_not_flexarray);
    5882              :     }
    5883              :   else
    5884              :     return;
    5885              : 
    5886      4914373 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5887              :     {
    5888      2313687 :       WB (t->decl_with_vis.defer_output);
    5889      2313687 :       WB (t->decl_with_vis.hard_register);
    5890      2313687 :       WB (t->decl_with_vis.common_flag);
    5891      2313687 :       WB (t->decl_with_vis.in_text_section);
    5892      2313687 :       WB (t->decl_with_vis.in_constant_pool);
    5893      2313687 :       WB (t->decl_with_vis.dllimport_flag);
    5894      2313687 :       WB (t->decl_with_vis.weak_flag);
    5895      2313687 :       WB (t->decl_with_vis.seen_in_bind_expr);
    5896      2313687 :       WB (t->decl_with_vis.comdat_flag);
    5897      2313687 :       WB (t->decl_with_vis.visibility_specified);
    5898      2313687 :       WB (t->decl_with_vis.init_priority_p);
    5899      2313687 :       WB (t->decl_with_vis.shadowed_for_var_p);
    5900      2313687 :       WB (t->decl_with_vis.cxx_constructor);
    5901      2313687 :       WB (t->decl_with_vis.cxx_destructor);
    5902      2313687 :       WB (t->decl_with_vis.final);
    5903      2313687 :       WB (t->decl_with_vis.regdecl_flag);
    5904              :     }
    5905              :   else
    5906              :     return;
    5907              : 
    5908      2313687 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5909              :     {
    5910       680083 :       WB (t->function_decl.static_ctor_flag);
    5911       680083 :       WB (t->function_decl.static_dtor_flag);
    5912       680083 :       WB (t->function_decl.uninlinable);
    5913       680083 :       WB (t->function_decl.possibly_inlined);
    5914       680083 :       WB (t->function_decl.novops_flag);
    5915       680083 :       WB (t->function_decl.returns_twice_flag);
    5916       680083 :       WB (t->function_decl.malloc_flag);
    5917       680083 :       WB (t->function_decl.declared_inline_flag);
    5918       680083 :       WB (t->function_decl.no_inline_warning_flag);
    5919       680083 :       WB (t->function_decl.no_instrument_function_entry_exit);
    5920       680083 :       WB (t->function_decl.no_limit_stack);
    5921       680083 :       WB (t->function_decl.disregard_inline_limits);
    5922       680083 :       WB (t->function_decl.pure_flag);
    5923       680083 :       WB (t->function_decl.looping_const_or_pure_flag);
    5924              : 
    5925       680083 :       WB (t->function_decl.has_debug_args_flag);
    5926       680083 :       WB (t->function_decl.versioned_function);
    5927       680083 :       WB (t->function_decl.replaceable_operator);
    5928              : 
    5929              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5930       680083 :       unsigned kind = (unsigned)t->function_decl.decl_type;
    5931       680083 :       WB ((kind >> 0) & 1);
    5932       680083 :       WB ((kind >> 1) & 1);
    5933              :     }
    5934              : #undef WB_IF
    5935              : #undef WB
    5936              : }
    5937              : 
    5938              : bool
    5939     15573287 : 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     15573287 :   tree_code code = TREE_CODE (t);
    5946              : 
    5947     15573287 :   RB (t->base.side_effects_flag);
    5948     15573287 :   RB (t->base.constant_flag);
    5949     15573287 :   RB (t->base.addressable_flag);
    5950     15573287 :   RB (t->base.volatile_flag);
    5951     15573287 :   RB (t->base.readonly_flag);
    5952              :   /* base.asm_written_flag is not streamed.  */
    5953     15573287 :   RB (t->base.nowarning_flag);
    5954              :   /* base.visited is not streamed.  */
    5955              :   /* base.used_flag is not streamed.  */
    5956     15573287 :   RB (t->base.nothrow_flag);
    5957     15573287 :   RB (t->base.static_flag);
    5958     15573287 :   RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5959     15573287 :   RB (t->base.private_flag);
    5960     15573287 :   RB (t->base.protected_flag);
    5961     15573287 :   RB (t->base.deprecated_flag);
    5962     15573287 :   RB (t->base.default_def_flag);
    5963              : 
    5964     15573287 :   switch (code)
    5965              :     {
    5966      2045929 :     case CALL_EXPR:
    5967      2045929 :     case INTEGER_CST:
    5968      2045929 :     case SSA_NAME:
    5969      2045929 :     case TARGET_MEM_REF:
    5970      2045929 :     case TREE_VEC:
    5971              :       /* These use different base.u fields.  */
    5972      2045929 :       goto done;
    5973              : 
    5974     13527358 :     default:
    5975     13527358 :       RB (t->base.u.bits.lang_flag_0);
    5976     13527358 :       RB (t->base.u.bits.lang_flag_1);
    5977     13527358 :       RB (t->base.u.bits.lang_flag_2);
    5978     13527358 :       RB (t->base.u.bits.lang_flag_3);
    5979     13527358 :       RB (t->base.u.bits.lang_flag_4);
    5980     13527358 :       RB (t->base.u.bits.lang_flag_5);
    5981     13527358 :       RB (t->base.u.bits.lang_flag_6);
    5982     13527358 :       RB (t->base.u.bits.saturating_flag);
    5983     13527358 :       RB (t->base.u.bits.unsigned_flag);
    5984     13527358 :       RB (t->base.u.bits.packed_flag);
    5985     13527358 :       RB (t->base.u.bits.user_align);
    5986     13527358 :       RB (t->base.u.bits.nameless_flag);
    5987     13527358 :       RB (t->base.u.bits.atomic_flag);
    5988     13527358 :       RB (t->base.u.bits.unavailable_flag);
    5989     13527358 :       break;
    5990              :     }
    5991              : 
    5992     13527358 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5993              :     {
    5994       590996 :       RB (t->type_common.no_force_blk_flag);
    5995       590996 :       RB (t->type_common.needs_constructing_flag);
    5996       590996 :       RB (t->type_common.transparent_aggr_flag);
    5997       590996 :       RB (t->type_common.restrict_flag);
    5998       590996 :       RB (t->type_common.string_flag);
    5999       590996 :       RB (t->type_common.lang_flag_0);
    6000       590996 :       RB (t->type_common.lang_flag_1);
    6001       590996 :       RB (t->type_common.lang_flag_2);
    6002       590996 :       RB (t->type_common.lang_flag_3);
    6003       590996 :       RB (t->type_common.lang_flag_4);
    6004       590996 :       RB (t->type_common.lang_flag_5);
    6005       590996 :       RB (t->type_common.lang_flag_6);
    6006       590996 :       RB (t->type_common.typeless_storage);
    6007              :     }
    6008              : 
    6009     13527358 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    6010     10206637 :     goto done;
    6011              : 
    6012      3320721 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6013              :     {
    6014      3320721 :       RB (t->decl_common.nonlocal_flag);
    6015      3320721 :       RB (t->decl_common.virtual_flag);
    6016      3320721 :       RB (t->decl_common.ignored_flag);
    6017      3320721 :       RB (t->decl_common.abstract_flag);
    6018      3320721 :       RB (t->decl_common.artificial_flag);
    6019      3320721 :       RB (t->decl_common.preserve_flag);
    6020      3320721 :       RB (t->decl_common.debug_expr_is_from);
    6021      3320721 :       RB (t->decl_common.lang_flag_0);
    6022      3320721 :       RB (t->decl_common.lang_flag_1);
    6023      3320721 :       RB (t->decl_common.lang_flag_2);
    6024      3320721 :       RB (t->decl_common.lang_flag_3);
    6025      3320721 :       RB (t->decl_common.lang_flag_4);
    6026      3320721 :       RB (t->decl_common.lang_flag_5);
    6027      3320721 :       RB (t->decl_common.lang_flag_6);
    6028      3320721 :       RB (t->decl_common.lang_flag_7);
    6029      3320721 :       RB (t->decl_common.lang_flag_8);
    6030      3320721 :       RB (t->decl_common.decl_flag_0);
    6031      3320721 :       RB (t->decl_common.decl_flag_1);
    6032      3320721 :       RB (t->decl_common.decl_flag_2);
    6033      3320721 :       RB (t->decl_common.decl_flag_3);
    6034      3320721 :       RB (t->decl_common.not_gimple_reg_flag);
    6035      3320721 :       RB (t->decl_common.decl_by_reference_flag);
    6036      3320721 :       RB (t->decl_common.decl_read_flag);
    6037      3320721 :       RB (t->decl_common.decl_nonshareable_flag);
    6038      3320721 :       RB (t->decl_common.decl_not_flexarray);
    6039              :     }
    6040              :   else
    6041            0 :     goto done;
    6042              : 
    6043      3320721 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6044              :     {
    6045      1536424 :       RB (t->decl_with_vis.defer_output);
    6046      1536424 :       RB (t->decl_with_vis.hard_register);
    6047      1536424 :       RB (t->decl_with_vis.common_flag);
    6048      1536424 :       RB (t->decl_with_vis.in_text_section);
    6049      1536424 :       RB (t->decl_with_vis.in_constant_pool);
    6050      1536424 :       RB (t->decl_with_vis.dllimport_flag);
    6051      1536424 :       RB (t->decl_with_vis.weak_flag);
    6052      1536424 :       RB (t->decl_with_vis.seen_in_bind_expr);
    6053      1536424 :       RB (t->decl_with_vis.comdat_flag);
    6054      1536424 :       RB (t->decl_with_vis.visibility_specified);
    6055      1536424 :       RB (t->decl_with_vis.init_priority_p);
    6056      1536424 :       RB (t->decl_with_vis.shadowed_for_var_p);
    6057      1536424 :       RB (t->decl_with_vis.cxx_constructor);
    6058      1536424 :       RB (t->decl_with_vis.cxx_destructor);
    6059      1536424 :       RB (t->decl_with_vis.final);
    6060      1536424 :       RB (t->decl_with_vis.regdecl_flag);
    6061              :     }
    6062              :   else
    6063      1784297 :     goto done;
    6064              : 
    6065      1536424 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    6066              :     {
    6067       465214 :       RB (t->function_decl.static_ctor_flag);
    6068       465214 :       RB (t->function_decl.static_dtor_flag);
    6069       465214 :       RB (t->function_decl.uninlinable);
    6070       465214 :       RB (t->function_decl.possibly_inlined);
    6071       465214 :       RB (t->function_decl.novops_flag);
    6072       465214 :       RB (t->function_decl.returns_twice_flag);
    6073       465214 :       RB (t->function_decl.malloc_flag);
    6074       465214 :       RB (t->function_decl.declared_inline_flag);
    6075       465214 :       RB (t->function_decl.no_inline_warning_flag);
    6076       465214 :       RB (t->function_decl.no_instrument_function_entry_exit);
    6077       465214 :       RB (t->function_decl.no_limit_stack);
    6078       465214 :       RB (t->function_decl.disregard_inline_limits);
    6079       465214 :       RB (t->function_decl.pure_flag);
    6080       465214 :       RB (t->function_decl.looping_const_or_pure_flag);
    6081              : 
    6082       465214 :       RB (t->function_decl.has_debug_args_flag);
    6083       465214 :       RB (t->function_decl.versioned_function);
    6084       465214 :       RB (t->function_decl.replaceable_operator);
    6085              : 
    6086              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    6087       465214 :       unsigned kind = 0;
    6088       465214 :       kind |= unsigned (bits.b ()) << 0;
    6089       465214 :       kind |= unsigned (bits.b ()) << 1;
    6090       465214 :       t->function_decl.decl_type = function_decl_type (kind);
    6091              :     }
    6092              : #undef RB_IF
    6093              : #undef RB
    6094      1071210 : done:
    6095     15573287 :   return !get_overrun ();
    6096              : }
    6097              : 
    6098              : void
    6099      2950364 : trees_out::lang_decl_bools (tree t, bits_out& bits)
    6100              : {
    6101              : #define WB(X) (bits.b (X))
    6102      2950364 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6103              : 
    6104      2950364 :   bits.bflush ();
    6105      2950364 :   WB (lang->u.base.language == lang_cplusplus);
    6106      2950364 :   WB ((lang->u.base.use_template >> 0) & 1);
    6107      2950364 :   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      2950364 :   WB (lang->u.base.initialized_in_class);
    6111              : 
    6112      2950364 :   WB (lang->u.base.threadprivate_or_deleted_p);
    6113      2950364 :   WB (lang->u.base.anticipated_p);
    6114      2950364 :   WB (lang->u.base.friend_or_tls);
    6115      2950364 :   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      2950364 :   WB (lang->u.base.concept_p);
    6119      2950364 :   WB (lang->u.base.var_declared_inline_p);
    6120      2950364 :   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      5810434 :   WB (lang->u.base.module_purview_p && !header_module_p ());
    6126      2950364 :   WB (lang->u.base.module_attach_p);
    6127              :   /* Importer will set module_import_p and module_entity_p themselves
    6128              :      as appropriate.  */
    6129      2950364 :   WB (lang->u.base.module_keyed_decls_p);
    6130              : 
    6131      2950364 :   WB (lang->u.base.omp_declare_mapper_p);
    6132              : 
    6133      2950364 :   switch (lang->u.base.selector)
    6134              :     {
    6135            0 :     default:
    6136            0 :       gcc_unreachable ();
    6137              : 
    6138       680083 :     case lds_fn:  /* lang_decl_fn.  */
    6139       680083 :       WB (lang->u.fn.global_ctor_p);
    6140       680083 :       WB (lang->u.fn.global_dtor_p);
    6141              : 
    6142       680083 :       WB (lang->u.fn.static_function);
    6143       680083 :       WB (lang->u.fn.pure_virtual);
    6144       680083 :       WB (lang->u.fn.defaulted_p);
    6145       680083 :       WB (lang->u.fn.has_in_charge_parm_p);
    6146       680083 :       WB (lang->u.fn.has_vtt_parm_p);
    6147              :       /* There shouldn't be a pending inline at this point.  */
    6148       680083 :       gcc_assert (!lang->u.fn.pending_inline_p);
    6149       680083 :       WB (lang->u.fn.nonconverting);
    6150       680083 :       WB (lang->u.fn.thunk_p);
    6151              : 
    6152       680083 :       WB (lang->u.fn.this_thunk_p);
    6153       680083 :       WB (lang->u.fn.omp_declare_reduction_p);
    6154       680083 :       WB (lang->u.fn.has_dependent_explicit_spec_p);
    6155       680083 :       WB (lang->u.fn.immediate_fn_p);
    6156       680083 :       WB (lang->u.fn.maybe_deleted);
    6157       680083 :       WB (lang->u.fn.coroutine_p);
    6158       680083 :       WB (lang->u.fn.implicit_constexpr);
    6159       680083 :       WB (lang->u.fn.escalated_p);
    6160       680083 :       WB (lang->u.fn.xobj_func);
    6161       680083 :       goto lds_min;
    6162              : 
    6163         3610 :     case lds_decomp:  /* lang_decl_decomp.  */
    6164              :       /* No bools.  */
    6165         3610 :       goto lds_min;
    6166              : 
    6167              :     case lds_min:  /* lang_decl_min.  */
    6168      2950364 :     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      2950364 : }
    6182              : 
    6183              : bool
    6184      2026684 : trees_in::lang_decl_bools (tree t, bits_in& bits)
    6185              : {
    6186              : #define RB(X) ((X) = bits.b ())
    6187      2026684 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6188              : 
    6189      2026684 :   bits.bflush ();
    6190      2026684 :   lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
    6191      2026684 :   unsigned v;
    6192      2026684 :   v = bits.b () << 0;
    6193      2026684 :   v |= bits.b () << 1;
    6194      2026684 :   lang->u.base.use_template = v;
    6195              :   /* lang->u.base.not_really_extern is not streamed.  */
    6196      2026684 :   RB (lang->u.base.initialized_in_class);
    6197              : 
    6198      2026684 :   RB (lang->u.base.threadprivate_or_deleted_p);
    6199      2026684 :   RB (lang->u.base.anticipated_p);
    6200      2026684 :   RB (lang->u.base.friend_or_tls);
    6201      2026684 :   RB (lang->u.base.unknown_bound_p);
    6202              :   /* lang->u.base.odr_used is not streamed.  */
    6203      2026684 :   RB (lang->u.base.concept_p);
    6204      2026684 :   RB (lang->u.base.var_declared_inline_p);
    6205      2026684 :   RB (lang->u.base.dependent_init_p);
    6206              : 
    6207      2026684 :   RB (lang->u.base.module_purview_p);
    6208      2026684 :   RB (lang->u.base.module_attach_p);
    6209              :   /* module_import_p and module_entity_p are not streamed.  */
    6210      2026684 :   RB (lang->u.base.module_keyed_decls_p);
    6211              : 
    6212      2026684 :   RB (lang->u.base.omp_declare_mapper_p);
    6213              : 
    6214      2026684 :   switch (lang->u.base.selector)
    6215              :     {
    6216            0 :     default:
    6217            0 :       gcc_unreachable ();
    6218              : 
    6219       465214 :     case lds_fn:  /* lang_decl_fn.  */
    6220       465214 :       RB (lang->u.fn.global_ctor_p);
    6221       465214 :       RB (lang->u.fn.global_dtor_p);
    6222              : 
    6223       465214 :       RB (lang->u.fn.static_function);
    6224       465214 :       RB (lang->u.fn.pure_virtual);
    6225       465214 :       RB (lang->u.fn.defaulted_p);
    6226       465214 :       RB (lang->u.fn.has_in_charge_parm_p);
    6227       465214 :       RB (lang->u.fn.has_vtt_parm_p);
    6228              :       /* lang->u.f.n.pending_inline_p is not streamed.  */
    6229       465214 :       RB (lang->u.fn.nonconverting);
    6230       465214 :       RB (lang->u.fn.thunk_p);
    6231              : 
    6232       465214 :       RB (lang->u.fn.this_thunk_p);
    6233       465214 :       RB (lang->u.fn.omp_declare_reduction_p);
    6234       465214 :       RB (lang->u.fn.has_dependent_explicit_spec_p);
    6235       465214 :       RB (lang->u.fn.immediate_fn_p);
    6236       465214 :       RB (lang->u.fn.maybe_deleted);
    6237       465214 :       RB (lang->u.fn.coroutine_p);
    6238       465214 :       RB (lang->u.fn.implicit_constexpr);
    6239       465214 :       RB (lang->u.fn.escalated_p);
    6240       465214 :       RB (lang->u.fn.xobj_func);
    6241       465214 :       goto lds_min;
    6242              : 
    6243         2911 :     case lds_decomp:  /* lang_decl_decomp.  */
    6244              :       /* No bools.  */
    6245         2911 :       goto lds_min;
    6246              : 
    6247              :     case lds_min:  /* lang_decl_min.  */
    6248      2026684 :     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      2026684 :   return !get_overrun ();
    6262              : }
    6263              : 
    6264              : void
    6265       234019 : trees_out::lang_type_bools (tree t, bits_out& bits)
    6266              : {
    6267              : #define WB(X) (bits.b (X))
    6268       234019 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6269              : 
    6270       234019 :   bits.bflush ();
    6271       234019 :   WB (lang->has_type_conversion);
    6272       234019 :   WB (lang->has_copy_ctor);
    6273       234019 :   WB (lang->has_default_ctor);
    6274       234019 :   WB (lang->const_needs_init);
    6275       234019 :   WB (lang->ref_needs_init);
    6276       234019 :   WB (lang->has_const_copy_assign);
    6277       234019 :   WB ((lang->use_template >> 0) & 1);
    6278       234019 :   WB ((lang->use_template >> 1) & 1);
    6279              : 
    6280       234019 :   WB (lang->has_mutable);
    6281       234019 :   WB (lang->com_interface);
    6282       234019 :   WB (lang->non_pod_class);
    6283       234019 :   WB (lang->nearly_empty_p);
    6284       234019 :   WB (lang->user_align);
    6285       234019 :   WB (lang->has_copy_assign);
    6286       234019 :   WB (lang->has_new);
    6287       234019 :   WB (lang->has_array_new);
    6288              : 
    6289       234019 :   WB ((lang->gets_delete >> 0) & 1);
    6290       234019 :   WB ((lang->gets_delete >> 1) & 1);
    6291       234019 :   WB (lang->interface_only);
    6292       234019 :   WB (lang->interface_unknown);
    6293       234019 :   WB (lang->contains_empty_class_p);
    6294       234019 :   WB (lang->anon_aggr);
    6295       234019 :   WB (lang->non_zero_init);
    6296       234019 :   WB (lang->empty_p);
    6297              : 
    6298       234019 :   WB (lang->vec_new_uses_cookie);
    6299       234019 :   WB (lang->declared_class);
    6300       234019 :   WB (lang->diamond_shaped);
    6301       234019 :   WB (lang->repeated_base);
    6302       234019 :   gcc_checking_assert (!lang->being_defined);
    6303              :   // lang->debug_requested
    6304       234019 :   WB (lang->fields_readonly);
    6305       234019 :   WB (lang->ptrmemfunc_flag);
    6306              : 
    6307       234019 :   WB (lang->lazy_default_ctor);
    6308       234019 :   WB (lang->lazy_copy_ctor);
    6309       234019 :   WB (lang->lazy_copy_assign);
    6310       234019 :   WB (lang->lazy_destructor);
    6311       234019 :   WB (lang->has_const_copy_ctor);
    6312       234019 :   WB (lang->has_complex_copy_ctor);
    6313       234019 :   WB (lang->has_complex_copy_assign);
    6314       234019 :   WB (lang->non_aggregate);
    6315              : 
    6316       234019 :   WB (lang->has_complex_dflt);
    6317       234019 :   WB (lang->has_list_ctor);
    6318       234019 :   WB (lang->non_std_layout);
    6319       234019 :   WB (lang->is_literal);
    6320       234019 :   WB (lang->lazy_move_ctor);
    6321       234019 :   WB (lang->lazy_move_assign);
    6322       234019 :   WB (lang->has_complex_move_ctor);
    6323       234019 :   WB (lang->has_complex_move_assign);
    6324              : 
    6325       234019 :   WB (lang->has_constexpr_ctor);
    6326       234019 :   WB (lang->unique_obj_representations);
    6327       234019 :   WB (lang->unique_obj_representations_set);
    6328       234019 :   gcc_checking_assert (!lang->erroneous);
    6329       234019 :   WB (lang->non_pod_aggregate);
    6330       234019 :   WB (lang->non_aggregate_pod);
    6331              : #undef WB
    6332       234019 : }
    6333              : 
    6334              : bool
    6335       153757 : trees_in::lang_type_bools (tree t, bits_in& bits)
    6336              : {
    6337              : #define RB(X) ((X) = bits.b ())
    6338       153757 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6339              : 
    6340       153757 :   bits.bflush ();
    6341       153757 :   RB (lang->has_type_conversion);
    6342       153757 :   RB (lang->has_copy_ctor);
    6343       153757 :   RB (lang->has_default_ctor);
    6344       153757 :   RB (lang->const_needs_init);
    6345       153757 :   RB (lang->ref_needs_init);
    6346       153757 :   RB (lang->has_const_copy_assign);
    6347       153757 :   unsigned v;
    6348       153757 :   v = bits.b () << 0;
    6349       153757 :   v |= bits.b () << 1;
    6350       153757 :   lang->use_template = v;
    6351              : 
    6352       153757 :   RB (lang->has_mutable);
    6353       153757 :   RB (lang->com_interface);
    6354       153757 :   RB (lang->non_pod_class);
    6355       153757 :   RB (lang->nearly_empty_p);
    6356       153757 :   RB (lang->user_align);
    6357       153757 :   RB (lang->has_copy_assign);
    6358       153757 :   RB (lang->has_new);
    6359       153757 :   RB (lang->has_array_new);
    6360              : 
    6361       153757 :   v = bits.b () << 0;
    6362       153757 :   v |= bits.b () << 1;
    6363       153757 :   lang->gets_delete = v;
    6364       153757 :   RB (lang->interface_only);
    6365       153757 :   RB (lang->interface_unknown);
    6366       153757 :   RB (lang->contains_empty_class_p);
    6367       153757 :   RB (lang->anon_aggr);
    6368       153757 :   RB (lang->non_zero_init);
    6369       153757 :   RB (lang->empty_p);
    6370              : 
    6371       153757 :   RB (lang->vec_new_uses_cookie);
    6372       153757 :   RB (lang->declared_class);
    6373       153757 :   RB (lang->diamond_shaped);
    6374       153757 :   RB (lang->repeated_base);
    6375       153757 :   gcc_checking_assert (!lang->being_defined);
    6376       153757 :   gcc_checking_assert (!lang->debug_requested);
    6377       153757 :   RB (lang->fields_readonly);
    6378       153757 :   RB (lang->ptrmemfunc_flag);
    6379              : 
    6380       153757 :   RB (lang->lazy_default_ctor);
    6381       153757 :   RB (lang->lazy_copy_ctor);
    6382       153757 :   RB (lang->lazy_copy_assign);
    6383       153757 :   RB (lang->lazy_destructor);
    6384       153757 :   RB (lang->has_const_copy_ctor);
    6385       153757 :   RB (lang->has_complex_copy_ctor);
    6386       153757 :   RB (lang->has_complex_copy_assign);
    6387       153757 :   RB (lang->non_aggregate);
    6388              : 
    6389       153757 :   RB (lang->has_complex_dflt);
    6390       153757 :   RB (lang->has_list_ctor);
    6391       153757 :   RB (lang->non_std_layout);
    6392       153757 :   RB (lang->is_literal);
    6393       153757 :   RB (lang->lazy_move_ctor);
    6394       153757 :   RB (lang->lazy_move_assign);
    6395       153757 :   RB (lang->has_complex_move_ctor);
    6396       153757 :   RB (lang->has_complex_move_assign);
    6397              : 
    6398       153757 :   RB (lang->has_constexpr_ctor);
    6399       153757 :   RB (lang->unique_obj_representations);
    6400       153757 :   RB (lang->unique_obj_representations_set);
    6401       153757 :   gcc_checking_assert (!lang->erroneous);
    6402       153757 :   RB (lang->non_pod_aggregate);
    6403       153757 :   RB (lang->non_aggregate_pod);
    6404              : #undef RB
    6405       153757 :   return !get_overrun ();
    6406              : }
    6407              : 
    6408              : /* Read & write the core values and pointers.  */
    6409              : 
    6410              : void
    6411     59270213 : trees_out::core_vals (tree t)
    6412              : {
    6413              : #define WU(X) (u (X))
    6414              : #define WT(X) (tree_node (X))
    6415     59270213 :   tree_code code = TREE_CODE (t);
    6416              : 
    6417              :   /* First by shape of the tree.  */
    6418              : 
    6419     59270213 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    6420              :     {
    6421              :       /* Write this early, for better log information.  */
    6422     12899358 :       WT (t->decl_minimal.name);
    6423     12899358 :       if (!DECL_TEMPLATE_PARM_P (t))
    6424      9962185 :         WT (t->decl_minimal.context);
    6425              : 
    6426     12899358 :       if (state)
    6427     10615200 :         state->write_location (*this, t->decl_minimal.locus);
    6428              : 
    6429     12899358 :       if (streaming_p ())
    6430      4914373 :         if (has_warning_spec (t))
    6431         1023 :           u (get_warning_spec (t));
    6432              :     }
    6433              : 
    6434     59270213 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    6435              :     {
    6436              :       /* The only types we write also have TYPE_NON_COMMON.  */
    6437      3176043 :       gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
    6438              : 
    6439              :       /* We only stream the main variant.  */
    6440      3176043 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    6441              : 
    6442              :       /* Stream the name & context first, for better log information  */
    6443      3176043 :       WT (t->type_common.name);
    6444      3176043 :       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      3176043 :       WT (t->type_common.main_variant);
    6450              : 
    6451      3176043 :       tree canonical = t->type_common.canonical;
    6452      3176043 :       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      3176043 :       WT (canonical);
    6457              : 
    6458              :       /* type_common.next_variant is internally manipulated.  */
    6459              :       /* type_common.pointer_to, type_common.reference_to.  */
    6460              : 
    6461      3176043 :       if (streaming_p ())
    6462              :         {
    6463       861295 :           WU (t->type_common.precision);
    6464       861295 :           WU (t->type_common.contains_placeholder_bits);
    6465       861295 :           WU (t->type_common.mode);
    6466       861295 :           WU (t->type_common.align);
    6467              :         }
    6468              : 
    6469      3176043 :       if (!RECORD_OR_UNION_CODE_P (code))
    6470              :         {
    6471      2641753 :           WT (t->type_common.size);
    6472      2641753 :           WT (t->type_common.size_unit);
    6473              :         }
    6474      3176043 :       WT (t->type_common.attributes);
    6475              : 
    6476      3176043 :       WT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    6477              :     }
    6478              : 
    6479     59270213 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6480              :     {
    6481     12899358 :       if (streaming_p ())
    6482              :         {
    6483      4914373 :           WU (t->decl_common.mode);
    6484      4914373 :           WU (t->decl_common.off_align);
    6485      4914373 :           WU (t->decl_common.align);
    6486              :         }
    6487              : 
    6488              :       /* For templates these hold instantiation (partial and/or
    6489              :          specialization) information.  */
    6490     12899358 :       if (code != TEMPLATE_DECL)
    6491              :         {
    6492     11925777 :           WT (t->decl_common.size);
    6493     11925777 :           WT (t->decl_common.size_unit);
    6494              :         }
    6495              : 
    6496     12899358 :       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     12899358 :       WT (t->decl_common.abstract_origin);
    6503              :     }
    6504              : 
    6505     59270213 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6506              :     {
    6507      6103385 :       WT (t->decl_with_vis.assembler_name);
    6508      6103385 :       if (streaming_p ())
    6509      2313687 :         WU (t->decl_with_vis.visibility);
    6510              :     }
    6511              : 
    6512     59270213 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6513              :     {
    6514      3176043 :       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        10236 :           WT (t->type_non_common.maxval);
    6520        10236 :           WT (t->type_non_common.minval);
    6521              :         }
    6522              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    6523              :          things.  */
    6524      3165807 :       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      2631517 :           gcc_checking_assert (code == TEMPLATE_TYPE_PARM
    6529              :                                || code == TEMPLATE_TEMPLATE_PARM
    6530              :                                || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    6531      2631517 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6532      2631517 :           WT (t->type_non_common.values);
    6533      2631517 :           WT (t->type_non_common.maxval);
    6534      2631517 :           WT (t->type_non_common.minval);
    6535              :         }
    6536              : 
    6537      3176043 :       WT (t->type_non_common.lang_1);
    6538              :     }
    6539              : 
    6540     59270213 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6541              :     {
    6542     18226489 :       if (state)
    6543     17872217 :         state->write_location (*this, t->exp.locus);
    6544              : 
    6545     18226489 :       if (streaming_p ())
    6546      8855088 :         if (has_warning_spec (t))
    6547       697720 :           u (get_warning_spec (t));
    6548              : 
    6549     18226489 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6550     18226489 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6551     18226489 :                         : TREE_OPERAND_LENGTH (t));
    6552     18226489 :       unsigned ix = unsigned (vl);
    6553     50141330 :       for (; ix != limit; ix++)
    6554     31914841 :         WT (TREE_OPERAND (t, ix));
    6555              :     }
    6556     41043724 :   else if (code == REQUIRES_EXPR)
    6557              :     {
    6558        25683 :       if (state)
    6559        21657 :         state->write_location (*this, REQUIRES_EXPR_LOCATION (t));
    6560              : 
    6561        25683 :       if (streaming_p ())
    6562        10556 :         if (has_warning_spec (t))
    6563            0 :           u (get_warning_spec (t));
    6564              : 
    6565        25683 :       chained_decls (REQUIRES_EXPR_PARMS (t));
    6566        25683 :       WT (REQUIRES_EXPR_REQS (t));
    6567        25683 :       WT (REQUIRES_EXPR_EXTRA_ARGS (t));
    6568              :     }
    6569              :   else
    6570              :     /* The CODE_CONTAINS tables were inaccurate when I started.  */
    6571     41018041 :     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     59270213 :   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      5381273 :     case INTEGER_CST:
    6608      5381273 :       if (streaming_p ())
    6609              :         {
    6610       904860 :           unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6611      1813230 :           for (unsigned ix = 0; ix != num; ix++)
    6612       908370 :             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        53802 :     case REAL_CST:
    6623        53802 :       if (streaming_p ())
    6624        26848 :         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       730731 :     case VAR_DECL:
    6654       730731 :       if (DECL_CONTEXT (t)
    6655       730731 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    6656              :         {
    6657       145353 :           if (DECL_HAS_VALUE_EXPR_P (t))
    6658           18 :             WT (DECL_VALUE_EXPR (t));
    6659              :           break;
    6660              :         }
    6661              :       /* FALLTHROUGH  */
    6662              : 
    6663      5899679 :     case RESULT_DECL:
    6664      5899679 :     case PARM_DECL:
    6665      5899679 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6666        58257 :         WT (DECL_VALUE_EXPR (t));
    6667              :       /* FALLTHROUGH  */
    6668              : 
    6669      6149292 :     case CONST_DECL:
    6670      6149292 :     case IMPORTED_DECL:
    6671      6149292 :       WT (t->decl_common.initial);
    6672      6149292 :       break;
    6673              : 
    6674       179553 :     case FIELD_DECL:
    6675       179553 :       WT (t->field_decl.offset);
    6676       179553 :       WT (t->field_decl.bit_field_type);
    6677       179553 :       {
    6678       179553 :         auto ovr = make_temp_override (walking_bit_field_unit, true);
    6679       179553 :         WT (t->field_decl.qualifier); /* bitfield unit.  */
    6680       179553 :       }
    6681       179553 :       WT (t->field_decl.bit_offset);
    6682       179553 :       WT (t->field_decl.fcontext);
    6683       179553 :       WT (t->decl_common.initial);
    6684       179553 :       break;
    6685              : 
    6686        62007 :     case LABEL_DECL:
    6687        62007 :       if (streaming_p ())
    6688              :         {
    6689        31002 :           WU (t->label_decl.label_decl_uid);
    6690        31002 :           WU (t->label_decl.eh_landing_pad_nr);
    6691              :         }
    6692              :       break;
    6693              : 
    6694      1360380 :     case FUNCTION_DECL:
    6695      1360380 :       if (streaming_p ())
    6696              :         {
    6697              :           /* Builtins can be streamed by value when a header declares
    6698              :              them.  */
    6699       680083 :           WU (DECL_BUILT_IN_CLASS (t));
    6700       680083 :           if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
    6701        13203 :             WU (DECL_UNCHECKED_FUNCTION_CODE (t));
    6702              :         }
    6703              : 
    6704      1360380 :       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      1360380 :       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      1360380 :       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      1360380 :       WT (t->function_decl.vindex);
    6714              : 
    6715      1360380 :       if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6716         9540 :         WT (lookup_explicit_specifier (t));
    6717              :       break;
    6718              : 
    6719       163933 :     case USING_DECL:
    6720              :       /* USING_DECL_DECLS  */
    6721       163933 :       WT (t->decl_common.initial);
    6722              :       /* FALLTHROUGH  */
    6723              : 
    6724      4011809 :     case TYPE_DECL:
    6725              :       /* USING_DECL: USING_DECL_SCOPE  */
    6726              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6727      4011809 :       WT (t->decl_non_common.result);
    6728      4011809 :       break;
    6729              : 
    6730              :       /* Miscellaneous common nodes.  */
    6731       905406 :     case BLOCK:
    6732       905406 :       if (state)
    6733              :         {
    6734       905406 :           state->write_location (*this, t->block.locus);
    6735       905406 :           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      1388170 :       for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
    6741              :         {
    6742       482764 :           if (VAR_OR_FUNCTION_DECL_P (decls)
    6743       482764 :               && DECL_LOCAL_DECL_P (decls))
    6744              :             {
    6745              :               /* Make sure this is the first encounter, and mark for
    6746              :                  walk-by-value.  */
    6747          332 :               gcc_checking_assert (!TREE_VISITED (decls)
    6748              :                                    && !DECL_TEMPLATE_INFO (decls));
    6749          332 :               mark_by_value (decls);
    6750              :             }
    6751       482764 :           tree_node (decls);
    6752              :         }
    6753       905406 :       tree_node (NULL_TREE);
    6754              : 
    6755              :       /* nonlocalized_vars is a middle-end thing.  */
    6756       905406 :       WT (t->block.subblocks);
    6757       905406 :       WT (t->block.supercontext);
    6758              :       // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
    6759       905406 :       WT (t->block.abstract_origin);
    6760              :       /* fragment_origin, fragment_chain are middle-end things.  */
    6761       905406 :       WT (t->block.chain);
    6762              :       /* nonlocalized_vars, block_num & die are middle endy/debug
    6763              :          things.  */
    6764       905406 :       break;
    6765              : 
    6766      1797222 :     case CALL_EXPR:
    6767      1797222 :       if (streaming_p ())
    6768       860587 :         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       770936 :     case STATEMENT_LIST:
    6792      3237893 :       for (tree stmt : tsi_range (t))
    6793      2466957 :         if (stmt)
    6794      2466957 :           WT (stmt);
    6795       770936 :       WT (NULL_TREE);
    6796       770936 :       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       300164 :       break;
    6810              : 
    6811       300164 :     case TREE_BINFO:
    6812       300164 :       {
    6813       300164 :         WT (t->binfo.common.chain);
    6814       300164 :         WT (t->binfo.offset);
    6815       300164 :         WT (t->binfo.inheritance);
    6816       300164 :         WT (t->binfo.vptr_field);
    6817              : 
    6818       300164 :         WT (t->binfo.vtable);
    6819       300164 :         WT (t->binfo.virtuals);
    6820       300164 :         WT (t->binfo.vtt_subvtt);
    6821       300164 :         WT (t->binfo.vtt_vptr);
    6822              : 
    6823       300164 :         tree_vec (BINFO_BASE_ACCESSES (t));
    6824       300164 :         unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6825       383208 :         for (unsigned ix = 0; ix != num; ix++)
    6826        83044 :           WT (BINFO_BASE_BINFO (t, ix));
    6827              :       }
    6828              :       break;
    6829              : 
    6830      4457617 :     case TREE_LIST:
    6831      4457617 :       WT (t->list.purpose);
    6832      4457617 :       WT (t->list.value);
    6833      4457617 :       WT (t->list.common.chain);
    6834      4457617 :       break;
    6835              : 
    6836      3826856 :     case TREE_VEC:
    6837     10631521 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6838      6804665 :         WT (TREE_VEC_ELT (t, ix));
    6839              :       /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN!  */
    6840      3826856 :       gcc_checking_assert (!t->type_common.common.chain
    6841              :                            || (TREE_CODE (t->type_common.common.chain)
    6842              :                                == INTEGER_CST));
    6843      3826856 :       WT (t->type_common.common.chain);
    6844      3826856 :       break;
    6845              : 
    6846              :       /* C++-specific nodes ...  */
    6847       313404 :     case BASELINK:
    6848       313404 :       WT (((lang_tree_node *)t)->baselink.binfo);
    6849       313404 :       WT (((lang_tree_node *)t)->baselink.functions);
    6850       313404 :       WT (((lang_tree_node *)t)->baselink.access_binfo);
    6851       313404 :       WT (((lang_tree_node *)t)->baselink.common.chain);
    6852       313404 :       break;
    6853              : 
    6854       143466 :     case CONSTRAINT_INFO:
    6855       143466 :       WT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6856       143466 :       WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6857       143466 :       WT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6858       143466 :       break;
    6859              : 
    6860        22650 :     case DEFERRED_NOEXCEPT:
    6861        22650 :       WT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    6862        22650 :       WT (((lang_tree_node *)t)->deferred_noexcept.args);
    6863        22650 :       break;
    6864              : 
    6865        22564 :     case LAMBDA_EXPR:
    6866        22564 :       WT (((lang_tree_node *)t)->lambda_expression.capture_list);
    6867        22564 :       WT (((lang_tree_node *)t)->lambda_expression.this_capture);
    6868        22564 :       WT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    6869        22564 :       WT (((lang_tree_node *)t)->lambda_expression.regen_info);
    6870        22564 :       WT (((lang_tree_node *)t)->lambda_expression.extra_args);
    6871              :       /* pending_proxies is a parse-time thing.  */
    6872        22564 :       gcc_assert (!((lang_tree_node *)t)->lambda_expression.pending_proxies);
    6873        22564 :       if (state)
    6874        22561 :         state->write_location
    6875        22561 :           (*this, ((lang_tree_node *)t)->lambda_expression.locus);
    6876        22564 :       if (streaming_p ())
    6877              :         {
    6878         7763 :           WU (((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    6879         7763 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    6880         7763 :           WU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    6881              :         }
    6882              :       break;
    6883              : 
    6884      3345910 :     case OVERLOAD:
    6885      3345910 :       WT (((lang_tree_node *)t)->overload.function);
    6886      3345910 :       WT (t->common.chain);
    6887      3345910 :       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        25143 :     case STATIC_ASSERT:
    6896        25143 :       WT (((lang_tree_node *)t)->static_assertion.condition);
    6897        25143 :       WT (((lang_tree_node *)t)->static_assertion.message);
    6898        25143 :       if (state)
    6899        25143 :         state->write_location
    6900        25143 :           (*this, ((lang_tree_node *)t)->static_assertion.location);
    6901              :       break;
    6902              : 
    6903       973581 :     case TEMPLATE_DECL:
    6904              :       /* Streamed with the template_decl node itself.  */
    6905       973581 :       gcc_checking_assert
    6906              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
    6907       973581 :       gcc_checking_assert
    6908              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
    6909       973581 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6910        19406 :         WT (DECL_CHAIN (t));
    6911              :       break;
    6912              : 
    6913      2212456 :     case TEMPLATE_INFO:
    6914      2212456 :       {
    6915      2212456 :         WT (((lang_tree_node *)t)->template_info.tmpl);
    6916      2212456 :         WT (((lang_tree_node *)t)->template_info.args);
    6917      2212456 :         WT (((lang_tree_node *)t)->template_info.partial);
    6918              : 
    6919      2212456 :         const auto *ac = (((lang_tree_node *)t)
    6920              :                           ->template_info.deferred_access_checks);
    6921      2212456 :         unsigned len = vec_safe_length (ac);
    6922      2212456 :         if (streaming_p ())
    6923      1103961 :           u (len);
    6924      2212456 :         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      2829244 :     case TEMPLATE_PARM_INDEX:
    6940      2829244 :       if (streaming_p ())
    6941              :         {
    6942       625658 :           WU (((lang_tree_node *)t)->tpi.index);
    6943       625658 :           WU (((lang_tree_node *)t)->tpi.level);
    6944       625658 :           WU (((lang_tree_node *)t)->tpi.orig_level);
    6945              :         }
    6946      2829244 :       WT (((lang_tree_node *)t)->tpi.decl);
    6947              :       /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
    6948              :          cache, do not stream.  */
    6949      2829244 :       break;
    6950              : 
    6951        32602 :     case TRAIT_EXPR:
    6952        32602 :       WT (((lang_tree_node *)t)->trait_expression.type1);
    6953        32602 :       WT (((lang_tree_node *)t)->trait_expression.type2);
    6954        32602 :       if (state)
    6955        25726 :         state->write_location
    6956        25726 :           (*this, ((lang_tree_node *)t)->trait_expression.locus);
    6957        32602 :       if (streaming_p ())
    6958        12681 :         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     59270213 :   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     55961088 :       tree type = t->typed.type;
    6980     55961088 :       unsigned prec = 0;
    6981              : 
    6982     55961088 :       switch (code)
    6983              :         {
    6984              :         default:
    6985              :           break;
    6986              : 
    6987              :         case TEMPLATE_DECL:
    6988              :           /* We fill in the template's type separately.  */
    6989     55961088 :           type = NULL_TREE;
    6990              :           break;
    6991              : 
    6992      3847876 :         case TYPE_DECL:
    6993      3847876 :           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        10236 :         case ENUMERAL_TYPE:
    6999        10236 :           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         5974 :               prec = TYPE_PRECISION (type);
    7004         5974 :               tree name = DECL_NAME (TYPE_NAME (type));
    7005              : 
    7006        78058 :               for (unsigned itk = itk_none; itk--;)
    7007        78058 :                 if (integer_types[itk]
    7008        78058 :                     && DECL_NAME (TYPE_NAME (integer_types[itk])) == name)
    7009              :                   {
    7010              :                     type = integer_types[itk];
    7011              :                     break;
    7012              :                   }
    7013         5974 :               gcc_assert (type != t->typed.type);
    7014              :             }
    7015              :           break;
    7016              :         }
    7017              : 
    7018     55961088 :       WT (type);
    7019     55961088 :       if (prec && streaming_p ())
    7020         2985 :         WU (prec);
    7021              :     }
    7022              : 
    7023     59270213 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7024              :     {
    7025       153726 :       unsigned len = vec_safe_length (t->constructor.elts);
    7026       153726 :       if (streaming_p ())
    7027        75787 :         WU (len);
    7028       153726 :       if (len)
    7029       543958 :         for (unsigned ix = 0; ix != len; ix++)
    7030              :           {
    7031       461636 :             const constructor_elt &elt = (*t->constructor.elts)[ix];
    7032              : 
    7033       461636 :             WT (elt.index);
    7034       461636 :             WT (elt.value);
    7035              :           }
    7036              :     }
    7037              : 
    7038              : #undef WT
    7039              : #undef WU
    7040     59270213 : }
    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     15549518 : 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     15549518 :   tree_code code = TREE_CODE (t);
    7055              : 
    7056              :   /* First by tree shape.  */
    7057     15549518 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    7058              :     {
    7059      3320721 :       RT (t->decl_minimal.name);
    7060      3320721 :       if (!DECL_TEMPLATE_PARM_P (t))
    7061      2887734 :         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      3320721 :       t->decl_minimal.locus = state->read_location (*this);
    7066      3320721 :       if (has_warning_spec (t))
    7067          629 :         put_warning_spec (t, u ());
    7068              :     }
    7069              : 
    7070     15549518 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    7071              :     {
    7072       567227 :       RT (t->type_common.name);
    7073       567227 :       RT (t->type_common.context);
    7074              : 
    7075       567227 :       RT (t->type_common.main_variant);
    7076       567227 :       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       567227 :       RU (t->type_common.precision);
    7082       567227 :       RU (t->type_common.contains_placeholder_bits);
    7083       567227 :       RUC (machine_mode, t->type_common.mode);
    7084       567227 :       RU (t->type_common.align);
    7085              : 
    7086       567227 :       if (!RECORD_OR_UNION_CODE_P (code))
    7087              :         {
    7088       392838 :           RT (t->type_common.size);
    7089       392838 :           RT (t->type_common.size_unit);
    7090              :         }
    7091       567227 :       RT (t->type_common.attributes);
    7092              : 
    7093       567227 :       RT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    7094              :     }
    7095              : 
    7096     15549518 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    7097              :     {
    7098      3320721 :       RUC (machine_mode, t->decl_common.mode);
    7099      3320721 :       RU (t->decl_common.off_align);
    7100      3320721 :       RU (t->decl_common.align);
    7101              : 
    7102      3320721 :       if (code != TEMPLATE_DECL)
    7103              :         {
    7104      2991068 :           RT (t->decl_common.size);
    7105      2991068 :           RT (t->decl_common.size_unit);
    7106              :         }
    7107              : 
    7108      3320721 :       RT (t->decl_common.attributes);
    7109      3320721 :       RT (t->decl_common.abstract_origin);
    7110              :     }
    7111              : 
    7112     15549518 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    7113              :     {
    7114      1536424 :       RT (t->decl_with_vis.assembler_name);
    7115      1536424 :       RUC (symbol_visibility, t->decl_with_vis.visibility);
    7116              :     }
    7117              : 
    7118     15549518 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    7119              :     {
    7120       567227 :       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         3284 :           RT (t->type_non_common.maxval);
    7126         3284 :           RT (t->type_non_common.minval);
    7127              :         }
    7128              :       /* Records and unions hold FIELDS, VFIELD & BINFO on these
    7129              :          things.  */
    7130       563943 :       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       389554 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    7135       389554 :           RT (t->type_non_common.values);
    7136       389554 :           RT (t->type_non_common.maxval);
    7137       389554 :           RT (t->type_non_common.minval);
    7138              :         }
    7139              : 
    7140       567227 :       RT (t->type_non_common.lang_1);
    7141              :     }
    7142              : 
    7143     15549518 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    7144              :     {
    7145      6143906 :       t->exp.locus = state->read_location (*this);
    7146      6143906 :       if (has_warning_spec (t))
    7147       483491 :         put_warning_spec (t, u ());
    7148              : 
    7149      6143906 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    7150      6143906 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    7151      6143906 :                         : TREE_OPERAND_LENGTH (t));
    7152      6143906 :       unsigned ix = unsigned (vl);
    7153     16834254 :       for (; ix != limit; ix++)
    7154     10690348 :         RTU (TREE_OPERAND (t, ix));
    7155              :     }
    7156      9405612 :   else if (code == REQUIRES_EXPR)
    7157              :     {
    7158         7646 :       REQUIRES_EXPR_LOCATION (t) = state->read_location (*this);
    7159         7646 :       if (has_warning_spec (t))
    7160            0 :         put_warning_spec (t, u ());
    7161              : 
    7162         7646 :       REQUIRES_EXPR_PARMS (t) = chained_decls ();
    7163         7646 :       RTU (REQUIRES_EXPR_REQS (t));
    7164         7646 :       RTU (REQUIRES_EXPR_EXTRA_ARGS (t));
    7165              :     }
    7166              : 
    7167              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    7168              :      correspondence. */
    7169     15549518 :   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       606309 :     case INTEGER_CST:
    7194       606309 :       {
    7195       606309 :         unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    7196      1214914 :         for (unsigned ix = 0; ix != num; ix++)
    7197       608605 :           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        18248 :     case REAL_CST:
    7207        18248 :       if (const void *bytes = buf (sizeof (real_value)))
    7208        18248 :         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       234807 :     case VAR_DECL:
    7229       234807 :       if (DECL_CONTEXT (t)
    7230       234807 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    7231              :         {
    7232        43985 :           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      1516916 :     case RESULT_DECL:
    7242      1516916 :     case PARM_DECL:
    7243      1516916 :       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        16084 :           tree val = tree_node ();
    7249        16084 :           SET_DECL_VALUE_EXPR (t, val);
    7250              :         }
    7251              :       /* FALLTHROUGH  */
    7252              : 
    7253      1562047 :     case CONST_DECL:
    7254      1562047 :     case IMPORTED_DECL:
    7255      1562047 :       RT (t->decl_common.initial);
    7256      1562047 :       break;
    7257              : 
    7258        57399 :     case FIELD_DECL:
    7259        57399 :       RT (t->field_decl.offset);
    7260        57399 :       RT (t->field_decl.bit_field_type);
    7261        57399 :       RT (t->field_decl.qualifier);
    7262        57399 :       RT (t->field_decl.bit_offset);
    7263        57399 :       RT (t->field_decl.fcontext);
    7264        57399 :       RT (t->decl_common.initial);
    7265        57399 :       break;
    7266              : 
    7267        19783 :     case LABEL_DECL:
    7268        19783 :       RU (t->label_decl.label_decl_uid);
    7269        19783 :       RU (t->label_decl.eh_landing_pad_nr);
    7270        19783 :       break;
    7271              : 
    7272       465214 :     case FUNCTION_DECL:
    7273       465214 :       {
    7274       465214 :         unsigned bltin = u ();
    7275       465214 :         t->function_decl.built_in_class = built_in_class (bltin);
    7276       465214 :         if (bltin != NOT_BUILT_IN)
    7277              :           {
    7278         9489 :             bltin = u ();
    7279         9489 :             DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
    7280              :           }
    7281              : 
    7282       465214 :         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       465214 :         RT (t->function_decl.vindex);
    7288              : 
    7289       465214 :         if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    7290              :           {
    7291         3625 :             tree spec;
    7292         3625 :             RT (spec);
    7293         3625 :             store_explicit_specifier (t, spec);
    7294              :           }
    7295              :       }
    7296              :       break;
    7297              : 
    7298        51962 :     case USING_DECL:
    7299              :       /* USING_DECL_DECLS  */
    7300        51962 :       RT (t->decl_common.initial);
    7301              :       /* FALLTHROUGH  */
    7302              : 
    7303       836235 :     case TYPE_DECL:
    7304              :       /* USING_DECL: USING_DECL_SCOPE  */
    7305              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    7306       836235 :       RT (t->decl_non_common.result);
    7307       836235 :       break;
    7308              : 
    7309              :       /* Miscellaneous common nodes.  */
    7310       314132 :     case BLOCK:
    7311       314132 :       t->block.locus = state->read_location (*this);
    7312       314132 :       t->block.end_locus = state->read_location (*this);
    7313              : 
    7314       314132 :       for (tree *chain = &t->block.vars;;)
    7315       477064 :         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       162932 :             decl = maybe_duplicate (decl);
    7324              : 
    7325       162932 :             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       162932 :             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       162932 :             *chain = decl;
    7343       162932 :             chain = &DECL_CHAIN (decl);
    7344              :           }
    7345              :         else
    7346       162932 :           break;
    7347              : 
    7348              :       /* nonlocalized_vars is middle-end.  */
    7349       314132 :       RT (t->block.subblocks);
    7350       314132 :       RT (t->block.supercontext);
    7351       314132 :       RT (t->block.abstract_origin);
    7352              :       /* fragment_origin, fragment_chain are middle-end.  */
    7353       314132 :       RT (t->block.chain);
    7354              :       /* nonlocalized_vars, block_num, die are middle endy/debug
    7355              :          things.  */
    7356       314132 :       break;
    7357              : 
    7358       591593 :     case CALL_EXPR:
    7359       591593 :       RUC (internal_fn, t->base.u.ifn);
    7360       591593 :       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       273935 :     case STATEMENT_LIST:
    7378       273935 :       {
    7379       273935 :         tree_stmt_iterator iter = tsi_start (t);
    7380      1414996 :         for (tree stmt; RT (stmt);)
    7381              :           {
    7382       867126 :             if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
    7383       148716 :                 && !MAY_HAVE_DEBUG_MARKER_STMTS)
    7384            0 :               continue;
    7385       867126 :             tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
    7386              :           }
    7387              :       }
    7388       273935 :       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        94375 :       break;
    7395              : 
    7396        94375 :     case TREE_BINFO:
    7397        94375 :       RT (t->binfo.common.chain);
    7398        94375 :       RT (t->binfo.offset);
    7399        94375 :       RT (t->binfo.inheritance);
    7400        94375 :       RT (t->binfo.vptr_field);
    7401              : 
    7402              :       /* Do not mark the vtables as USED in the address expressions
    7403              :          here.  */
    7404        94375 :       unused++;
    7405        94375 :       RT (t->binfo.vtable);
    7406        94375 :       RT (t->binfo.virtuals);
    7407        94375 :       RT (t->binfo.vtt_subvtt);
    7408        94375 :       RT (t->binfo.vtt_vptr);
    7409        94375 :       unused--;
    7410              : 
    7411        94375 :       BINFO_BASE_ACCESSES (t) = tree_vec ();
    7412        94375 :       if (!get_overrun ())
    7413              :         {
    7414        94375 :           unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    7415       120217 :           for (unsigned ix = 0; ix != num; ix++)
    7416        25842 :             BINFO_BASE_APPEND (t, tree_node ());
    7417              :         }
    7418              :       break;
    7419              : 
    7420      1341975 :     case TREE_LIST:
    7421      1341975 :       RT (t->list.purpose);
    7422      1341975 :       RT (t->list.value);
    7423      1341975 :       RT (t->list.common.chain);
    7424      1341975 :       break;
    7425              : 
    7426       848027 :     case TREE_VEC:
    7427      2282315 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    7428      1434288 :         RT (TREE_VEC_ELT (t, ix));
    7429       848027 :       RT (t->type_common.common.chain);
    7430       848027 :       break;
    7431              : 
    7432              :       /* C++-specific nodes ...  */
    7433       105428 :     case BASELINK:
    7434       105428 :       RT (((lang_tree_node *)t)->baselink.binfo);
    7435       105428 :       RTU (((lang_tree_node *)t)->baselink.functions);
    7436       105428 :       RT (((lang_tree_node *)t)->baselink.access_binfo);
    7437       105428 :       RT (((lang_tree_node *)t)->baselink.common.chain);
    7438       105428 :       break;
    7439              : 
    7440        45144 :     case CONSTRAINT_INFO:
    7441        45144 :       RT (((lang_tree_node *)t)->constraint_info.template_reqs);
    7442        45144 :       RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    7443        45144 :       RT (((lang_tree_node *)t)->constraint_info.associated_constr);
    7444        45144 :       break;
    7445              : 
    7446         7449 :     case DEFERRED_NOEXCEPT:
    7447         7449 :       RT (((lang_tree_node *)t)->deferred_noexcept.pattern);
    7448         7449 :       RT (((lang_tree_node *)t)->deferred_noexcept.args);
    7449         7449 :       break;
    7450              : 
    7451         4709 :     case LAMBDA_EXPR:
    7452         4709 :       RT (((lang_tree_node *)t)->lambda_expression.capture_list);
    7453         4709 :       RT (((lang_tree_node *)t)->lambda_expression.this_capture);
    7454         4709 :       RT (((lang_tree_node *)t)->lambda_expression.extra_scope);
    7455         4709 :       RT (((lang_tree_node *)t)->lambda_expression.regen_info);
    7456         4709 :       RT (((lang_tree_node *)t)->lambda_expression.extra_args);
    7457              :       /* lambda_expression.pending_proxies is NULL  */
    7458         4709 :       ((lang_tree_node *)t)->lambda_expression.locus
    7459         4709 :         = state->read_location (*this);
    7460         4709 :       RUC (cp_lambda_default_capture_mode_type,
    7461              :            ((lang_tree_node *)t)->lambda_expression.default_capture_mode);
    7462         4709 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_scope);
    7463         4709 :       RU (((lang_tree_node *)t)->lambda_expression.discriminator_sig);
    7464         4709 :       break;
    7465              : 
    7466       575928 :     case OVERLOAD:
    7467       575928 :       RT (((lang_tree_node *)t)->overload.function);
    7468       575928 :       RT (t->common.chain);
    7469       575928 :       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         8367 :     case STATIC_ASSERT:
    7477         8367 :       RT (((lang_tree_node *)t)->static_assertion.condition);
    7478         8367 :       RT (((lang_tree_node *)t)->static_assertion.message);
    7479         8367 :       ((lang_tree_node *)t)->static_assertion.location
    7480         8367 :         = state->read_location (*this);
    7481         8367 :       break;
    7482              : 
    7483       329653 :     case TEMPLATE_DECL:
    7484              :       /* Streamed when reading the raw template decl itself.  */
    7485       329653 :       gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
    7486       329653 :       gcc_assert (((lang_tree_node *)t)->template_decl.result);
    7487       329653 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    7488         6942 :         RT (DECL_CHAIN (t));
    7489              :       break;
    7490              : 
    7491       738077 :     case TEMPLATE_INFO:
    7492       738077 :       RT (((lang_tree_node *)t)->template_info.tmpl);
    7493       738077 :       RT (((lang_tree_node *)t)->template_info.args);
    7494       738077 :       RT (((lang_tree_node *)t)->template_info.partial);
    7495       738077 :       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       414898 :     case TEMPLATE_PARM_INDEX:
    7514       414898 :       RU (((lang_tree_node *)t)->tpi.index);
    7515       414898 :       RU (((lang_tree_node *)t)->tpi.level);
    7516       414898 :       RU (((lang_tree_node *)t)->tpi.orig_level);
    7517       414898 :       RT (((lang_tree_node *)t)->tpi.decl);
    7518       414898 :       break;
    7519              : 
    7520         8524 :     case TRAIT_EXPR:
    7521         8524 :       RT (((lang_tree_node *)t)->trait_expression.type1);
    7522         8524 :       RT (((lang_tree_node *)t)->trait_expression.type2);
    7523         8524 :       ((lang_tree_node *)t)->trait_expression.locus
    7524         8524 :         = state->read_location (*this);
    7525         8524 :       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
    7526         8524 :       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     15549518 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    7535              :     {
    7536     14436347 :       tree type = tree_node ();
    7537              : 
    7538     14436347 :       if (type && code == ENUMERAL_TYPE && !ENUM_FIXED_UNDERLYING_TYPE_P (t))
    7539              :         {
    7540         1827 :           unsigned precision = u ();
    7541              : 
    7542         1827 :           type = build_distinct_type_copy (type);
    7543         1827 :           TYPE_PRECISION (type) = precision;
    7544         3654 :           set_min_and_max_values_for_integral_type (type, precision,
    7545         1827 :                                                     TYPE_SIGN (type));
    7546              :         }
    7547              : 
    7548     14436347 :       if (code != TEMPLATE_DECL)
    7549     14106694 :         t->typed.type = type;
    7550              :     }
    7551              : 
    7552     15549518 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7553        54202 :     if (unsigned len = u ())
    7554              :       {
    7555        32274 :         vec_alloc (t->constructor.elts, len);
    7556       239875 :         for (unsigned ix = 0; ix != len; ix++)
    7557              :           {
    7558       175327 :             constructor_elt elt;
    7559              : 
    7560       175327 :             RT (elt.index);
    7561       175327 :             RTU (elt.value);
    7562       175327 :             t->constructor.elts->quick_push (elt);
    7563              :           }
    7564              :       }
    7565              : 
    7566              : #undef RT
    7567              : #undef RM
    7568              : #undef RU
    7569     15549518 :   return !get_overrun ();
    7570              : }
    7571              : 
    7572              : void
    7573      6859043 : trees_out::lang_decl_vals (tree t)
    7574              : {
    7575      6859043 :   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      6859043 :   switch (lang->u.base.selector)
    7580              :     {
    7581            0 :     default:
    7582            0 :       gcc_unreachable ();
    7583              : 
    7584      1360380 :     case lds_fn:  /* lang_decl_fn.  */
    7585      1360380 :       if (streaming_p ())
    7586              :         {
    7587       680083 :           if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7588       118559 :             WU (lang->u.fn.ovl_op_code);
    7589              :         }
    7590              : 
    7591      1687084 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7592      1069768 :         WT (lang->u.fn.context);
    7593              : 
    7594      1360380 :       if (lang->u.fn.thunk_p)
    7595              :         {
    7596              :           /* The thunked-to function.  */
    7597         1944 :           WT (lang->u.fn.befriending_classes);
    7598         1944 :           if (streaming_p ())
    7599          972 :             wi (lang->u.fn.u5.fixed_offset);
    7600              :         }
    7601      1358436 :       else if (decl_tls_wrapper_p (t))
    7602              :         /* The wrapped variable.  */
    7603           18 :         WT (lang->u.fn.befriending_classes);
    7604              :       else
    7605      1358418 :         WT (lang->u.fn.u5.cloned_function);
    7606              : 
    7607      1360380 :       if (FNDECL_USED_AUTO (t))
    7608         5671 :         WT (lang->u.fn.u.saved_auto_return_type);
    7609              : 
    7610      1360380 :       goto lds_min;
    7611              : 
    7612         7246 :     case lds_decomp:  /* lang_decl_decomp.  */
    7613         7246 :       WT (lang->u.decomp.base);
    7614         7246 :       goto lds_min;
    7615              : 
    7616      3926203 :     case lds_min:  /* lang_decl_min.  */
    7617      3926203 :     lds_min:
    7618      3926203 :       WT (lang->u.min.template_info);
    7619      3926203 :       {
    7620      3926203 :         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      1360380 :         if (!DECL_THUNK_P (t)
    7626      5284639 :             && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
    7627              :           access = NULL_TREE;
    7628              : 
    7629      3926203 :         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      3926203 :       if (decl_specialization_friend_p (t))
    7636          216 :         WT (t->common.chain);
    7637              :       break;
    7638              : 
    7639              :     case lds_ns:  /* lang_decl_ns.  */
    7640              :       break;
    7641              : 
    7642      2932467 :     case lds_parm:  /* lang_decl_parm.  */
    7643      2932467 :       if (streaming_p ())
    7644              :         {
    7645       997706 :           WU (lang->u.parm.level);
    7646       997706 :           WU (lang->u.parm.index);
    7647              :         }
    7648              :       break;
    7649              :     }
    7650              : #undef WU
    7651              : #undef WT
    7652      6859043 : }
    7653              : 
    7654              : bool
    7655      2026684 : trees_in::lang_decl_vals (tree t)
    7656              : {
    7657      2026684 :   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      2026684 :   switch (lang->u.base.selector)
    7663              :     {
    7664            0 :     default:
    7665            0 :       gcc_unreachable ();
    7666              : 
    7667       465214 :     case lds_fn:  /* lang_decl_fn.  */
    7668       465214 :       if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7669              :         {
    7670        80123 :           unsigned code = u ();
    7671              : 
    7672              :           /* Check consistency.  */
    7673        80123 :           if (code >= OVL_OP_MAX
    7674        80123 :               || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
    7675        80123 :                   .ovl_op_code) == OVL_OP_ERROR_MARK)
    7676            0 :             set_overrun ();
    7677              :           else
    7678        80123 :             lang->u.fn.ovl_op_code = code;
    7679              :         }
    7680              : 
    7681       577422 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7682       366082 :         RT (lang->u.fn.context);
    7683              : 
    7684       465214 :       if (lang->u.fn.thunk_p)
    7685              :         {
    7686          598 :           RT (lang->u.fn.befriending_classes);
    7687          598 :           lang->u.fn.u5.fixed_offset = wi ();
    7688              :         }
    7689       464616 :       else if (decl_tls_wrapper_p (t))
    7690           15 :         RT (lang->u.fn.befriending_classes);
    7691              :       else
    7692       464601 :         RT (lang->u.fn.u5.cloned_function);
    7693              : 
    7694       465214 :       if (FNDECL_USED_AUTO (t))
    7695         1721 :         RT (lang->u.fn.u.saved_auto_return_type);
    7696       465214 :       goto lds_min;
    7697              : 
    7698         2911 :     case lds_decomp:  /* lang_decl_decomp.  */
    7699         2911 :       RT (lang->u.decomp.base);
    7700         2911 :       goto lds_min;
    7701              : 
    7702      1339744 :     case lds_min:  /* lang_decl_min.  */
    7703      1339744 :     lds_min:
    7704      1339744 :       RT (lang->u.min.template_info);
    7705      1339744 :       RT (lang->u.min.access);
    7706      1339744 :       if (decl_specialization_friend_p (t))
    7707           70 :         RT (t->common.chain);
    7708              :       break;
    7709              : 
    7710              :     case lds_ns:  /* lang_decl_ns.  */
    7711              :       break;
    7712              : 
    7713       686796 :     case lds_parm:  /* lang_decl_parm.  */
    7714       686796 :       RU (lang->u.parm.level);
    7715       686796 :       RU (lang->u.parm.index);
    7716       686796 :       break;
    7717              :     }
    7718              : #undef RU
    7719              : #undef RT
    7720      2026684 :   return !get_overrun ();
    7721              : }
    7722              : 
    7723              : /* Most of the value contents of lang_type is streamed in
    7724              :    define_class.  */
    7725              : 
    7726              : void
    7727       468084 : trees_out::lang_type_vals (tree t)
    7728              : {
    7729       468084 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7730              : #define WU(X) (u (X))
    7731              : #define WT(X) (tree_node (X))
    7732       468084 :   if (streaming_p ())
    7733       234019 :     WU (lang->align);
    7734              : #undef WU
    7735              : #undef WT
    7736       468084 : }
    7737              : 
    7738              : bool
    7739       153757 : trees_in::lang_type_vals (tree t)
    7740              : {
    7741       153757 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7742              : #define RU(X) ((X) = u ())
    7743              : #define RT(X) ((X) = tree_node ())
    7744       153757 :   RU (lang->align);
    7745              : #undef RU
    7746              : #undef RT
    7747       153757 :   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     22852798 : trees_out::tree_node_bools (tree t)
    7756              : {
    7757     22852798 :   gcc_checking_assert (streaming_p ());
    7758              : 
    7759              :   /* We should never stream a namespace.  */
    7760     22852798 :   gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
    7761              :                        || DECL_NAMESPACE_ALIAS (t));
    7762              : 
    7763     22852798 :   bits_out bits = stream_bits ();
    7764     22852798 :   core_bools (t, bits);
    7765              : 
    7766     22852798 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7767              :     {
    7768      4914373 :     case tcc_declaration:
    7769      4914373 :       {
    7770      4914373 :         bool specific = DECL_LANG_SPECIFIC (t) != NULL;
    7771      4914373 :         bits.b (specific);
    7772      4914373 :         if (specific && VAR_P (t))
    7773       387128 :           bits.b (DECL_DECOMPOSITION_P (t));
    7774      2950364 :         if (specific)
    7775      2950364 :           lang_decl_bools (t, bits);
    7776              :       }
    7777              :       break;
    7778              : 
    7779       900374 :     case tcc_type:
    7780       900374 :       {
    7781       900374 :         bool specific = (TYPE_MAIN_VARIANT (t) == t
    7782       900374 :                          && TYPE_LANG_SPECIFIC (t) != NULL);
    7783       900374 :         gcc_assert (TYPE_LANG_SPECIFIC (t)
    7784              :                     == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
    7785              : 
    7786       900374 :         bits.b (specific);
    7787       900374 :         if (specific)
    7788       234019 :           lang_type_bools (t, bits);
    7789              :       }
    7790              :       break;
    7791              : 
    7792              :     default:
    7793              :       break;
    7794              :     }
    7795              : 
    7796     22852798 :   bits.bflush ();
    7797     22852798 : }
    7798              : 
    7799              : bool
    7800     15573287 : trees_in::tree_node_bools (tree t)
    7801              : {
    7802     15573287 :   bits_in bits = stream_bits ();
    7803     15573287 :   bool ok = core_bools (t, bits);
    7804              : 
    7805     15573287 :   if (ok)
    7806     15573287 :     switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7807              :       {
    7808      3320721 :       case tcc_declaration:
    7809      3320721 :         if (bits.b ())
    7810              :           {
    7811      2026684 :             bool decomp = VAR_P (t) && bits.b ();
    7812              : 
    7813      2026684 :             ok = maybe_add_lang_decl_raw (t, decomp);
    7814      2026684 :             if (ok)
    7815      2026684 :               ok = lang_decl_bools (t, bits);
    7816              :           }
    7817              :         break;
    7818              : 
    7819       590996 :       case tcc_type:
    7820       590996 :         if (bits.b ())
    7821              :           {
    7822       153757 :             ok = maybe_add_lang_type_raw (t);
    7823       153757 :             if (ok)
    7824       153757 :               ok = lang_type_bools (t, bits);
    7825              :           }
    7826              :         break;
    7827              : 
    7828              :       default:
    7829              :         break;
    7830              :       }
    7831              : 
    7832     15573287 :   bits.bflush ();
    7833     15573287 :   if (!ok || get_overrun ())
    7834            0 :     return false;
    7835              : 
    7836              :   return true;
    7837     15573287 : }
    7838              : 
    7839              : 
    7840              : /* Write out the lang-specific vals of node T.  */
    7841              : 
    7842              : void
    7843     59270213 : trees_out::lang_vals (tree t)
    7844              : {
    7845     59270213 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7846              :     {
    7847     12899358 :     case tcc_declaration:
    7848     12899358 :       if (DECL_LANG_SPECIFIC (t))
    7849      6859043 :         lang_decl_vals (t);
    7850              :       break;
    7851              : 
    7852      3176043 :     case tcc_type:
    7853      3176043 :       if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
    7854       468084 :         lang_type_vals (t);
    7855              :       break;
    7856              : 
    7857              :     default:
    7858              :       break;
    7859              :     }
    7860     59270213 : }
    7861              : 
    7862              : bool
    7863     15549518 : trees_in::lang_vals (tree t)
    7864              : {
    7865     15549518 :   bool ok = true;
    7866              : 
    7867     15549518 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7868              :     {
    7869      3320721 :     case tcc_declaration:
    7870      3320721 :       if (DECL_LANG_SPECIFIC (t))
    7871      2026684 :         ok = lang_decl_vals (t);
    7872              :       break;
    7873              : 
    7874       567227 :     case tcc_type:
    7875       567227 :       if (TYPE_LANG_SPECIFIC (t))
    7876       153757 :         ok = lang_type_vals (t);
    7877              :       else
    7878       413470 :         TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
    7879              :       break;
    7880              : 
    7881              :     default:
    7882              :       break;
    7883              :     }
    7884              : 
    7885     15549518 :   return ok;
    7886              : }
    7887              : 
    7888              : /* Write out the value fields of node T.  */
    7889              : 
    7890              : void
    7891     59270213 : trees_out::tree_node_vals (tree t)
    7892              : {
    7893     59270213 :   core_vals (t);
    7894     59270213 :   lang_vals (t);
    7895     59270213 : }
    7896              : 
    7897              : bool
    7898     15549518 : trees_in::tree_node_vals (tree t)
    7899              : {
    7900     15549518 :   bool ok = core_vals (t);
    7901     15549518 :   if (ok)
    7902     15549518 :     ok = lang_vals (t);
    7903              : 
    7904     15549518 :   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    386180224 : trees_out::ref_node (tree t)
    7914              : {
    7915    386180224 :   if (!t)
    7916              :     {
    7917    154530057 :       if (streaming_p ())
    7918              :         {
    7919              :           /* NULL_TREE -> tt_null.  */
    7920     58732044 :           null_count++;
    7921     58732044 :           i (tt_null);
    7922              :         }
    7923              :       return WK_none;
    7924              :     }
    7925              : 
    7926    231650167 :   if (!TREE_VISITED (t))
    7927              :     return WK_normal;
    7928              : 
    7929              :   /* An already-visited tree.  It must be in the map.  */
    7930    134929973 :   int val = get_tag (t);
    7931              : 
    7932    134929973 :   if (val == tag_value)
    7933              :     /* An entry we should walk into.  */
    7934              :     return WK_value;
    7935              : 
    7936    132890487 :   const char *kind;
    7937              : 
    7938    132890487 :   if (val <= tag_backref)
    7939              :     {
    7940              :       /* Back reference -> -ve number  */
    7941    101268337 :       if (streaming_p ())
    7942     47930870 :         i (val);
    7943              :       kind = "backref";
    7944              :     }
    7945     31622150 :   else if (val >= tag_fixed)
    7946              :     {
    7947              :       /* Fixed reference -> tt_fixed */
    7948     31622150 :       val -= tag_fixed;
    7949     31622150 :       if (streaming_p ())
    7950     11118185 :         i (tt_fixed), u (val);
    7951              :       kind = "fixed";
    7952              :     }
    7953              : 
    7954    132890487 :   if (streaming_p ())
    7955              :     {
    7956     59049055 :       back_ref_count++;
    7957     59049055 :       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     32543067 : trees_in::back_ref (int tag)
    7965              : {
    7966     32543067 :   tree res = NULL_TREE;
    7967              : 
    7968     32543067 :   if (tag < 0 && unsigned (~tag) < back_refs.length ())
    7969     32543067 :     res = back_refs[~tag];
    7970              : 
    7971     32543067 :   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     32543067 :       || TREE_CODE (res) >= MAX_TREE_CODES)
    7976            0 :     set_overrun ();
    7977              :   else
    7978     32557607 :     dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
    7979              :                                  TREE_CODE (res), res, res);
    7980     32543067 :   return res;
    7981              : }
    7982              : 
    7983              : unsigned
    7984      5556875 : trees_out::add_indirect_tpl_parms (tree parms)
    7985              : {
    7986      5556875 :   unsigned len = 0;
    7987     10470016 :   for (; parms; parms = TREE_CHAIN (parms), len++)
    7988              :     {
    7989      5884049 :       if (TREE_VISITED (parms))
    7990              :         break;
    7991              : 
    7992      4913141 :       int tag = insert (parms);
    7993      4913141 :       if (streaming_p ())
    7994      4913468 :         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      5556875 :   if (streaming_p ())
    8000       916028 :     u (len);
    8001              : 
    8002      5556875 :   return len;
    8003              : }
    8004              : 
    8005              : unsigned
    8006       559640 : trees_in::add_indirect_tpl_parms (tree parms)
    8007              : {
    8008       559640 :   unsigned len = u ();
    8009      1557659 :   for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
    8010              :     {
    8011       438379 :       int tag = insert (parms);
    8012       438859 :       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       559640 :   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     12175545 : trees_out::add_indirects (tree decl)
    8025              : {
    8026     12175545 :   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     12175545 :   tree inner = decl;
    8034     12175545 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8035              :     {
    8036      5556875 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8037              : 
    8038      5556875 :       inner = DECL_TEMPLATE_RESULT (decl);
    8039      5556875 :       int tag = insert (inner);
    8040      5556875 :       if (streaming_p ())
    8041       916028 :         dump (dumper::TREE)
    8042          219 :           && dump ("Indirect:%d template's result %C:%N",
    8043          219 :                    tag, TREE_CODE (inner), inner);
    8044      5556875 :       count++;
    8045              :     }
    8046              : 
    8047     12175545 :   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      6374888 :       tree type = TREE_TYPE (inner);
    8053      6374888 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8054              :                            || TYPE_NAME (type) == inner);
    8055      6374888 :       int tag = insert (type);
    8056      6374888 :       if (streaming_p ())
    8057       708965 :         dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
    8058          362 :                                      TREE_CODE (type), type);
    8059      6374888 :       count++;
    8060              :     }
    8061              : 
    8062     12175545 :   if (streaming_p ())
    8063              :     {
    8064      1846147 :       u (count);
    8065      1846676 :       dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8066              :     }
    8067     12175545 : }
    8068              : 
    8069              : bool
    8070      1133265 : trees_in::add_indirects (tree decl)
    8071              : {
    8072      1133265 :   unsigned count = 0;
    8073              : 
    8074      1133265 :   tree inner = decl;
    8075      1133265 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8076              :     {
    8077       559640 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8078              : 
    8079       559640 :       inner = DECL_TEMPLATE_RESULT (decl);
    8080       559640 :       int tag = insert (inner);
    8081       559640 :       dump (dumper::TREE)
    8082          228 :         && dump ("Indirect:%d templates's result %C:%N", tag,
    8083          228 :                  TREE_CODE (inner), inner);
    8084       559640 :       count++;
    8085              :     }
    8086              : 
    8087      1133265 :   if (TREE_CODE (inner) == TYPE_DECL)
    8088              :     {
    8089       435428 :       tree type = TREE_TYPE (inner);
    8090       435428 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8091              :                            || TYPE_NAME (type) == inner);
    8092       435428 :       int tag = insert (type);
    8093       435428 :       dump (dumper::TREE)
    8094          362 :         && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
    8095       435428 :       count++;
    8096              :     }
    8097              : 
    8098      1133870 :   dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8099      1133265 :   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      2937173 : trees_out::tpl_parm_value (tree parm)
    8117              : {
    8118      2937173 :   gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
    8119              : 
    8120      2937173 :   int parm_tag = insert (parm);
    8121      2937173 :   if (streaming_p ())
    8122              :     {
    8123       652130 :       i (tt_tpl_parm);
    8124       652130 :       dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
    8125          114 :                                    parm_tag, TREE_CODE (parm), parm);
    8126       652130 :       start (parm);
    8127       652130 :       tree_node_bools (parm);
    8128              :     }
    8129              : 
    8130      2937173 :   tree inner = parm;
    8131      2937173 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8132              :     {
    8133         7261 :       inner = DECL_TEMPLATE_RESULT (inner);
    8134         7261 :       int inner_tag = insert (inner);
    8135         7261 :       if (streaming_p ())
    8136              :         {
    8137         1914 :           dump (dumper::TREE) && dump ("Writing inner template parm:%d %C:%N",
    8138            0 :                                        inner_tag, TREE_CODE (inner), inner);
    8139         1914 :           start (inner);
    8140         1914 :           tree_node_bools (inner);
    8141              :         }
    8142              :     }
    8143              : 
    8144      2937173 :   tree type = NULL_TREE;
    8145      2937173 :   if (TREE_CODE (inner) == TYPE_DECL)
    8146              :     {
    8147      2631517 :       type = TREE_TYPE (inner);
    8148      2631517 :       int type_tag = insert (type);
    8149      2631517 :       if (streaming_p ())
    8150              :         {
    8151       589069 :           dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
    8152          108 :                                        type_tag, TREE_CODE (type), type);
    8153       589069 :           start (type);
    8154       589069 :           tree_node_bools (type);
    8155              :         }
    8156              :     }
    8157              : 
    8158      2937173 :   if (inner != parm)
    8159              :     {
    8160              :       /* This is a template-template parameter.  */
    8161         7261 :       unsigned tpl_levels = 0;
    8162         7261 :       tpl_header (parm, &tpl_levels);
    8163         7261 :       tpl_parms_fini (parm, tpl_levels);
    8164              :     }
    8165              : 
    8166      2937173 :   tree_node_vals (parm);
    8167      2937173 :   if (inner != parm)
    8168         7261 :     tree_node_vals (inner);
    8169      2937173 :   if (type)
    8170              :     {
    8171      2631517 :       tree_node_vals (type);
    8172      2631517 :       if (DECL_NAME (inner) == auto_identifier
    8173      2631517 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8174              :         {
    8175              :           /* Placeholder auto.  */
    8176       124812 :           tree_node (DECL_INITIAL (inner));
    8177       124812 :           tree_node (DECL_SIZE_UNIT (inner));
    8178              :         }
    8179              :     }
    8180              : 
    8181      2937173 :   if (streaming_p ())
    8182       652130 :     dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
    8183          114 :                                  parm_tag, TREE_CODE (parm), parm);
    8184      2937173 : }
    8185              : 
    8186              : tree
    8187       432987 : trees_in::tpl_parm_value ()
    8188              : {
    8189       432987 :   tree parm = start ();
    8190       432987 :   if (!parm || !tree_node_bools (parm))
    8191              :     return NULL_TREE;
    8192              : 
    8193       432987 :   int parm_tag = insert (parm);
    8194       432987 :   dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
    8195          198 :                                parm_tag, TREE_CODE (parm), parm);
    8196              : 
    8197       432987 :   tree inner = parm;
    8198       432987 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8199              :     {
    8200         1267 :       inner = start ();
    8201         1267 :       if (!inner || !tree_node_bools (inner))
    8202              :         return NULL_TREE;
    8203         1267 :       int inner_tag = insert (inner);
    8204         1267 :       dump (dumper::TREE) && dump ("Reading inner template parm:%d %C:%N",
    8205            0 :                                    inner_tag, TREE_CODE (inner), inner);
    8206         1267 :       DECL_TEMPLATE_RESULT (parm) = inner;
    8207              :     }
    8208              : 
    8209       432987 :   tree type = NULL_TREE;
    8210       432987 :   if (TREE_CODE (inner) == TYPE_DECL)
    8211              :     {
    8212       389554 :       type = start ();
    8213       389554 :       if (!type || !tree_node_bools (type))
    8214              :         return NULL_TREE;
    8215       389554 :       int type_tag = insert (type);
    8216       389554 :       dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
    8217          120 :                                    type_tag, TREE_CODE (type), type);
    8218              : 
    8219       389554 :       TREE_TYPE (inner) = TREE_TYPE (parm) = type;
    8220       389554 :       TYPE_NAME (type) = parm;
    8221              :     }
    8222              : 
    8223       432987 :   if (inner != parm)
    8224              :     {
    8225              :       /* A template template parameter.  */
    8226         1267 :       unsigned tpl_levels = 0;
    8227         1267 :       tpl_header (parm, &tpl_levels);
    8228         1267 :       tpl_parms_fini (parm, tpl_levels);
    8229              :     }
    8230              : 
    8231       432987 :   tree_node_vals (parm);
    8232       432987 :   if (inner != parm)
    8233         1267 :     tree_node_vals (inner);
    8234       432987 :   if (type)
    8235              :     {
    8236       389554 :       tree_node_vals (type);
    8237       389554 :       if (DECL_NAME (inner) == auto_identifier
    8238       389554 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8239              :         {
    8240              :           /* Placeholder auto.  */
    8241        33833 :           DECL_INITIAL (inner) = tree_node ();
    8242        33833 :           DECL_SIZE_UNIT (inner) = tree_node ();
    8243              :         }
    8244       389554 :       if (TYPE_CANONICAL (type))
    8245              :         {
    8246       389554 :           gcc_checking_assert (TYPE_CANONICAL (type) == type);
    8247       389554 :           TYPE_CANONICAL (type) = canonical_type_parameter (type);
    8248              :         }
    8249              :     }
    8250              : 
    8251       432987 :   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      1847503 : trees_out::install_entity (tree decl, depset *dep)
    8259              : {
    8260      1847503 :   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      1847503 :   u (dep ? dep->cluster + 1 : 0);
    8265      1847503 :   if (CHECKING_P && dep)
    8266              :     {
    8267              :       /* Add it to the entity map, such that we can tell it is
    8268              :          part of us.  */
    8269      1331526 :       bool existed;
    8270      1331526 :       unsigned *slot = &entity_map->get_or_insert
    8271      1331526 :         (DECL_UID (decl), &existed);
    8272      1331526 :       if (existed)
    8273              :         /* If it existed, it should match.  */
    8274         1416 :         gcc_checking_assert (decl == (*entity_ary)[*slot]);
    8275      1331526 :       *slot = ~dep->cluster;
    8276              :     }
    8277      1847503 : }
    8278              : 
    8279              : bool
    8280      1229465 : trees_in::install_entity (tree decl)
    8281              : {
    8282      1229465 :   unsigned entity_index = u ();
    8283      1229465 :   if (!entity_index)
    8284              :     return false;
    8285              : 
    8286       886523 :   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       886523 :   unsigned ident = state->entity_lwm + entity_index - 1;
    8294       886523 :   (*entity_ary)[ident] = decl;
    8295              : 
    8296              :   /* And into the entity map, if it's not already there.  */
    8297       886523 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    8298       886523 :   if (!DECL_LANG_SPECIFIC (not_tmpl)
    8299      1650051 :       || !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       885378 :       if (!DECL_LANG_SPECIFIC (not_tmpl))
    8304              :         {
    8305       122995 :           maybe_add_lang_decl_raw (not_tmpl, false);
    8306       122995 :           SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
    8307              :         }
    8308       885378 :       DECL_MODULE_ENTITY_P (not_tmpl) = true;
    8309              : 
    8310              :       /* Insert into the entity hash (it cannot already be there).  */
    8311       885378 :       bool existed;
    8312       885378 :       unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
    8313       885378 :       gcc_checking_assert (!existed);
    8314       885378 :       slot = ident;
    8315              :     }
    8316              :   else
    8317              :     {
    8318         1145 :       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         1145 :       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         1145 :       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      5052112 : trees_out::decl_value (tree decl, depset *dep)
    8359              : {
    8360              :   /* We should not be writing clones or template parms.  */
    8361      5052112 :   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      5052112 :   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      5052112 :   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      5051696 :   merge_kind mk = get_merge_kind (decl, dep);
    8382              : 
    8383      5051696 :   if (CHECKING_P)
    8384              :     {
    8385              :       /* Never start in the middle of a template.  */
    8386      5051696 :       int use_tpl = -1;
    8387      5051696 :       if (tree ti = node_template_info (decl, use_tpl))
    8388      1768830 :         gcc_checking_assert (TREE_CODE (TI_TEMPLATE (ti)) == OVERLOAD
    8389              :                              || TREE_CODE (TI_TEMPLATE (ti)) == IDENTIFIER_NODE
    8390              :                              || TREE_CODE (TI_TEMPLATE (ti)) == FIELD_DECL
    8391              :                              || (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti))
    8392              :                                  != decl));
    8393              :     }
    8394              : 
    8395      5051696 :   if (streaming_p ())
    8396              :     {
    8397              :       /* A new node -> tt_decl.  */
    8398      1847503 :       decl_val_count++;
    8399      1847503 :       i (tt_decl);
    8400      1847503 :       u (mk);
    8401      1847503 :       start (decl);
    8402              : 
    8403      1847503 :       if (mk != MK_unique)
    8404              :         {
    8405      1514032 :           bits_out bits = stream_bits ();
    8406      1514032 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8407              :             {
    8408              :               /* Tell the importer whether this is a global module entity,
    8409              :                  or a module entity.  */
    8410       402219 :               tree o = get_originating_module_decl (decl);
    8411       402219 :               bool is_attached = false;
    8412              : 
    8413       402219 :               tree not_tmpl = STRIP_TEMPLATE (o);
    8414       402219 :               if (DECL_LANG_SPECIFIC (not_tmpl)
    8415       641856 :                   && DECL_MODULE_ATTACH_P (not_tmpl))
    8416              :                 is_attached = true;
    8417              : 
    8418       402219 :               bits.b (is_attached);
    8419              :             }
    8420      1514032 :           bits.b (dep && dep->has_defn ());
    8421      1514032 :         }
    8422      1847503 :       tree_node_bools (decl);
    8423              :     }
    8424              : 
    8425      5051696 :   int tag = insert (decl, WK_value);
    8426      5051696 :   if (streaming_p ())
    8427      1847503 :     dump (dumper::TREE)
    8428          683 :       && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], tag,
    8429          683 :                TREE_CODE (decl), decl, decl);
    8430              : 
    8431      5051696 :   tree inner = decl;
    8432      5051696 :   int inner_tag = 0;
    8433      5051696 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8434              :     {
    8435      1449457 :       inner = DECL_TEMPLATE_RESULT (decl);
    8436      1449457 :       inner_tag = insert (inner, WK_value);
    8437              : 
    8438              :       /* On stream-in we assume that a template and its result will
    8439              :          have the same type.  */
    8440      1449457 :       gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
    8441              : 
    8442      1449457 :       if (streaming_p ())
    8443              :         {
    8444       483137 :           int code = TREE_CODE (inner);
    8445       483137 :           u (code);
    8446       483137 :           start (inner, true);
    8447       483137 :           tree_node_bools (inner);
    8448       483137 :           dump (dumper::TREE)
    8449          132 :             && dump ("Writing %s:%d %C:%N%S", merge_kind_name[mk], inner_tag,
    8450          132 :                      TREE_CODE (inner), inner, inner);
    8451              :         }
    8452              :     }
    8453              : 
    8454      5051696 :   tree type = NULL_TREE;
    8455      5051696 :   int type_tag = 0;
    8456      5051696 :   tree stub_decl = NULL_TREE;
    8457      5051696 :   int stub_tag = 0;
    8458      5051696 :   if (TREE_CODE (inner) == TYPE_DECL)
    8459              :     {
    8460      1792057 :       type = TREE_TYPE (inner);
    8461      1792057 :       bool has_type = (type == TYPE_MAIN_VARIANT (type)
    8462      1792057 :                        && TYPE_NAME (type) == inner);
    8463              : 
    8464      1792057 :       if (streaming_p ())
    8465       605532 :         u (has_type ? (unsigned) TREE_CODE (type) : 0);
    8466              : 
    8467      1792057 :       if (has_type)
    8468              :         {
    8469       816710 :           type_tag = insert (type, WK_value);
    8470       816710 :           if (streaming_p ())
    8471              :             {
    8472       272212 :               start (type, true);
    8473       272212 :               tree_node_bools (type);
    8474       272212 :               dump (dumper::TREE)
    8475          155 :                 && dump ("Writing type:%d %C:%N", type_tag,
    8476          155 :                          TREE_CODE (type), type);
    8477              :             }
    8478              : 
    8479       816710 :           stub_decl = TYPE_STUB_DECL (type);
    8480       816710 :           bool has_stub = inner != stub_decl;
    8481       816710 :           if (streaming_p ())
    8482       272212 :             u (has_stub ? (unsigned) TREE_CODE (stub_decl) : 0);
    8483       816710 :           if (has_stub)
    8484              :             {
    8485         2397 :               stub_tag = insert (stub_decl);
    8486         2397 :               if (streaming_p ())
    8487              :                 {
    8488          798 :                   start (stub_decl, true);
    8489          798 :                   tree_node_bools (stub_decl);
    8490          798 :                   dump (dumper::TREE)
    8491            0 :                     && dump ("Writing stub_decl:%d %C:%N", stub_tag,
    8492            0 :                              TREE_CODE (stub_decl), stub_decl);
    8493              :                 }
    8494              :             }
    8495              :           else
    8496              :             stub_decl = NULL_TREE;
    8497              :         }
    8498              :       else
    8499              :         /* Regular typedef.  */
    8500              :         type = NULL_TREE;
    8501              :     }
    8502              : 
    8503              :   /* Stream the container, we want it correctly canonicalized before
    8504              :      we start emitting keys for this decl.  */
    8505      5051696 :   tree container = decl_container (decl);
    8506      5051696 :   unsigned tpl_levels = 0;
    8507              : 
    8508              :   /* Also tell the importer whether this is a temploid friend attached
    8509              :      to a different module (which has implications for merging), so that
    8510              :      importers can reconstruct this information on stream-in.  */
    8511      5051696 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8512              :     {
    8513      3831391 :       tree* temploid_friend_slot = imported_temploid_friends->get (decl);
    8514      3831391 :       gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
    8515      3831391 :       tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
    8516              :     }
    8517              : 
    8518      5051696 :   {
    8519      5051696 :     auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
    8520      5051696 :     if (decl != inner)
    8521      1449457 :       tpl_header (decl, &tpl_levels);
    8522      5051696 :     if (TREE_CODE (inner) == FUNCTION_DECL)
    8523      2039334 :       fn_parms_init (inner);
    8524              : 
    8525              :     /* Now write out the merging information, and then really
    8526              :        install the tag values.  */
    8527      5051696 :     key_mergeable (tag, mk, decl, inner, container, dep);
    8528              : 
    8529      5051696 :     if (streaming_p ())
    8530      5055595 :       dump (dumper::MERGE)
    8531         1101 :         && dump ("Wrote:%d's %s merge key %C:%N", tag,
    8532         1101 :                  merge_kind_name[mk], TREE_CODE (decl), decl);
    8533      5051696 :   }
    8534              : 
    8535      5051696 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8536      5051696 :     fn_parms_fini (inner);
    8537              : 
    8538      5051696 :   if (!is_key_order ())
    8539      3717656 :     tree_node_vals (decl);
    8540              : 
    8541      5051696 :   if (inner_tag)
    8542              :     {
    8543      1449457 :       if (!is_key_order ())
    8544       966320 :         tree_node_vals (inner);
    8545      1449457 :       tpl_parms_fini (decl, tpl_levels);
    8546              :     }
    8547              : 
    8548      5051696 :   if (type && !is_key_order ())
    8549              :     {
    8550       544498 :       tree_node_vals (type);
    8551       544498 :       if (stub_decl)
    8552         1599 :         tree_node_vals (stub_decl);
    8553              :     }
    8554              : 
    8555      5051696 :   if (!is_key_order ())
    8556              :     {
    8557      3717656 :       if (mk & MK_template_mask
    8558      2589576 :           || mk == MK_partial
    8559      2589576 :           || mk == MK_friend_spec)
    8560              :         {
    8561        42478 :           if (mk != MK_partial)
    8562              :             {
    8563              :               // FIXME: We should make use of the merge-key by
    8564              :               // exposing it outside of key_mergeable.  But this gets
    8565              :               // the job done.
    8566      1128080 :               auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
    8567              : 
    8568      1128080 :               if (streaming_p ())
    8569       564040 :                 u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
    8570              :                                                        entry->tmpl, decl));
    8571      1128080 :               tree_node (entry->tmpl);
    8572      1128080 :               tree_node (entry->args);
    8573              :             }
    8574              :           else
    8575              :             {
    8576        42478 :               tree ti = get_template_info (inner);
    8577        42478 :               tree_node (TI_TEMPLATE (ti));
    8578        42478 :               tree_node (TI_ARGS (ti));
    8579              :             }
    8580              :         }
    8581      3717656 :       tree_node (get_constraints (decl));
    8582              :     }
    8583              : 
    8584      5051696 :   if (streaming_p ())
    8585              :     {
    8586              :       /* Do not stray outside this section.  */
    8587      1847503 :       gcc_checking_assert (!dep || dep->section == dep_hash->section);
    8588              : 
    8589              :       /* Write the entity index, so we can insert it as soon as we
    8590              :          know this is new.  */
    8591      1847503 :       install_entity (decl, dep);
    8592              :     }
    8593              : 
    8594      5051696 :   if (DECL_LANG_SPECIFIC (inner)
    8595      4182086 :       && DECL_MODULE_KEYED_DECLS_P (inner)
    8596      5053037 :       && streaming_p ())
    8597              :     {
    8598              :       /* Stream the keyed entities.  There may be keyed entities that we
    8599              :          choose not to stream, such as a lambda in a non-inline variable's
    8600              :          initializer, so don't build dependencies for them here; any deps
    8601              :          we need should be acquired during write_definition (possibly
    8602              :          indirectly).  */
    8603          440 :       auto *attach_vec = keyed_table->get (inner);
    8604          440 :       unsigned num = attach_vec->length ();
    8605          440 :       u (num);
    8606         1358 :       for (unsigned ix = 0; ix != num; ix++)
    8607              :         {
    8608          478 :           tree attached = (*attach_vec)[ix];
    8609          478 :           if (attached)
    8610              :             {
    8611          478 :               tree ti = TYPE_TEMPLATE_INFO (TREE_TYPE (attached));
    8612          478 :               if (!dep_hash->find_dependency (attached)
    8613          478 :                   && !(ti && dep_hash->find_dependency (TI_TEMPLATE (ti))))
    8614              :                 attached = NULL_TREE;
    8615              :             }
    8616              : 
    8617          478 :           tree_node (attached);
    8618          520 :           dump (dumper::MERGE)
    8619           30 :             && dump ("Written %d[%u] attached decl %N", tag, ix, attached);
    8620              :         }
    8621              :     }
    8622              : 
    8623      5051696 :   bool is_typedef = false;
    8624      5051696 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8625              :     {
    8626       975347 :       tree t = TREE_TYPE (inner);
    8627       975347 :       unsigned tdef_flags = 0;
    8628       975347 :       if (DECL_ORIGINAL_TYPE (inner)
    8629       975347 :           && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8630              :         {
    8631       975347 :           tdef_flags |= 1;
    8632       975347 :           if (TYPE_STRUCTURAL_EQUALITY_P (t)
    8633       215749 :               && TYPE_DEPENDENT_P_VALID (t)
    8634      1181370 :               && TYPE_DEPENDENT_P (t))
    8635              :             tdef_flags |= 2;
    8636              :         }
    8637       975347 :       if (streaming_p ())
    8638       333320 :         u (tdef_flags);
    8639              : 
    8640       975347 :       if (tdef_flags & 1)
    8641              :         {
    8642              :           /* A typedef type.  */
    8643       975347 :           int type_tag = insert (t);
    8644       975347 :           if (streaming_p ())
    8645       333320 :             dump (dumper::TREE)
    8646          206 :               && dump ("Cloned:%d %s %C:%N", type_tag,
    8647              :                        tdef_flags & 2 ? "depalias" : "typedef",
    8648          206 :                        TREE_CODE (t), t);
    8649              : 
    8650              :           is_typedef = true;
    8651              :         }
    8652              :     }
    8653              : 
    8654      5051696 :   if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    8655              :     {
    8656       155220 :       bool cloned_p
    8657       155220 :         = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    8658       110461 :       bool needs_vtt_parm_p
    8659       110461 :         = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
    8660       110461 :       bool omit_inherited_parms_p
    8661       110461 :         = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
    8662        88576 :            && base_ctor_omit_inherited_parms (decl));
    8663       155220 :       unsigned flags = (int (cloned_p) << 0
    8664       155220 :                         | int (needs_vtt_parm_p) << 1
    8665       155220 :                         | int (omit_inherited_parms_p) << 2);
    8666       155220 :       u (flags);
    8667       155299 :       dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    8668              :                                    decl, cloned_p ? "" : "not ");
    8669              :     }
    8670              : 
    8671      5051696 :   if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    8672          176 :     u (decl_tls_model (decl));
    8673              : 
    8674      5051696 :   if (streaming_p ())
    8675      1847503 :     dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
    8676          683 :                                  TREE_CODE (decl), decl);
    8677              : 
    8678      5051696 :   if (NAMESPACE_SCOPE_P (inner))
    8679      2798924 :     gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
    8680              :                                   && DECL_LOCAL_DECL_P (inner)));
    8681      3652071 :   else if ((TREE_CODE (inner) == TYPE_DECL
    8682       970522 :             && !is_typedef
    8683       184901 :             && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8684      4437692 :            || TREE_CODE (inner) == FUNCTION_DECL)
    8685              :     {
    8686      1734378 :       bool write_defn = !dep && has_definition (decl);
    8687      1734378 :       if (streaming_p ())
    8688       578391 :         u (write_defn);
    8689      1734378 :       if (write_defn)
    8690            6 :         write_definition (decl);
    8691              :     }
    8692              : }
    8693              : 
    8694              : tree
    8695      1229465 : trees_in::decl_value ()
    8696              : {
    8697      1229465 :   int tag = 0;
    8698      1229465 :   bool is_attached = false;
    8699      1229465 :   bool has_defn = false;
    8700      1229465 :   unsigned mk_u = u ();
    8701      1229465 :   if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
    8702              :     {
    8703            0 :       set_overrun ();
    8704            0 :       return NULL_TREE;
    8705              :     }
    8706              : 
    8707      1229465 :   unsigned saved_unused = unused;
    8708      1229465 :   unused = 0;
    8709              : 
    8710      1229465 :   merge_kind mk = merge_kind (mk_u);
    8711              : 
    8712      1229465 :   tree decl = start ();
    8713      1229465 :   if (decl)
    8714              :     {
    8715      1229465 :       if (mk != MK_unique)
    8716              :         {
    8717      1008012 :           bits_in bits = stream_bits ();
    8718      1008012 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8719       184698 :             is_attached = bits.b ();
    8720              : 
    8721      1008012 :           has_defn = bits.b ();
    8722      1008012 :         }
    8723              : 
    8724      1229465 :       if (!tree_node_bools (decl))
    8725            0 :         decl = NULL_TREE;
    8726              :     }
    8727              : 
    8728              :   /* Insert into map.  */
    8729      1229465 :   tag = insert (decl);
    8730      1229465 :   if (decl)
    8731      1229465 :     dump (dumper::TREE)
    8732          964 :       && dump ("Reading:%d %C", tag, TREE_CODE (decl));
    8733              : 
    8734      1229465 :   tree inner = decl;
    8735      1229465 :   int inner_tag = 0;
    8736      1229465 :   if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
    8737              :     {
    8738       328386 :       int code = u ();
    8739       328386 :       inner = start (code);
    8740       328386 :       if (inner && tree_node_bools (inner))
    8741       328386 :         DECL_TEMPLATE_RESULT (decl) = inner;
    8742              :       else
    8743            0 :         decl = NULL_TREE;
    8744              : 
    8745       328386 :       inner_tag = insert (inner);
    8746       328386 :       if (decl)
    8747       328386 :         dump (dumper::TREE)
    8748          204 :           && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
    8749              :     }
    8750              : 
    8751      1229465 :   tree type = NULL_TREE;
    8752      1229465 :   int type_tag = 0;
    8753      1229465 :   tree stub_decl = NULL_TREE;
    8754      1229465 :   int stub_tag = 0;
    8755      1229465 :   if (decl && TREE_CODE (inner) == TYPE_DECL)
    8756              :     {
    8757       394265 :       if (unsigned type_code = u ())
    8758              :         {
    8759       177659 :           type = start (type_code);
    8760       177659 :           if (type && tree_node_bools (type))
    8761              :             {
    8762       177659 :               TREE_TYPE (inner) = type;
    8763       177659 :               TYPE_NAME (type) = inner;
    8764              :             }
    8765              :           else
    8766            0 :             decl = NULL_TREE;
    8767              : 
    8768       177659 :           type_tag = insert (type);
    8769       177659 :           if (decl)
    8770       177659 :             dump (dumper::TREE)
    8771          212 :               && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
    8772              : 
    8773       177659 :           if (unsigned stub_code = u ())
    8774              :             {
    8775          440 :               stub_decl = start (stub_code);
    8776          440 :               if (stub_decl && tree_node_bools (stub_decl))
    8777              :                 {
    8778          440 :                   TREE_TYPE (stub_decl) = type;
    8779          440 :                   TYPE_STUB_DECL (type) = stub_decl;
    8780              :                 }
    8781              :               else
    8782            0 :                 decl = NULL_TREE;
    8783              : 
    8784          440 :               stub_tag = insert (stub_decl);
    8785          440 :               if (decl)
    8786          440 :                 dump (dumper::TREE)
    8787            0 :                   && dump ("Reading stub_decl:%d %C", stub_tag,
    8788            0 :                            TREE_CODE (stub_decl));
    8789              :             }
    8790              :         }
    8791              :     }
    8792              : 
    8793      1229465 :   if (!decl)
    8794              :     {
    8795            0 :     bail:
    8796            0 :       if (inner_tag != 0)
    8797            0 :         back_refs[~inner_tag] = NULL_TREE;
    8798            0 :       if (type_tag != 0)
    8799            0 :         back_refs[~type_tag] = NULL_TREE;
    8800            0 :       if (stub_tag != 0)
    8801            0 :         back_refs[~stub_tag] = NULL_TREE;
    8802            0 :       if (tag != 0)
    8803            0 :         back_refs[~tag] = NULL_TREE;
    8804            0 :       set_overrun ();
    8805              :       /* Bail.  */
    8806            0 :       unused = saved_unused;
    8807            0 :       return NULL_TREE;
    8808              :     }
    8809              : 
    8810              :   /* Read the container, to ensure it's already been streamed in.  */
    8811      1229465 :   tree container = decl_container ();
    8812      1229465 :   unsigned tpl_levels = 0;
    8813              : 
    8814              :   /* If this is an imported temploid friend, get the owning decl its
    8815              :      attachment is determined by (or NULL_TREE otherwise).  */
    8816      1229465 :   tree temploid_friend = NULL_TREE;
    8817      1229465 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8818       859479 :     temploid_friend = tree_node ();
    8819              : 
    8820              :   /* Figure out if this decl is already known about.  */
    8821      1229465 :   int parm_tag = 0;
    8822              : 
    8823      1229465 :   if (decl != inner)
    8824       328386 :     if (!tpl_header (decl, &tpl_levels))
    8825            0 :       goto bail;
    8826      1229465 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8827       465214 :     parm_tag = fn_parms_init (inner);
    8828              : 
    8829      1229465 :   tree existing = key_mergeable (tag, mk, decl, inner, type, container,
    8830              :                                  is_attached, temploid_friend);
    8831      1229465 :   tree existing_inner = existing;
    8832      1229465 :   if (existing)
    8833              :     {
    8834       432754 :       if (existing == error_mark_node)
    8835            0 :         goto bail;
    8836              : 
    8837       432754 :       if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
    8838              :         {
    8839       160356 :           tree etype = TREE_TYPE (existing);
    8840       160356 :           if (TYPE_LANG_SPECIFIC (etype)
    8841       115622 :               && COMPLETE_TYPE_P (etype)
    8842       228060 :               && !CLASSTYPE_MEMBER_VEC (etype))
    8843              :             /* Give it a member vec, we're likely gonna be looking
    8844              :                inside it.  */
    8845        14016 :             set_class_bindings (etype, -1);
    8846              :         }
    8847              : 
    8848              :       /* Install the existing decl into the back ref array.  */
    8849       432754 :       register_duplicate (decl, existing);
    8850       432754 :       back_refs[~tag] = existing;
    8851       432754 :       if (inner_tag != 0)
    8852              :         {
    8853       143162 :           existing_inner = DECL_TEMPLATE_RESULT (existing);
    8854       143162 :           back_refs[~inner_tag] = existing_inner;
    8855              :         }
    8856              : 
    8857       432754 :       if (type_tag != 0)
    8858              :         {
    8859        76770 :           tree existing_type = TREE_TYPE (existing);
    8860        76770 :           back_refs[~type_tag] = existing_type;
    8861        76770 :           if (stub_tag != 0)
    8862          242 :             back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
    8863              :         }
    8864              :     }
    8865              : 
    8866      1229465 :   if (parm_tag)
    8867       465214 :     fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
    8868              : 
    8869      1229465 :   if (!tree_node_vals (decl))
    8870            0 :     goto bail;
    8871              : 
    8872      1229465 :   if (inner_tag)
    8873              :     {
    8874       328386 :       gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
    8875              : 
    8876       328386 :       if (!tree_node_vals (inner))
    8877            0 :         goto bail;
    8878              : 
    8879       328386 :       if (!tpl_parms_fini (decl, tpl_levels))
    8880            0 :         goto bail;
    8881              :     }
    8882              : 
    8883      1229465 :   if (type && (!tree_node_vals (type)
    8884       177659 :                || (stub_decl && !tree_node_vals (stub_decl))))
    8885            0 :     goto bail;
    8886              : 
    8887      1229465 :   spec_entry spec;
    8888      1229465 :   unsigned spec_flags = 0;
    8889      1229465 :   if (mk & MK_template_mask
    8890       853261 :       || mk == MK_partial
    8891       853261 :       || mk == MK_friend_spec)
    8892              :     {
    8893        12277 :       if (mk == MK_partial)
    8894              :         spec_flags = 2;
    8895              :       else
    8896       376204 :         spec_flags = u ();
    8897              : 
    8898       388481 :       spec.tmpl = tree_node ();
    8899       388481 :       spec.args = tree_node ();
    8900              :     }
    8901              :   /* Hold constraints on the spec field, for a short while.  */
    8902      1229465 :   spec.spec = tree_node ();
    8903              : 
    8904      1230429 :   dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
    8905              : 
    8906      1229465 :   existing = back_refs[~tag];
    8907      1229465 :   bool installed = install_entity (existing);
    8908      1229465 :   bool is_new = existing == decl;
    8909              : 
    8910      1229465 :   if (DECL_LANG_SPECIFIC (inner)
    8911      2280966 :       && DECL_MODULE_KEYED_DECLS_P (inner))
    8912              :     {
    8913              :       /* Read and maybe install the attached entities.  */
    8914          354 :       bool existed;
    8915          354 :       auto &set = keyed_table->get_or_insert (STRIP_TEMPLATE (existing),
    8916              :                                               &existed);
    8917          354 :       unsigned num = u ();
    8918          354 :       if (is_new == existed)
    8919            0 :         set_overrun ();
    8920          354 :       if (is_new)
    8921          221 :         set.reserve (num);
    8922          756 :       for (unsigned ix = 0; !get_overrun () && ix != num; ix++)
    8923              :         {
    8924          402 :           tree attached = tree_node ();
    8925          402 :           dump (dumper::MERGE)
    8926          105 :             && dump ("Read %d[%u] %s attached decl %N", tag, ix,
    8927              :                      is_new ? "new" : "matched", attached);
    8928          402 :           if (is_new)
    8929          250 :             set.quick_push (attached);
    8930          152 :           else if (set[ix] != attached)
    8931              :             {
    8932            3 :               if (!set[ix] || !attached)
    8933              :                 /* One import left a hole for a lambda dep we chose not
    8934              :                    to stream, but another import chose to stream that lambda.
    8935              :                    Let's not error here: hopefully we'll complain later in
    8936              :                    is_matching_decl about whatever caused us to make a
    8937              :                    different decision.  */
    8938              :                 ;
    8939              :               else
    8940            0 :                 set_overrun ();
    8941              :             }
    8942              :         }
    8943              :     }
    8944              : 
    8945              :   /* Regular typedefs will have a NULL TREE_TYPE at this point.  */
    8946      1229465 :   unsigned tdef_flags = 0;
    8947      1229465 :   bool is_typedef = false;
    8948      1229465 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8949              :     {
    8950       216606 :       tdef_flags = u ();
    8951       216606 :       if (tdef_flags & 1)
    8952       216606 :         is_typedef = true;
    8953              :     }
    8954              : 
    8955      1229465 :   if (is_new)
    8956              :     {
    8957              :       /* A newly discovered node.  */
    8958       796711 :       if (TREE_CODE (decl) == FUNCTION_DECL && DECL_VIRTUAL_P (decl))
    8959              :         /* Mark this identifier as naming a virtual function --
    8960              :            lookup_overrides relies on this optimization.  */
    8961         6489 :         IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = true;
    8962              : 
    8963       796711 :       if (installed)
    8964              :         {
    8965              :           /* Mark the entity as imported.  */
    8966       506582 :           retrofit_lang_decl (inner);
    8967       506582 :           DECL_MODULE_IMPORT_P (inner) = true;
    8968              :         }
    8969              : 
    8970       796711 :       if (temploid_friend)
    8971           34 :         imported_temploid_friends->put (decl, temploid_friend);
    8972              : 
    8973       796711 :       if (spec.spec)
    8974        33389 :         set_constraints (decl, spec.spec);
    8975              : 
    8976       796711 :       if (TREE_CODE (decl) == INTEGER_CST && !TREE_OVERFLOW (decl))
    8977              :         {
    8978            0 :           decl = cache_integer_cst (decl, true);
    8979            0 :           back_refs[~tag] = decl;
    8980              :         }
    8981              : 
    8982       796711 :       if (is_typedef)
    8983              :         {
    8984              :           /* Frob it to be ready for cloning.  */
    8985       133020 :           TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
    8986       133020 :           DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
    8987       133020 :           if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
    8988              :             {
    8989       133017 :               set_underlying_type (inner);
    8990       133017 :               if (tdef_flags & 2)
    8991              :                 {
    8992              :                   /* Match instantiate_alias_template's handling.  */
    8993        32588 :                   tree type = TREE_TYPE (inner);
    8994        32588 :                   TYPE_DEPENDENT_P (type) = true;
    8995        32588 :                   TYPE_DEPENDENT_P_VALID (type) = true;
    8996        32588 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
    8997              :                 }
    8998              :             }
    8999              :         }
    9000              : 
    9001       796711 :       if (inner_tag)
    9002              :         /* Set the TEMPLATE_DECL's type.  */
    9003       185224 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9004              : 
    9005              :       /* Redetermine whether we need to import or export this declaration
    9006              :          for this TU.  But for extern templates we know we must import:
    9007              :          they'll be defined in a different TU.
    9008              :          FIXME: How do dllexport and dllimport interact across a module?
    9009              :          See also https://github.com/itanium-cxx-abi/cxx-abi/issues/170.
    9010              :          May have to revisit?  */
    9011       796711 :       if (type
    9012       100889 :           && CLASS_TYPE_P (type)
    9013        86610 :           && TYPE_LANG_SPECIFIC (type)
    9014       883321 :           && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
    9015          799 :                && CLASSTYPE_INTERFACE_KNOWN (type)
    9016          799 :                && CLASSTYPE_INTERFACE_ONLY (type)))
    9017              :         {
    9018        85863 :           CLASSTYPE_INTERFACE_ONLY (type) = false;
    9019        85863 :           CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
    9020              :         }
    9021              : 
    9022              :       /* Add to specialization tables now that constraints etc are
    9023              :          added.  */
    9024       796711 :       if (mk == MK_partial)
    9025              :         {
    9026         6120 :           bool is_type = TREE_CODE (inner) == TYPE_DECL;
    9027         6120 :           spec.spec = is_type ? type : inner;
    9028         6120 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9029              :         }
    9030       790591 :       else if (mk & MK_template_mask)
    9031              :         {
    9032       225504 :           bool is_type = !(mk & MK_tmpl_decl_mask);
    9033       225504 :           spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
    9034       225504 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9035              :         }
    9036              : 
    9037       796711 :       if (NAMESPACE_SCOPE_P (decl)
    9038       167655 :           && (mk == MK_named || mk == MK_unique
    9039       167655 :               || mk == MK_enum || mk == MK_friend_spec)
    9040       872630 :           && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
    9041        75821 :         add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
    9042              : 
    9043       796711 :       if (DECL_ARTIFICIAL (decl)
    9044       222596 :           && TREE_CODE (decl) == FUNCTION_DECL
    9045        23994 :           && !DECL_TEMPLATE_INFO (decl)
    9046        23612 :           && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
    9047        23429 :           && TYPE_SIZE (DECL_CONTEXT (decl))
    9048       798248 :           && !DECL_THUNK_P (decl))
    9049              :         /* A new implicit member function, when the class is
    9050              :            complete.  This means the importee declared it, and
    9051              :            we must now add it to the class.  Note that implicit
    9052              :            member fns of template instantiations do not themselves
    9053              :            look like templates.  */
    9054          939 :         if (!install_implicit_member (inner))
    9055            0 :           set_overrun ();
    9056              : 
    9057              :       /* When importing a TLS wrapper from a header unit, we haven't
    9058              :          actually emitted its definition yet. Remember it so we can
    9059              :          do this later.  */
    9060       796711 :       if (state->is_header ()
    9061       796711 :           && decl_tls_wrapper_p (decl))
    9062            6 :         note_vague_linkage_fn (decl);
    9063              : 
    9064              :       /* Apply relevant attributes.
    9065              :          FIXME should probably use cplus_decl_attributes for this,
    9066              :          but it's not yet ready for modules.  */
    9067              : 
    9068       796711 :       if (VAR_OR_FUNCTION_DECL_P (inner))
    9069       481490 :         if (tree attr = lookup_attribute ("section", DECL_ATTRIBUTES (inner)))
    9070              :           {
    9071            6 :             tree section_name = TREE_VALUE (TREE_VALUE (attr));
    9072            6 :             set_decl_section_name (inner, TREE_STRING_POINTER (section_name));
    9073              :           }
    9074              : 
    9075              :       /* Setup aliases for the declaration.  */
    9076       796711 :       if (tree alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl)))
    9077              :         {
    9078            3 :           alias = TREE_VALUE (TREE_VALUE (alias));
    9079            3 :           alias = get_identifier (TREE_STRING_POINTER (alias));
    9080            3 :           assemble_alias (decl, alias);
    9081              :         }
    9082              :     }
    9083              :   else
    9084              :     {
    9085              :       /* DECL is the to-be-discarded decl.  Its internal pointers will
    9086              :          be to the EXISTING's structure.  Frob it to point to its
    9087              :          own other structures, so loading its definition will alter
    9088              :          it, and not the existing decl.  */
    9089       434516 :       dump (dumper::MERGE) && dump ("Deduping %N", existing);
    9090              : 
    9091       432754 :       if (inner_tag)
    9092       143162 :         DECL_TEMPLATE_RESULT (decl) = inner;
    9093              : 
    9094       432754 :       if (type)
    9095              :         {
    9096              :           /* Point at the to-be-discarded type & decl.  */
    9097        76770 :           TYPE_NAME (type) = inner;
    9098        76770 :           TREE_TYPE (inner) = type;
    9099              : 
    9100       153298 :           TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
    9101        76770 :           if (stub_decl)
    9102          242 :             TREE_TYPE (stub_decl) = type;
    9103              : 
    9104        76770 :           tree etype = TREE_TYPE (existing);
    9105              : 
    9106              :           /* Handle separate declarations with different attributes.  */
    9107        76770 :           tree &dattr = TYPE_ATTRIBUTES (type);
    9108        76770 :           tree &eattr = TYPE_ATTRIBUTES (etype);
    9109        76770 :           check_abi_tags (existing, decl, eattr, dattr);
    9110              :           // TODO: handle other conflicting type attributes
    9111        76770 :           eattr = merge_attributes (eattr, dattr);
    9112              : 
    9113              :           /* When merging a partial specialisation, the existing decl may have
    9114              :              had its TYPE_CANONICAL adjusted.  If so we should use structural
    9115              :              equality to ensure is_matching_decl doesn't get confused.  */
    9116        76770 :           if ((spec_flags & 2)
    9117        76770 :               && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
    9118            3 :             SET_TYPE_STRUCTURAL_EQUALITY (type);
    9119              :         }
    9120              : 
    9121       432754 :       if (inner_tag)
    9122              :         /* Set the TEMPLATE_DECL's type.  */
    9123       143162 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9124              : 
    9125       432754 :       if (!is_matching_decl (existing, decl, is_typedef))
    9126           42 :         unmatched_duplicate (existing);
    9127              : 
    9128       432754 :       if (TREE_CODE (inner) == FUNCTION_DECL)
    9129              :         {
    9130       197837 :           tree e_inner = STRIP_TEMPLATE (existing);
    9131       197837 :           for (auto parm = DECL_ARGUMENTS (inner);
    9132       591491 :                parm; parm = DECL_CHAIN (parm))
    9133       393654 :             DECL_CONTEXT (parm) = e_inner;
    9134              :         }
    9135              : 
    9136              :       /* And our result is the existing node.  */
    9137       432754 :       decl = existing;
    9138              :     }
    9139              : 
    9140      1229465 :   if (mk == MK_friend_spec)
    9141              :     {
    9142            0 :       tree e = match_mergeable_specialization (true, &spec);
    9143            0 :       if (!e)
    9144              :         {
    9145            0 :           spec.spec = inner;
    9146            0 :           add_mergeable_specialization (true, &spec, decl, spec_flags);
    9147              :         }
    9148            0 :       else if (e != existing)
    9149            0 :         set_overrun ();
    9150              :     }
    9151              : 
    9152      1229465 :   if (is_typedef)
    9153              :     {
    9154              :       /* Insert the type into the array now.  */
    9155       216606 :       tag = insert (TREE_TYPE (decl));
    9156       216606 :       dump (dumper::TREE)
    9157          247 :         && dump ("Cloned:%d typedef %C:%N",
    9158          247 :                  tag, TREE_CODE (TREE_TYPE (decl)), TREE_TYPE (decl));
    9159              :     }
    9160              : 
    9161      1229465 :   unused = saved_unused;
    9162              : 
    9163      1229465 :   if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    9164              :     {
    9165       104153 :       unsigned flags = u ();
    9166              : 
    9167       104153 :       if (is_new)
    9168              :         {
    9169        64131 :           bool cloned_p = flags & 1;
    9170        64231 :           dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    9171              :                                        decl, cloned_p ? "" : "not ");
    9172        64131 :           if (cloned_p)
    9173              :             {
    9174              :               /* Update the member vec, if there is one (we're in a different
    9175              :                  cluster to the class defn) and this isn't a primary template
    9176              :                  specialization (as in tsubst_function_decl).  */
    9177        45595 :               bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
    9178        45595 :                          && !primary_template_specialization_p (decl));
    9179        45595 :               build_cdtor_clones (decl, flags & 2, flags & 4, up);
    9180              :             }
    9181              :         }
    9182              :     }
    9183              : 
    9184      1229465 :   if (VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    9185              :     {
    9186          158 :       enum tls_model model = tls_model (u ());
    9187          158 :       if (is_new)
    9188          138 :         set_decl_tls_model (decl, model);
    9189              :     }
    9190              : 
    9191      1229465 :   if (!NAMESPACE_SCOPE_P (inner)
    9192       917970 :       && ((TREE_CODE (inner) == TYPE_DECL
    9193       214035 :            && !is_typedef
    9194        38674 :            && TYPE_NAME (TREE_TYPE (inner)) == inner)
    9195       879296 :           || TREE_CODE (inner) == FUNCTION_DECL)
    9196      1621148 :       && u ())
    9197            3 :     read_definition (decl);
    9198              : 
    9199              :   return decl;
    9200              : }
    9201              : 
    9202              : /* DECL is an unnameable member of CTX.  Return a suitable identifying
    9203              :    index.  */
    9204              : 
    9205              : static unsigned
    9206         1737 : get_field_ident (tree ctx, tree decl)
    9207              : {
    9208         1737 :   gcc_checking_assert (TREE_CODE (decl) == USING_DECL
    9209              :                        || !DECL_NAME (decl)
    9210              :                        || IDENTIFIER_ANON_P (DECL_NAME (decl)));
    9211              : 
    9212         1737 :   unsigned ix = 0;
    9213         1737 :   for (tree fields = TYPE_FIELDS (ctx);
    9214        12627 :        fields; fields = DECL_CHAIN (fields))
    9215              :     {
    9216        12627 :       if (fields == decl)
    9217         1737 :         return ix;
    9218              : 
    9219        10890 :       if (DECL_CONTEXT (fields) == ctx
    9220        10890 :           && (TREE_CODE (fields) == USING_DECL
    9221        10867 :               || (TREE_CODE (fields) == FIELD_DECL
    9222           92 :                   && (!DECL_NAME (fields)
    9223           46 :                       || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9224              :         /* Count this field.  */
    9225           49 :         ix++;
    9226              :     }
    9227            0 :   gcc_unreachable ();
    9228              : }
    9229              : 
    9230              : static tree
    9231         1066 : lookup_field_ident (tree ctx, unsigned ix)
    9232              : {
    9233         1066 :   for (tree fields = TYPE_FIELDS (ctx);
    9234         7707 :        fields; fields = DECL_CHAIN (fields))
    9235         7707 :     if (DECL_CONTEXT (fields) == ctx
    9236         7707 :         && (TREE_CODE (fields) == USING_DECL
    9237         7691 :             || (TREE_CODE (fields) == FIELD_DECL
    9238         1112 :                 && (!DECL_NAME (fields)
    9239           21 :                     || IDENTIFIER_ANON_P (DECL_NAME (fields))))))
    9240         1097 :       if (!ix--)
    9241              :         return fields;
    9242              : 
    9243              :   return NULL_TREE;
    9244              : }
    9245              : 
    9246              : /* Reference DECL.  REF indicates the walk kind we are performing.
    9247              :    Return true if we should write this decl by value.  */
    9248              : 
    9249              : bool
    9250     17856383 : trees_out::decl_node (tree decl, walk_kind ref)
    9251              : {
    9252     17856383 :   gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
    9253              :                        && DECL_CONTEXT (decl));
    9254              : 
    9255     17856383 :   if (ref == WK_value)
    9256              :     {
    9257      1699816 :       depset *dep = dep_hash->find_dependency (decl);
    9258      1699816 :       decl_value (decl, dep);
    9259      1699816 :       return false;
    9260              :     }
    9261              : 
    9262     16156567 :   switch (TREE_CODE (decl))
    9263              :     {
    9264              :     default:
    9265              :       break;
    9266              : 
    9267      1705629 :     case FUNCTION_DECL:
    9268      1705629 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9269              :       break;
    9270              : 
    9271              :     case RESULT_DECL:
    9272              :       /* Unlike PARM_DECLs, RESULT_DECLs are only generated and
    9273              :          referenced when we're inside the function itself.  */
    9274              :       return true;
    9275              : 
    9276       265003 :     case PARM_DECL:
    9277       265003 :       {
    9278       265003 :         if (streaming_p ())
    9279       114931 :           i (tt_parm);
    9280       265003 :         tree_node (DECL_CONTEXT (decl));
    9281              : 
    9282              :         /* That must have put this in the map.  */
    9283       265003 :         walk_kind ref = ref_node (decl);
    9284       265003 :         if (ref != WK_none)
    9285              :           // FIXME:OPTIMIZATION We can wander into bits of the
    9286              :           // template this was instantiated from, for instance
    9287              :           // deferred noexcept and default parms, or references
    9288              :           // to parms from earlier forward-decls (PR c++/119608).
    9289              :           //
    9290              :           // Currently we'll end up cloning those bits of tree.
    9291              :           // It would be nice to reference those specific nodes.
    9292              :           // I think putting those things in the map when we
    9293              :           // reference their template by name.
    9294              :           //
    9295              :           // See the note in add_indirects.
    9296              :           return true;
    9297              : 
    9298            0 :         if (streaming_p ())
    9299            0 :           dump (dumper::TREE)
    9300            0 :             && dump ("Wrote %s reference %N",
    9301            0 :                      TREE_CODE (decl) == PARM_DECL ? "parameter" : "result",
    9302              :                      decl);
    9303              :       }
    9304              :       return false;
    9305              : 
    9306              :     case IMPORTED_DECL:
    9307              :       /* This describes a USING_DECL to the ME's debug machinery.  It
    9308              :          originates from the fortran FE, and has nothing to do with
    9309              :          C++ modules.  */
    9310              :       return true;
    9311              : 
    9312              :     case LABEL_DECL:
    9313              :       return true;
    9314              : 
    9315       100289 :     case CONST_DECL:
    9316       100289 :       {
    9317              :         /* If I end up cloning enum decls, implementing C++20 using
    9318              :            E::v, this will need tweaking.   */
    9319       100289 :         if (streaming_p ())
    9320        25001 :           i (tt_enum_decl);
    9321       100289 :         tree ctx = DECL_CONTEXT (decl);
    9322       100289 :         gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
    9323       100289 :         tree_node (ctx);
    9324       100289 :         tree_node (DECL_NAME (decl));
    9325              : 
    9326       100289 :         int tag = insert (decl);
    9327       100289 :         if (streaming_p ())
    9328        25001 :           dump (dumper::TREE)
    9329           21 :             && dump ("Wrote enum decl:%d %C:%N", tag, TREE_CODE (decl), decl);
    9330              :         return false;
    9331              :       }
    9332        40457 :       break;
    9333              : 
    9334        40457 :     case USING_DECL:
    9335        40457 :       if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
    9336              :         break;
    9337              :       /* FALLTHROUGH  */
    9338              : 
    9339       249988 :     case FIELD_DECL:
    9340       249988 :       {
    9341       249988 :         if (streaming_p ())
    9342        18897 :           i (tt_data_member);
    9343              : 
    9344       249988 :         tree ctx = DECL_CONTEXT (decl);
    9345       249988 :         tree_node (ctx);
    9346              : 
    9347       249988 :         tree name = NULL_TREE;
    9348              : 
    9349       249988 :         if (TREE_CODE (decl) == USING_DECL)
    9350              :           ;
    9351              :         else
    9352              :           {
    9353       248151 :             name = DECL_NAME (decl);
    9354       482018 :             if (name && IDENTIFIER_ANON_P (name))
    9355              :               name = NULL_TREE;
    9356              :           }
    9357              : 
    9358       249988 :         tree_node (name);
    9359       249988 :         if (!name && streaming_p ())
    9360              :           {
    9361         1737 :             unsigned ix = get_field_ident (ctx, decl);
    9362         1737 :             u (ix);
    9363              :           }
    9364              : 
    9365       249988 :         int tag = insert (decl);
    9366       249988 :         if (streaming_p ())
    9367        18897 :           dump (dumper::TREE)
    9368           26 :             && dump ("Wrote member:%d %C:%N", tag, TREE_CODE (decl), decl);
    9369              :         return false;
    9370              :       }
    9371       746575 :       break;
    9372              : 
    9373       746575 :     case VAR_DECL:
    9374       746575 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9375       746575 :       if (DECL_VTABLE_OR_VTT_P (decl))
    9376              :         {
    9377              :           /* VTT or VTABLE, they are all on the vtables list.  */
    9378         4719 :           tree ctx = CP_DECL_CONTEXT (decl);
    9379         4719 :           tree vtable = CLASSTYPE_VTABLES (ctx);
    9380         4836 :           for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
    9381         4836 :             if (vtable == decl)
    9382              :               {
    9383         4719 :                 gcc_checking_assert (DECL_VIRTUAL_P (decl));
    9384         4719 :                 if (streaming_p ())
    9385              :                   {
    9386           32 :                     u (tt_vtable);
    9387           32 :                     u (ix);
    9388           32 :                     dump (dumper::TREE)
    9389            0 :                       && dump ("Writing vtable %N[%u]", ctx, ix);
    9390              :                   }
    9391         4719 :                 tree_node (ctx);
    9392         4719 :                 return false;
    9393              :               }
    9394              :           gcc_unreachable ();
    9395              :         }
    9396              : 
    9397       741856 :       if (DECL_TINFO_P (decl))
    9398              :         {
    9399         9382 :         tinfo:
    9400              :           /* A typeinfo, tt_tinfo_typedef or tt_tinfo_var.  */
    9401        17285 :           bool is_var = VAR_P (decl);
    9402        17285 :           tree type = TREE_TYPE (decl);
    9403        17285 :           unsigned ix = get_pseudo_tinfo_index (type);
    9404        17285 :           if (streaming_p ())
    9405              :             {
    9406        10926 :               i (is_var ? tt_tinfo_var : tt_tinfo_typedef);
    9407         7737 :               u (ix);
    9408              :             }
    9409              : 
    9410        17285 :           if (is_var)
    9411              :             {
    9412              :               /* We also need the type it is for and mangled name, so
    9413              :                  the reader doesn't need to complete the type (which
    9414              :                  would break section ordering).  The type it is for is
    9415              :                  stashed on the name's TREE_TYPE.  */
    9416         9382 :               tree name = DECL_NAME (decl);
    9417         9382 :               tree_node (name);
    9418         9382 :               type = TREE_TYPE (name);
    9419         9382 :               tree_node (type);
    9420              :             }
    9421              : 
    9422        17285 :           int tag = insert (decl);
    9423        17285 :           if (streaming_p ())
    9424         7737 :             dump (dumper::TREE)
    9425           27 :               && dump ("Wrote tinfo_%s:%d %u %N", is_var ? "var" : "type",
    9426              :                        tag, ix, type);
    9427              : 
    9428        17285 :           if (!is_var)
    9429              :             {
    9430         7903 :               tag = insert (type);
    9431         7903 :               if (streaming_p ())
    9432         3189 :                 dump (dumper::TREE)
    9433            9 :                   && dump ("Wrote tinfo_type:%d %u %N", tag, ix, type);
    9434              :             }
    9435              :           return false;
    9436              :         }
    9437              : 
    9438       732474 :       if (DECL_NTTP_OBJECT_P (decl))
    9439              :         {
    9440              :           /* A NTTP parm object.  */
    9441           42 :           if (streaming_p ())
    9442           10 :             i (tt_nttp_var);
    9443           42 :           tree_node (tparm_object_argument (decl));
    9444           42 :           tree_node (DECL_NAME (decl));
    9445           42 :           int tag = insert (decl);
    9446           42 :           if (streaming_p ())
    9447           10 :             dump (dumper::TREE)
    9448            0 :               && dump ("Wrote nttp object:%d %N", tag, DECL_NAME (decl));
    9449              :           return false;
    9450              :         }
    9451              : 
    9452              :       break;
    9453              : 
    9454      5500812 :     case TYPE_DECL:
    9455      5500812 :       if (DECL_TINFO_P (decl))
    9456         7903 :         goto tinfo;
    9457              :       /* c++/125768: For an imported typedef, also mark the original type
    9458              :          reachable in case it was instantiated here.  */
    9459      5020204 :       if (!streaming_p () && DECL_ORIGINAL_TYPE (decl)
    9460      6671047 :           && (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl)))
    9461          415 :         tree_node (DECL_ORIGINAL_TYPE (decl));
    9462              :       break;
    9463              :     }
    9464              : 
    9465     14670118 :   if (DECL_THUNK_P (decl))
    9466              :     {
    9467              :       /* Thunks are similar to binfos -- write the thunked-to decl and
    9468              :          then thunk-specific key info.  */
    9469            0 :       if (streaming_p ())
    9470              :         {
    9471            0 :           i (tt_thunk);
    9472            0 :           i (THUNK_FIXED_OFFSET (decl));
    9473              :         }
    9474              : 
    9475            0 :       tree target = decl;
    9476            0 :       while (DECL_THUNK_P (target))
    9477            0 :         target = THUNK_TARGET (target);
    9478            0 :       tree_node (target);
    9479            0 :       tree_node (THUNK_VIRTUAL_OFFSET (decl));
    9480            0 :       int tag = insert (decl);
    9481            0 :       if (streaming_p ())
    9482            0 :         dump (dumper::TREE)
    9483            0 :           && dump ("Wrote:%d thunk %N to %N", tag, DECL_NAME (decl), target);
    9484              :       return false;
    9485              :     }
    9486              : 
    9487     14670118 :   if (DECL_CLONED_FUNCTION_P (decl))
    9488              :     {
    9489       507341 :       tree target = get_clone_target (decl);
    9490       507341 :       if (streaming_p ())
    9491       241160 :         i (tt_clone_ref);
    9492              : 
    9493       507341 :       tree_node (target);
    9494       507341 :       tree_node (DECL_NAME (decl));
    9495       507341 :       if (DECL_VIRTUAL_P (decl))
    9496        33865 :         tree_node (DECL_VINDEX (decl));
    9497       507341 :       int tag = insert (decl);
    9498       507341 :       if (streaming_p ())
    9499       241160 :         dump (dumper::TREE)
    9500          164 :           && dump ("Wrote:%d clone %N of %N", tag, DECL_NAME (decl), target);
    9501              :       return false;
    9502              :     }
    9503              : 
    9504              :   /* Everything left should be a thing that is in the entity table.
    9505              :      Mostly things that can be defined outside of their (original
    9506              :      declaration) context.  */
    9507     14162777 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
    9508              :                        || VAR_P (decl)
    9509              :                        || TREE_CODE (decl) == FUNCTION_DECL
    9510              :                        || TREE_CODE (decl) == TYPE_DECL
    9511              :                        || TREE_CODE (decl) == USING_DECL
    9512              :                        || TREE_CODE (decl) == CONCEPT_DECL
    9513              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
    9514              : 
    9515     14162777 :   int use_tpl = -1;
    9516     14162777 :   tree ti = node_template_info (decl, use_tpl);
    9517     14162777 :   tree tpl = NULL_TREE;
    9518              : 
    9519              :   /* If this is the TEMPLATE_DECL_RESULT of a TEMPLATE_DECL, get the
    9520              :      TEMPLATE_DECL.  Note TI_TEMPLATE is not a TEMPLATE_DECL for
    9521              :      (some) friends, so we need to check that.  */
    9522              :   // FIXME: Should local friend template specializations be by value?
    9523              :   // They don't get idents so we'll never know they're imported, but I
    9524              :   // think we can only reach them from the TU that defines the
    9525              :   // befriending class?
    9526      5149064 :   if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    9527     19311727 :       && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
    9528              :     {
    9529              :       tpl = TI_TEMPLATE (ti);
    9530      1298605 :     partial_template:
    9531      1298605 :       if (streaming_p ())
    9532              :         {
    9533         4386 :           i (tt_template);
    9534         4386 :           dump (dumper::TREE)
    9535            9 :             && dump ("Writing implicit template %C:%N%S",
    9536            9 :                      TREE_CODE (tpl), tpl, tpl);
    9537              :         }
    9538      1298605 :       tree_node (tpl);
    9539              : 
    9540              :       /* Streaming TPL caused us to visit DECL and maybe its type,
    9541              :          if it wasn't TU-local.  */
    9542      1298605 :       if (CHECKING_P && !has_tu_local_dep (tpl))
    9543              :         {
    9544      1298578 :           gcc_checking_assert (TREE_VISITED (decl));
    9545      1298578 :           if (DECL_IMPLICIT_TYPEDEF_P (decl))
    9546       669786 :             gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
    9547              :         }
    9548              :       return false;
    9549              :     }
    9550              : 
    9551     12995843 :   tree ctx = CP_DECL_CONTEXT (decl);
    9552     12995843 :   depset *dep = NULL;
    9553     12995843 :   if (streaming_p ())
    9554      2177992 :     dep = dep_hash->find_dependency (decl);
    9555     10817851 :   else if (TREE_CODE (ctx) != FUNCTION_DECL
    9556       439923 :            || TREE_CODE (decl) == TEMPLATE_DECL
    9557       398504 :            || DECL_IMPLICIT_TYPEDEF_P (decl)
    9558     11174373 :            || (DECL_LANG_SPECIFIC (decl)
    9559       173427 :                && DECL_MODULE_IMPORT_P (decl)))
    9560              :     {
    9561     10461329 :       auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
    9562       796079 :                    && !DECL_NAMESPACE_ALIAS (decl)
    9563     10461329 :                    ? depset::EK_NAMESPACE : depset::EK_DECL);
    9564     10461329 :       dep = dep_hash->add_dependency (decl, kind);
    9565              :     }
    9566              : 
    9567     12639321 :   if (!dep || dep->is_tu_local ())
    9568              :     {
    9569              :       /* Some internal entity of context.  Do by value.  */
    9570       688627 :       decl_value (decl, dep);
    9571       688627 :       return false;
    9572              :     }
    9573              : 
    9574     12307216 :   if (dep->get_entity_kind () == depset::EK_REDIRECT)
    9575              :     {
    9576              :       /* The DECL_TEMPLATE_RESULT of a partial specialization.
    9577              :          Write the partial specialization's template.  */
    9578       131671 :       depset *redirect = dep->deps[0];
    9579       131671 :       gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
    9580       131671 :       tpl = redirect->get_entity ();
    9581       131671 :       goto partial_template;
    9582              :     }
    9583              : 
    9584     12175545 :   if (streaming_p ())
    9585              :     {
    9586              :       /* Locate the entity.  */
    9587      1846147 :       unsigned index = dep->cluster;
    9588      1846147 :       unsigned import = 0;
    9589              : 
    9590      1846147 :       if (dep->is_import ())
    9591         4467 :         import = dep->section;
    9592      1841680 :       else if (CHECKING_P)
    9593              :         /* It should be what we put there.  */
    9594      1841680 :         gcc_checking_assert (index == ~import_entity_index (decl));
    9595              : 
    9596              : #if CHECKING_P
    9597         4467 :       gcc_assert (!import || importedness >= 0);
    9598              : #endif
    9599      1846147 :       i (tt_entity);
    9600      1846147 :       u (import);
    9601      1846147 :       u (index);
    9602              :     }
    9603              : 
    9604     12175545 :   int tag = insert (decl);
    9605     12175545 :   if (streaming_p () && dump (dumper::TREE))
    9606              :     {
    9607          529 :       char const *kind = "import";
    9608          529 :       module_state *from = this_module ();
    9609          529 :       if (dep->is_import ())
    9610              :         /* Rediscover the unremapped index.  */
    9611           78 :         from = import_entity_module (import_entity_index (decl));
    9612              :       else
    9613              :         {
    9614          451 :           tree o = get_originating_module_decl (decl);
    9615          451 :           o = STRIP_TEMPLATE (o);
    9616          902 :           kind = (DECL_LANG_SPECIFIC (o) && DECL_MODULE_PURVIEW_P (o)
    9617          451 :                   ? "purview" : "GMF");
    9618              :         }
    9619          529 :       dump ("Wrote %s:%d %C:%N@%M", kind,
    9620          529 :             tag, TREE_CODE (decl), decl, from);
    9621              :     }
    9622              : 
    9623     12175545 :   add_indirects (decl);
    9624              : 
    9625     12175545 :   return false;
    9626              : }
    9627              : 
    9628              : void
    9629     14471404 : trees_out::type_node (tree type)
    9630              : {
    9631     14471404 :   gcc_assert (TYPE_P (type));
    9632              : 
    9633     14471404 :   tree root = (TYPE_NAME (type)
    9634     14471404 :                ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
    9635     14471404 :   gcc_checking_assert (root);
    9636              : 
    9637     14471404 :   if (type != root)
    9638              :     {
    9639      3234562 :       if (streaming_p ())
    9640       670465 :         i (tt_variant_type);
    9641      3234562 :       tree_node (root);
    9642              : 
    9643      3234562 :       int flags = -1;
    9644              : 
    9645      3234562 :       if (TREE_CODE (type) == FUNCTION_TYPE
    9646      3234562 :           || TREE_CODE (type) == METHOD_TYPE)
    9647              :         {
    9648       754591 :           int quals = type_memfn_quals (type);
    9649       754591 :           int rquals = type_memfn_rqual (type);
    9650       754591 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9651       754591 :           bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
    9652              : 
    9653       754591 :           if (raises != TYPE_RAISES_EXCEPTIONS (root)
    9654        25093 :               || rquals != type_memfn_rqual (root)
    9655        18013 :               || quals != type_memfn_quals (root)
    9656       772586 :               || late != TYPE_HAS_LATE_RETURN_TYPE (root))
    9657       754591 :             flags = rquals | (int (late) << 2) | (quals << 3);
    9658              :         }
    9659              :       else
    9660              :         {
    9661      2479971 :           if (TYPE_USER_ALIGN (type))
    9662        28268 :             flags = TYPE_ALIGN_RAW (type);
    9663              :         }
    9664              : 
    9665      3234562 :       if (streaming_p ())
    9666       670465 :         i (flags);
    9667              : 
    9668      3234562 :       if (flags < 0)
    9669              :         ;
    9670       782859 :       else if (TREE_CODE (type) == FUNCTION_TYPE
    9671       782859 :                || TREE_CODE (type) == METHOD_TYPE)
    9672              :         {
    9673       754591 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9674       754591 :           if (raises == TYPE_RAISES_EXCEPTIONS (root))
    9675        25093 :             raises = error_mark_node;
    9676       754591 :           tree_node (raises);
    9677              :         }
    9678              : 
    9679              :       /* build_type_attribute_variant creates a new TYPE_MAIN_VARIANT, so
    9680              :          variants should all have the same set of attributes.  */
    9681      3234562 :       gcc_checking_assert (TYPE_ATTRIBUTES (type)
    9682              :                            == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
    9683              : 
    9684      3234562 :       if (streaming_p ())
    9685              :         {
    9686              :           /* Qualifiers.  */
    9687       670465 :           int rquals = cp_type_quals (root);
    9688       670465 :           int quals = cp_type_quals (type);
    9689       670465 :           if (quals == rquals)
    9690       308815 :             quals = -1;
    9691       670465 :           i (quals);
    9692              :         }
    9693              : 
    9694      3234562 :       if (ref_node (type) != WK_none)
    9695              :         {
    9696      3234562 :           int tag = insert (type);
    9697      3234562 :           if (streaming_p ())
    9698              :             {
    9699       670465 :               i (0);
    9700       670465 :               dump (dumper::TREE)
    9701          203 :                 && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
    9702              :             }
    9703              :         }
    9704              :       return;
    9705              :     }
    9706              : 
    9707     11236842 :   if (tree name = TYPE_NAME (type))
    9708      4447147 :     if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
    9709      3496285 :         || DECL_TEMPLATE_PARM_P (name)
    9710      2384120 :         || TREE_CODE (type) == RECORD_TYPE
    9711       417179 :         || TREE_CODE (type) == UNION_TYPE
    9712      4855421 :         || TREE_CODE (type) == ENUMERAL_TYPE)
    9713              :       {
    9714      4160120 :         gcc_checking_assert (DECL_P (name));
    9715              : 
    9716              :         /* We can meet template parms that we didn't meet in the
    9717              :            tpl_parms walk, because we're referring to a derived type
    9718              :            that was previously constructed from equivalent template
    9719              :            parms. */
    9720      4160120 :         if (streaming_p ())
    9721              :           {
    9722       286451 :             i (tt_typedef_type);
    9723       286451 :             dump (dumper::TREE)
    9724           59 :               && dump ("Writing %stypedef %C:%N",
    9725           59 :                        DECL_IMPLICIT_TYPEDEF_P (name) ? "implicit " : "",
    9726           59 :                        TREE_CODE (name), name);
    9727              :           }
    9728      4160120 :         tree_node (name);
    9729      4160120 :         if (streaming_p ())
    9730       286451 :           dump (dumper::TREE) && dump ("Wrote typedef %C:%N%S",
    9731           59 :                                        TREE_CODE (name), name, name);
    9732              : 
    9733              :         /* We'll have either visited this type or have newly discovered
    9734              :            that it's TU-local; either way we won't need to visit it again.  */
    9735      4160120 :         gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
    9736              :         return;
    9737              :       }
    9738              : 
    9739      7076722 :   if (TYPE_PTRMEMFUNC_P (type))
    9740              :     {
    9741              :       /* This is a distinct type node, masquerading as a structure. */
    9742         5943 :       tree fn_type = TYPE_PTRMEMFUNC_FN_TYPE (type);
    9743         5943 :       if (streaming_p ())
    9744         1661 :         i (tt_ptrmem_type);
    9745         5943 :       tree_node (fn_type);
    9746         5943 :       int tag = insert (type);
    9747         5943 :       if (streaming_p ())
    9748         1664 :         dump (dumper::TREE) && dump ("Written:%d ptrmem type", tag);
    9749              :       return;
    9750              :     }
    9751              : 
    9752      7070779 :   if (streaming_p ())
    9753              :     {
    9754      2254946 :       u (tt_derived_type);
    9755      2254946 :       u (TREE_CODE (type));
    9756              :     }
    9757              : 
    9758      7070779 :   tree_node (TREE_TYPE (type));
    9759      7070779 :   switch (TREE_CODE (type))
    9760              :     {
    9761            0 :     default:
    9762              :       /* We should never meet a type here that is indescribable in
    9763              :          terms of other types.  */
    9764            0 :       gcc_unreachable ();
    9765              : 
    9766       106263 :     case ARRAY_TYPE:
    9767       106263 :       tree_node (TYPE_DOMAIN (type));
    9768       106263 :       if (streaming_p ())
    9769              :         /* Dependent arrays are constructed with TYPE_DEPENENT_P
    9770              :            already set.  */
    9771        34422 :         u (TYPE_DEPENDENT_P (type));
    9772              :       break;
    9773              : 
    9774              :     case COMPLEX_TYPE:
    9775              :       /* No additional data.  */
    9776              :       break;
    9777              : 
    9778           12 :     case BOOLEAN_TYPE:
    9779              :       /* A non-standard boolean type.  */
    9780           12 :       if (streaming_p ())
    9781            6 :         u (TYPE_PRECISION (type));
    9782              :       break;
    9783              : 
    9784        99479 :     case INTEGER_TYPE:
    9785        99479 :       if (TREE_TYPE (type))
    9786              :         {
    9787              :           /* A range type (representing an array domain).  */
    9788        92144 :           tree_node (TYPE_MIN_VALUE (type));
    9789        92144 :           tree_node (TYPE_MAX_VALUE (type));
    9790              :         }
    9791              :       else
    9792              :         {
    9793              :           /* A new integral type (representing a bitfield).  */
    9794         7335 :           if (streaming_p ())
    9795              :             {
    9796         2246 :               unsigned prec = TYPE_PRECISION (type);
    9797         2246 :               bool unsigned_p = TYPE_UNSIGNED (type);
    9798              : 
    9799         2246 :               u ((prec << 1) | unsigned_p);
    9800              :             }
    9801              :         }
    9802              :       break;
    9803              : 
    9804      1537468 :     case METHOD_TYPE:
    9805      1537468 :     case FUNCTION_TYPE:
    9806      1537468 :       {
    9807      1537468 :         gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
    9808              : 
    9809      1537468 :         tree arg_types = TYPE_ARG_TYPES (type);
    9810      1537468 :         if (TREE_CODE (type) == METHOD_TYPE)
    9811              :           {
    9812      1001827 :             tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
    9813      1001827 :             arg_types = TREE_CHAIN (arg_types);
    9814              :           }
    9815      1537468 :         tree_node (arg_types);
    9816              :       }
    9817      1537468 :       break;
    9818              : 
    9819         1726 :     case OFFSET_TYPE:
    9820         1726 :       tree_node (TYPE_OFFSET_BASETYPE (type));
    9821         1726 :       break;
    9822              : 
    9823              :     case POINTER_TYPE:
    9824              :       /* No additional data.  */
    9825              :       break;
    9826              : 
    9827      1217010 :     case REFERENCE_TYPE:
    9828      1217010 :       if (streaming_p ())
    9829       270637 :         u (TYPE_REF_IS_RVALUE (type));
    9830              :       break;
    9831              : 
    9832      1440029 :     case DECLTYPE_TYPE:
    9833      1440029 :     case TYPEOF_TYPE:
    9834      1440029 :     case DEPENDENT_OPERATOR_TYPE:
    9835      1440029 :       tree_node (TYPE_VALUES_RAW (type));
    9836      1440029 :       if (TREE_CODE (type) == DECLTYPE_TYPE)
    9837              :         /* We stash a whole bunch of things into decltype's
    9838              :            flags.  */
    9839       114893 :         if (streaming_p ())
    9840        39079 :           tree_node_bools (type);
    9841              :       break;
    9842              : 
    9843         9045 :     case TRAIT_TYPE:
    9844         9045 :       tree_node (TRAIT_TYPE_KIND_RAW (type));
    9845         9045 :       tree_node (TRAIT_TYPE_TYPE1 (type));
    9846         9045 :       tree_node (TRAIT_TYPE_TYPE2 (type));
    9847         9045 :       break;
    9848              : 
    9849              :     case TYPE_ARGUMENT_PACK:
    9850              :       /* No additional data.  */
    9851              :       break;
    9852              : 
    9853       238345 :     case TYPE_PACK_EXPANSION:
    9854       238345 :       if (streaming_p ())
    9855        96018 :         u (PACK_EXPANSION_LOCAL_P (type));
    9856       476690 :       tree_node (PACK_EXPANSION_PARAMETER_PACKS (type));
    9857       238345 :       tree_node (PACK_EXPANSION_EXTRA_ARGS (type));
    9858       238345 :       break;
    9859              : 
    9860           58 :     case PACK_INDEX_TYPE:
    9861           58 :       tree_node (PACK_INDEX_PACK (type));
    9862           58 :       tree_node (PACK_INDEX_INDEX (type));
    9863           58 :       break;
    9864              : 
    9865       287752 :     case TYPENAME_TYPE:
    9866       287752 :       {
    9867       287752 :         tree_node (TYPE_CONTEXT (type));
    9868       287752 :         tree_node (DECL_NAME (TYPE_NAME (type)));
    9869       287752 :         tree_node (TYPENAME_TYPE_FULLNAME (type));
    9870       287752 :         if (streaming_p ())
    9871        99545 :           u (get_typename_tag (type));
    9872              :         }
    9873              :       break;
    9874              : 
    9875          276 :     case UNBOUND_CLASS_TEMPLATE:
    9876          276 :       {
    9877          276 :         tree decl = TYPE_NAME (type);
    9878          276 :         tree_node (DECL_CONTEXT (decl));
    9879          276 :         tree_node (DECL_NAME (decl));
    9880          276 :         tree_node (DECL_TEMPLATE_PARMS (decl));
    9881              :       }
    9882          276 :       break;
    9883              : 
    9884           42 :     case VECTOR_TYPE:
    9885           42 :       if (streaming_p ())
    9886              :         {
    9887           21 :           poly_uint64 nunits = TYPE_VECTOR_SUBPARTS (type);
    9888           63 :           for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
    9889           21 :             wu (nunits.coeffs[ix]);
    9890              :         }
    9891              :       break;
    9892              : 
    9893         2366 :     case OPAQUE_TYPE:
    9894              :       /* No additional data.  OPAQUE_TYPEs created by the rs6000 backend
    9895              :          are registered as built-in types and are recorded in fixed_trees
    9896              :          so they will not get here.  */
    9897         2366 :       gcc_checking_assert (REFLECTION_TYPE_P (type));
    9898              :       break;
    9899              : 
    9900            8 :     case SPLICE_SCOPE:
    9901            8 :       if (streaming_p ())
    9902            4 :         u (SPLICE_SCOPE_TYPE_P (type));
    9903            8 :       tree_node (SPLICE_SCOPE_EXPR (type));
    9904            8 :       break;
    9905              :     }
    9906              : 
    9907      7070779 :   tree_node (TYPE_ATTRIBUTES (type));
    9908              : 
    9909              :   /* We may have met the type during emitting the above.  */
    9910      7070779 :   if (ref_node (type) != WK_none)
    9911              :     {
    9912      6408211 :       int tag = insert (type);
    9913      6408211 :       if (streaming_p ())
    9914              :         {
    9915      1945248 :           i (0);
    9916      1945248 :           dump (dumper::TREE)
    9917          558 :             && dump ("Wrote:%d derived type %C", tag, TREE_CODE (type));
    9918              :         }
    9919              :     }
    9920              : 
    9921              :   return;
    9922              : }
    9923              : 
    9924              : /* T is (mostly*) a non-mergeable node that must be written by value.
    9925              :    The mergeable case is a BINFO, which are as-if DECLSs.   */
    9926              : 
    9927              : void
    9928     44343536 : trees_out::tree_value (tree t)
    9929              : {
    9930              :   /* We should never be writing a type by value.  tree_type should
    9931              :      have streamed it, or we're going via its TYPE_DECL.  */
    9932     44343536 :   gcc_checking_assert (!TYPE_P (t));
    9933              : 
    9934     44343536 :   if (DECL_P (t))
    9935              :     /* No template, type, var or function, except anonymous
    9936              :        non-context vars and types.  */
    9937      1148724 :     gcc_checking_assert ((TREE_CODE (t) != TEMPLATE_DECL
    9938              :                           && (TREE_CODE (t) != TYPE_DECL
    9939              :                               || (DECL_ARTIFICIAL (t) && !DECL_CONTEXT (t)))
    9940              :                           && (TREE_CODE (t) != VAR_DECL
    9941              :                               || ((!DECL_NAME (t)
    9942              :                                    || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
    9943              :                                   && !DECL_CONTEXT (t)))
    9944              :                           && TREE_CODE (t) != FUNCTION_DECL));
    9945              : 
    9946     66995008 :   if (is_initial_scan () && EXPR_P (t))
    9947      9017129 :     dep_hash->add_dependent_adl_entities (t);
    9948              : 
    9949     44343536 :   if (streaming_p ())
    9950              :     {
    9951              :       /* A new node -> tt_node.  */
    9952     17593434 :       tree_val_count++;
    9953     17593434 :       i (tt_node);
    9954     17593434 :       start (t);
    9955     17593434 :       tree_node_bools (t);
    9956              :     }
    9957              : 
    9958     44343536 :   if (TREE_CODE (t) == TREE_BINFO)
    9959              :     /* Binfos are decl-like and need merging information.  */
    9960       300164 :     binfo_mergeable (t);
    9961              : 
    9962     44343536 :   int tag = insert (t, WK_value);
    9963     44343536 :   if (streaming_p ())
    9964     17593434 :     dump (dumper::TREE)
    9965         2823 :       && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
    9966              : 
    9967     44343536 :   int type_tag = 0;
    9968     44343536 :   tree type = NULL_TREE;
    9969     44343536 :   if (TREE_CODE (t) == TYPE_DECL)
    9970              :     {
    9971           28 :       type = TREE_TYPE (t);
    9972              : 
    9973              :       /* We only support a limited set of features for uncontexted types;
    9974              :          these are typically types created in the language-independent
    9975              :          parts of the frontend (such as ubsan).  */
    9976           28 :       gcc_checking_assert (RECORD_OR_UNION_TYPE_P (type)
    9977              :                            && TYPE_MAIN_VARIANT (type) == type
    9978              :                            && TYPE_NAME (type) == t
    9979              :                            && TYPE_STUB_DECL (type) == t
    9980              :                            && !TYPE_VFIELD (type)
    9981              :                            && !TYPE_BINFO (type)
    9982              :                            && !CLASS_TYPE_P (type));
    9983              : 
    9984           28 :       if (streaming_p ())
    9985              :         {
    9986           14 :           start (type);
    9987           14 :           tree_node_bools (type);
    9988              :         }
    9989              : 
    9990           28 :       type_tag = insert (type, WK_value);
    9991           28 :       if (streaming_p ())
    9992           14 :         dump (dumper::TREE)
    9993            0 :           && dump ("Writing type: %d %C:%N", type_tag,
    9994            0 :                    TREE_CODE (type), type);
    9995              :     }
    9996              : 
    9997     44343536 :   tree_node_vals (t);
    9998              : 
    9999     44343536 :   if (type)
   10000              :     {
   10001           28 :       tree_node_vals (type);
   10002           28 :       tree_node (TYPE_SIZE (type));
   10003           28 :       tree_node (TYPE_SIZE_UNIT (type));
   10004           28 :       chained_decls (TYPE_FIELDS (type));
   10005           28 :       if (streaming_p ())
   10006           14 :         dump (dumper::TREE)
   10007            0 :           && dump ("Written type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10008              :     }
   10009              : 
   10010              :   /* For uncontexted VAR_DECLs we need to stream the definition so that
   10011              :      importers can recreate their value.  */
   10012     44343536 :   if (TREE_CODE (t) == VAR_DECL)
   10013              :     {
   10014          850 :       gcc_checking_assert (!DECL_NONTRIVIALLY_INITIALIZED_P (t));
   10015          850 :       tree_node (DECL_INITIAL (t));
   10016              :     }
   10017              : 
   10018     44343536 :   if (streaming_p ())
   10019     17596257 :     dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
   10020     44343536 : }
   10021              : 
   10022              : tree
   10023     12049506 : trees_in::tree_value ()
   10024              : {
   10025     12049506 :   tree t = start ();
   10026     12049506 :   if (!t || !tree_node_bools (t))
   10027              :     return NULL_TREE;
   10028              : 
   10029     12049506 :   tree existing = t;
   10030     12049506 :   if (TREE_CODE (t) == TREE_BINFO)
   10031              :     {
   10032        94375 :       tree type;
   10033        94375 :       unsigned ix = binfo_mergeable (&type);
   10034        94375 :       if (TYPE_BINFO (type))
   10035              :         {
   10036              :           /* We already have a definition, this must be a duplicate.  */
   10037        40987 :           dump (dumper::MERGE)
   10038          271 :             && dump ("Deduping binfo %N[%u]", type, ix);
   10039        40987 :           existing = TYPE_BINFO (type);
   10040        56027 :           while (existing && ix--)
   10041        15040 :             existing = TREE_CHAIN (existing);
   10042        40987 :           if (existing)
   10043        40987 :             register_duplicate (t, existing);
   10044              :           else
   10045              :             /* Error, mismatch -- diagnose in read_class_def's
   10046              :                checking.  */
   10047              :             existing = t;
   10048              :         }
   10049              :     }
   10050              : 
   10051              :   /* Insert into map.  */
   10052     12049506 :   int tag = insert (existing);
   10053     12049506 :   dump (dumper::TREE)
   10054         3677 :     && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
   10055              : 
   10056     12049506 :   int type_tag = 0;
   10057     12049506 :   tree type = NULL_TREE;
   10058     12049506 :   if (TREE_CODE (t) == TYPE_DECL)
   10059              :     {
   10060           14 :       type = start ();
   10061           14 :       if (!type || !tree_node_bools (type))
   10062              :         t = NULL_TREE;
   10063              : 
   10064           14 :       type_tag = insert (type);
   10065           14 :       if (t)
   10066           14 :         dump (dumper::TREE)
   10067            0 :           && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
   10068              :     }
   10069              : 
   10070              :   if (!t)
   10071              :     {
   10072            0 : bail:
   10073            0 :       back_refs[~tag] = NULL_TREE;
   10074            0 :       if (type_tag)
   10075            0 :         back_refs[~type_tag] = NULL_TREE;
   10076            0 :       set_overrun ();
   10077            0 :       return NULL_TREE;
   10078              :     }
   10079              : 
   10080     12049506 :   if (!tree_node_vals (t))
   10081            0 :     goto bail;
   10082              : 
   10083     12049506 :   if (type)
   10084              :     {
   10085           14 :       if (!tree_node_vals (type))
   10086            0 :         goto bail;
   10087              : 
   10088           14 :       TYPE_SIZE (type) = tree_node ();
   10089           14 :       TYPE_SIZE_UNIT (type) = tree_node ();
   10090           14 :       TYPE_FIELDS (type) = chained_decls ();
   10091           14 :       if (get_overrun ())
   10092            0 :         goto bail;
   10093              : 
   10094           14 :       dump (dumper::TREE)
   10095            0 :         && dump ("Read type:%d %C:%N", type_tag, TREE_CODE (type), type);
   10096              :     }
   10097              : 
   10098     12049506 :   if (TREE_CODE (t) == VAR_DECL)
   10099              :     {
   10100          350 :       DECL_INITIAL (t) = tree_node ();
   10101          350 :       if (TREE_STATIC (t))
   10102            8 :         varpool_node::finalize_decl (t);
   10103              :     }
   10104              : 
   10105     12049506 :   if (TREE_CODE (t) == LAMBDA_EXPR
   10106     12049506 :       && CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t)))
   10107              :     {
   10108         2065 :       existing = CLASSTYPE_LAMBDA_EXPR (TREE_TYPE (t));
   10109         2065 :       back_refs[~tag] = existing;
   10110              :     }
   10111              : 
   10112     12053183 :   dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
   10113              : 
   10114     12049506 :   if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
   10115              :     {
   10116       606309 :       existing = cache_integer_cst (t, true);
   10117       606309 :       back_refs[~tag] = existing;
   10118              :     }
   10119              : 
   10120              :   return existing;
   10121              : }
   10122              : 
   10123              : /* Whether DECL has a TU-local dependency in the hash.  */
   10124              : 
   10125              : bool
   10126      1298966 : trees_out::has_tu_local_dep (tree decl) const
   10127              : {
   10128              :   /* Only the contexts of fields or enums remember that they're
   10129              :      TU-local.  */
   10130      1298966 :   if (DECL_CONTEXT (decl)
   10131      1298966 :       && (TREE_CODE (decl) == FIELD_DECL
   10132      1298963 :           || TREE_CODE (decl) == CONST_DECL))
   10133            3 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   10134              : 
   10135      1298966 :   depset *dep = dep_hash->find_dependency (decl);
   10136      1298966 :   if (!dep)
   10137              :     {
   10138              :       /* This might be the DECL_TEMPLATE_RESULT of a TEMPLATE_DECL
   10139              :          which we found was TU-local and gave up early.  */
   10140        16036 :       int use_tpl = -1;
   10141        16036 :       if (tree ti = node_template_info (decl, use_tpl))
   10142         2622 :         dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
   10143              :     }
   10144              : 
   10145      1298966 :   return dep && dep->is_tu_local ();
   10146              : }
   10147              : 
   10148              : /* If T depends on a TU-local entity, return that decl.  */
   10149              : 
   10150              : tree
   10151          395 : trees_out::find_tu_local_decl (tree t)
   10152              : {
   10153              :   /* We need to have walked all deps first before we can check.  */
   10154          395 :   gcc_checking_assert (!is_initial_scan ());
   10155              : 
   10156          951 :   auto walker = [](tree *tp, int *walk_subtrees, void *data) -> tree
   10157              :     {
   10158          556 :       auto self = (trees_out *)data;
   10159              : 
   10160          556 :       tree decl = NULL_TREE;
   10161          556 :       if (TYPE_P (*tp))
   10162              :         {
   10163              :           /* A PMF type is a record type, which we otherwise wouldn't walk;
   10164              :              return whether the function type is TU-local.  */
   10165          370 :           if (TYPE_PTRMEMFUNC_P (*tp))
   10166              :             {
   10167            3 :               *walk_subtrees = 0;
   10168            3 :               return self->find_tu_local_decl (TYPE_PTRMEMFUNC_FN_TYPE (*tp));
   10169              :             }
   10170              :           else
   10171          367 :             decl = TYPE_MAIN_DECL (*tp);
   10172              :         }
   10173          186 :       else if (DECL_P (*tp))
   10174              :         decl = *tp;
   10175              : 
   10176          373 :       if (decl)
   10177              :         {
   10178              :           /* We found a DECL, this will tell us whether we're TU-local.  */
   10179           59 :           *walk_subtrees = 0;
   10180           59 :           return self->has_tu_local_dep (decl) ? decl : NULL_TREE;
   10181              :         }
   10182              :       return NULL_TREE;
   10183              :     };
   10184              : 
   10185              :   /* We need to walk without duplicates so that we step into the pointed-to
   10186              :      types of array types.  */
   10187          395 :   return cp_walk_tree_without_duplicates (&t, walker, this);
   10188              : }
   10189              : 
   10190              : /* Get the name for TU-local decl T to be used in diagnostics.  */
   10191              : 
   10192              : static tree
   10193          206 : name_for_tu_local_decl (tree t)
   10194              : {
   10195          206 :   int flags = (TFF_SCOPE | TFF_DECL_SPECIFIERS);
   10196          206 :   const char *str = decl_as_string (t, flags);
   10197          206 :   return get_identifier (str);
   10198              : }
   10199              : 
   10200              : /* Stream out tree node T.  We automatically create local back
   10201              :    references, which is essentially a single pass lisp
   10202              :    self-referential structure pretty-printer.  */
   10203              : 
   10204              : void
   10205    374933955 : trees_out::tree_node (tree t)
   10206              : {
   10207    374933955 :   dump.indent ();
   10208    374933955 :   walk_kind ref = ref_node (t);
   10209    374933955 :   if (ref == WK_none)
   10210    286082051 :     goto done;
   10211              : 
   10212              :   /* Find TU-local entities and intercept streaming to instead write a
   10213              :      placeholder value; this way we don't need to emit such decls.
   10214              :      We only need to do this when writing a definition of an entity
   10215              :      that we know names a TU-local entity.  */
   10216    103882924 :   if (!is_initial_scan () && writing_local_entities)
   10217              :     {
   10218          952 :       tree local_decl = NULL_TREE;
   10219          952 :       if (DECL_P (t) && has_tu_local_dep (t))
   10220              :         local_decl = t;
   10221              :       /* Consider a type to be TU-local if it refers to any TU-local decl,
   10222              :          no matter how deep.
   10223              : 
   10224              :          This worsens diagnostics slightly, as we often no longer point
   10225              :          directly to the at-fault entity when instantiating.  However, this
   10226              :          reduces the module size slightly and means that much less of pt.cc
   10227              :          needs to know about us.  */
   10228          848 :       else if (TYPE_P (t))
   10229          142 :         local_decl = find_tu_local_decl (t);
   10230          706 :       else if (EXPR_P (t))
   10231          250 :         local_decl = find_tu_local_decl (TREE_TYPE (t));
   10232              : 
   10233          496 :       if (local_decl)
   10234              :         {
   10235          158 :           int tag = insert (t, WK_value);
   10236          158 :           if (streaming_p ())
   10237              :             {
   10238          158 :               tu_local_count++;
   10239          158 :               i (tt_tu_local);
   10240          158 :               dump (dumper::TREE)
   10241            0 :                 && dump ("Writing TU-local entity:%d %C:%N",
   10242            0 :                          tag, TREE_CODE (t), t);
   10243              :             }
   10244          158 :           tree_node (name_for_tu_local_decl (local_decl));
   10245          158 :           if (state)
   10246          158 :             state->write_location (*this, DECL_SOURCE_LOCATION (local_decl));
   10247          158 :           goto done;
   10248              :         }
   10249              :     }
   10250              : 
   10251     88851746 :   if (ref != WK_normal)
   10252      2039486 :     goto skip_normal;
   10253              : 
   10254     86812260 :   if (TREE_CODE (t) == IDENTIFIER_NODE)
   10255              :     {
   10256              :       /* An identifier node -> tt_id, tt_conv_id, tt_anon_id, tt_lambda_id,
   10257              :          tt_internal_id.  */
   10258     10215918 :       int code = tt_id;
   10259     10215918 :       if (IDENTIFIER_ANON_P (t))
   10260        40030 :         code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
   10261     10175888 :       else if (IDENTIFIER_INTERNAL_P (t))
   10262              :         code = tt_internal_id;
   10263     10175874 :       else if (IDENTIFIER_CONV_OP_P (t))
   10264        15436 :         code = tt_conv_id;
   10265              : 
   10266     10215918 :       if (streaming_p ())
   10267      1982921 :         i (code);
   10268              : 
   10269     10215918 :       if (code == tt_conv_id)
   10270              :         {
   10271        15436 :           tree type = TREE_TYPE (t);
   10272        15436 :           gcc_checking_assert (type || t == conv_op_identifier);
   10273        15436 :           tree_node (type);
   10274              :         }
   10275     10200482 :       else if (code == tt_id && streaming_p ())
   10276      1966226 :         str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
   10277      8234256 :       else if (code == tt_internal_id && streaming_p ())
   10278            7 :         str (prefix_for_internal_label (t));
   10279              : 
   10280     10215918 :       int tag = insert (t);
   10281     10215918 :       if (streaming_p ())
   10282              :         {
   10283              :           /* We know the ordering of the 5 id tags.  */
   10284      1982921 :           static const char *const kinds[] =
   10285              :             {"", "conv_op ", "anon ", "lambda ", "internal "};
   10286      1982921 :           dump (dumper::TREE)
   10287         1074 :             && dump ("Written:%d %sidentifier:%N", tag,
   10288         1071 :                      kinds[code - tt_id],
   10289            3 :                      code == tt_conv_id ? TREE_TYPE (t) : t);
   10290              :         }
   10291     10215918 :       goto done;
   10292              :     }
   10293              : 
   10294     76596342 :   if (TREE_CODE (t) == TREE_BINFO)
   10295              :     {
   10296              :       /* A BINFO -> tt_binfo.
   10297              :          We must do this by reference.  We stream the binfo tree
   10298              :          itself when streaming its owning RECORD_TYPE.  That we got
   10299              :          here means the dominating type is not in this SCC.  */
   10300        89567 :       if (streaming_p ())
   10301         2889 :         i (tt_binfo);
   10302        89567 :       binfo_mergeable (t);
   10303        89567 :       gcc_checking_assert (!TREE_VISITED (t));
   10304        89567 :       int tag = insert (t);
   10305        89567 :       if (streaming_p ())
   10306         2889 :         dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
   10307        89567 :       goto done;
   10308              :     }
   10309              : 
   10310     76506775 :   if (TREE_CODE (t) == INTEGER_CST
   10311      5393658 :       && !TREE_OVERFLOW (t)
   10312     81900433 :       && TREE_CODE (TREE_TYPE (t)) == ENUMERAL_TYPE)
   10313              :     {
   10314              :       /* An integral constant of enumeral type.  See if it matches one
   10315              :          of the enumeration values.  */
   10316        54783 :       for (tree values = TYPE_VALUES (TREE_TYPE (t));
   10317      1112243 :            values; values = TREE_CHAIN (values))
   10318              :         {
   10319      1109351 :           tree decl = TREE_VALUE (values);
   10320      1109351 :           if (tree_int_cst_equal (DECL_INITIAL (decl), t))
   10321              :             {
   10322        51891 :               if (streaming_p ())
   10323        15065 :                 u (tt_enum_value);
   10324        51891 :               tree_node (decl);
   10325        51933 :               dump (dumper::TREE) && dump ("Written enum value %N", decl);
   10326        51891 :               goto done;
   10327              :             }
   10328              :         }
   10329              :       /* It didn't match.  We'll write it a an explicit INTEGER_CST
   10330              :          node.  */
   10331              :     }
   10332              : 
   10333     76454884 :   if (TYPE_P (t))
   10334              :     {
   10335     14471404 :       type_node (t);
   10336     14471404 :       goto done;
   10337              :     }
   10338              : 
   10339     61983480 :   if (DECL_P (t))
   10340              :     {
   10341     19128338 :       if (DECL_TEMPLATE_PARM_P (t))
   10342              :         {
   10343      2937173 :           tpl_parm_value (t);
   10344      2937173 :           goto done;
   10345              :         }
   10346              : 
   10347     16191165 :       if (!DECL_CONTEXT (t))
   10348              :         {
   10349              :           /* There are a few cases of decls with no context.  We'll write
   10350              :              these by value, but first assert they are cases we expect.  */
   10351        34598 :           gcc_checking_assert (ref == WK_normal);
   10352        34598 :           switch (TREE_CODE (t))
   10353              :             {
   10354            0 :             default: gcc_unreachable ();
   10355              : 
   10356        13832 :             case LABEL_DECL:
   10357              :               /* CASE_LABEL_EXPRs contain uncontexted LABEL_DECLs.  */
   10358        13832 :               gcc_checking_assert (!DECL_NAME (t));
   10359              :               break;
   10360              : 
   10361          850 :             case VAR_DECL:
   10362              :               /* AGGR_INIT_EXPRs cons up anonymous uncontexted VAR_DECLs,
   10363              :                  and internal vars are created by sanitizers and
   10364              :                  __builtin_source_location.  */
   10365          850 :               gcc_checking_assert ((!DECL_NAME (t)
   10366              :                                     || IDENTIFIER_INTERNAL_P (DECL_NAME (t)))
   10367              :                                    && DECL_ARTIFICIAL (t));
   10368              :               break;
   10369              : 
   10370        19888 :             case PARM_DECL:
   10371              :               /* REQUIRES_EXPRs have a chain of uncontexted PARM_DECLS,
   10372              :                  and an implicit this parm in an NSDMI has no context.  */
   10373        19888 :               gcc_checking_assert (CONSTRAINT_VAR_P (t)
   10374              :                                    || DECL_NAME (t) == this_identifier);
   10375              :               break;
   10376              : 
   10377           28 :             case TYPE_DECL:
   10378              :               /* Some parts of the compiler need internal struct types;
   10379              :                  these types may not have an appropriate context to use.
   10380              :                  Walk the whole type (including its definition) by value.  */
   10381           28 :               gcc_checking_assert (DECL_ARTIFICIAL (t)
   10382              :                                    && TYPE_ARTIFICIAL (TREE_TYPE (t))
   10383              :                                    && RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
   10384              :                                    && !CLASS_TYPE_P (TREE_TYPE (t)));
   10385              :               break;
   10386              :             }
   10387        34598 :           mark_declaration (t, has_definition (t));
   10388        34598 :           goto by_value;
   10389              :         }
   10390              :     }
   10391              : 
   10392     42855142 :  skip_normal:
   10393     61051195 :   if (DECL_P (t) && !decl_node (t, ref))
   10394     16742257 :     goto done;
   10395              : 
   10396              :   /* Otherwise by value */
   10397     44343536 :  by_value:
   10398     44343536 :   tree_value (t);
   10399              : 
   10400    374933955 :  done:
   10401              :   /* And, breath out.  */
   10402    374933955 :   dump.outdent ();
   10403    374933955 : }
   10404              : 
   10405              : /* Stream in a tree node.  */
   10406              : 
   10407              : tree
   10408     97875246 : trees_in::tree_node (bool is_use)
   10409              : {
   10410     97875246 :   if (get_overrun ())
   10411              :     return NULL_TREE;
   10412              : 
   10413     97875246 :   dump.indent ();
   10414     97875246 :   int tag = i ();
   10415     97875246 :   tree res = NULL_TREE;
   10416     97875246 :   switch (tag)
   10417              :     {
   10418     32128364 :     default:
   10419              :       /* backref, pull it out of the map.  */
   10420     32128364 :       res = back_ref (tag);
   10421     32128364 :       break;
   10422              : 
   10423              :     case tt_null:
   10424              :       /* NULL_TREE.  */
   10425              :       break;
   10426              : 
   10427          158 :     case tt_tu_local:
   10428          158 :       {
   10429              :         /* A translation-unit-local entity.  */
   10430          158 :         res = make_node (TU_LOCAL_ENTITY);
   10431          158 :         int tag = insert (res);
   10432              : 
   10433          158 :         TU_LOCAL_ENTITY_NAME (res) = tree_node ();
   10434          158 :         TU_LOCAL_ENTITY_LOCATION (res) = state->read_location (*this);
   10435          158 :         dump (dumper::TREE) && dump ("Read TU-local entity:%d %N", tag, res);
   10436              :       }
   10437              :       break;
   10438              : 
   10439      7567348 :     case tt_fixed:
   10440              :       /* A fixed ref, find it in the fixed_ref array.   */
   10441      7567348 :       {
   10442      7567348 :         unsigned fix = u ();
   10443      7567348 :         if (fix < (*fixed_trees).length ())
   10444              :           {
   10445      7567348 :             res = (*fixed_trees)[fix];
   10446      7567348 :             dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
   10447         5046 :                                          TREE_CODE (res), res, res);
   10448              :           }
   10449              : 
   10450      7567348 :         if (!res)
   10451            0 :           set_overrun ();
   10452              :       }
   10453              :       break;
   10454              : 
   10455        83365 :     case tt_parm:
   10456        83365 :       {
   10457        83365 :         tree fn = tree_node ();
   10458        83365 :         if (fn && TREE_CODE (fn) == FUNCTION_DECL)
   10459        83365 :           res = tree_node ();
   10460        83365 :         if (res)
   10461        83365 :           dump (dumper::TREE)
   10462           21 :             && dump ("Read %s reference %N",
   10463           21 :                      TREE_CODE (res) == PARM_DECL ? "parameter" : "result",
   10464              :                      res);
   10465              :       }
   10466              :       break;
   10467              : 
   10468     12049506 :     case tt_node:
   10469              :       /* A new node.  Stream it in.  */
   10470     12049506 :       res = tree_value ();
   10471     12049506 :       break;
   10472              : 
   10473      1229465 :     case tt_decl:
   10474              :       /* A new decl.  Stream it in.  */
   10475      1229465 :       res = decl_value ();
   10476      1229465 :       break;
   10477              : 
   10478       432987 :     case tt_tpl_parm:
   10479              :       /* A template parameter.  Stream it in.  */
   10480       432987 :       res = tpl_parm_value ();
   10481       432987 :       break;
   10482              : 
   10483      1272870 :     case tt_id:
   10484              :       /* An identifier node.  */
   10485      1272870 :       {
   10486      1272870 :         size_t l;
   10487      1272870 :         const char *chars = str (&l);
   10488      1272870 :         res = get_identifier_with_length (chars, l);
   10489      1272870 :         int tag = insert (res);
   10490      1272870 :         dump (dumper::TREE)
   10491         1488 :           && dump ("Read identifier:%d %N", tag, res);
   10492              :       }
   10493      1272870 :       break;
   10494              : 
   10495         3375 :     case tt_conv_id:
   10496              :       /* A conversion operator.  Get the type and recreate the
   10497              :          identifier.  */
   10498         3375 :       {
   10499         3375 :         tree type = tree_node ();
   10500         3375 :         if (!get_overrun ())
   10501              :           {
   10502         3375 :             res = type ? make_conv_op_name (type) : conv_op_identifier;
   10503         3375 :             int tag = insert (res);
   10504         3375 :             dump (dumper::TREE)
   10505           27 :               && dump ("Created conv_op:%d %S for %N", tag, res, type);
   10506              :           }
   10507              :       }
   10508              :       break;
   10509              : 
   10510         7271 :     case tt_anon_id:
   10511         7271 :     case tt_lambda_id:
   10512              :       /* An anonymous or lambda id.  */
   10513         7271 :       {
   10514         7271 :         res = make_anon_name ();
   10515         7271 :         if (tag == tt_lambda_id)
   10516         4431 :           IDENTIFIER_LAMBDA_P (res) = true;
   10517         7271 :         int tag = insert (res);
   10518         7271 :         dump (dumper::TREE)
   10519            3 :           && dump ("Read %s identifier:%d %N",
   10520            3 :                    IDENTIFIER_LAMBDA_P (res) ? "lambda" : "anon", tag, res);
   10521              :       }
   10522              :       break;
   10523              : 
   10524            8 :     case tt_internal_id:
   10525              :       /* An internal label.  */
   10526            8 :       {
   10527            8 :         const char *prefix = str ();
   10528            8 :         res = generate_internal_label (prefix);
   10529            8 :         int tag = insert (res);
   10530            8 :         dump (dumper::TREE)
   10531            1 :           && dump ("Read internal identifier:%d %N", tag, res);
   10532              :       }
   10533              :       break;
   10534              : 
   10535       193904 :     case tt_typedef_type:
   10536       193904 :       res = tree_node ();
   10537       193904 :       if (res)
   10538              :         {
   10539       193904 :           dump (dumper::TREE)
   10540           74 :             && dump ("Read %stypedef %C:%N",
   10541           74 :                      DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
   10542           74 :                      TREE_CODE (res), res);
   10543       193904 :           if (TREE_CODE (res) != TU_LOCAL_ENTITY)
   10544       193903 :             res = TREE_TYPE (res);
   10545              :         }
   10546              :       break;
   10547              : 
   10548      1511262 :     case tt_derived_type:
   10549              :       /* A type derived from some other type.  */
   10550      1511262 :       {
   10551      1511262 :         enum tree_code code = tree_code (u ());
   10552      1511262 :         res = tree_node ();
   10553              : 
   10554      1511262 :         switch (code)
   10555              :           {
   10556            0 :           default:
   10557            0 :             set_overrun ();
   10558            0 :             break;
   10559              : 
   10560        21951 :           case ARRAY_TYPE:
   10561        21951 :             {
   10562        21951 :               tree elt_type = res;
   10563        21951 :               tree domain = tree_node ();
   10564        21951 :               int dep = u ();
   10565        21951 :               if (!get_overrun ())
   10566              :                 {
   10567        21951 :                   res = build_cplus_array_type (elt_type, domain, dep);
   10568              :                   /* If we're an array of an incomplete imported type,
   10569              :                      save it for post-processing so that we can attempt
   10570              :                      to complete the type later if it will get a
   10571              :                      definition later in the cluster.  */
   10572        21951 :                   if (!dep
   10573        19089 :                       && !COMPLETE_TYPE_P (elt_type)
   10574           32 :                       && CLASS_TYPE_P (elt_type)
   10575           32 :                       && DECL_LANG_SPECIFIC (TYPE_NAME (elt_type))
   10576        21983 :                       && DECL_MODULE_IMPORT_P (TYPE_NAME (elt_type)))
   10577           32 :                     post_process_type (res);
   10578              :                 }
   10579              :             }
   10580              :             break;
   10581              : 
   10582          260 :           case COMPLEX_TYPE:
   10583          260 :             if (!get_overrun ())
   10584          260 :               res = build_complex_type (res);
   10585              :             break;
   10586              : 
   10587            9 :           case BOOLEAN_TYPE:
   10588            9 :             {
   10589            9 :               unsigned precision = u ();
   10590            9 :               if (!get_overrun ())
   10591            9 :                 res = build_nonstandard_boolean_type (precision);
   10592              :             }
   10593              :             break;
   10594              : 
   10595        20418 :           case INTEGER_TYPE:
   10596        20418 :             if (res)
   10597              :               {
   10598              :                 /* A range type (representing an array domain).  */
   10599        19129 :                 tree min = tree_node ();
   10600        19129 :                 tree max = tree_node ();
   10601              : 
   10602        19129 :                 if (!get_overrun ())
   10603        19129 :                   res = build_range_type (res, min, max);
   10604              :               }
   10605              :             else
   10606              :               {
   10607              :                 /* A new integral type (representing a bitfield).  */
   10608         1289 :                 unsigned enc = u ();
   10609         1289 :                 if (!get_overrun ())
   10610         1289 :                   res = build_nonstandard_integer_type (enc >> 1, enc & 1);
   10611              :               }
   10612              :             break;
   10613              : 
   10614       426511 :           case FUNCTION_TYPE:
   10615       426511 :           case METHOD_TYPE:
   10616       426511 :             {
   10617       426511 :               tree klass =  code == METHOD_TYPE ? tree_node () : NULL_TREE;
   10618       426511 :               tree args = tree_node ();
   10619       426511 :               if (!get_overrun ())
   10620              :                 {
   10621       426511 :                   if (klass)
   10622       270033 :                     res = build_method_type_directly (klass, res, args);
   10623              :                   else
   10624       156478 :                     res = cp_build_function_type (res, args);
   10625              :                 }
   10626              :             }
   10627              :             break;
   10628              : 
   10629          285 :           case OFFSET_TYPE:
   10630          285 :             {
   10631          285 :               tree base = tree_node ();
   10632          285 :               if (!get_overrun ())
   10633          285 :                 res = build_offset_type (base, res);
   10634              :             }
   10635              :             break;
   10636              : 
   10637       210966 :           case POINTER_TYPE:
   10638       210966 :             if (!get_overrun ())
   10639       210966 :               res = build_pointer_type (res);
   10640              :             break;
   10641              : 
   10642       177525 :           case REFERENCE_TYPE:
   10643       177525 :             {
   10644       177525 :               bool rval = bool (u ());
   10645       177525 :               if (!get_overrun ())
   10646       177525 :                 res = cp_build_reference_type (res, rval);
   10647              :             }
   10648              :             break;
   10649              : 
   10650       457638 :           case DECLTYPE_TYPE:
   10651       457638 :           case TYPEOF_TYPE:
   10652       457638 :           case DEPENDENT_OPERATOR_TYPE:
   10653       457638 :             {
   10654       457638 :               tree expr = tree_node ();
   10655       457638 :               if (!get_overrun ())
   10656              :                 {
   10657       457638 :                   res = cxx_make_type (code);
   10658       457638 :                   TYPE_VALUES_RAW (res) = expr;
   10659       457638 :                   if (code == DECLTYPE_TYPE)
   10660        23769 :                     tree_node_bools (res);
   10661       457638 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10662              :                 }
   10663              :             }
   10664              :             break;
   10665              : 
   10666         2250 :           case TRAIT_TYPE:
   10667         2250 :             {
   10668         2250 :               tree kind = tree_node ();
   10669         2250 :               tree type1 = tree_node ();
   10670         2250 :               tree type2 = tree_node ();
   10671         2250 :               if (!get_overrun ())
   10672              :                 {
   10673         2250 :                   res = cxx_make_type (TRAIT_TYPE);
   10674         2250 :                   TRAIT_TYPE_KIND_RAW (res) = kind;
   10675         2250 :                   TRAIT_TYPE_TYPE1 (res) = type1;
   10676         2250 :                   TRAIT_TYPE_TYPE2 (res) = type2;
   10677         2250 :                   SET_TYPE_STRUCTURAL_EQUALITY (res);
   10678              :                 }
   10679              :             }
   10680              :             break;
   10681              : 
   10682        61834 :           case TYPE_ARGUMENT_PACK:
   10683        61834 :             if (!get_overrun ())
   10684              :               {
   10685        61834 :                 tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   10686        61834 :                 ARGUMENT_PACK_ARGS (pack) = res;
   10687        61834 :                 res = pack;
   10688              :               }
   10689              :             break;
   10690              : 
   10691        65216 :           case TYPE_PACK_EXPANSION:
   10692        65216 :             {
   10693        65216 :               bool local = u ();
   10694        65216 :               tree param_packs = tree_node ();
   10695        65216 :               tree extra_args = tree_node ();
   10696        65216 :               if (!get_overrun ())
   10697              :                 {
   10698        65216 :                   tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
   10699        65216 :                   SET_TYPE_STRUCTURAL_EQUALITY (expn);
   10700        65216 :                   PACK_EXPANSION_PATTERN (expn) = res;
   10701       130432 :                   PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
   10702        65216 :                   PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
   10703        65216 :                   PACK_EXPANSION_LOCAL_P (expn) = local;
   10704        65216 :                   res = expn;
   10705              :                 }
   10706              :             }
   10707              :             break;
   10708              : 
   10709           21 :           case PACK_INDEX_TYPE:
   10710           21 :             {
   10711           21 :               tree pack = tree_node ();
   10712           21 :               tree index = tree_node ();
   10713           21 :               if (!get_overrun ())
   10714           21 :                 res = make_pack_index (pack, index);
   10715              :             }
   10716              :             break;
   10717              : 
   10718        66202 :           case TYPENAME_TYPE:
   10719        66202 :             {
   10720        66202 :               tree ctx = tree_node ();
   10721        66202 :               tree name = tree_node ();
   10722        66202 :               tree fullname = tree_node ();
   10723        66202 :               enum tag_types tag_type = tag_types (u ());
   10724              : 
   10725        66202 :               if (!get_overrun ())
   10726        66202 :                 res = build_typename_type (ctx, name, fullname, tag_type);
   10727              :             }
   10728              :             break;
   10729              : 
   10730           52 :           case UNBOUND_CLASS_TEMPLATE:
   10731           52 :             {
   10732           52 :               tree ctx = tree_node ();
   10733           52 :               tree name = tree_node ();
   10734           52 :               tree parms = tree_node ();
   10735              : 
   10736           52 :               if (!get_overrun ())
   10737           52 :                 res = make_unbound_class_template_raw (ctx, name, parms);
   10738              :             }
   10739              :             break;
   10740              : 
   10741              :           case VECTOR_TYPE:
   10742              :             {
   10743              :               poly_uint64 nunits;
   10744           60 :               for (unsigned ix = 0; ix != NUM_POLY_INT_COEFFS; ix++)
   10745           30 :                 nunits.coeffs[ix] = wu ();
   10746           30 :               if (!get_overrun ())
   10747           30 :                 res = build_vector_type (res, nunits);
   10748              :             }
   10749              :             break;
   10750              : 
   10751              :           /* OPAQUE_TYPE can be more things, but here we assume it represents
   10752              :              std::meta::info.  Unfortunately here it's not possible to check
   10753              :              REFLECTION_TYPE_P.  */
   10754           90 :           case OPAQUE_TYPE:
   10755           90 :             if (!get_overrun ())
   10756           90 :               res = meta_info_type_node;
   10757              :             break;
   10758              : 
   10759            4 :           case SPLICE_SCOPE:
   10760            4 :             {
   10761            4 :               bool type = u ();
   10762            4 :               tree expr = tree_node ();
   10763              : 
   10764            4 :               if (!get_overrun ())
   10765            4 :                 res = make_splice_scope (expr, type);
   10766              :             }
   10767              :             break;
   10768              :           }
   10769              : 
   10770              :         /* In the exporting TU, a derived type with attributes was built by
   10771              :            build_type_attribute_variant as a distinct copy, with itself as
   10772              :            TYPE_MAIN_VARIANT.  We repeat that on import to get the version
   10773              :            without attributes as TYPE_CANONICAL.  */
   10774      1511262 :         if (tree attribs = tree_node ())
   10775        17292 :           res = cp_build_type_attribute_variant (res, attribs);
   10776              : 
   10777      1511262 :         int tag = i ();
   10778      1511262 :         if (!tag)
   10779              :           {
   10780      1294329 :             tag = insert (res);
   10781      1294329 :             if (res)
   10782      1294329 :               dump (dumper::TREE)
   10783          678 :                 && dump ("Created:%d derived type %C", tag, code);
   10784              :           }
   10785              :         else
   10786       216933 :           res = back_ref (tag);
   10787              :       }
   10788              :       break;
   10789              : 
   10790       438636 :     case tt_variant_type:
   10791              :       /* Variant of some type.  */
   10792       438636 :       {
   10793       438636 :         res = tree_node ();
   10794       438636 :         int flags = i ();
   10795       438636 :         if (get_overrun ())
   10796              :           ;
   10797       438636 :         else if (flags < 0)
   10798              :           /* No change.  */;
   10799       209881 :         else if (TREE_CODE (res) == FUNCTION_TYPE
   10800       209881 :                  || TREE_CODE (res) == METHOD_TYPE)
   10801              :           {
   10802       208169 :             cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
   10803       208169 :             bool late = (flags >> 2) & 1;
   10804       208169 :             cp_cv_quals quals = cp_cv_quals (flags >> 3);
   10805              : 
   10806       208169 :             tree raises = tree_node ();
   10807       208169 :             if (raises == error_mark_node)
   10808         7809 :               raises = TYPE_RAISES_EXCEPTIONS (res);
   10809              : 
   10810       208169 :             res = build_cp_fntype_variant (res, rqual, raises, late);
   10811       208169 :             if (TREE_CODE (res) == FUNCTION_TYPE)
   10812        73662 :               res = apply_memfn_quals (res, quals, rqual);
   10813              :           }
   10814              :         else
   10815              :           {
   10816         1712 :             res = build_aligned_type (res, (1u << flags) >> 1);
   10817         1712 :             TYPE_USER_ALIGN (res) = true;
   10818              :           }
   10819              : 
   10820       438636 :         int quals = i ();
   10821       438636 :         if (quals >= 0 && !get_overrun ())
   10822       230039 :           res = cp_build_qualified_type (res, quals);
   10823              : 
   10824       438636 :         int tag = i ();
   10825       438636 :         if (!tag)
   10826              :           {
   10827       438636 :             tag = insert (res);
   10828       438636 :             if (res)
   10829       438636 :               dump (dumper::TREE)
   10830          292 :                 && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
   10831              :           }
   10832              :         else
   10833            0 :           res = back_ref (tag);
   10834              :       }
   10835              :       break;
   10836              : 
   10837         5201 :     case tt_tinfo_var:
   10838         5201 :     case tt_tinfo_typedef:
   10839              :       /* A tinfo var or typedef.  */
   10840         5201 :       {
   10841         5201 :         bool is_var = tag == tt_tinfo_var;
   10842         5201 :         unsigned ix = u ();
   10843         5201 :         tree type = NULL_TREE;
   10844              : 
   10845         5201 :         if (is_var)
   10846              :           {
   10847         3119 :             tree name = tree_node ();
   10848         3119 :             type = tree_node ();
   10849              : 
   10850         3119 :             if (!get_overrun ())
   10851         3119 :               res = get_tinfo_decl_direct (type, name, int (ix));
   10852              :           }
   10853              :         else
   10854              :           {
   10855         2082 :             if (!get_overrun ())
   10856              :               {
   10857         2082 :                 type = get_pseudo_tinfo_type (ix);
   10858         2082 :                 res = TYPE_NAME (type);
   10859              :               }
   10860              :           }
   10861         5201 :         if (res)
   10862              :           {
   10863         5201 :             int tag = insert (res);
   10864         5201 :             dump (dumper::TREE)
   10865           36 :               && dump ("Created tinfo_%s:%d %S:%u for %N",
   10866              :                        is_var ? "var" : "decl", tag, res, ix, type);
   10867         5201 :             if (!is_var)
   10868              :               {
   10869         2082 :                 tag = insert (type);
   10870         2082 :                 dump (dumper::TREE)
   10871           12 :                   && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
   10872              :               }
   10873              :           }
   10874              :       }
   10875              :       break;
   10876              : 
   10877         1164 :     case tt_ptrmem_type:
   10878              :       /* A pointer to member function.  */
   10879         1164 :       {
   10880         1164 :         tree type = tree_node ();
   10881         1164 :         if (type && TREE_CODE (type) == POINTER_TYPE
   10882         2328 :             && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
   10883              :           {
   10884         1164 :             res = build_ptrmemfunc_type (type);
   10885         1164 :             int tag = insert (res);
   10886         1167 :             dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
   10887              :           }
   10888              :         else
   10889            0 :           set_overrun ();
   10890              :       }
   10891              :       break;
   10892              : 
   10893            9 :     case tt_nttp_var:
   10894              :       /* An NTTP object. */
   10895            9 :       {
   10896            9 :         tree init = tree_node ();
   10897            9 :         tree name = tree_node ();
   10898            9 :         if (!get_overrun ())
   10899              :           {
   10900              :             /* We don't want to check the initializer as that may require
   10901              :                name lookup, which could recursively start lazy loading.
   10902              :                Instead we know that INIT is already valid so we can just
   10903              :                apply that directly.  */
   10904            9 :             res = get_template_parm_object (init, name, /*check_init=*/false);
   10905            9 :             int tag = insert (res);
   10906            9 :             dump (dumper::TREE)
   10907            0 :               && dump ("Created nttp object:%d %N", tag, name);
   10908            9 :             vec_safe_push (post_load_decls, res);
   10909              :           }
   10910              :       }
   10911              :       break;
   10912              : 
   10913         8686 :     case tt_enum_value:
   10914              :       /* An enum const value.  */
   10915         8686 :       {
   10916         8686 :         if (tree decl = tree_node ())
   10917              :           {
   10918         8704 :             dump (dumper::TREE) && dump ("Read enum value %N", decl);
   10919         8686 :             res = DECL_INITIAL (decl);
   10920              :           }
   10921              : 
   10922         8686 :         if (!res)
   10923            0 :           set_overrun ();
   10924              :       }
   10925              :       break;
   10926              : 
   10927        14079 :     case tt_enum_decl:
   10928              :       /* An enum decl.  */
   10929        14079 :       {
   10930        14079 :         tree ctx = tree_node ();
   10931        14079 :         tree name = tree_node ();
   10932              : 
   10933        14079 :         if (!get_overrun ()
   10934        14079 :             && TREE_CODE (ctx) == ENUMERAL_TYPE)
   10935        14079 :           res = find_enum_member (ctx, name);
   10936              : 
   10937        14079 :         if (!res)
   10938            0 :           set_overrun ();
   10939              :         else
   10940              :           {
   10941        14079 :             int tag = insert (res);
   10942        14079 :             dump (dumper::TREE)
   10943           18 :               && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
   10944              :           }
   10945              :       }
   10946              :       break;
   10947              : 
   10948        10589 :     case tt_data_member:
   10949              :       /* A data member.  */
   10950        10589 :       {
   10951        10589 :         tree ctx = tree_node ();
   10952        10589 :         tree name = tree_node ();
   10953              : 
   10954        10589 :         if (!get_overrun ()
   10955        10589 :             && RECORD_OR_UNION_TYPE_P (ctx))
   10956              :           {
   10957        10589 :             if (name)
   10958         9523 :               res = lookup_class_binding (ctx, name);
   10959              :             else
   10960         1066 :               res = lookup_field_ident (ctx, u ());
   10961              : 
   10962        10589 :             if (!res
   10963        10589 :                 || (TREE_CODE (res) != FIELD_DECL
   10964        10589 :                     && TREE_CODE (res) != USING_DECL)
   10965        21178 :                 || DECL_CONTEXT (res) != ctx)
   10966            0 :               res = NULL_TREE;
   10967              :           }
   10968              : 
   10969        10589 :         if (!res)
   10970            0 :           set_overrun ();
   10971              :         else
   10972              :           {
   10973        10589 :             int tag = insert (res);
   10974        10589 :             dump (dumper::TREE)
   10975           26 :               && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
   10976              :           }
   10977              :       }
   10978              :       break;
   10979              : 
   10980         1702 :     case tt_binfo:
   10981              :       /* A BINFO.  Walk the tree of the dominating type.  */
   10982         1702 :       {
   10983         1702 :         tree type;
   10984         1702 :         unsigned ix = binfo_mergeable (&type);
   10985         1702 :         if (type)
   10986              :           {
   10987         1702 :             res = TYPE_BINFO (type);
   10988         1803 :             for (; ix && res; res = TREE_CHAIN (res))
   10989          101 :               ix--;
   10990         1702 :             if (!res)
   10991            0 :               set_overrun ();
   10992              :           }
   10993              : 
   10994         1702 :         if (get_overrun ())
   10995              :           break;
   10996              : 
   10997              :         /* Insert binfo into backreferences.  */
   10998         1702 :         tag = insert (res);
   10999         1702 :         dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
   11000              :       }
   11001         1702 :       break;
   11002              : 
   11003           27 :     case tt_vtable:
   11004           27 :       {
   11005           27 :         unsigned ix = u ();
   11006           27 :         tree ctx = tree_node ();
   11007           27 :         dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
   11008           27 :         if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
   11009           27 :           for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
   11010           27 :             if (!ix--)
   11011              :               break;
   11012           27 :         if (!res)
   11013            0 :           set_overrun ();
   11014              :       }
   11015              :       break;
   11016              : 
   11017            0 :     case tt_thunk:
   11018            0 :       {
   11019            0 :         int fixed = i ();
   11020            0 :         tree target = tree_node ();
   11021            0 :         tree virt = tree_node ();
   11022              : 
   11023            0 :         for (tree thunk = DECL_THUNKS (target);
   11024            0 :              thunk; thunk = DECL_CHAIN (thunk))
   11025            0 :           if (THUNK_FIXED_OFFSET (thunk) == fixed
   11026            0 :               && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
   11027            0 :               && (!virt
   11028            0 :                   || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
   11029              :             {
   11030            0 :               res = thunk;
   11031            0 :               break;
   11032              :             }
   11033              : 
   11034            0 :         int tag = insert (res);
   11035            0 :         if (res)
   11036            0 :           dump (dumper::TREE)
   11037            0 :             && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
   11038              :         else
   11039            0 :           set_overrun ();
   11040              :       }
   11041              :       break;
   11042              : 
   11043       156131 :     case tt_clone_ref:
   11044       156131 :       {
   11045       156131 :         tree target = tree_node ();
   11046       156131 :         tree name = tree_node ();
   11047              : 
   11048       156131 :         if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
   11049              :           {
   11050       156131 :             tree clone;
   11051       243010 :             FOR_EVERY_CLONE (clone, target)
   11052       243010 :               if (DECL_NAME (clone) == name)
   11053              :                 {
   11054       156131 :                   res = clone;
   11055       156131 :                   break;
   11056              :                 }
   11057              :           }
   11058              : 
   11059              :         /* A clone might have a different vtable entry.  */
   11060       156131 :         if (res && DECL_VIRTUAL_P (res))
   11061         8987 :           DECL_VINDEX (res) = tree_node ();
   11062              : 
   11063       156131 :         if (!res)
   11064            0 :           set_overrun ();
   11065       156131 :         int tag = insert (res);
   11066       156131 :         if (res)
   11067       156131 :           dump (dumper::TREE)
   11068          230 :             && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
   11069              :         else
   11070            0 :           set_overrun ();
   11071              :        }
   11072              :       break;
   11073              : 
   11074      1133265 :     case tt_entity:
   11075              :       /* Index into the entity table.  Perhaps not loaded yet!  */
   11076      1133265 :       {
   11077      1133265 :         unsigned origin = state->slurp->remap_module (u ());
   11078      1133265 :         unsigned ident = u ();
   11079      1133265 :         module_state *from = (*modules)[origin];
   11080              : 
   11081      1133265 :         if (!origin || ident >= from->entity_num)
   11082            0 :           set_overrun ();
   11083      1133265 :         if (!get_overrun ())
   11084              :           {
   11085      1133265 :             binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
   11086      1133265 :             if (slot->is_lazy ())
   11087        32177 :               if (!from->lazy_load (ident, slot))
   11088            0 :                 set_overrun ();
   11089      1133265 :             res = *slot;
   11090              :           }
   11091              : 
   11092      1133265 :         if (res)
   11093              :           {
   11094      1133265 :             const char *kind = (origin != state->mod ? "Imported" : "Named");
   11095      1133265 :             int tag = insert (res);
   11096      1133265 :             dump (dumper::TREE)
   11097          605 :               && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
   11098          605 :                        res, (*modules)[origin]);
   11099              : 
   11100      1133265 :             if (!add_indirects (res))
   11101              :               {
   11102            0 :                 set_overrun ();
   11103            0 :                 res = NULL_TREE;
   11104              :               }
   11105              :           }
   11106              :       }
   11107              :       break;
   11108              : 
   11109         2964 :     case tt_template:
   11110              :       /* A template.  */
   11111         2964 :       if (tree tpl = tree_node ())
   11112              :         {
   11113         2964 :           res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
   11114         2964 :                  tpl : DECL_TEMPLATE_RESULT (tpl));
   11115         2964 :           dump (dumper::TREE)
   11116            9 :             && dump ("Read template %C:%N", TREE_CODE (res), res);
   11117              :         }
   11118              :       break;
   11119              :     }
   11120              : 
   11121     97875246 :   if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
   11122              :     {
   11123              :       /* Mark decl used as mark_used does -- we cannot call
   11124              :          mark_used in the middle of streaming, we only need a subset
   11125              :          of its functionality.   */
   11126       771327 :       TREE_USED (res) = true;
   11127              : 
   11128              :       /* And for structured bindings also the underlying decl.  */
   11129       771327 :       if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
   11130         1923 :         TREE_USED (DECL_DECOMP_BASE (res)) = true;
   11131              : 
   11132       771327 :       if (DECL_CLONED_FUNCTION_P (res))
   11133         7211 :         TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
   11134              :     }
   11135              : 
   11136     97875246 :   dump.outdent ();
   11137     97875246 :   return res;
   11138              : }
   11139              : 
   11140              : void
   11141      2399956 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
   11142              : {
   11143      2399956 :   if (!parms)
   11144              :     return;
   11145              : 
   11146      1619163 :   if (TREE_VISITED (parms))
   11147              :     {
   11148       675925 :       ref_node (parms);
   11149       675925 :       return;
   11150              :     }
   11151              : 
   11152       943238 :   tpl_parms (TREE_CHAIN (parms), tpl_levels);
   11153              : 
   11154       943238 :   tree vec = TREE_VALUE (parms);
   11155       943238 :   unsigned len = TREE_VEC_LENGTH (vec);
   11156              :   /* Depth.  */
   11157       943238 :   int tag = insert (parms);
   11158       943238 :   if (streaming_p ())
   11159              :     {
   11160       250291 :       i (len + 1);
   11161       250357 :       dump (dumper::TREE)
   11162           66 :         && dump ("Writing template parms:%d level:%N length:%d",
   11163           66 :                  tag, TREE_PURPOSE (parms), len);
   11164              :     }
   11165       943238 :   tree_node (TREE_PURPOSE (parms));
   11166              : 
   11167      3554989 :   for (unsigned ix = 0; ix != len; ix++)
   11168              :     {
   11169      1668513 :       tree parm = TREE_VEC_ELT (vec, ix);
   11170      1668513 :       tree decl = TREE_VALUE (parm);
   11171              : 
   11172      1668513 :       gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
   11173      1668513 :       if (CHECKING_P)
   11174      1668513 :         switch (TREE_CODE (decl))
   11175              :           {
   11176            0 :           default: gcc_unreachable ();
   11177              : 
   11178         4155 :           case TEMPLATE_DECL:
   11179         4155 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
   11180              :                         && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
   11181              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11182              :             break;
   11183              : 
   11184      1556257 :           case TYPE_DECL:
   11185      1556257 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
   11186              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11187              :             break;
   11188              : 
   11189       108101 :           case PARM_DECL:
   11190       108101 :             gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
   11191              :                         && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
   11192              :                             == CONST_DECL)
   11193              :                         && (DECL_TEMPLATE_PARM_P
   11194              :                             (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
   11195              :             break;
   11196              :           }
   11197              : 
   11198      1668513 :       tree_node (decl);
   11199      1668513 :       tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
   11200              :     }
   11201              : 
   11202       943238 :   tpl_levels++;
   11203              : }
   11204              : 
   11205              : tree
   11206       329653 : trees_in::tpl_parms (unsigned &tpl_levels)
   11207              : {
   11208       329653 :   tree parms = NULL_TREE;
   11209              : 
   11210       695194 :   while (int len = i ())
   11211              :     {
   11212       365541 :       if (len < 0)
   11213              :         {
   11214       197770 :           parms = back_ref (len);
   11215       197770 :           continue;
   11216              :         }
   11217              : 
   11218       167771 :       len -= 1;
   11219       167771 :       parms = tree_cons (NULL_TREE, NULL_TREE, parms);
   11220       167771 :       int tag = insert (parms);
   11221       167771 :       TREE_PURPOSE (parms) = tree_node ();
   11222              : 
   11223       167771 :       dump (dumper::TREE)
   11224          105 :         && dump ("Reading template parms:%d level:%N length:%d",
   11225          105 :                  tag, TREE_PURPOSE (parms), len);
   11226              : 
   11227       167771 :       tree vec = make_tree_vec (len);
   11228       612665 :       for (int ix = 0; ix != len; ix++)
   11229              :         {
   11230       277123 :           tree decl = tree_node ();
   11231       277123 :           if (!decl)
   11232              :             return NULL_TREE;
   11233              : 
   11234       277123 :           tree parm = build_tree_list (NULL, decl);
   11235       277123 :           TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
   11236              : 
   11237       277123 :           TREE_VEC_ELT (vec, ix) = parm;
   11238              :         }
   11239              : 
   11240       167771 :       TREE_VALUE (parms) = vec;
   11241       167771 :       tpl_levels++;
   11242              :     }
   11243              : 
   11244              :   return parms;
   11245              : }
   11246              : 
   11247              : void
   11248      1456718 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11249              : {
   11250      1456718 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11251      2399956 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11252              :     {
   11253       943238 :       tree vec = TREE_VALUE (parms);
   11254              : 
   11255       943238 :       tree_node (TREE_TYPE (vec));
   11256      2611751 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11257              :         {
   11258      1668513 :           tree parm = TREE_VEC_ELT (vec, ix);
   11259      1668513 :           tree dflt = TREE_PURPOSE (parm);
   11260      1668513 :           tree_node (dflt);
   11261              : 
   11262              :           /* Template template parameters need a context of their owning
   11263              :              template. This is quite tricky to infer correctly on stream-in
   11264              :              (see PR c++/98881) so we'll just provide it directly.  */
   11265      1668513 :           tree decl = TREE_VALUE (parm);
   11266      1668513 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11267         4155 :             tree_node (DECL_CONTEXT (decl));
   11268              :         }
   11269              :     }
   11270      1456718 : }
   11271              : 
   11272              : bool
   11273       329653 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11274              : {
   11275       329653 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11276       497424 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11277              :     {
   11278       167771 :       tree vec = TREE_VALUE (parms);
   11279              : 
   11280       167771 :       TREE_TYPE (vec) = tree_node ();
   11281       444894 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11282              :         {
   11283       277123 :           tree parm = TREE_VEC_ELT (vec, ix);
   11284       277123 :           tree dflt = tree_node ();
   11285       277123 :           TREE_PURPOSE (parm) = dflt;
   11286              : 
   11287       277123 :           tree decl = TREE_VALUE (parm);
   11288       277123 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11289          890 :             DECL_CONTEXT (decl) = tree_node ();
   11290              : 
   11291       277123 :           if (get_overrun ())
   11292              :             return false;
   11293              :         }
   11294              :     }
   11295              :   return true;
   11296              : }
   11297              : 
   11298              : /* PARMS is a LIST, one node per level.
   11299              :    TREE_VALUE is a TREE_VEC of parm info for that level.
   11300              :    each ELT is a TREE_LIST
   11301              :    TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
   11302              :    TREE_PURPOSE is the default value.  */
   11303              : 
   11304              : void
   11305      1456718 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
   11306              : {
   11307      1456718 :   tree parms = DECL_TEMPLATE_PARMS (tpl);
   11308      1456718 :   tpl_parms (parms, *tpl_levels);
   11309              : 
   11310              :   /* Mark end.  */
   11311      1456718 :   if (streaming_p ())
   11312       485051 :     u (0);
   11313              : 
   11314      1456718 :   if (*tpl_levels)
   11315       885687 :     tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
   11316      1456718 : }
   11317              : 
   11318              : bool
   11319       329653 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
   11320              : {
   11321       329653 :   tree parms = tpl_parms (*tpl_levels);
   11322       329653 :   if (!parms)
   11323              :     return false;
   11324              : 
   11325       329653 :   DECL_TEMPLATE_PARMS (tpl) = parms;
   11326              : 
   11327       329653 :   if (*tpl_levels)
   11328       165673 :     TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
   11329              : 
   11330              :   return true;
   11331              : }
   11332              : 
   11333              : /* Stream skeleton parm nodes, with their flags, type & parm indices.
   11334              :    All the parms will have consecutive tags.  */
   11335              : 
   11336              : void
   11337      2039334 : trees_out::fn_parms_init (tree fn)
   11338              : {
   11339              :   /* First init them.  */
   11340      2039334 :   int base_tag = ref_num - 1;
   11341      2039334 :   int ix = 0;
   11342      2039334 :   for (tree parm = DECL_ARGUMENTS (fn);
   11343      6159959 :        parm; parm = DECL_CHAIN (parm), ix++)
   11344              :     {
   11345      4120625 :       if (streaming_p ())
   11346              :         {
   11347      1373508 :           start (parm);
   11348      1373508 :           tree_node_bools (parm);
   11349              :         }
   11350      4120625 :       int tag = insert (parm);
   11351      4120625 :       gcc_checking_assert (base_tag - ix == tag);
   11352              :     }
   11353              :   /* Mark the end.  */
   11354      2039334 :   if (streaming_p ())
   11355       680083 :     u (0);
   11356              : 
   11357              :   /* Now stream their contents.  */
   11358      2039334 :   ix = 0;
   11359      2039334 :   for (tree parm = DECL_ARGUMENTS (fn);
   11360      6159959 :        parm; parm = DECL_CHAIN (parm), ix++)
   11361              :     {
   11362      4120625 :       if (streaming_p ())
   11363      1373508 :         dump (dumper::TREE)
   11364          222 :           && dump ("Writing parm:%d %u (%N) of %N",
   11365              :                    base_tag - ix, ix, parm, fn);
   11366      4120625 :       tree_node_vals (parm);
   11367              :     }
   11368              : 
   11369      2039334 :   if (!streaming_p ())
   11370              :     {
   11371              :       /* We must walk contract specifiers so the dependency graph is
   11372              :          complete.  */
   11373      1359251 :       if (tree contracts = get_fn_contract_specifiers (fn))
   11374            0 :         for (tree contract : tree_vec_range (contracts))
   11375            0 :           tree_node (contract);
   11376              :     }
   11377              : 
   11378              :   /* Write a reference to contracts pre/post functions, if any, to avoid
   11379              :      regenerating them in importers.  */
   11380      2039334 :   tree_node (DECL_PRE_FN (fn));
   11381      2039334 :   tree_node (DECL_POST_FN (fn));
   11382      2039334 : }
   11383              : 
   11384              : /* Build skeleton parm nodes, read their flags, type & parm indices.  */
   11385              : 
   11386              : int
   11387       465214 : trees_in::fn_parms_init (tree fn)
   11388              : {
   11389       465214 :   int base_tag = ~(int)back_refs.length ();
   11390              : 
   11391       465214 :   tree *parm_ptr = &DECL_ARGUMENTS (fn);
   11392       465214 :   int ix = 0;
   11393      1405454 :   for (; int code = u (); ix++)
   11394              :     {
   11395       940240 :       tree parm = start (code);
   11396       940240 :       if (!tree_node_bools (parm))
   11397              :         return 0;
   11398              : 
   11399       940240 :       int tag = insert (parm);
   11400       940240 :       gcc_checking_assert (base_tag - ix == tag);
   11401       940240 :       *parm_ptr = parm;
   11402       940240 :       parm_ptr = &DECL_CHAIN (parm);
   11403       940240 :     }
   11404              : 
   11405       465214 :   ix = 0;
   11406       465214 :   for (tree parm = DECL_ARGUMENTS (fn);
   11407      1405454 :        parm; parm = DECL_CHAIN (parm), ix++)
   11408              :     {
   11409       940240 :       dump (dumper::TREE)
   11410          362 :         && dump ("Reading parm:%d %u (%N) of %N",
   11411              :                  base_tag - ix, ix, parm, fn);
   11412       940240 :       if (!tree_node_vals (parm))
   11413              :         return 0;
   11414              : 
   11415              :       /* Apply relevant attributes.
   11416              :          FIXME should probably use cplus_decl_attributes for this,
   11417              :          but it's not yet ready for modules.  */
   11418              : 
   11419              :       /* TREE_USED is deliberately not streamed for most declarations,
   11420              :          but needs to be set if we have the [[maybe_unused]] attribute.  */
   11421       940240 :       if (lookup_attribute ("unused", DECL_ATTRIBUTES (parm))
   11422       940240 :           || lookup_attribute ("maybe_unused", DECL_ATTRIBUTES (parm)))
   11423              :         {
   11424         2036 :           TREE_USED (parm) = true;
   11425         2036 :           DECL_READ_P (parm) = true;
   11426              :         }
   11427              :     }
   11428              : 
   11429              :   /* Reload references to contract functions, if any.  */
   11430       465214 :   tree pre_fn = tree_node ();
   11431       465214 :   tree post_fn = tree_node ();
   11432       465214 :   set_contract_functions (fn, pre_fn, post_fn);
   11433              : 
   11434       465214 :   return base_tag;
   11435              : }
   11436              : 
   11437              : /* Read the remaining parm node data.  Replace with existing (if
   11438              :    non-null) in the map.  */
   11439              : 
   11440              : void
   11441       465214 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
   11442              : {
   11443       663051 :   tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
   11444       465214 :   tree parms = DECL_ARGUMENTS (fn);
   11445      1405454 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   11446              :     {
   11447       940240 :       if (existing_parm)
   11448              :         {
   11449       574810 :           if (is_defn && !DECL_SAVED_TREE (existing))
   11450              :             {
   11451              :               /* If we're about to become the definition, set the
   11452              :                  names of the parms from us.  */
   11453        14545 :               DECL_NAME (existing_parm) = DECL_NAME (parm);
   11454        14545 :               DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
   11455              : 
   11456              :               /* And some other flags important for codegen are only set
   11457              :                  by the definition.  */
   11458        14545 :               TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
   11459        14545 :               DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
   11460        14545 :               DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
   11461        14545 :               DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
   11462              : 
   11463              :               /* Invisiref parms had their types adjusted by cp_genericize. */
   11464        14545 :               if (DECL_BY_REFERENCE (parm))
   11465              :                 {
   11466            6 :                   TREE_TYPE (existing_parm) = TREE_TYPE (parm);
   11467            6 :                   relayout_decl (existing_parm);
   11468              :                 }
   11469              :             }
   11470              : 
   11471       390813 :           back_refs[~tag] = existing_parm;
   11472       390813 :           existing_parm = DECL_CHAIN (existing_parm);
   11473              :         }
   11474       940240 :       tag--;
   11475              :     }
   11476       465214 : }
   11477              : 
   11478              : /* Encode into KEY the position of the local type (class or enum)
   11479              :    declaration DECL within FN.  The position is encoded as the
   11480              :    index of the innermost BLOCK (numbered in BFS order) along with
   11481              :    the index within its BLOCK_VARS list.  */
   11482              : 
   11483              : void
   11484        22974 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
   11485              : {
   11486        22974 :   auto_vec<tree, 4> blocks;
   11487        22974 :   blocks.quick_push (DECL_INITIAL (fn));
   11488        22974 :   unsigned block_ix = 0;
   11489       113310 :   while (block_ix != blocks.length ())
   11490              :     {
   11491        45168 :       tree block = blocks[block_ix];
   11492        45168 :       unsigned decl_ix = 0;
   11493       136800 :       for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11494              :         {
   11495       114606 :           if (TREE_CODE (var) != TYPE_DECL)
   11496        72741 :             continue;
   11497        41865 :           if (var == decl)
   11498              :             {
   11499        22974 :               key.index = (block_ix << 10) | decl_ix;
   11500        22974 :               return;
   11501              :             }
   11502        18891 :           ++decl_ix;
   11503              :         }
   11504        46380 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11505        24186 :         blocks.safe_push (sub);
   11506        22194 :       ++block_ix;
   11507              :     }
   11508              : 
   11509              :   /* Not-found value.  */
   11510            0 :   key.index = 1023;
   11511        22974 : }
   11512              : 
   11513              : /* Look up the local type corresponding at the position encoded by
   11514              :    KEY within FN and named NAME.  */
   11515              : 
   11516              : tree
   11517         4782 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
   11518              : {
   11519         4782 :   if (!DECL_INITIAL (fn))
   11520              :     return NULL_TREE;
   11521              : 
   11522         1931 :   const unsigned block_pos = key.index >> 10;
   11523         1931 :   const unsigned decl_pos = key.index & 1023;
   11524              : 
   11525         1931 :   if (decl_pos == 1023)
   11526              :     return NULL_TREE;
   11527              : 
   11528         1931 :   auto_vec<tree, 4> blocks;
   11529         1931 :   blocks.quick_push (DECL_INITIAL (fn));
   11530         1931 :   unsigned block_ix = 0;
   11531         8941 :   while (block_ix != blocks.length ())
   11532              :     {
   11533         3505 :       tree block = blocks[block_ix];
   11534         3505 :       if (block_ix == block_pos)
   11535              :         {
   11536         1931 :           unsigned decl_ix = 0;
   11537         5243 :           for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11538              :             {
   11539         5243 :               if (TREE_CODE (var) != TYPE_DECL)
   11540         2340 :                 continue;
   11541              :               /* Prefer using the identifier as the key for more robustness
   11542              :                  to ODR violations, except for anonymous types since their
   11543              :                  compiler-generated identifiers aren't stable.  */
   11544         5806 :               if (IDENTIFIER_ANON_P (name)
   11545         2903 :                   ? decl_ix == decl_pos
   11546          320 :                   : DECL_NAME (var) == name)
   11547              :                 return var;
   11548          972 :               ++decl_ix;
   11549              :             }
   11550              :           return NULL_TREE;
   11551              :         }
   11552         3257 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11553         1683 :         blocks.safe_push (sub);
   11554         1574 :       ++block_ix;
   11555              :     }
   11556              : 
   11557              :   return NULL_TREE;
   11558         1931 : }
   11559              : 
   11560              : /* DEP is the depset of some decl we're streaming by value.  Determine
   11561              :    the merging behaviour.  */
   11562              : 
   11563              : merge_kind
   11564      5051696 : trees_out::get_merge_kind (tree decl, depset *dep)
   11565              : {
   11566      5051696 :   if (!dep)
   11567              :     {
   11568      1056657 :       if (VAR_OR_FUNCTION_DECL_P (decl))
   11569              :         {
   11570              :           /* Any var or function with template info should have DEP.  */
   11571       597455 :           gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   11572              :                                || !DECL_TEMPLATE_INFO (decl));
   11573       597455 :           if (DECL_LOCAL_DECL_P (decl))
   11574              :             return MK_unique;
   11575              :         }
   11576              : 
   11577              :       /* Either unique, or some member of a class that cannot have an
   11578              :          out-of-class definition.  For instance a FIELD_DECL.  */
   11579      1056325 :       tree ctx = CP_DECL_CONTEXT (decl);
   11580      1056325 :       if (TREE_CODE (ctx) == FUNCTION_DECL)
   11581              :         {
   11582              :           /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
   11583              :              this isn't permitting them to have one.   */
   11584       688367 :           gcc_checking_assert (TREE_CODE (decl) == USING_DECL
   11585              :                                || TREE_CODE (decl) == NAMESPACE_DECL
   11586              :                                || !DECL_LANG_SPECIFIC (decl)
   11587              :                                || !DECL_TEMPLATE_INFO (decl));
   11588              : 
   11589              :           return MK_unique;
   11590              :         }
   11591              : 
   11592       367958 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   11593       367958 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   11594              :         return MK_local_friend;
   11595              : 
   11596       367958 :       gcc_checking_assert (TYPE_P (ctx));
   11597              : 
   11598              :       /* Internal-only types will not need to dedup their members.  */
   11599       367958 :       if (!DECL_CONTEXT (TYPE_NAME (ctx)))
   11600              :         return MK_unique;
   11601              : 
   11602       367902 :       if (TREE_CODE (decl) == USING_DECL)
   11603              :         return MK_field;
   11604              : 
   11605       242589 :       if (TREE_CODE (decl) == FIELD_DECL)
   11606              :         {
   11607       179497 :           if (DECL_NAME (decl))
   11608              :             {
   11609              :               /* Anonymous FIELD_DECLs have a NULL name.  */
   11610       145650 :               gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
   11611              :               return MK_named;
   11612              :             }
   11613              : 
   11614        33847 :           if (walking_bit_field_unit)
   11615              :             {
   11616              :               /* The underlying storage unit for a bitfield.  We do not
   11617              :                  need to dedup it, because it's only reachable through
   11618              :                  the bitfields it represents.  And those are deduped.  */
   11619              :               // FIXME: Is that assertion correct -- do we ever fish it
   11620              :               // out and put it in an expr?
   11621          578 :               gcc_checking_assert (!DECL_NAME (decl)
   11622              :                                    && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   11623              :                                    && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
   11624          578 :               gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
   11625              :                                     ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   11626              :                                     : TREE_CODE (TREE_TYPE (decl)))
   11627              :                                    == INTEGER_TYPE);
   11628              :               return MK_unique;
   11629              :             }
   11630              : 
   11631              :           return MK_field;
   11632              :         }
   11633              : 
   11634        63092 :       if (TREE_CODE (decl) == CONST_DECL)
   11635              :         return MK_named;
   11636              : 
   11637        11298 :       if (TREE_CODE (decl) == VAR_DECL
   11638        11298 :           && DECL_VTABLE_OR_VTT_P (decl))
   11639              :         return MK_vtable;
   11640              : 
   11641         1944 :       if (DECL_THUNK_P (decl))
   11642              :         /* Thunks are unique-enough, because they're only referenced
   11643              :            from the vtable.  And that's either new (so we want the
   11644              :            thunks), or it's a duplicate (so it will be dropped).  */
   11645              :         return MK_unique;
   11646              : 
   11647              :       /* There should be no other cases.  */
   11648            0 :       gcc_unreachable ();
   11649              :     }
   11650              : 
   11651      3995039 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   11652              :                        && TREE_CODE (decl) != USING_DECL
   11653              :                        && TREE_CODE (decl) != CONST_DECL);
   11654              : 
   11655      3995039 :   if (is_key_order ())
   11656              :     {
   11657              :       /* When doing the mergeablilty graph, there's an indirection to
   11658              :          the actual depset.  */
   11659      1331526 :       gcc_assert (dep->is_special ());
   11660      1331526 :       dep = dep->deps[0];
   11661              :     }
   11662              : 
   11663      3995039 :   gcc_checking_assert (decl == dep->get_entity ());
   11664              : 
   11665      3995039 :   merge_kind mk = MK_named;
   11666      3995039 :   switch (dep->get_entity_kind ())
   11667              :     {
   11668            0 :     default:
   11669            0 :       gcc_unreachable ();
   11670              : 
   11671              :     case depset::EK_PARTIAL:
   11672              :       mk = MK_partial;
   11673              :       break;
   11674              : 
   11675      2239202 :     case depset::EK_DECL:
   11676      2239202 :       {
   11677      2239202 :         tree ctx = CP_DECL_CONTEXT (decl);
   11678              : 
   11679      2239202 :         switch (TREE_CODE (ctx))
   11680              :           {
   11681            0 :           default:
   11682            0 :             gcc_unreachable ();
   11683              : 
   11684        23238 :           case FUNCTION_DECL:
   11685        23238 :             gcc_checking_assert
   11686              :               (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   11687              : 
   11688        23238 :             if (has_definition (ctx))
   11689              :               mk = MK_local_type;
   11690              :             else
   11691              :               /* We're not providing a definition of the context to key
   11692              :                  the local type into; use the keyed map instead.  */
   11693         1360 :               mk = MK_keyed;
   11694              :             break;
   11695              : 
   11696      2215964 :           case RECORD_TYPE:
   11697      2215964 :           case UNION_TYPE:
   11698      2215964 :           case NAMESPACE_DECL:
   11699      2215964 :             if (DECL_NAME (decl) == as_base_identifier)
   11700              :               {
   11701              :                 mk = MK_as_base;
   11702              :                 break;
   11703              :               }
   11704              : 
   11705              :             /* A lambda may have a class as its context, even though it
   11706              :                isn't a member in the traditional sense; see the test
   11707              :                g++.dg/modules/lambda-6_a.C.  */
   11708      2755973 :             if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
   11709      2405048 :                 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   11710              :               {
   11711         1132 :                 if (get_keyed_decl_scope (decl))
   11712              :                   mk = MK_keyed;
   11713              :                 else
   11714              :                   /* Lambdas not attached to any mangling scope are TU-local
   11715              :                      and so cannot be deduplicated.  */
   11716       691790 :                   mk = MK_unique;
   11717              :                 break;
   11718              :               }
   11719              : 
   11720      2115571 :             if (TREE_CODE (decl) == TEMPLATE_DECL
   11721      2115571 :                 ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11722       894522 :                 : decl_specialization_friend_p (decl))
   11723              :               {
   11724              :                 mk = MK_local_friend;
   11725              :                 break;
   11726              :               }
   11727              : 
   11728      2086138 :             if (DECL_DECOMPOSITION_P (decl))
   11729              :               {
   11730              :                 mk = MK_unique;
   11731              :                 break;
   11732              :               }
   11733              : 
   11734      2085661 :             if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   11735              :               {
   11736        36752 :                 if (RECORD_OR_UNION_TYPE_P (ctx))
   11737              :                   mk = MK_field;
   11738         1195 :                 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   11739         1195 :                          && UNSCOPED_ENUM_P (TREE_TYPE (decl))
   11740         2390 :                          && TYPE_VALUES (TREE_TYPE (decl)))
   11741              :                   /* Keyed by first enum value, and underlying type.  */
   11742              :                   mk = MK_enum;
   11743              :                 else
   11744              :                   /* No way to merge it, it is an ODR land-mine.  */
   11745              :                   mk = MK_unique;
   11746              :               }
   11747              :           }
   11748              :       }
   11749              :       break;
   11750              : 
   11751      1692120 :     case depset::EK_SPECIALIZATION:
   11752      1692120 :       {
   11753      1692120 :         gcc_checking_assert (dep->is_special ());
   11754              : 
   11755      1692120 :         if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   11756              :           /* An block-scope classes of templates are themselves
   11757              :              templates.  */
   11758         6033 :           gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
   11759              : 
   11760      1692120 :         if (dep->is_friend_spec ())
   11761              :           mk = MK_friend_spec;
   11762      1692120 :         else if (dep->is_type_spec ())
   11763              :           mk = MK_type_spec;
   11764              :         else
   11765      1206246 :           mk = MK_decl_spec;
   11766              : 
   11767      1692120 :         if (TREE_CODE (decl) == TEMPLATE_DECL)
   11768              :           {
   11769       144213 :             spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11770       144213 :             if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
   11771        15183 :               mk = merge_kind (mk | MK_tmpl_tmpl_mask);
   11772              :           }
   11773              :       }
   11774              :       break;
   11775              :     }
   11776              : 
   11777              :   return mk;
   11778              : }
   11779              : 
   11780              : 
   11781              : /* The container of DECL -- not necessarily its context!  */
   11782              : 
   11783              : tree
   11784      5051696 : trees_out::decl_container (tree decl)
   11785              : {
   11786      5051696 :   int use_tpl;
   11787      5051696 :   tree tpl = NULL_TREE;
   11788      5051696 :   if (tree template_info = node_template_info (decl, use_tpl))
   11789      1768830 :     tpl = TI_TEMPLATE (template_info);
   11790      5051696 :   if (tpl == decl)
   11791            0 :     tpl = nullptr;
   11792              : 
   11793              :   /* Stream the template we're instantiated from.  */
   11794      5051696 :   tree_node (tpl);
   11795              : 
   11796      5051696 :   tree container = NULL_TREE;
   11797      5051696 :   if (TREE_CODE (decl) == TEMPLATE_DECL
   11798      5051696 :       ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11799      3602239 :       : decl_specialization_friend_p (decl))
   11800        29433 :     container = DECL_CHAIN (decl);
   11801              :   else
   11802      5022263 :     container = CP_DECL_CONTEXT (decl);
   11803              : 
   11804      5051696 :   if (TYPE_P (container))
   11805      2963713 :     container = TYPE_NAME (container);
   11806              : 
   11807      5051696 :   tree_node (container);
   11808              : 
   11809      5051696 :   return container;
   11810              : }
   11811              : 
   11812              : tree
   11813      1229465 : trees_in::decl_container ()
   11814              : {
   11815              :   /* The maybe-template.  */
   11816      1229465 :   (void)tree_node ();
   11817              : 
   11818      1229465 :   tree container = tree_node ();
   11819              : 
   11820      1229465 :   return container;
   11821              : }
   11822              : 
   11823              : /* Gets a 2-bit discriminator to distinguish coroutine actor or destroy
   11824              :    functions from a normal function.  */
   11825              : 
   11826              : static int
   11827      1389940 : get_coroutine_discriminator (tree inner)
   11828              : {
   11829      1389940 :   if (DECL_COROUTINE_P (inner))
   11830           78 :     if (tree ramp = DECL_RAMP_FN (inner))
   11831              :       {
   11832           18 :         if (DECL_ACTOR_FN (ramp) == inner)
   11833              :           return 1;
   11834            9 :         else if (DECL_DESTROY_FN (ramp) == inner)
   11835              :           return 2;
   11836              :         else
   11837            0 :           gcc_unreachable ();
   11838              :       }
   11839              :   return 0;
   11840              : }
   11841              : 
   11842              : /* Write out key information about a mergeable DEP.  Does not write
   11843              :    the contents of DEP itself.  The context has already been
   11844              :    written.  The container has already been streamed.  */
   11845              : 
   11846              : void
   11847      5051696 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   11848              :                           tree container, depset *dep)
   11849              : {
   11850      5051696 :   if (dep && is_key_order ())
   11851              :     {
   11852      1331526 :       gcc_checking_assert (dep->is_special ());
   11853      1331526 :       dep = dep->deps[0];
   11854              :     }
   11855              : 
   11856      5051696 :   if (streaming_p ())
   11857      1847503 :     dump (dumper::MERGE)
   11858         1101 :       && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   11859          993 :                dep ? dep->entity_kind_name () : "contained",
   11860         1101 :                TREE_CODE (decl), decl);
   11861              : 
   11862              :   /* Now write the locating information. */
   11863      5051696 :   if (mk & MK_template_mask)
   11864              :     {
   11865              :       /* Specializations are located via their originating template,
   11866              :          and the set of template args they specialize.  */
   11867      1692120 :       gcc_checking_assert (dep && dep->is_special ());
   11868      1692120 :       spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11869              : 
   11870      1692120 :       tree_node (entry->tmpl);
   11871      1692120 :       tree_node (entry->args);
   11872      1692120 :       if (mk & MK_tmpl_decl_mask)
   11873      1206246 :         if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   11874              :           {
   11875              :             /* Variable template partial specializations might need
   11876              :                constraints (see spec_hasher::equal).  It's simpler to
   11877              :                write NULL when we don't need them.  */
   11878        26958 :             tree constraints = NULL_TREE;
   11879              : 
   11880        26958 :             if (uses_template_parms (entry->args))
   11881          954 :               constraints = get_constraints (inner);
   11882        26958 :             tree_node (constraints);
   11883              :           }
   11884              : 
   11885      1692120 :       if (CHECKING_P)
   11886              :         {
   11887              :           /* Make sure we can locate the decl.  */
   11888      1692120 :           tree existing = match_mergeable_specialization
   11889      1692120 :             (bool (mk & MK_tmpl_decl_mask), entry);
   11890              : 
   11891      1692120 :           gcc_assert (existing);
   11892      1692120 :           if (mk & MK_tmpl_decl_mask)
   11893              :             {
   11894      1206246 :               if (mk & MK_tmpl_tmpl_mask)
   11895        12027 :                 existing = DECL_TI_TEMPLATE (existing);
   11896              :             }
   11897              :           else
   11898              :             {
   11899       485874 :               if (mk & MK_tmpl_tmpl_mask)
   11900         3156 :                 existing = CLASSTYPE_TI_TEMPLATE (existing);
   11901              :               else
   11902       482718 :                 existing = TYPE_NAME (existing);
   11903              :             }
   11904              : 
   11905              :           /* The walkabout should have found ourselves.  */
   11906      1692120 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
   11907              :                                ? same_type_p (TREE_TYPE (decl),
   11908              :                                               TREE_TYPE (existing))
   11909              :                                : existing == decl);
   11910              :         }
   11911              :     }
   11912      3359576 :   else if (mk != MK_unique)
   11913              :     {
   11914      2667786 :       merge_key key;
   11915      2667786 :       tree name = DECL_NAME (decl);
   11916              : 
   11917      2667786 :       switch (mk)
   11918              :         {
   11919            0 :         default:
   11920            0 :           gcc_unreachable ();
   11921              : 
   11922      2246353 :         case MK_named:
   11923      2246353 :         case MK_friend_spec:
   11924      2246353 :           if (IDENTIFIER_CONV_OP_P (name))
   11925         7987 :             name = conv_op_identifier;
   11926              : 
   11927      2246353 :           if (TREE_CODE (inner) == FUNCTION_DECL)
   11928              :             {
   11929              :               /* Functions are distinguished by parameter types.  */
   11930      1260463 :               tree fn_type = TREE_TYPE (inner);
   11931              : 
   11932      1260463 :               key.ref_q = type_memfn_rqual (fn_type);
   11933      1260463 :               key.coro_disc = get_coroutine_discriminator (inner);
   11934      1260463 :               key.iobj_p = DECL_IOBJ_MEMBER_FUNCTION_P (inner);
   11935      1260463 :               key.xobj_p = DECL_XOBJ_MEMBER_FUNCTION_P (inner);
   11936      1260463 :               key.args = TYPE_ARG_TYPES (fn_type);
   11937              : 
   11938      1260463 :               if (tree reqs = get_constraints (inner))
   11939              :                 {
   11940        93075 :                   if (cxx_dialect < cxx20)
   11941           48 :                     reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
   11942              :                   else
   11943       186102 :                     reqs = CI_DECLARATOR_REQS (reqs);
   11944        93075 :                   key.constraints = reqs;
   11945              :                 }
   11946              : 
   11947      1260463 :               if (IDENTIFIER_CONV_OP_P (name)
   11948      1260463 :                   || (decl != inner
   11949       698649 :                       && !(name == fun_identifier
   11950              :                            /* In case the user names something _FUN  */
   11951          168 :                            && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
   11952              :                 /* And a function template, or conversion operator needs
   11953              :                    the return type.  Except for the _FUN thunk of a
   11954              :                    generic lambda, which has a recursive decl_type'd
   11955              :                    return type.  */
   11956              :                 // FIXME: What if the return type is a voldemort?
   11957       706552 :                 key.ret = fndecl_declared_return_type (inner);
   11958              :             }
   11959              :           break;
   11960              : 
   11961       194139 :         case MK_field:
   11962       194139 :           {
   11963       194139 :             unsigned ix = 0;
   11964       194139 :             if (TREE_CODE (inner) != FIELD_DECL)
   11965              :               name = NULL_TREE;
   11966              :             else
   11967        33269 :               gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
   11968              : 
   11969       194139 :             for (tree field = TYPE_FIELDS (TREE_TYPE (container));
   11970      3983775 :                  ; field = DECL_CHAIN (field))
   11971              :               {
   11972      4177914 :                 tree finner = STRIP_TEMPLATE (field);
   11973      4177914 :                 if (TREE_CODE (finner) == TREE_CODE (inner))
   11974              :                   {
   11975      1440491 :                     if (finner == inner)
   11976              :                       break;
   11977      1246352 :                     ix++;
   11978              :                   }
   11979      3983775 :               }
   11980       194139 :             key.index = ix;
   11981              :           }
   11982       194139 :           break;
   11983              : 
   11984         9354 :         case MK_vtable:
   11985         9354 :           {
   11986         9354 :             tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
   11987        12322 :             for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
   11988        12322 :               if (vtable == decl)
   11989              :                 {
   11990         9354 :                   key.index = ix;
   11991         9354 :                   break;
   11992              :                 }
   11993         9354 :             name = NULL_TREE;
   11994              :           }
   11995         9354 :           break;
   11996              : 
   11997        99261 :         case MK_as_base:
   11998        99261 :           gcc_checking_assert
   11999              :             (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
   12000              :           break;
   12001              : 
   12002        29433 :         case MK_local_friend:
   12003        29433 :           {
   12004              :             /* Find by index on the class's DECL_LIST.  We set TREE_CHAIN to
   12005              :                point to the class in push_template_decl or grokfndecl.  */
   12006        29433 :             unsigned ix = 0;
   12007        29433 :             for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
   12008       822552 :                  decls; decls = TREE_CHAIN (decls))
   12009       822552 :               if (!TREE_PURPOSE (decls))
   12010              :                 {
   12011       112956 :                   tree frnd = friend_from_decl_list (TREE_VALUE (decls));
   12012       112956 :                   if (frnd == decl)
   12013              :                     break;
   12014        83523 :                   ix++;
   12015              :                 }
   12016        29433 :             key.index = ix;
   12017        29433 :             name = NULL_TREE;
   12018              :           }
   12019        29433 :           break;
   12020              : 
   12021        22974 :         case MK_local_type:
   12022        22974 :           key_local_type (key, STRIP_TEMPLATE (decl), container);
   12023        22974 :           break;
   12024              : 
   12025         1195 :         case MK_enum:
   12026         1195 :           {
   12027              :             /* Anonymous enums are located by their first identifier,
   12028              :                and underlying type.  */
   12029         1195 :             tree type = TREE_TYPE (decl);
   12030              : 
   12031         1195 :             gcc_checking_assert (UNSCOPED_ENUM_P (type));
   12032              :             /* Using the type name drops the bit precision we might
   12033              :                have been using on the enum.  */
   12034         1195 :             key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
   12035         1195 :             if (tree values = TYPE_VALUES (type))
   12036         1195 :               name = DECL_NAME (TREE_VALUE (values));
   12037              :           }
   12038              :           break;
   12039              : 
   12040         1360 :         case MK_keyed:
   12041         1360 :           {
   12042         1360 :             tree scope = get_keyed_decl_scope (inner);
   12043         1360 :             gcc_checking_assert (scope);
   12044              : 
   12045         1360 :             auto *root = keyed_table->get (scope);
   12046         1360 :             unsigned ix = root->length ();
   12047              :             /* If we don't find it, we'll write a really big number
   12048              :                that the reader will ignore.  */
   12049         1484 :             while (ix--)
   12050         1484 :               if ((*root)[ix] == inner)
   12051              :                 break;
   12052              : 
   12053              :             /* Use the keyed-to decl as the 'name'.  */
   12054         1360 :             name = scope;
   12055         1360 :             key.index = ix;
   12056              :           }
   12057         1360 :           break;
   12058              : 
   12059        63717 :         case MK_partial:
   12060        63717 :           {
   12061        63717 :             tree ti = get_template_info (inner);
   12062        63717 :             key.constraints = get_constraints (inner);
   12063        63717 :             key.ret = TI_TEMPLATE (ti);
   12064        63717 :             key.args = TI_ARGS (ti);
   12065              :           }
   12066        63717 :           break;
   12067              :         }
   12068              : 
   12069      2667786 :       tree_node (name);
   12070      2667786 :       if (streaming_p ())
   12071              :         {
   12072              :           /* Check we have enough bits for the index.  */
   12073       949992 :           gcc_checking_assert (key.index < (1u << (sizeof (unsigned) * 8 - 6)));
   12074              : 
   12075       949992 :           unsigned code = ((key.ref_q << 0)
   12076       949992 :                            | (key.coro_disc << 2)
   12077       949992 :                            | (key.iobj_p << 4)
   12078       949992 :                            | (key.xobj_p << 5)
   12079       949992 :                            | (key.index << 6));
   12080       949992 :           u (code);
   12081              :         }
   12082              : 
   12083      2667786 :       if (mk == MK_enum)
   12084         1195 :         tree_node (key.ret);
   12085      2666591 :       else if (mk == MK_partial
   12086      2602874 :                || (mk == MK_named && inner
   12087      2246353 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12088              :         {
   12089      1324180 :           tree_node (key.ret);
   12090      1324180 :           tree arg = key.args;
   12091      1324180 :           if (mk == MK_named)
   12092      3680019 :             while (arg && arg != void_list_node)
   12093              :               {
   12094      2419556 :                 tree_node (TREE_VALUE (arg));
   12095      2419556 :                 arg = TREE_CHAIN (arg);
   12096              :               }
   12097      1324180 :           tree_node (arg);
   12098      1324180 :           tree_node (key.constraints);
   12099              :         }
   12100              :     }
   12101      5051696 : }
   12102              : 
   12103              : /* DECL is a new declaration that may be duplicated in OVL.  Use KEY
   12104              :    to find its clone, or NULL.  If DECL's DECL_NAME is NULL, this
   12105              :    has been found by a proxy.  It will be an enum type located by its
   12106              :    first member.
   12107              : 
   12108              :    We're conservative with matches, so ambiguous decls will be
   12109              :    registered as different, then lead to a lookup error if the two
   12110              :    modules are both visible.  Perhaps we want to do something similar
   12111              :    to duplicate decls to get ODR errors on loading?  We already have
   12112              :    some special casing for namespaces.  */
   12113              : 
   12114              : static tree
   12115       302035 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
   12116              : {
   12117       302035 :   tree found = NULL_TREE;
   12118       910358 :   for (ovl_iterator iter (ovl); !found && iter; ++iter)
   12119              :     {
   12120       667521 :       tree match = *iter;
   12121              : 
   12122       667521 :       tree d_inner = decl;
   12123       667521 :       tree m_inner = match;
   12124              : 
   12125       771053 :     again:
   12126       771053 :       if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
   12127              :         {
   12128       116286 :           if (TREE_CODE (match) == NAMESPACE_DECL
   12129       116286 :               && !DECL_NAMESPACE_ALIAS (match))
   12130              :             /* Namespaces are never overloaded.  */
   12131              :             found = match;
   12132              : 
   12133       116286 :           continue;
   12134              :         }
   12135              : 
   12136       654767 :       switch (TREE_CODE (d_inner))
   12137              :         {
   12138       244964 :         case TEMPLATE_DECL:
   12139       244964 :           if (template_heads_equivalent_p (d_inner, m_inner))
   12140              :             {
   12141       103532 :               d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12142       103532 :               m_inner = DECL_TEMPLATE_RESULT (m_inner);
   12143       103532 :               if (d_inner == error_mark_node
   12144       103532 :                   && TYPE_DECL_ALIAS_P (m_inner))
   12145              :                 {
   12146              :                   found = match;
   12147              :                   break;
   12148              :                 }
   12149       103532 :               goto again;
   12150              :             }
   12151              :           break;
   12152              : 
   12153       311015 :         case FUNCTION_DECL:
   12154       311015 :           if (tree m_type = TREE_TYPE (m_inner))
   12155       311015 :             if ((!key.ret
   12156       154053 :                  || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
   12157       285541 :                 && type_memfn_rqual (m_type) == key.ref_q
   12158       285279 :                 && key.iobj_p == DECL_IOBJ_MEMBER_FUNCTION_P (m_inner)
   12159       570298 :                 && key.xobj_p == DECL_XOBJ_MEMBER_FUNCTION_P (m_inner)
   12160       285258 :                 && compparms (key.args, TYPE_ARG_TYPES (m_type))
   12161       129477 :                 && get_coroutine_discriminator (m_inner) == key.coro_disc
   12162              :                 /* Reject if old is a "C" builtin and new is not "C".
   12163              :                    Matches decls_match behaviour.  */
   12164       129474 :                 && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
   12165         8122 :                     || !DECL_EXTERN_C_P (m_inner)
   12166         7895 :                     || DECL_EXTERN_C_P (d_inner))
   12167              :                 /* Reject if one is a different member of a
   12168              :                    guarded/pre/post fn set.  */
   12169       129449 :                 && (!flag_contracts
   12170       239300 :                     || (DECL_IS_PRE_FN_P (d_inner)
   12171       119650 :                         == DECL_IS_PRE_FN_P (m_inner)))
   12172       440464 :                 && (!flag_contracts
   12173       239300 :                     || (DECL_IS_POST_FN_P (d_inner)
   12174       119650 :                         == DECL_IS_POST_FN_P (m_inner))))
   12175              :               {
   12176       129449 :                 tree m_reqs = get_constraints (m_inner);
   12177       129449 :                 if (m_reqs)
   12178              :                   {
   12179         9479 :                     if (cxx_dialect < cxx20)
   12180            8 :                       m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
   12181              :                     else
   12182        18950 :                       m_reqs = CI_DECLARATOR_REQS (m_reqs);
   12183              :                   }
   12184              : 
   12185       129449 :                 if (cp_tree_equal (key.constraints, m_reqs))
   12186       608323 :                   found = match;
   12187              :               }
   12188              :           break;
   12189              : 
   12190        59363 :         case TYPE_DECL:
   12191       118726 :           if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
   12192        59363 :               == DECL_IMPLICIT_TYPEDEF_P (m_inner))
   12193              :             {
   12194        59327 :               if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
   12195        59198 :                 return match;
   12196          129 :               else if (mk == MK_enum
   12197          129 :                        && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
   12198          129 :                            == key.ret))
   12199              :                 found = match;
   12200              :             }
   12201              :           /* With -freflection, typedef struct { } A is now represented the same
   12202              :              as typedef struct A_ { } A except the TYPE_DECL for A_ is invisible
   12203              :              to name lookup, so we won't be able to find and match it directly.
   12204              :              But we will find the in-TU A (m_inner), through which we can obtain
   12205              :              the in-TU A_ when d_inner is the streamed-in A_.  */
   12206           36 :           else if (flag_reflection
   12207           10 :                    && TYPE_DECL_WAS_UNNAMED (d_inner)
   12208           42 :                    && DECL_ORIGINAL_TYPE (m_inner))
   12209              :             {
   12210            6 :               tree orig = TYPE_NAME (DECL_ORIGINAL_TYPE (m_inner));
   12211            6 :               if (TYPE_DECL_WAS_UNNAMED (orig)
   12212           12 :                   && DECL_NAME (orig) == DECL_NAME (d_inner))
   12213              :                 found = orig;
   12214              :             }
   12215              :           break;
   12216              : 
   12217              :         default:
   12218              :           found = match;
   12219              :           break;
   12220              :         }
   12221              :     }
   12222              : 
   12223       242837 :   return found;
   12224              : }
   12225              : 
   12226              : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl.  Only
   12227              :    the bools have been filled in.  Read its merging key and merge it.
   12228              :    Returns the existing decl if there is one.  */
   12229              : 
   12230              : tree
   12231      1229465 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   12232              :                          tree type, tree container, bool is_attached,
   12233              :                          bool is_imported_temploid_friend)
   12234              : {
   12235      1229465 :   const char *kind = "new";
   12236      1229465 :   tree existing = NULL_TREE;
   12237              : 
   12238      1229465 :   if (mk & MK_template_mask)
   12239              :     {
   12240              :       // FIXME: We could stream the specialization hash?
   12241       376204 :       spec_entry spec;
   12242       376204 :       spec.tmpl = tree_node ();
   12243       376204 :       spec.args = tree_node ();
   12244              : 
   12245       376204 :       if (get_overrun ())
   12246            0 :         return error_mark_node;
   12247              : 
   12248       376204 :       DECL_NAME (decl) = DECL_NAME (spec.tmpl);
   12249       376204 :       DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
   12250       376204 :       DECL_NAME (inner) = DECL_NAME (decl);
   12251       376204 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12252              : 
   12253       376204 :       tree constr = NULL_TREE;
   12254       376204 :       bool is_decl = mk & MK_tmpl_decl_mask;
   12255       376204 :       if (is_decl)
   12256              :         {
   12257       269342 :           if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   12258              :             {
   12259         5875 :               constr = tree_node ();
   12260         5875 :               if (constr)
   12261            0 :                 set_constraints (inner, constr);
   12262              :             }
   12263       536095 :           spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
   12264              :         }
   12265              :       else
   12266       106862 :         spec.spec = type;
   12267       376204 :       existing = match_mergeable_specialization (is_decl, &spec);
   12268       376204 :       if (constr)
   12269              :         /* We'll add these back later, if this is the new decl.  */
   12270            0 :         remove_constraints (inner);
   12271              : 
   12272       376204 :       if (!existing)
   12273              :         ; /* We'll add to the table once read.  */
   12274       150700 :       else if (mk & MK_tmpl_decl_mask)
   12275              :         {
   12276              :           /* A declaration specialization.  */
   12277       104679 :           if (mk & MK_tmpl_tmpl_mask)
   12278         1144 :             existing = DECL_TI_TEMPLATE (existing);
   12279              :         }
   12280              :       else
   12281              :         {
   12282              :           /* A type specialization.  */
   12283        46021 :           if (mk & MK_tmpl_tmpl_mask)
   12284          246 :             existing = CLASSTYPE_TI_TEMPLATE (existing);
   12285              :           else
   12286        45775 :             existing = TYPE_NAME (existing);
   12287              :         }
   12288              :     }
   12289       853261 :   else if (mk == MK_unique)
   12290              :     kind = "unique";
   12291              :   else
   12292              :     {
   12293       631808 :       tree name = tree_node ();
   12294              : 
   12295       631808 :       merge_key key;
   12296       631808 :       unsigned code = u ();
   12297       631808 :       key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
   12298       631808 :       key.coro_disc = (code >> 2) & 3;
   12299       631808 :       key.iobj_p = (code >> 4) & 1;
   12300       631808 :       key.xobj_p = (code >> 5) & 1;
   12301       631808 :       key.index = code >> 6;
   12302              : 
   12303       631808 :       if (mk == MK_enum)
   12304          237 :         key.ret = tree_node ();
   12305       631571 :       else if (mk == MK_partial
   12306       619294 :                || ((mk == MK_named || mk == MK_friend_spec)
   12307       524303 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12308              :         {
   12309       301607 :           key.ret = tree_node ();
   12310       301607 :           tree arg, *arg_ptr = &key.args;
   12311       301607 :           while ((arg = tree_node ())
   12312       852576 :                  && arg != void_list_node
   12313      1418930 :                  && mk != MK_partial)
   12314              :             {
   12315       552523 :               *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
   12316       552523 :               arg_ptr = &TREE_CHAIN (*arg_ptr);
   12317              :             }
   12318       301607 :           *arg_ptr = arg;
   12319       301607 :           key.constraints = tree_node ();
   12320              :         }
   12321              : 
   12322       631808 :       if (get_overrun ())
   12323            0 :         return error_mark_node;
   12324              : 
   12325       631808 :       if (mk < MK_indirect_lwm)
   12326              :         {
   12327       619402 :           DECL_NAME (decl) = name;
   12328       619402 :           DECL_CONTEXT (decl) = FROB_CONTEXT (container);
   12329              :         }
   12330       631808 :       DECL_NAME (inner) = DECL_NAME (decl);
   12331       631808 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12332              : 
   12333       631808 :       if (mk == MK_partial)
   12334              :         {
   12335        12277 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
   12336        60532 :                spec; spec = TREE_CHAIN (spec))
   12337              :             {
   12338        54412 :               tree tmpl = TREE_VALUE (spec);
   12339        54412 :               tree ti = get_template_info (tmpl);
   12340        54412 :               if (template_args_equal (key.args, TI_ARGS (ti))
   12341        61371 :                   && cp_tree_equal (key.constraints,
   12342              :                                     get_constraints
   12343         6959 :                                     (DECL_TEMPLATE_RESULT (tmpl))))
   12344              :                 {
   12345              :                   existing = tmpl;
   12346              :                   break;
   12347              :                 }
   12348              :             }
   12349              :         }
   12350       619531 :       else if (mk == MK_keyed
   12351          375 :                && DECL_LANG_SPECIFIC (name)
   12352       619906 :                && DECL_MODULE_KEYED_DECLS_P (name))
   12353              :         {
   12354          375 :           gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
   12355              :                                || TREE_CODE (container) == TYPE_DECL
   12356              :                                || TREE_CODE (container) == FUNCTION_DECL);
   12357          375 :           if (auto *set = keyed_table->get (name))
   12358       631957 :             if (key.index < set->length ())
   12359              :               {
   12360          149 :                 existing = (*set)[key.index];
   12361          149 :                 if (existing)
   12362              :                   {
   12363          149 :                     gcc_checking_assert
   12364              :                       (DECL_IMPLICIT_TYPEDEF_P (existing));
   12365          149 :                     if (inner != decl)
   12366           91 :                       existing
   12367           91 :                         = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12368              :                   }
   12369              :               }
   12370              :         }
   12371              :       else
   12372       619156 :         switch (TREE_CODE (container))
   12373              :           {
   12374            0 :           default:
   12375            0 :             gcc_unreachable ();
   12376              : 
   12377       151250 :           case NAMESPACE_DECL:
   12378       151250 :             if (is_attached
   12379       151250 :                 && !is_imported_temploid_friend
   12380       151250 :                 && !(state->is_module () || state->is_partition ()))
   12381              :               kind = "unique";
   12382              :             else
   12383              :               {
   12384       149117 :                 gcc_checking_assert (mk == MK_named || mk == MK_enum);
   12385       149117 :                 tree mvec;
   12386       149117 :                 tree *vslot = mergeable_namespace_slots (container, name,
   12387              :                                                          is_attached, &mvec);
   12388       149117 :                 existing = check_mergeable_decl (mk, decl, *vslot, key);
   12389       149117 :                 if (!existing)
   12390        73517 :                   add_mergeable_namespace_entity (vslot, decl);
   12391              :                 else
   12392              :                   {
   12393              :                     /* Note that we now have duplicates to deal with in
   12394              :                        name lookup.  */
   12395        75600 :                     if (is_attached)
   12396           66 :                       BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
   12397              :                     else
   12398        75534 :                       BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
   12399              :                   }
   12400              :               }
   12401       156032 :             break;
   12402              : 
   12403         4782 :           case FUNCTION_DECL:
   12404         4782 :             gcc_checking_assert (mk == MK_local_type);
   12405         4782 :             existing = key_local_type (key, container, name);
   12406         4782 :             if (existing && inner != decl)
   12407         3484 :               existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12408              :             break;
   12409              : 
   12410       463124 :           case TYPE_DECL:
   12411       463124 :             gcc_checking_assert (!is_imported_temploid_friend);
   12412       463124 :             int use_tmpl = 0;
   12413         5991 :             if (is_attached && !(state->is_module () || state->is_partition ())
   12414              :                 /* Implicit or in-class defaulted member functions
   12415              :                    can come from anywhere.  */
   12416         4303 :                 && !(TREE_CODE (decl) == FUNCTION_DECL
   12417         1271 :                      && !DECL_THUNK_P (decl)
   12418         1271 :                      && DECL_DEFAULTED_FN (decl)
   12419          820 :                      && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
   12420              :                 /* As can members of template specialisations.  */
   12421       466607 :                 && !(node_template_info (container, use_tmpl)
   12422         1307 :                      && use_tmpl != 0))
   12423              :               kind = "unique";
   12424              :             else
   12425              :               {
   12426       459889 :                 tree ctx = TREE_TYPE (container);
   12427              : 
   12428              :                 /* For some reason templated enumeral types are not marked
   12429              :                    as COMPLETE_TYPE_P, even though they have members.
   12430              :                    This may well be a bug elsewhere.  */
   12431       459889 :                 if (TREE_CODE (ctx) == ENUMERAL_TYPE)
   12432        15257 :                   existing = find_enum_member (ctx, name);
   12433       444632 :                 else if (COMPLETE_TYPE_P (ctx))
   12434              :                   {
   12435       192492 :                     switch (mk)
   12436              :                       {
   12437            0 :                       default:
   12438            0 :                         gcc_unreachable ();
   12439              : 
   12440       153353 :                       case MK_named:
   12441       153353 :                         existing = lookup_class_binding (ctx, name);
   12442       153353 :                         if (existing)
   12443              :                           {
   12444       152918 :                             tree inner = decl;
   12445       152918 :                             if (TREE_CODE (inner) == TEMPLATE_DECL
   12446       152918 :                                 && !DECL_MEMBER_TEMPLATE_P (inner))
   12447        67667 :                               inner = DECL_TEMPLATE_RESULT (inner);
   12448              : 
   12449       152918 :                             existing = check_mergeable_decl
   12450       152918 :                               (mk, inner, existing, key);
   12451              : 
   12452       152918 :                             if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
   12453              :                               {} // FIXME: Insert into specialization
   12454              :                             // tables, we'll need the arguments for that!
   12455              :                           }
   12456              :                         break;
   12457              : 
   12458        26591 :                       case MK_field:
   12459        26591 :                         {
   12460        26591 :                           unsigned ix = key.index;
   12461        26591 :                           for (tree field = TYPE_FIELDS (ctx);
   12462       892107 :                                field; field = DECL_CHAIN (field))
   12463              :                             {
   12464       892107 :                               tree finner = STRIP_TEMPLATE (field);
   12465       892107 :                               if (TREE_CODE (finner) == TREE_CODE (inner))
   12466       299369 :                                 if (!ix--)
   12467              :                                   {
   12468              :                                     existing = field;
   12469              :                                     break;
   12470              :                                   }
   12471              :                             }
   12472              :                         }
   12473              :                         break;
   12474              : 
   12475         1492 :                       case MK_vtable:
   12476         1492 :                         {
   12477         1492 :                           unsigned ix = key.index;
   12478         1492 :                           for (tree vtable = CLASSTYPE_VTABLES (ctx);
   12479         1831 :                                vtable; vtable = DECL_CHAIN (vtable))
   12480         1557 :                             if (!ix--)
   12481              :                               {
   12482              :                                 existing = vtable;
   12483              :                                 break;
   12484              :                               }
   12485              :                         }
   12486              :                         break;
   12487              : 
   12488         8037 :                       case MK_as_base:
   12489         8037 :                         {
   12490         8037 :                           tree as_base = CLASSTYPE_AS_BASE (ctx);
   12491         8037 :                           if (as_base && as_base != ctx)
   12492         8037 :                             existing = TYPE_NAME (as_base);
   12493              :                         }
   12494              :                         break;
   12495              : 
   12496         3019 :                       case MK_local_friend:
   12497         3019 :                         {
   12498         3019 :                           unsigned ix = key.index;
   12499         3019 :                           for (tree decls = CLASSTYPE_DECL_LIST (ctx);
   12500        84156 :                                decls; decls = TREE_CHAIN (decls))
   12501        84156 :                             if (!TREE_PURPOSE (decls) && !ix--)
   12502              :                               {
   12503         3019 :                                 existing
   12504         3019 :                                   = friend_from_decl_list (TREE_VALUE (decls));
   12505         3019 :                                 break;
   12506              :                               }
   12507              :                         }
   12508              :                         break;
   12509              :                       }
   12510              : 
   12511       192492 :                     if (existing && mk < MK_indirect_lwm && mk != MK_partial
   12512       188260 :                         && TREE_CODE (decl) == TEMPLATE_DECL
   12513       278819 :                         && !DECL_MEMBER_TEMPLATE_P (decl))
   12514              :                       {
   12515        69683 :                         tree ti;
   12516        69683 :                         if (DECL_IMPLICIT_TYPEDEF_P (existing))
   12517          887 :                           ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
   12518              :                         else
   12519        68796 :                           ti = DECL_TEMPLATE_INFO (existing);
   12520        69683 :                         existing = TI_TEMPLATE (ti);
   12521              :                       }
   12522              :                   }
   12523              :               }
   12524              :           }
   12525              :     }
   12526              : 
   12527      1229465 :   dump (dumper::MERGE)
   12528         3208 :     && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   12529         3208 :              existing ? "matched" : kind, TREE_CODE (decl), decl);
   12530              : 
   12531              :   return existing;
   12532              : }
   12533              : 
   12534              : void
   12535       389731 : trees_out::binfo_mergeable (tree binfo)
   12536              : {
   12537       389731 :   tree dom = binfo;
   12538       492301 :   while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
   12539              :     dom = parent;
   12540       389731 :   tree type = BINFO_TYPE (dom);
   12541       389731 :   gcc_checking_assert (TYPE_BINFO (type) == dom);
   12542       389731 :   tree_node (type);
   12543       389731 :   if (streaming_p ())
   12544              :     {
   12545              :       unsigned ix = 0;
   12546       206993 :       for (; dom != binfo; dom = TREE_CHAIN (dom))
   12547        54045 :         ix++;
   12548       152948 :       u (ix);
   12549              :     }
   12550       389731 : }
   12551              : 
   12552              : unsigned
   12553        96077 : trees_in::binfo_mergeable (tree *type)
   12554              : {
   12555        96077 :   *type = tree_node ();
   12556        96077 :   return u ();
   12557              : }
   12558              : 
   12559              : /* DECL is a just streamed declaration with attributes DATTR that should
   12560              :    have matching ABI tags as EXISTING's attributes EATTR.  Check that the
   12561              :    ABI tags match, and report an error if not.  */
   12562              : 
   12563              : void
   12564       294936 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
   12565              : {
   12566       294936 :   tree etags = lookup_attribute ("abi_tag", eattr);
   12567       294936 :   tree dtags = lookup_attribute ("abi_tag", dattr);
   12568       294936 :   if ((etags == nullptr) != (dtags == nullptr)
   12569       294936 :       || (etags && !attribute_value_equal (etags, dtags)))
   12570              :     {
   12571           33 :       if (etags)
   12572           24 :         etags = TREE_VALUE (etags);
   12573           33 :       if (dtags)
   12574           24 :         dtags = TREE_VALUE (dtags);
   12575              : 
   12576              :       /* We only error if mangling wouldn't consider the tags equivalent.
   12577              :          Since tags might have been inherited during mangling, ignore
   12578              :          inherited tags if there's a mangled-ness mismatch.  */
   12579           33 :       bool ignore_inherited_p
   12580           33 :         = (DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (existing))
   12581           33 :            != DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (decl)));
   12582           33 :       if (!equal_abi_tags (etags, dtags, ignore_inherited_p))
   12583              :         {
   12584           21 :           auto_diagnostic_group d;
   12585           21 :           if (dtags)
   12586           15 :             error_at (DECL_SOURCE_LOCATION (decl),
   12587              :                       "mismatching abi tags for %qD with tags %qE",
   12588              :                       decl, dtags);
   12589              :           else
   12590            6 :             error_at (DECL_SOURCE_LOCATION (decl),
   12591              :                       "mismatching abi tags for %qD with no tags", decl);
   12592           21 :           if (etags)
   12593           15 :             inform (DECL_SOURCE_LOCATION (existing),
   12594              :                     "existing declaration here with tags %qE", etags);
   12595              :           else
   12596            6 :             inform (DECL_SOURCE_LOCATION (existing),
   12597              :                     "existing declaration here with no tags");
   12598           21 :         }
   12599              : 
   12600              :       /* Always use the existing abi_tags as the canonical set so that
   12601              :          later processing doesn't get confused.  */
   12602           33 :       if (dtags)
   12603           24 :         dattr = remove_attribute ("abi_tag", dattr);
   12604           33 :       if (etags)
   12605           24 :         duplicate_one_attribute (&dattr, eattr, "abi_tag");
   12606              :     }
   12607       294936 : }
   12608              : 
   12609              : /* DECL is a just streamed mergeable decl that should match EXISTING.  Check
   12610              :    it does and issue an appropriate diagnostic if not.  Merge any
   12611              :    bits from DECL to EXISTING.  This is stricter matching than
   12612              :    decls_match, because we can rely on ODR-sameness, and we cannot use
   12613              :    decls_match because it can cause instantiations of constraints.  */
   12614              : 
   12615              : bool
   12616       432754 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
   12617              : {
   12618              :   // FIXME: We should probably do some duplicate decl-like stuff here
   12619              :   // (beware, default parms should be the same?)  Can we just call
   12620              :   // duplicate_decls and teach it how to handle the module-specific
   12621              :   // permitted/required duplications?
   12622              : 
   12623              :   // We know at this point that the decls have matched by key, so we
   12624              :   // can elide some of the checking
   12625       432754 :   gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
   12626              : 
   12627       432754 :   tree d_inner = decl;
   12628       432754 :   tree e_inner = existing;
   12629       432754 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12630              :     {
   12631       143162 :       d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12632       143162 :       e_inner = DECL_TEMPLATE_RESULT (e_inner);
   12633       143162 :       gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
   12634              :     }
   12635              : 
   12636              :   // FIXME: do more precise errors at point of mismatch
   12637       432754 :   const char *mismatch_msg = nullptr;
   12638              : 
   12639       432754 :   if (VAR_OR_FUNCTION_DECL_P (d_inner)
   12640       432754 :       && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
   12641              :     {
   12642            6 :       mismatch_msg = G_("conflicting language linkage for imported "
   12643              :                         "declaration %#qD");
   12644            6 :       goto mismatch;
   12645              :     }
   12646       432748 :   else if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12647              :     {
   12648       197837 :       tree e_ret = fndecl_declared_return_type (existing);
   12649       197837 :       tree d_ret = fndecl_declared_return_type (decl);
   12650              : 
   12651        84347 :       if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
   12652       197861 :           && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
   12653              :         /* This has a recursive type that will compare different.  */;
   12654       197825 :       else if (!same_type_p (d_ret, e_ret))
   12655              :         {
   12656           26 :           mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12657           26 :           goto mismatch;
   12658              :         }
   12659              : 
   12660       197811 :       tree& e_type = TREE_TYPE (e_inner);
   12661       197811 :       tree d_type = TREE_TYPE (d_inner);
   12662              : 
   12663       197811 :       for (tree e_args = TYPE_ARG_TYPES (e_type),
   12664       197811 :              d_args = TYPE_ARG_TYPES (d_type);
   12665       339412 :            e_args != d_args && (e_args || d_args);
   12666       141601 :            e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
   12667              :         {
   12668       141601 :           if (!(e_args && d_args))
   12669              :             {
   12670            0 :               mismatch_msg = G_("conflicting argument list for imported "
   12671              :                                 "declaration %#qD");
   12672            0 :               goto mismatch;
   12673              :             }
   12674              : 
   12675       141601 :           if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
   12676              :             {
   12677            0 :               mismatch_msg = G_("conflicting argument types for imported "
   12678              :                                 "declaration %#qD");
   12679            0 :               goto mismatch;
   12680              :             }
   12681              :         }
   12682              : 
   12683              :       /* If EXISTING has an undeduced or uninstantiated exception
   12684              :          specification, but DECL does not, propagate the exception
   12685              :          specification.  Otherwise we end up asserting or trying to
   12686              :          instantiate it in the middle of loading.   */
   12687       197811 :       tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
   12688       197811 :       tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
   12689       289406 :       if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
   12690              :         {
   12691        31367 :           if (!(DECL_MAYBE_DELETED (d_inner)
   12692        15628 :                 || DEFERRED_NOEXCEPT_SPEC_P (d_spec))
   12693        46747 :               || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
   12694        25406 :                   && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
   12695              :             {
   12696          153 :               dump (dumper::MERGE)
   12697            6 :                 && dump ("Propagating instantiated noexcept to %N", existing);
   12698          153 :               gcc_checking_assert (existing == e_inner);
   12699          153 :               e_type = build_exception_variant (e_type, d_spec);
   12700              : 
   12701              :               /* Propagate to existing clones.  */
   12702          153 :               tree clone;
   12703          439 :               FOR_EACH_CLONE (clone, existing)
   12704          286 :                 TREE_TYPE (clone)
   12705          572 :                   = build_exception_variant (TREE_TYPE (clone), d_spec);
   12706              :             }
   12707              :         }
   12708       182072 :       else if (!DECL_MAYBE_DELETED (d_inner)
   12709       259689 :                && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   12710       364143 :                && !comp_except_specs (d_spec, e_spec, ce_type))
   12711              :         {
   12712         1721 :           mismatch_msg = G_("conflicting %<noexcept%> specifier for "
   12713              :                             "imported declaration %#qD");
   12714         1721 :           goto mismatch;
   12715              :         }
   12716              : 
   12717              :       /* Similarly if EXISTING has an undeduced return type, but DECL's
   12718              :          is already deduced.  */
   12719       196090 :       bool e_undeduced = undeduced_auto_decl (existing);
   12720       196090 :       bool d_undeduced = undeduced_auto_decl (decl);
   12721       196090 :       if (e_undeduced && !d_undeduced)
   12722              :         {
   12723           16 :           dump (dumper::MERGE)
   12724            0 :             && dump ("Propagating deduced return type to %N", existing);
   12725           16 :           gcc_checking_assert (existing == e_inner);
   12726           16 :           FNDECL_USED_AUTO (existing) = true;
   12727           16 :           DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
   12728           16 :           e_type = change_return_type (TREE_TYPE (d_type), e_type);
   12729              :         }
   12730       196074 :       else if (d_undeduced && !e_undeduced)
   12731              :         /* EXISTING was deduced, leave it alone.  */;
   12732       196071 :       else if (type_uses_auto (d_ret)
   12733       196071 :                && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
   12734              :         {
   12735            9 :           mismatch_msg = G_("conflicting deduced return type for "
   12736              :                             "imported declaration %#qD");
   12737            9 :           goto mismatch;
   12738              :         }
   12739              : 
   12740              :       /* Similarly if EXISTING has undeduced constexpr, but DECL's
   12741              :          is already deduced.  */
   12742       196081 :       if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12743       196081 :           == DECL_DECLARED_CONSTEXPR_P (d_inner))
   12744              :         /* Already matches.  */;
   12745            2 :       else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
   12746            2 :                && (DECL_MAYBE_DELETED (e_inner)
   12747            0 :                    || decl_implicit_constexpr_p (d_inner)))
   12748              :         /* DECL was deduced, copy to EXISTING.  */
   12749              :         {
   12750            1 :           DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
   12751            1 :           if (decl_implicit_constexpr_p (d_inner))
   12752            0 :             DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
   12753              :         }
   12754            1 :       else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12755            1 :                && (DECL_MAYBE_DELETED (d_inner)
   12756            0 :                    || decl_implicit_constexpr_p (e_inner)))
   12757              :         /* EXISTING was deduced, leave it alone.  */;
   12758              :       else
   12759              :         {
   12760            0 :           mismatch_msg = G_("conflicting %<constexpr%> for imported "
   12761              :                             "declaration %#qD");
   12762            0 :           goto mismatch;
   12763              :         }
   12764              : 
   12765              :       /* Don't synthesize a defaulted function if we're importing one
   12766              :          we've already determined.  */
   12767       196081 :       if (!DECL_MAYBE_DELETED (d_inner))
   12768       195969 :         DECL_MAYBE_DELETED (e_inner) = false;
   12769              :     }
   12770       234911 :   else if (is_typedef)
   12771              :     {
   12772        83586 :       if (!DECL_ORIGINAL_TYPE (e_inner)
   12773        83586 :           || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
   12774              :                            DECL_ORIGINAL_TYPE (e_inner)))
   12775              :         {
   12776            3 :           mismatch_msg = G_("conflicting imported declaration %q#D");
   12777            3 :           goto mismatch;
   12778              :         }
   12779              :     }
   12780              :   /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
   12781              :      here. I suspect the entities that directly do that are things
   12782              :      that shouldn't go to duplicate_decls (FIELD_DECLs etc).   */
   12783       151325 :   else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
   12784              :     {
   12785              :       mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12786         2513 :     mismatch:
   12787         2513 :       if (DECL_IS_UNDECLARED_BUILTIN (existing))
   12788              :         /* Just like duplicate_decls, presum the user knows what
   12789              :            they're doing in overriding a builtin.   */
   12790         1744 :         TREE_TYPE (existing) = TREE_TYPE (decl);
   12791          769 :       else if (decl_function_context (decl))
   12792              :         /* The type of a mergeable local entity (such as a function scope
   12793              :            capturing lambda's closure type fields) can depend on an
   12794              :            unmergeable local entity (such as a local variable), so type
   12795              :            equality isn't feasible in general for local entities.  */;
   12796              :       else
   12797              :         {
   12798           21 :           gcc_checking_assert (mismatch_msg);
   12799           21 :           auto_diagnostic_group d;
   12800           21 :           error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
   12801           21 :           inform (DECL_SOURCE_LOCATION (existing),
   12802              :                   "existing declaration %#qD", existing);
   12803           21 :           return false;
   12804           21 :         }
   12805              :     }
   12806              : 
   12807       432733 :   if (DECL_IS_UNDECLARED_BUILTIN (existing)
   12808       432733 :       && !DECL_IS_UNDECLARED_BUILTIN (decl))
   12809              :     {
   12810              :       /* We're matching a builtin that the user has yet to declare.
   12811              :          We are the one!  This is very much duplicate-decl
   12812              :          shenanigans. */
   12813         2030 :       DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
   12814         2030 :       if (TREE_CODE (decl) != TYPE_DECL)
   12815              :         {
   12816              :           /* Propagate exceptions etc.  */
   12817         2006 :           TREE_TYPE (existing) = TREE_TYPE (decl);
   12818         2006 :           TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
   12819              :         }
   12820              :       /* This is actually an import! */
   12821         2030 :       DECL_MODULE_IMPORT_P (existing) = true;
   12822              : 
   12823              :       /* Yay, sliced!  */
   12824         2030 :       existing->base = decl->base;
   12825              : 
   12826         2030 :       if (TREE_CODE (decl) == FUNCTION_DECL)
   12827              :         {
   12828              :           /* Ew :(  */
   12829         2006 :           memcpy (&existing->decl_common.size,
   12830              :                   &decl->decl_common.size,
   12831              :                   (offsetof (tree_decl_common, pt_uid)
   12832              :                    - offsetof (tree_decl_common, size)));
   12833         2006 :           auto bltin_class = DECL_BUILT_IN_CLASS (decl);
   12834         2006 :           existing->function_decl.built_in_class = bltin_class;
   12835         2006 :           auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
   12836         2006 :           DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
   12837         2006 :           if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
   12838              :             {
   12839         1796 :               if (builtin_decl_explicit_p (built_in_function (fncode)))
   12840         1796 :                 switch (fncode)
   12841              :                   {
   12842            0 :                   case BUILT_IN_STPCPY:
   12843            0 :                     set_builtin_decl_implicit_p
   12844            0 :                       (built_in_function (fncode), true);
   12845            0 :                     break;
   12846         1796 :                   default:
   12847         1796 :                     set_builtin_decl_declared_p
   12848         1796 :                       (built_in_function (fncode), true);
   12849         1796 :                     break;
   12850              :                   }
   12851         1796 :               copy_attributes_to_builtin (decl);
   12852              :             }
   12853              :         }
   12854              :     }
   12855              : 
   12856       432733 :   if (VAR_OR_FUNCTION_DECL_P (decl)
   12857       432733 :       && DECL_TEMPLATE_INSTANTIATED (decl))
   12858              :     /* Don't instantiate again!  */
   12859         8868 :     DECL_TEMPLATE_INSTANTIATED (existing) = true;
   12860              : 
   12861       432733 :   if (TREE_CODE (d_inner) == FUNCTION_DECL
   12862       432733 :       && DECL_DECLARED_INLINE_P (d_inner))
   12863              :     {
   12864       160313 :       DECL_DECLARED_INLINE_P (e_inner) = true;
   12865       160313 :       if (!DECL_SAVED_TREE (e_inner)
   12866        79924 :           && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
   12867       160330 :           && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
   12868              :         {
   12869           51 :           DECL_INTERFACE_KNOWN (e_inner)
   12870           17 :             |= DECL_INTERFACE_KNOWN (d_inner);
   12871           17 :           DECL_DISREGARD_INLINE_LIMITS (e_inner)
   12872           17 :             |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
   12873              :           // TODO: we will eventually want to merge all decl attributes
   12874           17 :           duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
   12875           17 :                                    DECL_ATTRIBUTES (d_inner), "gnu_inline");
   12876              :         }
   12877              :     }
   12878       432733 :   if (!DECL_EXTERNAL (d_inner))
   12879       202309 :     DECL_EXTERNAL (e_inner) = false;
   12880              : 
   12881       432733 :   if (VAR_OR_FUNCTION_DECL_P (d_inner))
   12882       436332 :     check_abi_tags (existing, decl,
   12883       218166 :                     DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
   12884              : 
   12885       432733 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12886              :     {
   12887              :       /* Merge default template arguments.  */
   12888       143160 :       tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
   12889       143160 :       tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
   12890       143160 :       gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
   12891              :                            == TREE_VEC_LENGTH (e_parms));
   12892       410672 :       for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
   12893              :         {
   12894       267521 :           tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
   12895       267521 :           tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
   12896       267521 :           if (e_default == NULL_TREE)
   12897       226660 :             e_default = d_default;
   12898        40861 :           else if (d_default != NULL_TREE
   12899        40861 :                    && !cp_tree_equal (d_default, e_default))
   12900              :             {
   12901            9 :               auto_diagnostic_group d;
   12902            9 :               tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
   12903            9 :               tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
   12904            9 :               error_at (DECL_SOURCE_LOCATION (d_parm),
   12905              :                         "conflicting default argument for %#qD", d_parm);
   12906            9 :               inform (DECL_SOURCE_LOCATION (e_parm),
   12907              :                       "existing default declared here");
   12908            9 :               return false;
   12909            9 :             }
   12910              :         }
   12911              :     }
   12912              : 
   12913       432724 :   if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12914              :     {
   12915              :       /* Merge default function arguments.  */
   12916       197828 :       tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
   12917       197828 :       tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
   12918       197828 :       int i = 0;
   12919       668797 :       for (; d_parm && d_parm != void_list_node;
   12920       273141 :            d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
   12921              :         {
   12922       273153 :           tree d_default = TREE_PURPOSE (d_parm);
   12923       273153 :           tree& e_default = TREE_PURPOSE (e_parm);
   12924       273153 :           if (e_default == NULL_TREE)
   12925       256913 :             e_default = d_default;
   12926        16240 :           else if (d_default != NULL_TREE
   12927        16240 :                    && !cp_tree_equal (d_default, e_default))
   12928              :             {
   12929           12 :               auto_diagnostic_group d;
   12930           12 :               error_at (get_fndecl_argument_location (d_inner, i),
   12931              :                         "conflicting default argument for parameter %P of %#qD",
   12932              :                         i, decl);
   12933           12 :               inform (get_fndecl_argument_location (e_inner, i),
   12934              :                       "existing default declared here");
   12935           12 :               return false;
   12936           12 :             }
   12937              :         }
   12938              :     }
   12939              : 
   12940              :   return true;
   12941              : }
   12942              : 
   12943              : /* FN is an implicit member function that we've discovered is new to
   12944              :    the class.  Add it to the TYPE_FIELDS chain and the method vector.
   12945              :    Reset the appropriate classtype lazy flag.   */
   12946              : 
   12947              : bool
   12948          939 : trees_in::install_implicit_member (tree fn)
   12949              : {
   12950          939 :   tree ctx = DECL_CONTEXT (fn);
   12951          939 :   tree name = DECL_NAME (fn);
   12952              :   /* We know these are synthesized, so the set of expected prototypes
   12953              :      is quite restricted.  We're not validating correctness, just
   12954              :      distinguishing between the small set of possibilities.  */
   12955          939 :   tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
   12956          939 :   if (IDENTIFIER_CTOR_P (name))
   12957              :     {
   12958          615 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
   12959          615 :           && VOID_TYPE_P (parm_type))
   12960          147 :         CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
   12961          468 :       else if (!TYPE_REF_P (parm_type))
   12962              :         return false;
   12963          468 :       else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
   12964          468 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12965          237 :         CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
   12966          231 :       else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
   12967          231 :         CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
   12968              :       else
   12969              :         return false;
   12970              :     }
   12971          324 :   else if (IDENTIFIER_DTOR_P (name))
   12972              :     {
   12973          252 :       if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
   12974          252 :         CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
   12975              :       else
   12976              :         return false;
   12977          252 :       if (DECL_VIRTUAL_P (fn))
   12978              :         /* A virtual dtor should have been created when the class
   12979              :            became complete.  */
   12980              :         return false;
   12981              :     }
   12982           72 :   else if (name == assign_op_identifier)
   12983              :     {
   12984           72 :       if (!TYPE_REF_P (parm_type))
   12985              :         return false;
   12986           72 :       else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
   12987           72 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12988           36 :         CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
   12989           36 :       else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
   12990           36 :         CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
   12991              :       else
   12992              :         return false;
   12993              :     }
   12994              :   else
   12995              :     return false;
   12996              : 
   12997          984 :   dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
   12998              : 
   12999          939 :   DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
   13000          939 :   TYPE_FIELDS (ctx) = fn;
   13001              : 
   13002          939 :   add_method (ctx, fn, false);
   13003              : 
   13004              :     /* Propagate TYPE_FIELDS.  */
   13005          939 :   fixup_type_variants (ctx);
   13006              : 
   13007          939 :   return true;
   13008              : }
   13009              : 
   13010              : /* Return non-zero if DECL has a definition that would be interesting to
   13011              :    write out.  */
   13012              : 
   13013              : static bool
   13014      2268904 : has_definition (tree decl)
   13015              : {
   13016      2268910 :   bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
   13017      2268910 :   if (is_tmpl)
   13018       506716 :     decl = DECL_TEMPLATE_RESULT (decl);
   13019              : 
   13020      2268910 :   switch (TREE_CODE (decl))
   13021              :     {
   13022              :     default:
   13023              :       break;
   13024              : 
   13025       853179 :     case FUNCTION_DECL:
   13026       853179 :       if (!DECL_SAVED_TREE (decl))
   13027              :         /* Not defined.  */
   13028              :         break;
   13029              : 
   13030       390112 :       if (DECL_DECLARED_INLINE_P (decl))
   13031              :         return true;
   13032              : 
   13033        31067 :       if (header_module_p ())
   13034              :         /* We always need to write definitions in header modules,
   13035              :            since there's no TU to emit them in otherwise.  */
   13036              :         return true;
   13037              : 
   13038        19686 :       if (DECL_TEMPLATE_INFO (decl))
   13039              :         {
   13040        18395 :           int use_tpl = DECL_USE_TEMPLATE (decl);
   13041              : 
   13042              :           // FIXME: Partial specializations have definitions too.
   13043        18395 :           if (use_tpl < 2)
   13044              :             return true;
   13045              :         }
   13046              : 
   13047              :       /* Coroutine transform functions always need to be emitted
   13048              :          into the importing TU if the ramp function will be.  */
   13049         1351 :       if (DECL_COROUTINE_P (decl))
   13050           12 :         if (tree ramp = DECL_RAMP_FN (decl))
   13051              :           return has_definition (ramp);
   13052              :       break;
   13053              : 
   13054       936398 :     case TYPE_DECL:
   13055       936398 :       {
   13056       936398 :         tree type = TREE_TYPE (decl);
   13057       936398 :         if (type == TYPE_MAIN_VARIANT (type)
   13058       428239 :             && decl == TYPE_NAME (type)
   13059      1364637 :             && (TREE_CODE (type) == ENUMERAL_TYPE
   13060       428239 :                 ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
   13061       212459 :           return true;
   13062              :       }
   13063              :       break;
   13064              : 
   13065       118655 :     case VAR_DECL:
   13066              :       /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL.  */
   13067       118655 :       if (DECL_LANG_SPECIFIC (decl)
   13068       117745 :           && DECL_TEMPLATE_INFO (decl)
   13069       194692 :           && DECL_INITIAL (decl))
   13070              :         return true;
   13071              :       else
   13072              :         {
   13073        48068 :           if (!DECL_INITIALIZED_P (decl))
   13074              :             /* Not defined.  */
   13075              :             return false;
   13076              : 
   13077        40225 :           if (header_module_p ())
   13078              :             /* We always need to write definitions in header modules,
   13079              :                since there's no TU to emit them in otherwise.  */
   13080              :             return true;
   13081              : 
   13082        14749 :           if (decl_maybe_constant_var_p (decl))
   13083              :             /* We might need its constant value.  */
   13084              :             return true;
   13085              : 
   13086          503 :           if (vague_linkage_p (decl))
   13087              :             /* These are emitted as needed.  */
   13088              :             return true;
   13089              : 
   13090              :           return false;
   13091              :         }
   13092         8506 :       break;
   13093              : 
   13094         8506 :     case CONCEPT_DECL:
   13095         8506 :       if (DECL_INITIAL (decl))
   13096         8506 :         return true;
   13097              : 
   13098              :       break;
   13099              :     }
   13100              : 
   13101              :   return false;
   13102              : }
   13103              : 
   13104              : uintptr_t *
   13105       648977 : trees_in::find_duplicate (tree existing)
   13106              : {
   13107       302676 :   if (!duplicates)
   13108              :     return NULL;
   13109              : 
   13110       431293 :   return duplicates->get (existing);
   13111              : }
   13112              : 
   13113              : /* We're starting to read a duplicate DECL.  EXISTING is the already
   13114              :    known node.  */
   13115              : 
   13116              : void
   13117       616903 : trees_in::register_duplicate (tree decl, tree existing)
   13118              : {
   13119       616903 :   if (!duplicates)
   13120       101989 :     duplicates = new duplicate_hash_map (40);
   13121              : 
   13122       616903 :   bool existed;
   13123       616903 :   uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
   13124       616903 :   gcc_checking_assert (!existed);
   13125       616903 :   slot = reinterpret_cast<uintptr_t> (decl);
   13126              : 
   13127       616903 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   13128              :     /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
   13129              :        that passing decl's _RESULT to maybe_duplicate naturally
   13130              :        gives us existing's _RESULT back.  */
   13131       286324 :     register_duplicate (DECL_TEMPLATE_RESULT (decl),
   13132       143162 :                         DECL_TEMPLATE_RESULT (existing));
   13133       616903 : }
   13134              : 
   13135              : /* We've read a definition of MAYBE_EXISTING.  If not a duplicate,
   13136              :    return MAYBE_EXISTING (into which the definition should be
   13137              :    installed).  Otherwise return NULL if already known bad, or the
   13138              :    duplicate we read (for ODR checking, or extracting additional merge
   13139              :    information).  */
   13140              : 
   13141              : tree
   13142       346301 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
   13143              : {
   13144       346301 :   tree res = NULL_TREE;
   13145              : 
   13146       546507 :   if (uintptr_t *dup = find_duplicate (maybe_existing))
   13147              :     {
   13148       145006 :       if (!(*dup & 1))
   13149       145003 :         res = reinterpret_cast<tree> (*dup);
   13150              :     }
   13151              :   else
   13152              :     res = maybe_existing;
   13153              : 
   13154       346301 :   assert_definition (maybe_existing, res && !has_defn);
   13155              : 
   13156              :   // FIXME: We probably need to return the template, so that the
   13157              :   // template header can be checked?
   13158       346301 :   return res ? STRIP_TEMPLATE (res) : NULL_TREE;
   13159              : }
   13160              : 
   13161              : /* The following writer functions rely on the current behaviour of
   13162              :    depset::hash::add_dependency making the decl and defn depset nodes
   13163              :    depend on each other.  That way we don't have to worry about seeding
   13164              :    the tree map with named decls that cannot be looked up by name (I.e
   13165              :    template and function parms).  We know the decl and definition will
   13166              :    be in the same cluster, which is what we want.  */
   13167              : 
   13168              : void
   13169       653995 : trees_out::write_function_def (tree decl)
   13170              : {
   13171       653995 :   tree_node (DECL_RESULT (decl));
   13172              : 
   13173       653995 :   {
   13174              :     /* The function body for a non-inline function or function template
   13175              :        is ignored for determining exposures.  This should only matter
   13176              :        for templates (we don't emit the bodies of non-inline functions
   13177              :        to begin with).  */
   13178       653995 :     auto ovr = dep_hash->ignore_exposure_if (!DECL_DECLARED_INLINE_P (decl));
   13179       653995 :     tree_node (DECL_INITIAL (decl));
   13180       653995 :     tree_node (DECL_SAVED_TREE (decl));
   13181       653995 :   }
   13182              : 
   13183      1298444 :   tree_node (DECL_FRIEND_CONTEXT (decl));
   13184              : 
   13185       653995 :   constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
   13186              : 
   13187       653995 :   if (streaming_p ())
   13188       326956 :     u (cexpr != nullptr);
   13189       653995 :   if (cexpr)
   13190              :     {
   13191       149663 :       chained_decls (cexpr->parms);
   13192       149663 :       tree_node (cexpr->result);
   13193       149663 :       tree_node (cexpr->body);
   13194              :     }
   13195              : 
   13196       653995 :   function* f = DECL_STRUCT_FUNCTION (decl);
   13197              : 
   13198       653995 :   if (streaming_p ())
   13199              :     {
   13200       326956 :       unsigned flags = 0;
   13201              : 
   13202              :       /* Whether the importer should emit this definition, if used.  */
   13203       326956 :       flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
   13204       326956 :                     && (get_importer_interface (decl)
   13205              :                         != importer_interface::external));
   13206              : 
   13207              :       /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
   13208              :          on non-templates or we'll crash later in import_export_decl.  */
   13209       219337 :       gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
   13210              :                            || (DECL_LANG_SPECIFIC (decl)
   13211              :                                && DECL_LOCAL_DECL_P (decl)
   13212              :                                && DECL_OMP_DECLARE_REDUCTION_P (decl))
   13213              :                            || (DECL_LANG_SPECIFIC (decl)
   13214              :                                && DECL_TEMPLATE_INFO (decl)
   13215              :                                && uses_template_parms (DECL_TI_ARGS (decl))));
   13216              : 
   13217       326956 :       if (f)
   13218              :         {
   13219       325617 :           flags |= 2;
   13220              :           /* These flags are needed in tsubst_lambda_expr.  */
   13221       325617 :           flags |= 4 * f->language->returns_value;
   13222       325617 :           flags |= 8 * f->language->returns_null;
   13223       325617 :           flags |= 16 * f->language->returns_abnormally;
   13224       325617 :           flags |= 32 * f->language->infinite_loop;
   13225              :         }
   13226              : 
   13227       326956 :       u (flags);
   13228              :     }
   13229              : 
   13230       653995 :   if (state && f)
   13231              :     {
   13232       651317 :       state->write_location (*this, f->function_start_locus);
   13233       651317 :       state->write_location (*this, f->function_end_locus);
   13234              :     }
   13235              : 
   13236       653995 :   if (DECL_COROUTINE_P (decl))
   13237              :     {
   13238           44 :       tree ramp = DECL_RAMP_FN (decl);
   13239           44 :       tree_node (ramp);
   13240           44 :       if (!ramp)
   13241              :         {
   13242           32 :           tree_node (DECL_ACTOR_FN (decl));
   13243           32 :           tree_node (DECL_DESTROY_FN (decl));
   13244              :         }
   13245              :     }
   13246       653995 : }
   13247              : 
   13248              : void
   13249            0 : trees_out::mark_function_def (tree)
   13250              : {
   13251            0 : }
   13252              : 
   13253              : bool
   13254       226584 : trees_in::read_function_def (tree decl, tree maybe_template)
   13255              : {
   13256       227134 :   dump () && dump ("Reading function definition %N", decl);
   13257       226584 :   tree result = tree_node ();
   13258       226584 :   tree initial = tree_node ();
   13259       226584 :   tree saved = tree_node ();
   13260       226584 :   tree context = tree_node ();
   13261       226584 :   post_process_data pdata {};
   13262       226584 :   pdata.decl = maybe_template;
   13263              : 
   13264       226584 :   tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
   13265       453165 :   bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
   13266              : 
   13267       226584 :   constexpr_fundef cexpr;
   13268       226584 :   if (u ())
   13269              :     {
   13270        52644 :       cexpr.parms = chained_decls ();
   13271        52644 :       cexpr.result = tree_node ();
   13272        52644 :       cexpr.body = tree_node ();
   13273        52644 :       cexpr.decl = decl;
   13274              :     }
   13275              :   else
   13276              :     cexpr.decl = NULL_TREE;
   13277              : 
   13278       226584 :   unsigned flags = u ();
   13279       226584 :   if (flags & 2)
   13280              :     {
   13281       225691 :       pdata.start_locus = state->read_location (*this);
   13282       225691 :       pdata.end_locus = state->read_location (*this);
   13283       225691 :       pdata.returns_value = flags & 4;
   13284       225691 :       pdata.returns_null = flags & 8;
   13285       225691 :       pdata.returns_abnormally = flags & 16;
   13286       225691 :       pdata.infinite_loop = flags & 32;
   13287              :     }
   13288              : 
   13289       226584 :   tree coro_actor = NULL_TREE;
   13290       226584 :   tree coro_destroy = NULL_TREE;
   13291       226584 :   tree coro_ramp = NULL_TREE;
   13292       226584 :   if (DECL_COROUTINE_P (decl))
   13293              :     {
   13294           17 :       coro_ramp = tree_node ();
   13295           17 :       if (!coro_ramp)
   13296              :         {
   13297           11 :           coro_actor = tree_node ();
   13298           11 :           coro_destroy = tree_node ();
   13299           11 :           if ((coro_actor == NULL_TREE) != (coro_destroy == NULL_TREE))
   13300            0 :             set_overrun ();
   13301              :         }
   13302              :     }
   13303              : 
   13304       226584 :   if (get_overrun ())
   13305              :     return NULL_TREE;
   13306              : 
   13307       226584 :   if (installing)
   13308              :     {
   13309       141761 :       DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
   13310       141761 :       DECL_RESULT (decl) = result;
   13311       141761 :       DECL_INITIAL (decl) = initial;
   13312       141761 :       DECL_SAVED_TREE (decl) = saved;
   13313              : 
   13314              :       /* Some entities (like anticipated builtins) were declared without
   13315              :          DECL_ARGUMENTS, so update them now.  But don't do it if there's
   13316              :          already an argument list, because we've already built the
   13317              :          definition referencing those merged PARM_DECLs.  */
   13318       141761 :       if (!DECL_ARGUMENTS (decl))
   13319         7089 :         DECL_ARGUMENTS (decl) = DECL_ARGUMENTS (maybe_dup);
   13320              : 
   13321       141761 :       if (context)
   13322         5947 :         SET_DECL_FRIEND_CONTEXT (decl, context);
   13323       141761 :       if (cexpr.decl)
   13324        38682 :         register_constexpr_fundef (cexpr);
   13325              : 
   13326       141761 :       if (coro_ramp)
   13327            6 :         coro_set_ramp_function (decl, coro_ramp);
   13328       141755 :       else if (coro_actor && coro_destroy)
   13329            3 :         coro_set_transform_functions (decl, coro_actor, coro_destroy);
   13330              : 
   13331       141761 :       if (DECL_LOCAL_DECL_P (decl))
   13332              :         /* Block-scope OMP UDRs aren't real functions, and don't need a
   13333              :            function structure to be allocated or to be expanded.  */
   13334            3 :         gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (decl));
   13335              :       else
   13336       141758 :         post_process (pdata);
   13337              :     }
   13338              :   else if (maybe_dup)
   13339              :     {
   13340              :       // FIXME:QOI Check matching defn
   13341              :     }
   13342              : 
   13343              :   return true;
   13344              : }
   13345              : 
   13346              : /* Also for CONCEPT_DECLs.  */
   13347              : 
   13348              : void
   13349       151278 : trees_out::write_var_def (tree decl)
   13350              : {
   13351              :   /* The initializer of a non-inline variable or variable template is
   13352              :      ignored for determining exposures.  */
   13353       151278 :   auto ovr = dep_hash->ignore_exposure_if (VAR_P (decl)
   13354       151278 :                                            && !DECL_INLINE_VAR_P (decl));
   13355              : 
   13356       151278 :   tree init = DECL_INITIAL (decl);
   13357       151278 :   tree_node (init);
   13358       151278 :   if (!init)
   13359              :     {
   13360         1904 :       tree dyn_init = NULL_TREE;
   13361              : 
   13362              :       /* We only need to write initializers in header modules.  */
   13363         3088 :       if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
   13364              :         {
   13365          450 :           dyn_init = value_member (decl,
   13366          450 :                                    CP_DECL_THREAD_LOCAL_P (decl)
   13367              :                                    ? tls_aggregates : static_aggregates);
   13368          450 :           gcc_checking_assert (dyn_init);
   13369              :           /* Mark it so write_inits knows this is needed.  */
   13370          450 :           TREE_LANG_FLAG_0 (dyn_init) = true;
   13371          450 :           dyn_init = TREE_PURPOSE (dyn_init);
   13372              :         }
   13373         1904 :       tree_node (dyn_init);
   13374              :     }
   13375       151278 : }
   13376              : 
   13377              : void
   13378            0 : trees_out::mark_var_def (tree)
   13379              : {
   13380            0 : }
   13381              : 
   13382              : bool
   13383        46540 : trees_in::read_var_def (tree decl, tree maybe_template)
   13384              : {
   13385              :   /* Do not mark the virtual table entries as used.  */
   13386        46540 :   bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
   13387        46540 :   unused += vtable;
   13388        46540 :   tree init = tree_node ();
   13389        46540 :   tree dyn_init = init ? NULL_TREE : tree_node ();
   13390        46540 :   unused -= vtable;
   13391              : 
   13392        46540 :   if (get_overrun ())
   13393              :     return false;
   13394              : 
   13395        46540 :   bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
   13396        46540 :                       : bool (DECL_INITIAL (decl)));
   13397        46540 :   tree maybe_dup = odr_duplicate (maybe_template, initialized);
   13398        46540 :   bool installing = maybe_dup && !initialized;
   13399        46540 :   if (installing)
   13400              :     {
   13401        29876 :       DECL_INITIAL (decl) = init;
   13402        29876 :       if (DECL_EXTERNAL (decl))
   13403         3656 :         DECL_NOT_REALLY_EXTERN (decl) = true;
   13404        29876 :       if (VAR_P (decl))
   13405              :         {
   13406        26232 :           DECL_INITIALIZED_P (decl) = true;
   13407        26232 :           if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
   13408        25791 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   13409        26232 :           tentative_decl_linkage (decl);
   13410        26232 :           if (DECL_EXPLICIT_INSTANTIATION (decl)
   13411        26232 :               && !DECL_EXTERNAL (decl))
   13412            9 :             setup_explicit_instantiation_definition_linkage (decl);
   13413              :           /* Class non-template static members are handled in read_class_def.
   13414              :              But still handle specialisations of member templates.  */
   13415        52464 :           if ((!DECL_CLASS_SCOPE_P (decl)
   13416        16777 :                || primary_template_specialization_p (decl))
   13417        35827 :               && (DECL_IMPLICIT_INSTANTIATION (decl)
   13418         9515 :                   || (DECL_EXPLICIT_INSTANTIATION (decl)
   13419           21 :                       && !DECL_EXTERNAL (decl))))
   13420           89 :             note_vague_linkage_variable (decl);
   13421              :         }
   13422        29876 :       if (!dyn_init)
   13423              :         ;
   13424          216 :       else if (CP_DECL_THREAD_LOCAL_P (decl))
   13425           96 :         tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
   13426              :       else
   13427          120 :         static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
   13428              :     }
   13429              :   else if (maybe_dup)
   13430              :     {
   13431              :       // FIXME:QOI Check matching defn
   13432              :     }
   13433              : 
   13434              :   return true;
   13435              : }
   13436              : 
   13437              : /* If MEMBER doesn't have an independent life outside the class,
   13438              :    return it (or its TEMPLATE_DECL).  Otherwise NULL.  */
   13439              : 
   13440              : static tree
   13441       313072 : member_owned_by_class (tree member)
   13442              : {
   13443       313072 :   gcc_assert (DECL_P (member));
   13444              : 
   13445              :   /* Clones are owned by their origin.  */
   13446       313072 :   if (DECL_CLONED_FUNCTION_P (member))
   13447              :     return NULL;
   13448              : 
   13449       313072 :   if (TREE_CODE (member) == FIELD_DECL)
   13450              :     /* FIELD_DECLS can have template info in some cases.  We always
   13451              :        want the FIELD_DECL though, as there's never a TEMPLATE_DECL
   13452              :        wrapping them.  */
   13453              :     return member;
   13454              : 
   13455       134097 :   int use_tpl = -1;
   13456       134097 :   if (tree ti = node_template_info (member, use_tpl))
   13457              :     {
   13458              :       // FIXME: Don't bail on things that CANNOT have their own
   13459              :       // template header.  No, make sure they're in the same cluster.
   13460            0 :       if (use_tpl > 0)
   13461              :         return NULL_TREE;
   13462              : 
   13463            0 :       if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
   13464       313072 :         member = TI_TEMPLATE (ti);
   13465              :     }
   13466              :   return member;
   13467              : }
   13468              : 
   13469              : void
   13470       221284 : trees_out::write_class_def (tree defn)
   13471              : {
   13472       221284 :   gcc_assert (DECL_P (defn));
   13473       221284 :   if (streaming_p ())
   13474       111039 :     dump () && dump ("Writing class definition %N", defn);
   13475              : 
   13476       221284 :   tree type = TREE_TYPE (defn);
   13477       221284 :   tree_node (TYPE_SIZE (type));
   13478       221284 :   tree_node (TYPE_SIZE_UNIT (type));
   13479       221284 :   tree_node (TYPE_VFIELD (type));
   13480       221284 :   tree_node (TYPE_BINFO (type));
   13481              : 
   13482       221284 :   vec_chained_decls (TYPE_FIELDS (type));
   13483              : 
   13484              :   /* Every class but __as_base has a type-specific.  */
   13485       438638 :   gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
   13486              : 
   13487       221284 :   if (TYPE_LANG_SPECIFIC (type))
   13488              :     {
   13489       217354 :       {
   13490       217354 :         vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
   13491       217354 :         if (!v)
   13492              :           {
   13493        50944 :             gcc_checking_assert (!streaming_p ());
   13494              :             /* Force a class vector.  */
   13495        50944 :             v = set_class_bindings (type, -1);
   13496        50944 :             gcc_checking_assert (v);
   13497              :           }
   13498              : 
   13499       217354 :         unsigned len = v->length ();
   13500       217354 :         if (streaming_p ())
   13501       108654 :           u (len);
   13502      1856914 :         for (unsigned ix = 0; ix != len; ix++)
   13503              :           {
   13504      1639560 :             tree m = (*v)[ix];
   13505      1639560 :             if (TREE_CODE (m) == TYPE_DECL
   13506       474321 :                 && DECL_ARTIFICIAL (m)
   13507      1875795 :                 && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
   13508              :               /* This is a using-decl for a type, or an anonymous
   13509              :                  struct (maybe with a typedef name).  Write the type.  */
   13510        17512 :               m = TREE_TYPE (m);
   13511      1639560 :             tree_node (m);
   13512              :           }
   13513              :       }
   13514       217354 :       tree_node (CLASSTYPE_LAMBDA_EXPR (type));
   13515              : 
   13516              :       /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
   13517              :          reader won't know at this point.  */
   13518       217354 :       int has_vptr = TYPE_CONTAINS_VPTR_P (type);
   13519              : 
   13520       217354 :       if (streaming_p ())
   13521              :         {
   13522       108654 :           unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
   13523       108654 :           u (nvbases);
   13524       108654 :           i (has_vptr);
   13525              :         }
   13526              : 
   13527       217354 :       if (has_vptr)
   13528              :         {
   13529         7836 :           tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
   13530         7836 :           tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
   13531         7836 :           tree_node (CLASSTYPE_KEY_METHOD (type));
   13532              :         }
   13533              :     }
   13534              : 
   13535       221284 :   if (TYPE_LANG_SPECIFIC (type))
   13536              :     {
   13537       217354 :       tree_node (CLASSTYPE_PRIMARY_BINFO (type));
   13538              : 
   13539       217354 :       tree as_base = CLASSTYPE_AS_BASE (type);
   13540       217354 :       if (as_base)
   13541       113319 :         as_base = TYPE_NAME (as_base);
   13542       217354 :       tree_node (as_base);
   13543              : 
   13544              :       /* Write the vtables.  */
   13545       217354 :       tree vtables = CLASSTYPE_VTABLES (type);
   13546       217354 :       vec_chained_decls (vtables);
   13547       444062 :       for (; vtables; vtables = TREE_CHAIN (vtables))
   13548         9354 :         write_definition (vtables);
   13549              : 
   13550       217354 :       {
   13551              :         /* Friend declarations in class definitions are ignored when
   13552              :            determining exposures.  */
   13553       217354 :         auto ovr = dep_hash->ignore_exposure_if (true);
   13554              : 
   13555              :         /* Write the friend classes.  */
   13556       217354 :         tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
   13557              : 
   13558              :         /* Write the friend functions.  */
   13559       217354 :         for (tree friends = DECL_FRIENDLIST (defn);
   13560       250280 :              friends; friends = TREE_CHAIN (friends))
   13561              :           {
   13562        32926 :             tree_node (FRIEND_NAME (friends));
   13563        32926 :             tree_list (FRIEND_DECLS (friends), false);
   13564              :           }
   13565              :         /* End of friend fns.  */
   13566       217354 :         tree_node (NULL_TREE);
   13567       217354 :       }
   13568              : 
   13569              :       /* Write the decl list.  We don't need to ignore exposures of friend
   13570              :          decls here as any such decls should already have been added and
   13571              :          ignored above.  */
   13572       217354 :       tree_list (CLASSTYPE_DECL_LIST (type), true);
   13573              : 
   13574       217354 :       if (TYPE_CONTAINS_VPTR_P (type))
   13575              :         {
   13576              :           /* Write the thunks.  */
   13577         7836 :           for (tree decls = TYPE_FIELDS (type);
   13578       223030 :                decls; decls = DECL_CHAIN (decls))
   13579       215194 :             if (TREE_CODE (decls) == FUNCTION_DECL
   13580       155060 :                 && DECL_VIRTUAL_P (decls)
   13581       255874 :                 && DECL_THUNKS (decls))
   13582              :               {
   13583         1464 :                 tree_node (decls);
   13584              :                 /* Thunks are always unique, so chaining is ok.  */
   13585         1464 :                 chained_decls (DECL_THUNKS (decls));
   13586              :               }
   13587         7836 :           tree_node (NULL_TREE);
   13588              :         }
   13589              :     }
   13590       221284 : }
   13591              : 
   13592              : void
   13593       313072 : trees_out::mark_class_member (tree member, bool do_defn)
   13594              : {
   13595       313072 :   gcc_assert (DECL_P (member));
   13596              : 
   13597       313072 :   member = member_owned_by_class (member);
   13598       313072 :   if (member)
   13599       626144 :     mark_declaration (member, do_defn && has_definition (member));
   13600       313072 : }
   13601              : 
   13602              : void
   13603       221312 : trees_out::mark_class_def (tree defn)
   13604              : {
   13605       221312 :   gcc_assert (DECL_P (defn));
   13606       221312 :   tree type = TREE_TYPE (defn);
   13607              :   /* Mark the class members that are not type-decls and cannot have
   13608              :      independent definitions.  */
   13609      2586562 :   for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
   13610      2365250 :     if (TREE_CODE (member) == FIELD_DECL
   13611      2365250 :         || TREE_CODE (member) == USING_DECL
   13612              :         /* A cloned enum-decl from 'using enum unrelated;'   */
   13613      2365250 :         || (TREE_CODE (member) == CONST_DECL
   13614        18220 :             && DECL_CONTEXT (member) == type))
   13615              :       {
   13616       313072 :         mark_class_member (member);
   13617       313072 :         if (TREE_CODE (member) == FIELD_DECL)
   13618       178975 :           if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
   13619              :             /* If we're marking a class template definition, then
   13620              :                this'll contain the width (as set by grokbitfield)
   13621              :                instead of a decl.  */
   13622         3284 :             if (DECL_P (repr))
   13623         2540 :               mark_declaration (repr, false);
   13624              :       }
   13625              : 
   13626              :   /* Mark the binfo hierarchy.  */
   13627       521476 :   for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
   13628       300164 :     mark_by_value (child);
   13629              : 
   13630       221312 :   if (TYPE_LANG_SPECIFIC (type))
   13631              :     {
   13632       217358 :       for (tree vtable = CLASSTYPE_VTABLES (type);
   13633       226712 :            vtable; vtable = TREE_CHAIN (vtable))
   13634         9354 :         mark_declaration (vtable, true);
   13635              : 
   13636       217358 :       if (TYPE_CONTAINS_VPTR_P (type))
   13637              :         /* Mark the thunks, they belong to the class definition,
   13638              :            /not/ the thunked-to function.  */
   13639         7836 :         for (tree decls = TYPE_FIELDS (type);
   13640       223030 :              decls; decls = DECL_CHAIN (decls))
   13641       215194 :           if (TREE_CODE (decls) == FUNCTION_DECL)
   13642       155060 :             for (tree thunks = DECL_THUNKS (decls);
   13643       157004 :                  thunks; thunks = DECL_CHAIN (thunks))
   13644         1944 :               mark_declaration (thunks, false);
   13645              :     }
   13646       221312 : }
   13647              : 
   13648              : /* Nop sorting, needed for resorting the member vec.  */
   13649              : 
   13650              : static void
   13651     11188428 : nop (void *, void *, void *)
   13652              : {
   13653     11188428 : }
   13654              : 
   13655              : bool
   13656        69976 : trees_in::read_class_def (tree defn, tree maybe_template)
   13657              : {
   13658        69976 :   gcc_assert (DECL_P (defn));
   13659        70631 :   dump () && dump ("Reading class definition %N", defn);
   13660        69976 :   tree type = TREE_TYPE (defn);
   13661        69976 :   tree size = tree_node ();
   13662        69976 :   tree size_unit = tree_node ();
   13663        69976 :   tree vfield = tree_node ();
   13664        69976 :   tree binfo = tree_node ();
   13665        69976 :   vec<tree, va_gc> *vbase_vec = NULL;
   13666        69976 :   vec<tree, va_gc> *member_vec = NULL;
   13667        69976 :   vec<tree, va_gc> *pure_virts = NULL;
   13668        69976 :   vec<tree_pair_s, va_gc> *vcall_indices = NULL;
   13669        69976 :   tree key_method = NULL_TREE;
   13670        69976 :   tree lambda = NULL_TREE;
   13671              : 
   13672              :   /* Read the fields.  */
   13673        69976 :   vec<tree, va_heap> *fields = vec_chained_decls ();
   13674              : 
   13675        69976 :   if (TYPE_LANG_SPECIFIC (type))
   13676              :     {
   13677        68604 :       if (unsigned len = u ())
   13678              :         {
   13679        68604 :           vec_alloc (member_vec, len);
   13680       676474 :           for (unsigned ix = 0; ix != len; ix++)
   13681              :             {
   13682       539266 :               tree m = tree_node ();
   13683       539266 :               if (get_overrun ())
   13684              :                 break;
   13685       539266 :               if (TYPE_P (m))
   13686         5539 :                 m = TYPE_STUB_DECL (m);
   13687       539266 :               member_vec->quick_push (m);
   13688              :             }
   13689              :         }
   13690        68604 :       lambda = tree_node ();
   13691              : 
   13692        68604 :       if (!get_overrun ())
   13693              :         {
   13694        68604 :           unsigned nvbases = u ();
   13695        68604 :           if (nvbases)
   13696              :             {
   13697          292 :               vec_alloc (vbase_vec, nvbases);
   13698         1342 :               for (tree child = binfo; child; child = TREE_CHAIN (child))
   13699         1050 :                 if (BINFO_VIRTUAL_P (child))
   13700          292 :                   vbase_vec->quick_push (child);
   13701              :             }
   13702              :         }
   13703              : 
   13704        68604 :       if (!get_overrun ())
   13705              :         {
   13706        68604 :           int has_vptr = i ();
   13707        68604 :           if (has_vptr)
   13708              :             {
   13709         2699 :               pure_virts = tree_vec ();
   13710         2699 :               vcall_indices = tree_pair_vec ();
   13711         2699 :               key_method = tree_node ();
   13712              :             }
   13713              :         }
   13714              :     }
   13715              : 
   13716        69976 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
   13717        69976 :   bool installing = maybe_dup && !TYPE_SIZE (type);
   13718        40259 :   if (installing)
   13719              :     {
   13720        40259 :       if (maybe_dup != defn)
   13721              :         {
   13722              :           // FIXME: This is needed on other defns too, almost
   13723              :           // duplicate-decl like?  See is_matching_decl too.
   13724              :           /* Copy flags from the duplicate.  */
   13725          227 :           tree type_dup = TREE_TYPE (maybe_dup);
   13726              : 
   13727              :           /* Core pieces.  */
   13728          227 :           TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
   13729          227 :           TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
   13730          454 :           TYPE_WARN_IF_NOT_ALIGN_RAW (type)
   13731          227 :             = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
   13732          227 :           TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
   13733              : 
   13734          227 :           SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
   13735          227 :           DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
   13736          227 :           DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
   13737          227 :           DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
   13738          454 :           DECL_WARN_IF_NOT_ALIGN_RAW (defn)
   13739          227 :             = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
   13740          227 :           DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
   13741              : 
   13742          227 :           TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
   13743          227 :           TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
   13744          227 :           TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
   13745          227 :           TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
   13746          454 :           TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
   13747          227 :             = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
   13748              : 
   13749          227 :           TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
   13750          227 :           TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
   13751              : 
   13752              :           /* C++ pieces.  */
   13753          227 :           TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
   13754          227 :           CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
   13755              : 
   13756          454 :           TYPE_HAS_USER_CONSTRUCTOR (type)
   13757          227 :             = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
   13758          454 :           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   13759          227 :             = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
   13760          454 :           TYPE_NEEDS_CONSTRUCTING (type)
   13761          227 :             = TYPE_NEEDS_CONSTRUCTING (type_dup);
   13762              : 
   13763          227 :           if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
   13764              :             {
   13765          227 :               if (TYPE_LANG_SPECIFIC (type))
   13766              :                 {
   13767          681 :                   CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
   13768          227 :                     = CLASSTYPE_BEFRIENDING_CLASSES (type);
   13769          454 :                   SET_CLASSTYPE_TYPEINFO_VAR (type_dup,
   13770              :                                               CLASSTYPE_TYPEINFO_VAR (type));
   13771              :                 }
   13772          886 :               for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
   13773          659 :                 TYPE_LANG_SPECIFIC (v) = ls;
   13774              :             }
   13775              :         }
   13776              : 
   13777        40259 :       TYPE_SIZE (type) = size;
   13778        40259 :       TYPE_SIZE_UNIT (type) = size_unit;
   13779              : 
   13780        40259 :       if (fields)
   13781              :         {
   13782        40259 :           tree *chain = &TYPE_FIELDS (type);
   13783        40259 :           unsigned len = fields->length ();
   13784       501840 :           for (unsigned ix = 0; ix != len; ix++)
   13785              :             {
   13786       461581 :               tree decl = (*fields)[ix];
   13787              : 
   13788       461581 :               if (!decl)
   13789              :                 {
   13790              :                   /* An anonymous struct with typedef name.  */
   13791            3 :                   tree tdef = (*fields)[ix+1];
   13792            3 :                   decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
   13793            3 :                   gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
   13794              :                                        && decl != tdef);
   13795              :                 }
   13796              : 
   13797       834301 :               gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
   13798       461581 :               *chain = decl;
   13799       461581 :               chain = &DECL_CHAIN (decl);
   13800              : 
   13801       461581 :               if (TREE_CODE (decl) == FIELD_DECL
   13802       461581 :                   && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   13803              :                 {
   13804          271 :                   tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
   13805          271 :                   if (DECL_NAME (defn) == as_base_identifier)
   13806              :                     /* ANON_AGGR_TYPE_FIELD should already point to the
   13807              :                        original FIELD_DECL; don't overwrite it to point
   13808              :                        to the as-base FIELD_DECL copy.  */
   13809           24 :                     gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
   13810              :                   else
   13811          259 :                     SET_ANON_AGGR_TYPE_FIELD (anon_type, decl);
   13812              :                 }
   13813              : 
   13814       461581 :               if (TREE_CODE (decl) == USING_DECL
   13815       461581 :                   && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
   13816              :                 {
   13817              :                   /* Reconstruct DECL_ACCESS.  */
   13818        20102 :                   tree decls = USING_DECL_DECLS (decl);
   13819        20102 :                   tree access = declared_access (decl);
   13820              : 
   13821        23496 :                   for (ovl_iterator iter (decls); iter; ++iter)
   13822              :                     {
   13823         2183 :                       tree d = *iter;
   13824              : 
   13825         2183 :                       retrofit_lang_decl (d);
   13826         2183 :                       tree list = DECL_ACCESS (d);
   13827              : 
   13828         2183 :                       if (!purpose_member (type, list))
   13829         2609 :                         DECL_ACCESS (d) = tree_cons (type, access, list);
   13830              :                     }
   13831              :                 }
   13832              : 
   13833       461581 :               if (TREE_CODE (decl) == VAR_DECL
   13834        14091 :                   && TREE_CODE (maybe_template) != TEMPLATE_DECL)
   13835        12244 :                 note_vague_linkage_variable (decl);
   13836              :             }
   13837              :         }
   13838              : 
   13839        40259 :       TYPE_VFIELD (type) = vfield;
   13840        40259 :       TYPE_BINFO (type) = binfo;
   13841              : 
   13842        40259 :       if (TYPE_LANG_SPECIFIC (type))
   13843              :         {
   13844        39413 :           if (!TYPE_POLYMORPHIC_P (type))
   13845        37801 :             SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
   13846              :           else
   13847         1612 :             gcc_checking_assert (lambda == NULL_TREE);
   13848              : 
   13849        39413 :           CLASSTYPE_MEMBER_VEC (type) = member_vec;
   13850        39413 :           CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
   13851        39413 :           CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
   13852              : 
   13853        39413 :           if (TYPE_POLYMORPHIC_P (type))
   13854         1612 :             SET_CLASSTYPE_KEY_METHOD (type, key_method);
   13855              :           else
   13856        37801 :             gcc_checking_assert (key_method == NULL_TREE);
   13857              : 
   13858        39413 :           CLASSTYPE_VBASECLASSES (type) = vbase_vec;
   13859              : 
   13860              :           /* Resort the member vector.  */
   13861        39413 :           resort_type_member_vec (member_vec, NULL, nop, NULL);
   13862              :         }
   13863              :     }
   13864              :   else if (maybe_dup)
   13865              :     {
   13866              :       // FIXME:QOI Check matching defn
   13867              :     }
   13868              : 
   13869        69976 :   if (TYPE_LANG_SPECIFIC (type))
   13870              :     {
   13871        68604 :       tree primary = tree_node ();
   13872        68604 :       tree as_base = tree_node ();
   13873              : 
   13874        68604 :       if (as_base)
   13875        35141 :         as_base = TREE_TYPE (as_base);
   13876              : 
   13877              :       /* Read the vtables.  */
   13878        68604 :       vec<tree, va_heap> *vtables = vec_chained_decls ();
   13879        68604 :       if (vtables)
   13880              :         {
   13881         2659 :           unsigned len = vtables->length ();
   13882         5820 :           for (unsigned ix = 0; ix != len; ix++)
   13883              :             {
   13884         3161 :               tree vtable = (*vtables)[ix];
   13885         3161 :               read_var_def (vtable, vtable);
   13886              :             }
   13887              :         }
   13888              : 
   13889        68604 :       tree friend_classes = tree_list (false);
   13890        68604 :       tree friend_functions = NULL_TREE;
   13891        68604 :       for (tree *chain = &friend_functions;
   13892        80589 :            tree name = tree_node (); chain = &TREE_CHAIN (*chain))
   13893              :         {
   13894        11985 :           tree val = tree_list (false);
   13895        11985 :           *chain = build_tree_list (name, val);
   13896        11985 :         }
   13897        68604 :       tree decl_list = tree_list (true);
   13898              : 
   13899        68604 :       if (installing)
   13900              :         {
   13901        39413 :           CLASSTYPE_PRIMARY_BINFO (type) = primary;
   13902        39413 :           CLASSTYPE_AS_BASE (type) = as_base;
   13903              : 
   13904        39413 :           if (vtables)
   13905              :             {
   13906         1624 :               if ((!CLASSTYPE_KEY_METHOD (type)
   13907              :                    /* Sneaky user may have defined it inline
   13908              :                       out-of-class.  */
   13909         1168 :                    || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
   13910              :                   /* An imported non-template class attached to a module
   13911              :                      doesn't need to have its vtables emitted here.  */
   13912         1783 :                   && (CLASSTYPE_USE_TEMPLATE (type)
   13913          292 :                       || !DECL_MODULE_ATTACH_P (defn)))
   13914         1151 :                 vec_safe_push (keyed_classes, type);
   13915         1624 :               unsigned len = vtables->length ();
   13916         1624 :               tree *chain = &CLASSTYPE_VTABLES (type);
   13917         3567 :               for (unsigned ix = 0; ix != len; ix++)
   13918              :                 {
   13919         1943 :                   tree vtable = (*vtables)[ix];
   13920         1943 :                   gcc_checking_assert (!*chain);
   13921         1943 :                   *chain = vtable;
   13922         1943 :                   chain = &DECL_CHAIN (vtable);
   13923              :                 }
   13924              :             }
   13925        39413 :           CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
   13926        39413 :           DECL_FRIENDLIST (defn) = friend_functions;
   13927        39413 :           CLASSTYPE_DECL_LIST (type) = decl_list;
   13928              : 
   13929        42314 :           for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
   13930              :             {
   13931         2901 :               tree f = TREE_VALUE (friend_classes);
   13932         2901 :               if (TREE_CODE (f) == TEMPLATE_DECL)
   13933         1204 :                 f = TREE_TYPE (f);
   13934              : 
   13935         2901 :               if (CLASS_TYPE_P (f))
   13936              :                 {
   13937         2865 :                   CLASSTYPE_BEFRIENDING_CLASSES (f)
   13938         5730 :                     = tree_cons (NULL_TREE, type,
   13939         2865 :                                  CLASSTYPE_BEFRIENDING_CLASSES (f));
   13940         2907 :                   dump () && dump ("Class %N befriending %C:%N",
   13941            6 :                                    type, TREE_CODE (f), f);
   13942              :                 }
   13943              :             }
   13944              : 
   13945        46569 :           for (; friend_functions;
   13946         7156 :                friend_functions = TREE_CHAIN (friend_functions))
   13947         7156 :             for (tree friend_decls = TREE_VALUE (friend_functions);
   13948        16206 :                  friend_decls; friend_decls = TREE_CHAIN (friend_decls))
   13949              :               {
   13950         9050 :                 tree f = TREE_VALUE (friend_decls);
   13951         9050 :                 if (TREE_CODE (f) == TU_LOCAL_ENTITY)
   13952           36 :                   continue;
   13953              : 
   13954         9014 :                 DECL_BEFRIENDING_CLASSES (f)
   13955         9014 :                   = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
   13956         9080 :                 dump () && dump ("Class %N befriending %C:%N",
   13957           30 :                                  type, TREE_CODE (f), f);
   13958              :               }
   13959              :         }
   13960              : 
   13961        68604 :       if (TYPE_CONTAINS_VPTR_P (type))
   13962              :         /* Read and install the thunks.  */
   13963         3155 :         while (tree vfunc = tree_node ())
   13964              :           {
   13965          456 :             tree thunks = chained_decls ();
   13966          456 :             if (installing)
   13967          288 :               SET_DECL_THUNKS (vfunc, thunks);
   13968              :           }
   13969              : 
   13970        68604 :       vec_free (vtables);
   13971              :     }
   13972              : 
   13973              :   /* Propagate to all variants.  */
   13974        69976 :   if (installing)
   13975        40259 :     fixup_type_variants (type, /*update_align=*/true);
   13976              : 
   13977              :   /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
   13978              :      the fake base, we've not hooked it into the containing class's
   13979              :      data structure yet.  Fortunately it has a unique name.  */
   13980        40259 :   if (installing
   13981        40259 :       && DECL_NAME (defn) != as_base_identifier
   13982        39413 :       && (!CLASSTYPE_TEMPLATE_INFO (type)
   13983        33452 :           || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
   13984              :     /* Emit debug info.  It'd be nice to know if the interface TU
   13985              :        already emitted this.  */
   13986        21454 :     rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
   13987              : 
   13988        69976 :   vec_free (fields);
   13989              : 
   13990        69976 :   return !get_overrun ();
   13991              : }
   13992              : 
   13993              : void
   13994        10006 : trees_out::write_enum_def (tree decl)
   13995              : {
   13996        10006 :   tree type = TREE_TYPE (decl);
   13997              : 
   13998        10006 :   tree_node (TYPE_VALUES (type));
   13999              :   /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   14000              :      ENUMERAL_TYPE.  */
   14001        10006 : }
   14002              : 
   14003              : void
   14004        10006 : trees_out::mark_enum_def (tree decl)
   14005              : {
   14006        10006 :   tree type = TREE_TYPE (decl);
   14007              : 
   14008        53016 :   for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
   14009              :     {
   14010        43010 :       tree cst = TREE_VALUE (values);
   14011        43010 :       mark_by_value (cst);
   14012              :       /* We must mark the init to avoid circularity in tt_enum_int.  */
   14013        43010 :       if (tree init = DECL_INITIAL (cst))
   14014        42510 :         if (TREE_CODE (init) == INTEGER_CST)
   14015        41746 :           mark_by_value (init);
   14016              :     }
   14017        10006 : }
   14018              : 
   14019              : bool
   14020         3201 : trees_in::read_enum_def (tree defn, tree maybe_template)
   14021              : {
   14022         3201 :   tree type = TREE_TYPE (defn);
   14023         3201 :   tree values = tree_node ();
   14024              : 
   14025         3201 :   if (get_overrun ())
   14026              :     return false;
   14027              : 
   14028         3201 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
   14029         6402 :   bool installing = maybe_dup && !TYPE_VALUES (type);
   14030              : 
   14031         3201 :   if (installing)
   14032              :     {
   14033         1671 :       TYPE_VALUES (type) = values;
   14034              :       /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   14035              :          ENUMERAL_TYPE.  */
   14036              : 
   14037         2749 :       rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
   14038              :     }
   14039         1530 :   else if (maybe_dup)
   14040              :     {
   14041         1530 :       tree known = TYPE_VALUES (type);
   14042         8465 :       for (; known && values;
   14043         6935 :            known = TREE_CHAIN (known), values = TREE_CHAIN (values))
   14044              :         {
   14045         6944 :           tree known_decl = TREE_VALUE (known);
   14046         6944 :           tree new_decl = TREE_VALUE (values);
   14047              : 
   14048         6944 :           if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
   14049              :             break;
   14050              : 
   14051         6938 :           new_decl = maybe_duplicate (new_decl);
   14052              : 
   14053         6938 :           if (!cp_tree_equal (DECL_INITIAL (known_decl),
   14054         6938 :                               DECL_INITIAL (new_decl)))
   14055              :             break;
   14056              :         }
   14057              : 
   14058         1530 :       if (known || values)
   14059              :         {
   14060           12 :           auto_diagnostic_group d;
   14061           12 :           error_at (DECL_SOURCE_LOCATION (maybe_dup),
   14062              :                     "definition of %qD does not match", maybe_dup);
   14063           12 :           inform (DECL_SOURCE_LOCATION (defn),
   14064              :                   "existing definition %qD", defn);
   14065              : 
   14066           12 :           tree known_decl = NULL_TREE, new_decl = NULL_TREE;
   14067              : 
   14068           12 :           if (known)
   14069            9 :             known_decl = TREE_VALUE (known);
   14070           12 :           if (values)
   14071           12 :             new_decl = maybe_duplicate (TREE_VALUE (values));
   14072              : 
   14073           12 :           if (known_decl && new_decl)
   14074              :             {
   14075            9 :               inform (DECL_SOURCE_LOCATION (new_decl),
   14076              :                       "enumerator %qD does not match ...", new_decl);
   14077            9 :               inform (DECL_SOURCE_LOCATION (known_decl),
   14078              :                       "... this enumerator %qD", known_decl);
   14079              :             }
   14080            3 :           else if (known_decl || new_decl)
   14081              :             {
   14082            3 :               tree extra = known_decl ? known_decl : new_decl;
   14083            3 :               inform (DECL_SOURCE_LOCATION (extra),
   14084              :                       "additional enumerators beginning with %qD", extra);
   14085              :             }
   14086              :           else
   14087            0 :             inform (DECL_SOURCE_LOCATION (maybe_dup),
   14088              :                     "enumeration range differs");
   14089              : 
   14090              :           /* Mark it bad.  */
   14091           12 :           unmatched_duplicate (maybe_template);
   14092           12 :         }
   14093              :     }
   14094              : 
   14095              :   return true;
   14096              : }
   14097              : 
   14098              : /* Write out the body of DECL.  See above circularity note.  */
   14099              : 
   14100              : void
   14101      1036563 : trees_out::write_definition (tree decl, bool refs_tu_local)
   14102              : {
   14103      1036563 :   auto ovr = make_temp_override (writing_local_entities,
   14104      1036563 :                                  writing_local_entities || refs_tu_local);
   14105              : 
   14106      1036563 :   if (streaming_p ())
   14107              :     {
   14108       518191 :       assert_definition (decl);
   14109       518191 :       dump ()
   14110          952 :         && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
   14111              :     }
   14112              :   else
   14113       518372 :     dump (dumper::DEPEND)
   14114           96 :       && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
   14115              : 
   14116      1036563 :  again:
   14117      1624664 :   switch (TREE_CODE (decl))
   14118              :     {
   14119            0 :     default:
   14120            0 :       gcc_unreachable ();
   14121              : 
   14122       588101 :     case TEMPLATE_DECL:
   14123       588101 :       decl = DECL_TEMPLATE_RESULT (decl);
   14124       588101 :       goto again;
   14125              : 
   14126       653995 :     case FUNCTION_DECL:
   14127       653995 :       write_function_def (decl);
   14128       653995 :       break;
   14129              : 
   14130       231290 :     case TYPE_DECL:
   14131       231290 :       {
   14132       231290 :         tree type = TREE_TYPE (decl);
   14133       231290 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14134              :                     && TYPE_NAME (type) == decl);
   14135       231290 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14136        10006 :           write_enum_def (decl);
   14137              :         else
   14138       221284 :           write_class_def (decl);
   14139              :       }
   14140              :       break;
   14141              : 
   14142       151278 :     case VAR_DECL:
   14143       151278 :     case CONCEPT_DECL:
   14144       151278 :       write_var_def (decl);
   14145       151278 :       break;
   14146              :     }
   14147      1036563 : }
   14148              : 
   14149              : /* Mark a declaration for by-value walking.  If DO_DEFN is true, mark
   14150              :    its body too.  */
   14151              : 
   14152              : void
   14153      4356703 : trees_out::mark_declaration (tree decl, bool do_defn)
   14154              : {
   14155      4356703 :   mark_by_value (decl);
   14156              : 
   14157      4356703 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14158      1449480 :     decl = DECL_TEMPLATE_RESULT (decl);
   14159              : 
   14160      4356703 :   if (!do_defn)
   14161              :     return;
   14162              : 
   14163      1036585 :   switch (TREE_CODE (decl))
   14164              :     {
   14165            0 :     default:
   14166            0 :       gcc_unreachable ();
   14167              : 
   14168              :     case FUNCTION_DECL:
   14169              :       mark_function_def (decl);
   14170              :       break;
   14171              : 
   14172       231318 :     case TYPE_DECL:
   14173       231318 :       {
   14174       231318 :         tree type = TREE_TYPE (decl);
   14175       231318 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14176              :                     && TYPE_NAME (type) == decl);
   14177       231318 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14178        10006 :           mark_enum_def (decl);
   14179              :         else
   14180       221312 :           mark_class_def (decl);
   14181              :       }
   14182              :       break;
   14183              : 
   14184              :     case VAR_DECL:
   14185              :     case CONCEPT_DECL:
   14186              :       mark_var_def (decl);
   14187              :       break;
   14188              :     }
   14189              : }
   14190              : 
   14191              : /* Read in the body of DECL.  See above circularity note.  */
   14192              : 
   14193              : bool
   14194       343140 : trees_in::read_definition (tree decl)
   14195              : {
   14196       344480 :   dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
   14197              : 
   14198       343140 :   tree maybe_template = decl;
   14199              : 
   14200       545853 :  again:
   14201       545853 :   switch (TREE_CODE (decl))
   14202              :     {
   14203              :     default:
   14204              :       break;
   14205              : 
   14206       202713 :     case TEMPLATE_DECL:
   14207       202713 :       decl = DECL_TEMPLATE_RESULT (decl);
   14208       202713 :       goto again;
   14209              : 
   14210       226584 :     case FUNCTION_DECL:
   14211       226584 :       return read_function_def (decl, maybe_template);
   14212              : 
   14213        73177 :     case TYPE_DECL:
   14214        73177 :       {
   14215        73177 :         tree type = TREE_TYPE (decl);
   14216        73177 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14217              :                     && TYPE_NAME (type) == decl);
   14218        73177 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14219         3201 :           return read_enum_def (decl, maybe_template);
   14220              :         else
   14221        69976 :           return read_class_def (decl, maybe_template);
   14222              :       }
   14223        43379 :       break;
   14224              : 
   14225        43379 :     case VAR_DECL:
   14226        43379 :     case CONCEPT_DECL:
   14227        43379 :       return read_var_def (decl, maybe_template);
   14228              :     }
   14229              : 
   14230              :   return false;
   14231              : }
   14232              : 
   14233              : /* Lookup an maybe insert a slot for depset for KEY.  */
   14234              : 
   14235              : depset **
   14236     21881488 : depset::hash::entity_slot (tree entity, bool insert)
   14237              : {
   14238     21881488 :   traits::compare_type key (entity, NULL);
   14239     33025847 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14240              :                                        insert ? INSERT : NO_INSERT);
   14241              : 
   14242     21881488 :   return slot;
   14243              : }
   14244              : 
   14245              : depset **
   14246       244156 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
   14247              : {
   14248       244156 :   traits::compare_type key (ctx, name);
   14249       324307 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14250              :                                        insert ? INSERT : NO_INSERT);
   14251              : 
   14252       244156 :   return slot;
   14253              : }
   14254              : 
   14255              : depset *
   14256     10637643 : depset::hash::find_dependency (tree decl)
   14257              : {
   14258     10637643 :   depset **slot = entity_slot (decl, false);
   14259              : 
   14260     10637643 :   return slot ? *slot : NULL;
   14261              : }
   14262              : 
   14263              : depset *
   14264        80151 : depset::hash::find_binding (tree ctx, tree name)
   14265              : {
   14266        80151 :   depset **slot = binding_slot (ctx, name, false);
   14267              : 
   14268        80151 :   return slot ? *slot : NULL;
   14269              : }
   14270              : 
   14271              : static bool is_tu_local_entity (tree decl, bool explain = false);
   14272              : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
   14273              : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
   14274              : 
   14275              : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
   14276              :    If EXPLAIN is true, emit an informative note about why DECL is TU-local.  */
   14277              : 
   14278              : static bool
   14279      4990008 : is_tu_local_entity (tree decl, bool explain/*=false*/)
   14280              : {
   14281      5177752 :   gcc_checking_assert (DECL_P (decl));
   14282      5177752 :   location_t loc = DECL_SOURCE_LOCATION (decl);
   14283      5177752 :   tree type = TREE_TYPE (decl);
   14284              : 
   14285              :   /* Only types, functions, variables, and template (specialisations)
   14286              :      can be TU-local.  */
   14287      5177752 :   if (TREE_CODE (decl) != TYPE_DECL
   14288              :       && TREE_CODE (decl) != FUNCTION_DECL
   14289              :       && TREE_CODE (decl) != VAR_DECL
   14290              :       && TREE_CODE (decl) != TEMPLATE_DECL)
   14291              :     return false;
   14292              : 
   14293              :   /* An explicit type alias is not an entity; we don't want to stream
   14294              :      such aliases if they refer to TU-local entities, so propagate this
   14295              :      from the original type. The built-in declarations of 'int' and such
   14296              :      are never TU-local.  */
   14297      5173723 :   if (TREE_CODE (decl) == TYPE_DECL
   14298      1735986 :       && !DECL_SELF_REFERENCE_P (decl)
   14299      6835739 :       && !DECL_IMPLICIT_TYPEDEF_P (decl))
   14300              :     {
   14301       884620 :       tree orig = DECL_ORIGINAL_TYPE (decl);
   14302       884620 :       if (orig && TYPE_NAME (orig))
   14303              :         {
   14304       187744 :           if (explain)
   14305           11 :             inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
   14306       187744 :           return is_tu_local_entity (TYPE_NAME (orig), explain);
   14307              :         }
   14308              :       else
   14309              :         return false;
   14310              :     }
   14311              : 
   14312              :   /* Check specializations first for slightly better explanations.  */
   14313      4289103 :   int use_tpl = -1;
   14314      4289103 :   tree ti = node_template_info (decl, use_tpl);
   14315      5242836 :   if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
   14316              :     {
   14317              :       /* A specialization of a TU-local template.  */
   14318       953550 :       tree tmpl = TI_TEMPLATE (ti);
   14319       953550 :       if (is_tu_local_entity (tmpl))
   14320              :         {
   14321           72 :           if (explain)
   14322              :             {
   14323           18 :               inform (loc, "%qD is a specialization of TU-local template %qD",
   14324              :                       decl, tmpl);
   14325           18 :               is_tu_local_entity (tmpl, /*explain=*/true);
   14326              :             }
   14327              :           return true;
   14328              :         }
   14329              : 
   14330              :       /* A specialization of a template with any TU-local template argument.  */
   14331       953478 :       if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
   14332              :         return true;
   14333              : 
   14334              :       /* FIXME A specialization of a template whose (possibly instantiated)
   14335              :          declaration is an exposure.  This should always be covered by the
   14336              :          above cases??  */
   14337              :     }
   14338              : 
   14339              :   /* A type, function, variable, or template with internal linkage.  */
   14340      4288999 :   linkage_kind kind = decl_linkage (decl);
   14341      4288999 :   if (kind == lk_internal
   14342              :       /* But although weakrefs are marked static, don't consider them
   14343              :          to be TU-local.  */
   14344      4288999 :       && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
   14345              :     {
   14346          853 :       if (explain)
   14347          168 :         inform (loc, "%qD declared with internal linkage", decl);
   14348              :       return true;
   14349              :     }
   14350              : 
   14351              :   /* Does not have a name with linkage and is declared, or introduced by a
   14352              :      lambda-expression, within the definition of a TU-local entity.  */
   14353      4288146 :   if (kind == lk_none)
   14354              :     {
   14355       440279 :       tree ctx = CP_DECL_CONTEXT (decl);
   14356       561065 :       if (LAMBDA_TYPE_P (type))
   14357        86980 :         if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
   14358       440279 :           ctx = extra;
   14359              : 
   14360       440279 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   14361              :         {
   14362           31 :           if (!TREE_PUBLIC (ctx))
   14363              :             {
   14364            0 :               if (explain)
   14365            0 :                 inform (loc, "%qD has no linkage and is declared in an "
   14366              :                         "anonymous namespace", decl);
   14367              :               return true;
   14368              :             }
   14369              :         }
   14370       440248 :       else if (TYPE_P (ctx))
   14371              :         {
   14372        49825 :           tree ctx_decl = TYPE_MAIN_DECL (ctx);
   14373        49825 :           if (is_tu_local_entity (ctx_decl))
   14374              :             {
   14375            6 :               if (explain)
   14376              :                 {
   14377            0 :                   inform (loc, "%qD has no linkage and is declared within "
   14378              :                           "TU-local entity %qT", decl, ctx);
   14379            0 :                   is_tu_local_entity (ctx_decl, /*explain=*/true);
   14380              :                 }
   14381              :               return true;
   14382              :             }
   14383              :         }
   14384       390423 :       else if (is_tu_local_entity (ctx))
   14385              :         {
   14386           33 :           if (explain)
   14387              :             {
   14388            6 :               inform (loc, "%qD has no linkage and is declared within "
   14389              :                       "TU-local entity %qD", decl, ctx);
   14390            6 :               is_tu_local_entity (ctx, /*explain=*/true);
   14391              :             }
   14392              :           return true;
   14393              :         }
   14394              :     }
   14395              : 
   14396              :   /* A type with no name that is defined outside a class-specifier, function
   14397              :      body, or initializer; or is introduced by a defining-type-specifier that
   14398              :      is used to declare only TU-local entities.
   14399              : 
   14400              :      We consider types with names for linkage purposes as having names, since
   14401              :      these aren't really TU-local.  */
   14402      4288107 :   tree inner = STRIP_TEMPLATE (decl);
   14403      1816285 :   if (inner
   14404      4288107 :       && TREE_CODE (inner) == TYPE_DECL
   14405      3237449 :       && TYPE_ANON_P (type)
   14406        47348 :       && !DECL_SELF_REFERENCE_P (inner)
   14407              :       /* An enum with an enumerator name for linkage.  */
   14408      1858664 :       && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
   14409              :     {
   14410        40091 :       tree main_decl = TYPE_MAIN_DECL (type);
   14411        79638 :       if (LAMBDA_TYPE_P (type))
   14412              :         {
   14413              :           /* A lambda expression is, in practice, TU-local iff it has no
   14414              :              mangling scope.  This currently doesn't line up exactly with
   14415              :              the standard's definition due to some ABI issues, but it's
   14416              :              pretty close, and avoids other issues down the line.  */
   14417        78944 :           if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
   14418              :             {
   14419            4 :               if (explain)
   14420            2 :                 inform (loc, "%qT has no name and cannot be differentiated "
   14421              :                         "from similar lambdas in other TUs", type);
   14422              :               return true;
   14423              :             }
   14424              :         }
   14425         1238 :       else if (!DECL_CLASS_SCOPE_P (main_decl)
   14426          649 :                && !decl_function_context (main_decl))
   14427              :         {
   14428           30 :           if (explain)
   14429           12 :             inform (loc, "%qT has no name and is not defined within a class, "
   14430              :                     "function, or initializer", type);
   14431              :           return true;
   14432              :         }
   14433              : 
   14434              :       // FIXME introduced by a defining-type-specifier only declaring TU-local
   14435              :       // entities; does this refer to e.g. 'static struct {} a;"?  I can't
   14436              :       // think of any cases where this isn't covered by earlier cases.  */
   14437              :     }
   14438              : 
   14439              :   return false;
   14440              : }
   14441              : 
   14442              : /* Helper for is_tu_local_entity.  Returns true if one of the ARGS of
   14443              :    DECL is TU-local.  Emits an explanation if EXPLAIN is true.  */
   14444              : 
   14445              : static bool
   14446      1082903 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
   14447              : {
   14448      1082903 :   if (!args || TREE_CODE (args) != TREE_VEC)
   14449              :     return false;
   14450              : 
   14451      2931589 :   for (tree a : tree_vec_range (args))
   14452              :     {
   14453      1848718 :       if (TREE_CODE (a) == TREE_VEC)
   14454              :         {
   14455       129425 :           if (has_tu_local_tmpl_arg (decl, a, explain))
   14456           32 :             return true;
   14457              :         }
   14458              :       else if (!WILDCARD_TYPE_P (a))
   14459              :         {
   14460      1565530 :           if (DECL_P (a) && is_tu_local_entity (a))
   14461              :             {
   14462            0 :               if (explain)
   14463              :                 {
   14464            0 :                   inform (DECL_SOURCE_LOCATION (decl),
   14465              :                           "%qD has TU-local template argument %qD",
   14466              :                           decl, a);
   14467            0 :                   is_tu_local_entity (a, /*explain=*/true);
   14468              :                 }
   14469              :               return true;
   14470              :             }
   14471              : 
   14472      1565530 :           if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
   14473              :             {
   14474           17 :               if (explain)
   14475              :                 {
   14476            1 :                   inform (DECL_SOURCE_LOCATION (decl),
   14477              :                           "%qD has TU-local template argument %qT",
   14478              :                           decl, a);
   14479            1 :                   is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
   14480              :                 }
   14481              :               return true;
   14482              :             }
   14483              : 
   14484      1565513 :           if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
   14485              :             return true;
   14486              :         }
   14487              :     }
   14488              : 
   14489      1082871 :   return false;
   14490              : }
   14491              : 
   14492              : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
   14493              :    or object.  Emits an explanation if EXPLAIN is true.  */
   14494              : 
   14495              : static bool
   14496       124472 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
   14497              : {
   14498       124472 :   if (!expr)
   14499              :     return false;
   14500              : 
   14501       122435 :   tree e = expr;
   14502       122435 :   STRIP_ANY_LOCATION_WRAPPER (e);
   14503       122435 :   STRIP_NOPS (e);
   14504       122435 :   if (TREE_CODE (e) == TARGET_EXPR)
   14505            0 :     e = TARGET_EXPR_INITIAL (e);
   14506            0 :   if (!e)
   14507              :     return false;
   14508              : 
   14509              :   /* It is, or is a pointer to, a TU-local function or the object associated
   14510              :      with a TU-local variable.  */
   14511       122435 :   tree object = NULL_TREE;
   14512       122435 :   if (TREE_CODE (e) == ADDR_EXPR)
   14513         4586 :     object = TREE_OPERAND (e, 0);
   14514       117849 :   else if (TREE_CODE (e) == PTRMEM_CST)
   14515            0 :     object = PTRMEM_CST_MEMBER (e);
   14516       117849 :   else if (VAR_OR_FUNCTION_DECL_P (e))
   14517              :     object = e;
   14518              : 
   14519         4586 :   if (object
   14520         5358 :       && VAR_OR_FUNCTION_DECL_P (object)
   14521         5445 :       && is_tu_local_entity (object))
   14522              :     {
   14523           54 :       if (explain)
   14524              :         {
   14525              :           /* We've lost a lot of location information by the time we get here,
   14526              :              so let's just do our best effort.  */
   14527           18 :           auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
   14528           18 :           if (VAR_P (object))
   14529            9 :             inform (loc, "%qD refers to TU-local object %qD", decl, object);
   14530              :           else
   14531            9 :             inform (loc, "%qD refers to TU-local function %qD", decl, object);
   14532           18 :           is_tu_local_entity (object, true);
   14533              :         }
   14534              :       return true;
   14535              :     }
   14536              : 
   14537              :   /* It is an object of class or array type and any of its subobjects or
   14538              :      any of the objects or functions to which its non-static data members
   14539              :      of reference type refer is TU-local and is usable in constant
   14540              :      expressions.  */
   14541       122381 :   if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
   14542        70796 :     for (auto &f : CONSTRUCTOR_ELTS (e))
   14543        54438 :       if (is_tu_local_value (decl, f.value, explain))
   14544              :         return true;
   14545              : 
   14546              :   return false;
   14547              : }
   14548              : 
   14549              : /* Complains if DECL is a TU-local entity imported from a named module.
   14550              :    Returns TRUE if instantiation should fail.  */
   14551              : 
   14552              : bool
   14553   9303327772 : instantiating_tu_local_entity (tree decl)
   14554              : {
   14555   9303327772 :   if (!modules_p ())
   14556              :     return false;
   14557              : 
   14558     36484244 :   if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
   14559              :     {
   14560           92 :       auto_diagnostic_group d;
   14561           92 :       error ("instantiation exposes TU-local entity %qD",
   14562           92 :              TU_LOCAL_ENTITY_NAME (decl));
   14563           92 :       inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
   14564           92 :       return true;
   14565           92 :     }
   14566              : 
   14567              :   /* Currently, only TU-local variables and functions, or possibly
   14568              :      templates thereof, will be emitted from named modules.  */
   14569     36484152 :   tree inner = STRIP_TEMPLATE (decl);
   14570     36484152 :   if (!VAR_OR_FUNCTION_DECL_P (inner))
   14571              :     return false;
   14572              : 
   14573              :   /* From this point we will only be emitting warnings; if we're not
   14574              :      warning about this case then there's no need to check further.  */
   14575      1523242 :   if (!warn_expose_global_module_tu_local
   14576      3046484 :       || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   14577      1523242 :                               OPT_Wexpose_global_module_tu_local))
   14578              :     return false;
   14579              : 
   14580      1511258 :   if (!is_tu_local_entity (decl))
   14581              :     return false;
   14582              : 
   14583           65 :   if (!DECL_LANG_SPECIFIC (inner)
   14584          120 :       || !DECL_MODULE_IMPORT_P (inner))
   14585              :     return false;
   14586              : 
   14587              :   /* Referencing TU-local entities from a header is generally OK.
   14588              :      We don't have an easy way to detect if this declaration came
   14589              :      from a header via a separate named module, but we can just
   14590              :      ignore that case for warning purposes.  */
   14591            9 :   unsigned index = import_entity_index (decl);
   14592            9 :   module_state *mod = import_entity_module (index);
   14593            9 :   if (mod->is_header ())
   14594              :     return false;
   14595              : 
   14596            9 :   auto_diagnostic_group d;
   14597            9 :   pedwarn (input_location, OPT_Wexpose_global_module_tu_local,
   14598              :            "instantiation exposes TU-local entity %qD", decl);
   14599            9 :   inform (DECL_SOURCE_LOCATION (decl), "declared here");
   14600              : 
   14601              :   /* We treat TU-local entities from the GMF as not actually being
   14602              :      TU-local as an extension, so allow instantiation to proceed.  */
   14603            9 :   return false;
   14604            9 : }
   14605              : 
   14606              : /* DECL is a newly discovered dependency.  Create the depset, if it
   14607              :    doesn't already exist.  Add it to the worklist if so.
   14608              : 
   14609              :    DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
   14610              :    a using decl.
   14611              : 
   14612              :    We do not have to worry about adding the same dependency more than
   14613              :    once.  First it's harmless, but secondly the TREE_VISITED marking
   14614              :    prevents us wanting to do it anyway.  */
   14615              : 
   14616              : depset *
   14617      9405603 : depset::hash::make_dependency (tree decl, entity_kind ek)
   14618              : {
   14619              :   /* Make sure we're being told consistent information.  */
   14620     17616190 :   gcc_checking_assert ((ek == EK_NAMESPACE)
   14621              :                        == (TREE_CODE (decl) == NAMESPACE_DECL
   14622              :                            && !DECL_NAMESPACE_ALIAS (decl)));
   14623      9405603 :   gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
   14624      9405603 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   14625              :                        && (TREE_CODE (decl) != USING_DECL
   14626              :                            || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
   14627      9405603 :   gcc_checking_assert (!is_key_order ());
   14628      9405603 :   if (ek == EK_USING)
   14629        41617 :     gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
   14630      9405603 :   if (ek == EK_TU_LOCAL)
   14631           93 :     gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
   14632              : 
   14633      9405603 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14634              :     /* The template should have copied these from its result decl.  */
   14635      3676792 :     gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
   14636              :                          == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
   14637              : 
   14638      9405603 :   depset **slot = entity_slot (decl, true);
   14639      9405603 :   depset *dep = *slot;
   14640      9405603 :   bool for_binding = ek == EK_FOR_BINDING;
   14641              : 
   14642      9405603 :   if (!dep)
   14643              :     {
   14644       780184 :       if ((DECL_IMPLICIT_TYPEDEF_P (decl)
   14645              :            /* ... not an enum, for instance.  */
   14646       386002 :            && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   14647       380733 :            && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
   14648       347612 :            && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   14649      2690654 :           || (VAR_P (decl)
   14650       103888 :               && DECL_LANG_SPECIFIC (decl)
   14651       103828 :               && DECL_USE_TEMPLATE (decl) == 2))
   14652              :         {
   14653              :           /* A partial or explicit specialization. Partial
   14654              :              specializations might not be in the hash table, because
   14655              :              there can be multiple differently-constrained variants.
   14656              : 
   14657              :              template<typename T> class silly;
   14658              :              template<typename T> requires true class silly {};
   14659              : 
   14660              :              We need to find them, insert their TEMPLATE_DECL in the
   14661              :              dep_hash, and then convert the dep we just found into a
   14662              :              redirect.  */
   14663              : 
   14664        51120 :           tree ti = get_template_info (decl);
   14665        51120 :           tree tmpl = TI_TEMPLATE (ti);
   14666        51120 :           tree partial = NULL_TREE;
   14667        51120 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   14668       188863 :                spec; spec = TREE_CHAIN (spec))
   14669       165295 :             if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
   14670              :               {
   14671              :                 partial = TREE_VALUE (spec);
   14672              :                 break;
   14673              :               }
   14674              : 
   14675        51120 :           if (partial)
   14676              :             {
   14677              :               /* Eagerly create an empty redirect.  The following
   14678              :                  make_dependency call could cause hash reallocation,
   14679              :                  and invalidate slot's value.  */
   14680        27552 :               depset *redirect = make_entity (decl, EK_REDIRECT);
   14681              : 
   14682              :               /* Redirects are never reached -- always snap to their target.  */
   14683        27552 :               redirect->set_flag_bit<DB_UNREACHED_BIT> ();
   14684              : 
   14685        27552 :               *slot = redirect;
   14686              : 
   14687        27552 :               depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
   14688        27552 :               gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
   14689              : 
   14690        27552 :               redirect->deps.safe_push (tmpl_dep);
   14691              : 
   14692        27552 :               return redirect;
   14693              :             }
   14694              :         }
   14695              : 
   14696      1924858 :       bool has_def = ek != EK_USING && has_definition (decl);
   14697      1883241 :       if (ek > EK_BINDING)
   14698       157708 :         ek = EK_DECL;
   14699              : 
   14700              :       /* The only OVERLOADS we should see are USING decls from
   14701              :          bindings.  */
   14702      1924858 :       *slot = dep = make_entity (decl, ek, has_def);
   14703              : 
   14704      1924858 :       if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
   14705              :         /* The template_result should otherwise not be in the
   14706              :            table, or be an empty redirect (created above).  */
   14707       506716 :         if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
   14708        27552 :           gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
   14709              :                                && !(*eslot)->deps.length ());
   14710              : 
   14711      1924858 :       if (ignore_exposure)
   14712        54174 :         dep->set_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14713              : 
   14714      1924858 :       if (ek != EK_USING)
   14715              :         {
   14716      1883241 :           tree not_tmpl = STRIP_TEMPLATE (decl);
   14717      1883241 :           bool imported_from_module_p = false;
   14718              : 
   14719      1883241 :           if (DECL_LANG_SPECIFIC (not_tmpl)
   14720      3580399 :               && DECL_MODULE_IMPORT_P (not_tmpl))
   14721              :             {
   14722              :               /* Store the module number and index in cluster/section,
   14723              :                  so we don't have to look them up again.  */
   14724        67340 :               unsigned index = import_entity_index (decl);
   14725        67340 :               module_state *from = import_entity_module (index);
   14726              :               /* Remap will be zero for imports from partitions, which
   14727              :                  we want to treat as-if declared in this TU.  */
   14728        67340 :               if (from->remap)
   14729              :                 {
   14730        66583 :                   dep->cluster = index - from->entity_lwm;
   14731        66583 :                   dep->section = from->remap;
   14732        66583 :                   dep->set_flag_bit<DB_IMPORTED_BIT> ();
   14733              : 
   14734        66583 :                   if (!from->is_header ())
   14735      1883241 :                     imported_from_module_p = true;
   14736              :                 }
   14737              :             }
   14738              : 
   14739              :           /* Check for TU-local entities.  This is unnecessary in header
   14740              :              units because we can export internal-linkage decls, and
   14741              :              no declarations are exposures.  Similarly, if the decl was
   14742              :              imported from a non-header module we know it cannot have
   14743              :              been TU-local.  */
   14744      1883241 :           if (!header_module_p () && !imported_from_module_p)
   14745              :             {
   14746       917759 :               if (is_tu_local_entity (decl))
   14747          309 :                 dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
   14748              : 
   14749       917759 :               if (VAR_P (decl)
   14750        48514 :                   && decl_maybe_constant_var_p (decl)
   14751       964924 :                   && is_tu_local_value (decl, DECL_INITIAL (decl)))
   14752              :                 {
   14753              :                   /* A potentially-constant variable initialized to a TU-local
   14754              :                      value is not usable in constant expressions within other
   14755              :                      translation units.  We can achieve this by simply not
   14756              :                      streaming the definition in such cases.  */
   14757           24 :                   dep->clear_flag_bit<DB_DEFN_BIT> ();
   14758              : 
   14759           24 :                   if (DECL_DECLARED_CONSTEXPR_P (decl)
   14760           39 :                       || DECL_INLINE_VAR_P (decl))
   14761              :                     /* A constexpr variable initialized to a TU-local value,
   14762              :                        or an inline value (PR c++/119996), is an exposure.
   14763              : 
   14764              :                        For simplicity, we don't support "non-strict" TU-local
   14765              :                        values: even if the TU-local entity we refer to in the
   14766              :                        initialiser is in the GMF, we still won't consider this
   14767              :                        valid in constant expressions in other TUs, and so
   14768              :                        complain accordingly.  */
   14769           15 :                     dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14770              :                 }
   14771              :             }
   14772              : 
   14773              :           /* A namespace-scope type may be declared in one module unit
   14774              :              and defined in another; make sure that we're found when
   14775              :              completing the class.  */
   14776      1883241 :           if (ek == EK_DECL
   14777       750973 :               && !dep->is_import ()
   14778       746812 :               && dep->has_defn ()
   14779       397920 :               && DECL_NAMESPACE_SCOPE_P (not_tmpl)
   14780       143661 :               && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
   14781              :               /* Anonymous types can't be forward-declared.  */
   14782      1920649 :               && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
   14783        36888 :             dep->set_flag_bit<DB_IS_PENDING_BIT> ();
   14784              : 
   14785              :           /* Namespace-scope functions can be found by ADL by template
   14786              :              instantiations in this module.  We need to create bindings
   14787              :              for them so that name lookup recognises they exist, if they
   14788              :              won't be discarded.  add_binding_entity is too early to do
   14789              :              this for GM functions, because if nobody ends up using them
   14790              :              we'll have leftover bindings laying around, and it's tricky
   14791              :              to delete them and any namespaces they've implicitly created
   14792              :              deps on.  The downside is this means we don't pick up on
   14793              :              using-decls, but by [module.global.frag] p3.6 we don't have
   14794              :              to.  */
   14795      1883241 :           if (ek == EK_DECL
   14796      1883241 :               && !for_binding
   14797       593277 :               && !dep->is_import ()
   14798       589122 :               && !dep->is_tu_local ()
   14799       589018 :               && DECL_NAMESPACE_SCOPE_P (decl)
   14800        76279 :               && DECL_DECLARES_FUNCTION_P (decl)
   14801              :               /* Compiler-generated functions won't participate in ADL.  */
   14802        59124 :               && !DECL_ARTIFICIAL (decl)
   14803              :               /* A hidden friend doesn't need a binding.  */
   14804      1933945 :               && !(DECL_LANG_SPECIFIC (not_tmpl)
   14805        50704 :                    && DECL_UNIQUE_FRIEND_P (not_tmpl)))
   14806              :             {
   14807              :               /* This will only affect GM functions.  */
   14808        65556 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
   14809              :                                    || !DECL_MODULE_PURVIEW_P (not_tmpl));
   14810              :               /* We shouldn't see any instantiations or specialisations.  */
   14811        32778 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   14812              :                                    || !DECL_USE_TEMPLATE (decl));
   14813              : 
   14814        32778 :               tree ns = CP_DECL_CONTEXT (decl);
   14815        32778 :               tree name = DECL_NAME (decl);
   14816        32778 :               depset *binding = find_binding (ns, name);
   14817        32778 :               if (!binding)
   14818              :                 {
   14819        10169 :                   binding = make_binding (ns, name);
   14820        10169 :                   add_namespace_context (binding, ns);
   14821              : 
   14822        10169 :                   depset **slot = binding_slot (ns, name, /*insert=*/true);
   14823        10169 :                   *slot = binding;
   14824              :                 }
   14825              : 
   14826        32778 :               binding->deps.safe_push (dep);
   14827        32778 :               dep->deps.safe_push (binding);
   14828        32813 :               dump (dumper::DEPEND)
   14829            9 :                 && dump ("Built ADL binding for %C:%N",
   14830            9 :                          TREE_CODE (decl), decl);
   14831              :             }
   14832              :         }
   14833              : 
   14834      1924858 :       if (!dep->is_import ())
   14835      1858275 :         worklist.safe_push (dep);
   14836              :     }
   14837      7453193 :   else if (!ignore_exposure)
   14838      6576098 :     dep->clear_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14839              : 
   14840      9378051 :   dump (dumper::DEPEND)
   14841        37533 :     && dump ("%s on %s %C:%N found",
   14842              :              ek == EK_REDIRECT ? "Redirect"
   14843        37533 :              : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
   14844              :              : "Dependency",
   14845        37533 :              dep->entity_kind_name (), TREE_CODE (decl), decl);
   14846              : 
   14847      9378051 :   return dep;
   14848              : }
   14849              : 
   14850              : /* Whether REF is an exposure of a member type of SOURCE.
   14851              : 
   14852              :    This comes up with exposures of class-scope lambdas, that we currently
   14853              :    treat as TU-local due to ABI reasons.  In such a case the type of the
   14854              :    lambda will be exposed in two places, first by the class type it is in
   14855              :    the TYPE_FIELDS list of, and second by the actual member declaring that
   14856              :    lambda.  We only want the second case to warn.  */
   14857              : 
   14858              : static bool
   14859          253 : is_exposure_of_member_type (depset *source, depset *ref)
   14860              : {
   14861          253 :   gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
   14862              :                        && ref->is_tu_local (/*strict=*/true));
   14863          253 :   tree source_entity = STRIP_TEMPLATE (source->get_entity ());
   14864          253 :   tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
   14865              : 
   14866          253 :   if (!source->is_tu_local (/*strict=*/true)
   14867          241 :       && source_entity
   14868          241 :       && ref_entity
   14869          241 :       && DECL_IMPLICIT_TYPEDEF_P (source_entity)
   14870            2 :       && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
   14871            2 :       && DECL_CLASS_SCOPE_P (ref_entity)
   14872          255 :       && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
   14873              :     {
   14874            4 :       gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
   14875              :       return true;
   14876              :     }
   14877              :   else
   14878              :     return false;
   14879              : }
   14880              : 
   14881              : /* DEP is a newly discovered dependency.  Append it to current's
   14882              :    depset.  */
   14883              : 
   14884              : void
   14885      7103887 : depset::hash::add_dependency (depset *dep)
   14886              : {
   14887      7103887 :   gcc_checking_assert (current && !is_key_order ());
   14888      7103887 :   current->deps.safe_push (dep);
   14889              : 
   14890      7103887 :   if (dep->is_tu_local (/*strict=*/true))
   14891              :     {
   14892          329 :       if (dep->is_tu_local ())
   14893          260 :         current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
   14894              :       else
   14895           69 :         current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
   14896              : 
   14897          329 :       if (!ignore_exposure && !is_exposure_of_member_type (current, dep))
   14898              :         {
   14899          133 :           if (dep->is_tu_local ())
   14900           91 :             current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14901              :           else
   14902           42 :             current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
   14903              :         }
   14904              :     }
   14905              : 
   14906      7103887 :   if (current->get_entity_kind () == EK_USING
   14907        41730 :       && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
   14908      7109280 :       && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
   14909              :     {
   14910              :       /* CURRENT is an unwrapped using-decl and DECL is an enum's
   14911              :          implicit typedef.  Is CURRENT a member of the enum?  */
   14912         5084 :       tree c_decl = OVL_FUNCTION (current->get_entity ());
   14913              : 
   14914         5084 :       if (TREE_CODE (c_decl) == CONST_DECL
   14915        10149 :           && (current->deps[0]->get_entity ()
   14916         5065 :               == CP_DECL_CONTEXT (dep->get_entity ())))
   14917              :         /* Make DECL depend on CURRENT.  */
   14918         5011 :         dep->deps.safe_push (current);
   14919              :     }
   14920              : 
   14921              :   /* If two dependencies recursively depend on each other existing within
   14922              :      their own merge keys, we must ensure that the first dep we saw while
   14923              :      walking is written first in this cluster.  See sort_cluster for more
   14924              :      details.  */
   14925      7103887 :   if (writing_merge_key)
   14926              :     {
   14927      1279717 :       if (!dep->is_maybe_recursive () && !current->is_maybe_recursive ())
   14928        66886 :         current->set_flag_bit<DB_ENTRY_BIT> ();
   14929      1279717 :       dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14930      1279717 :       current->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14931              :     }
   14932              : 
   14933      7103887 :   if (dep->is_unreached ())
   14934              :     {
   14935              :       /* The dependency is reachable now.  */
   14936       517044 :       reached_unreached = true;
   14937       517044 :       dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   14938       517044 :       dump (dumper::DEPEND)
   14939           30 :         && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
   14940           30 :                  TREE_CODE (dep->get_entity ()), dep->get_entity ());
   14941              :     }
   14942      7103887 : }
   14943              : 
   14944              : depset *
   14945     10461329 : depset::hash::add_dependency (tree decl, entity_kind ek)
   14946              : {
   14947     10461329 :   depset *dep;
   14948              : 
   14949     10461329 :   if (is_key_order ())
   14950              :     {
   14951      3289866 :       dep = find_dependency (decl);
   14952      3289866 :       if (dep)
   14953              :         {
   14954      1492963 :           current->deps.safe_push (dep);
   14955      1492963 :           dump (dumper::MERGE)
   14956          723 :             && dump ("Key dependency on %s %C:%N found",
   14957          723 :                      dep->entity_kind_name (), TREE_CODE (decl), decl);
   14958              :         }
   14959              :       else
   14960              :         {
   14961              :           /* It's not a mergeable decl, look for it in the original
   14962              :              table.  */
   14963      1796903 :           dep = chain->find_dependency (decl);
   14964      1796903 :           gcc_checking_assert (dep);
   14965              :         }
   14966              :     }
   14967              :   else
   14968              :     {
   14969      7171463 :       dep = make_dependency (decl, ek);
   14970      7171463 :       if (dep->get_entity_kind () != EK_REDIRECT)
   14971      7103887 :         add_dependency (dep);
   14972              :     }
   14973              : 
   14974     10461329 :   return dep;
   14975              : }
   14976              : 
   14977              : void
   14978       789000 : depset::hash::add_namespace_context (depset *dep, tree ns)
   14979              : {
   14980       789000 :   depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
   14981       789000 :   dep->deps.safe_push (ns_dep);
   14982              : 
   14983              :   /* Mark it as special if imported so we don't walk connect when
   14984              :      SCCing.  */
   14985       789000 :   if (!dep->is_binding () && ns_dep->is_import ())
   14986            0 :     dep->set_special ();
   14987       789000 : }
   14988              : 
   14989              : struct add_binding_data
   14990              : {
   14991              :   tree ns;
   14992              :   bitmap partitions;
   14993              :   depset *binding;
   14994              :   depset::hash *hash;
   14995              :   bool met_namespace;
   14996              : };
   14997              : 
   14998              : /* Return true if we are, or contain something that is exported.  */
   14999              : 
   15000              : bool
   15001      6004186 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
   15002              : {
   15003      6004186 :   auto data = static_cast <add_binding_data *> (data_);
   15004      6004186 :   decl = strip_using_decl (decl);
   15005              : 
   15006      6004186 :   if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
   15007              :     {
   15008      5995397 :       tree inner = decl;
   15009              : 
   15010      5995397 :       if (TREE_CODE (inner) == CONST_DECL
   15011        11057 :           && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
   15012              :           /* A using-decl could make a CONST_DECL purview for a non-purview
   15013              :              enumeration.  */
   15014      6006454 :           && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
   15015        11016 :         inner = TYPE_NAME (DECL_CONTEXT (inner));
   15016      5984381 :       else if (TREE_CODE (inner) == TEMPLATE_DECL)
   15017       152530 :         inner = DECL_TEMPLATE_RESULT (inner);
   15018              : 
   15019     11799300 :       if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
   15020     11621054 :           && !((flags & WMB_Using) && (flags & WMB_Purview)))
   15021              :         /* Ignore entities not within the module purview.  We'll need to
   15022              :            create bindings for any non-discarded function calls for ADL,
   15023              :            but it's simpler to handle that at the point of use rather
   15024              :            than trying to clear out bindings after the fact.  */
   15025              :         return false;
   15026              : 
   15027       203500 :       if ((flags & WMB_Hidden)
   15028         5609 :           && DECL_LANG_SPECIFIC (inner)
   15029       209109 :           && DECL_UNIQUE_FRIEND_P (inner))
   15030              :         /* Hidden friends will be found via ADL on the class type,
   15031              :            and so do not need to have bindings.  Anticipated builtin
   15032              :            functions and the hidden decl underlying a DECL_LOCAL_DECL_P
   15033              :            also don't need exporting, but we should create a binding
   15034              :            anyway so that we can have a common decl to match against.  */
   15035              :         return false;
   15036              : 
   15037       197980 :       bool internal_decl = false;
   15038       197980 :       if (!header_module_p () && is_tu_local_entity (decl)
   15039       198244 :           && !((flags & WMB_Using) && (flags & WMB_Export)))
   15040              :         {
   15041              :           /* A TU-local entity.  For ADL we still need to create bindings
   15042              :              for internal-linkage functions attached to a named module.  */
   15043          153 :           if (DECL_DECLARES_FUNCTION_P (inner)
   15044          105 :               && DECL_LANG_SPECIFIC (inner)
   15045          363 :               && DECL_MODULE_ATTACH_P (inner))
   15046              :             {
   15047           93 :               gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
   15048              :               internal_decl = true;
   15049              :             }
   15050              :           else
   15051              :             return false;
   15052              :         }
   15053              : 
   15054       197815 :       if ((TREE_CODE (decl) == VAR_DECL
   15055       197815 :            || TREE_CODE (decl) == TYPE_DECL)
   15056       197815 :           && DECL_TINFO_P (decl))
   15057              :         /* Ignore TINFO things.  */
   15058              :         return false;
   15059              : 
   15060       197815 :       if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
   15061              :         /* Ignore NTTP objects.  */
   15062              :         return false;
   15063              : 
   15064       197815 :       if (deduction_guide_p (decl))
   15065              :         {
   15066              :           /* Ignore deduction guides, bindings for them will be created within
   15067              :              find_dependencies for their class template.  But still build a dep
   15068              :              for them so that we don't discard them.  */
   15069         1616 :           data->hash->make_dependency (decl, EK_FOR_BINDING);
   15070         1616 :           return false;
   15071              :         }
   15072              : 
   15073       196199 :       if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
   15074              :         {
   15075              :           /* An unscoped enum constant implicitly brought into the containing
   15076              :              namespace.  We treat this like a using-decl.  */
   15077         4187 :           gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
   15078              : 
   15079         4187 :           flags = WMB_Flags (flags | WMB_Using);
   15080         4187 :           if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
   15081              :               /* A using-decl can make an enum constant exported for a
   15082              :                  non-exported enumeration.  */
   15083         4187 :               || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
   15084         3956 :             flags = WMB_Flags (flags | WMB_Export);
   15085              :         }
   15086              : 
   15087       196199 :       if (!data->binding)
   15088              :         /* No binding to check.  */;
   15089        43488 :       else if (flags & WMB_Using)
   15090              :         {
   15091              :           /* Look in the binding to see if we already have this
   15092              :              using.  */
   15093       181866 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15094              :             {
   15095       147233 :               depset *d = data->binding->deps[ix];
   15096       294466 :               if (d->get_entity_kind () == EK_USING
   15097       147233 :                   && OVL_FUNCTION (d->get_entity ()) == decl)
   15098              :                 {
   15099            3 :                   if (!(flags & WMB_Hidden))
   15100            3 :                     d->clear_hidden_binding ();
   15101            3 :                   OVL_PURVIEW_P (d->get_entity ()) = true;
   15102            3 :                   if (flags & WMB_Export)
   15103            3 :                     OVL_EXPORT_P (d->get_entity ()) = true;
   15104            3 :                   return bool (flags & WMB_Export);
   15105              :                 }
   15106              :             }
   15107              :         }
   15108        26170 :       else if (flags & WMB_Dups)
   15109              :         {
   15110              :           /* Look in the binding to see if we already have this decl.  */
   15111           78 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15112              :             {
   15113           39 :               depset *d = data->binding->deps[ix];
   15114           39 :               if (d->get_entity () == decl)
   15115              :                 {
   15116           33 :                   if (!(flags & WMB_Hidden))
   15117           33 :                     d->clear_hidden_binding ();
   15118              :                   return false;
   15119              :                 }
   15120              :             }
   15121              :         }
   15122              : 
   15123              :       /* We're adding something.  */
   15124       196163 :       if (!data->binding)
   15125              :         {
   15126       152711 :           data->binding = make_binding (data->ns, DECL_NAME (decl));
   15127       152711 :           data->hash->add_namespace_context (data->binding, data->ns);
   15128              : 
   15129       152711 :           depset **slot = data->hash->binding_slot (data->ns,
   15130       152711 :                                                     DECL_NAME (decl), true);
   15131       152711 :           gcc_checking_assert (!*slot);
   15132       152711 :           *slot = data->binding;
   15133              :         }
   15134              : 
   15135              :       /* Make sure nobody left a tree visited lying about.  */
   15136       196163 :       gcc_checking_assert (!TREE_VISITED (decl));
   15137              : 
   15138       196163 :       if (flags & WMB_Using)
   15139              :         {
   15140        41617 :           decl = ovl_make (decl, NULL_TREE);
   15141        41617 :           OVL_USING_P (decl) = true;
   15142        41617 :           OVL_PURVIEW_P (decl) = true;
   15143        41617 :           if (flags & WMB_Export)
   15144        40417 :             OVL_EXPORT_P (decl) = true;
   15145              :         }
   15146              : 
   15147       196163 :       entity_kind ek = EK_FOR_BINDING;
   15148       196163 :       if (internal_decl)
   15149              :         ek = EK_TU_LOCAL;
   15150       196070 :       else if (flags & WMB_Using)
   15151        41617 :         ek = EK_USING;
   15152              : 
   15153       196163 :       depset *dep = data->hash->make_dependency (decl, ek);
   15154       196163 :       if (flags & WMB_Hidden)
   15155           89 :         dep->set_hidden_binding ();
   15156       196163 :       data->binding->deps.safe_push (dep);
   15157              :       /* Binding and contents are mutually dependent.  */
   15158       196163 :       dep->deps.safe_push (data->binding);
   15159              : 
   15160       196163 :       return (flags & WMB_Using
   15161       196163 :               ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
   15162              :     }
   15163         8789 :   else if (!data->met_namespace)
   15164              :     {
   15165              :       /* Namespace, walk exactly once.  */
   15166         8780 :       data->met_namespace = true;
   15167         8780 :       if (data->hash->add_namespace_entities (decl, data->partitions))
   15168              :         {
   15169              :           /* It contains an exported thing, so it is exported.  */
   15170         1735 :           gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
   15171         1735 :           gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
   15172         1735 :           DECL_MODULE_EXPORT_P (decl) = true;
   15173              :         }
   15174              : 
   15175         8780 :       if (DECL_MODULE_PURVIEW_P (decl))
   15176              :         {
   15177         2104 :           data->hash->make_dependency (decl, depset::EK_NAMESPACE);
   15178              : 
   15179         2104 :           return DECL_MODULE_EXPORT_P (decl);
   15180              :         }
   15181              :     }
   15182              : 
   15183              :   return false;
   15184              : }
   15185              : 
   15186              : /* Recursively find all the namespace bindings of NS.  Add a depset
   15187              :    for every binding that contains an export or module-linkage entity.
   15188              :    Add a defining depset for every such decl that we need to write a
   15189              :    definition.  Such defining depsets depend on the binding depset.
   15190              :    Returns true if we contain something exported.  */
   15191              : 
   15192              : bool
   15193        11624 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   15194              : {
   15195        12838 :   dump () && dump ("Looking for writables in %N", ns);
   15196        11624 :   dump.indent ();
   15197              : 
   15198        11624 :   unsigned count = 0;
   15199        11624 :   add_binding_data data;
   15200        11624 :   data.ns = ns;
   15201        11624 :   data.partitions = partitions;
   15202        11624 :   data.hash = this;
   15203              : 
   15204      7791692 :   for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
   15205              :     {
   15206      7780068 :       data.binding = nullptr;
   15207      7780068 :       data.met_namespace = false;
   15208      7780068 :       if (walk_module_binding (binding, partitions, add_binding_entity, &data))
   15209       144826 :         count++;
   15210              :     }
   15211              : 
   15212              :   /* Seed any using-directives so that we emit the relevant namespaces.  */
   15213        12263 :   for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
   15214          219 :     if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
   15215              :       {
   15216              :         /* Unless it's a (TU-local) anonymous namespace.
   15217              : 
   15218              :            FIXME instead of checking here, they should be
   15219              :            is_tu_local_entity.  */
   15220          182 :         if (!TREE_PUBLIC (USING_DECL_DECLS (udir)))
   15221           73 :           continue;
   15222          109 :         make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
   15223          109 :         if (DECL_MODULE_EXPORT_P (udir))
   15224           80 :           count++;
   15225              :       }
   15226              : 
   15227        11624 :   if (count)
   15228         4094 :     dump () && dump ("Found %u entries", count);
   15229        11624 :   dump.outdent ();
   15230              : 
   15231        11624 :   return count != 0;
   15232              : }
   15233              : 
   15234              : void
   15235          216 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
   15236              : {
   15237        23385 :   for (unsigned ix = 0; ix != partial_classes->length (); ix++)
   15238              :     {
   15239        23169 :       tree inner = (*partial_classes)[ix];
   15240              : 
   15241        23169 :       depset *dep = make_dependency (inner, depset::EK_DECL);
   15242              : 
   15243        23169 :       if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15244              :         {
   15245        23169 :           dep = dep->deps[0];
   15246              :           /* We should have recorded the template as a partial
   15247              :              specialization.  */
   15248        23169 :           gcc_checking_assert (dep->get_entity_kind ()
   15249              :                                == depset::EK_PARTIAL);
   15250              : 
   15251              :           /* Only emit GM entities if reached.  */
   15252        23169 :           if (!DECL_LANG_SPECIFIC (inner)
   15253        36022 :               || !DECL_MODULE_PURVIEW_P (inner))
   15254        11444 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15255              :         }
   15256              :       else
   15257              :         {
   15258              :           /* It was an explicit specialization, not a partial one.
   15259              :              We should have already added this.  */
   15260            0 :           gcc_checking_assert (dep->get_entity_kind ()
   15261              :                                == depset::EK_SPECIALIZATION);
   15262            0 :           gcc_checking_assert (dep->is_special ());
   15263              :         }
   15264              :     }
   15265          216 : }
   15266              : 
   15267              : /* Add the members of imported classes that we defined in this TU.
   15268              :    This will also include lazily created implicit member function
   15269              :    declarations.  (All others will be definitions.)  */
   15270              : 
   15271              : void
   15272           12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
   15273              : {
   15274           24 :   for (unsigned ix = 0; ix != class_members->length (); ix++)
   15275              :     {
   15276           12 :       tree defn = (*class_members)[ix];
   15277           12 :       depset *dep = make_dependency (defn, EK_INNER_DECL);
   15278              : 
   15279           12 :       if (dep->get_entity_kind () == EK_REDIRECT)
   15280            0 :         dep = dep->deps[0];
   15281              : 
   15282              :       /* Only non-instantiations need marking as pendings.  */
   15283           24 :       if (dep->get_entity_kind () == EK_DECL)
   15284           12 :         dep->set_flag_bit <DB_IS_PENDING_BIT> ();
   15285              :     }
   15286           12 : }
   15287              : 
   15288              : /* Add any entities found via dependent ADL.  */
   15289              : 
   15290              : void
   15291      9017129 : depset::hash::add_dependent_adl_entities (tree expr)
   15292              : {
   15293      9017129 :   gcc_checking_assert (!is_key_order ());
   15294              : 
   15295              :   /* This is not needed for header units where everything is
   15296              :      visible to name lookup, nothing is discarded.  */
   15297      9017129 :   if (header_module_p ())
   15298      8969145 :     return;
   15299              : 
   15300      4405933 :   if (TREE_CODE (current->get_entity ()) != TEMPLATE_DECL)
   15301              :     return;
   15302              : 
   15303      2450726 :   dep_adl_info info;
   15304      2450726 :   auto_vec<tree, 3> args;
   15305      2450726 :   switch (TREE_CODE (expr))
   15306              :     {
   15307       259247 :     case CALL_EXPR:
   15308       259247 :       if (!KOENIG_LOOKUP_P (expr)
   15309       259247 :           || !type_dependent_expression_p_push (expr))
   15310              :         return;
   15311         8970 :       info.name = CALL_EXPR_FN (expr);
   15312         8970 :       if (!info.name)
   15313              :         return;
   15314         8970 :       if (TREE_CODE (info.name) == TEMPLATE_ID_EXPR)
   15315         1472 :         info.name = TREE_OPERAND (info.name, 0);
   15316         8970 :       if (TREE_CODE (info.name) == TU_LOCAL_ENTITY)
   15317              :         return;
   15318        15730 :       if (!identifier_p (info.name))
   15319         8796 :         info.name = OVL_NAME (info.name);
   15320        28741 :       for (int ix = 0; ix < call_expr_nargs (expr); ix++)
   15321        19771 :         args.safe_push (CALL_EXPR_ARG (expr, ix));
   15322              :       break;
   15323              : 
   15324        19039 :     case LE_EXPR:
   15325        19039 :     case GE_EXPR:
   15326        19039 :     case LT_EXPR:
   15327        19039 :     case GT_EXPR:
   15328        19039 :       info.rewrite = SPACESHIP_EXPR;
   15329        19039 :       goto overloadable_expr;
   15330              : 
   15331        10330 :     case NE_EXPR:
   15332        10330 :       info.rewrite = EQ_EXPR;
   15333        10330 :       goto overloadable_expr;
   15334              : 
   15335        20997 :     case EQ_EXPR:
   15336              :       /* Not strictly a rewrite candidate, but we need to ensure
   15337              :          that lookup of a matching NE_EXPR can succeed if that
   15338              :          would inhibit a rewrite with reversed parameters.  */
   15339        20997 :       info.rewrite = NE_EXPR;
   15340        20997 :       goto overloadable_expr;
   15341              : 
   15342       154095 :     case COMPOUND_EXPR:
   15343       154095 :     case MEMBER_REF:
   15344       154095 :     case MULT_EXPR:
   15345       154095 :     case TRUNC_DIV_EXPR:
   15346       154095 :     case TRUNC_MOD_EXPR:
   15347       154095 :     case PLUS_EXPR:
   15348       154095 :     case MINUS_EXPR:
   15349       154095 :     case LSHIFT_EXPR:
   15350       154095 :     case RSHIFT_EXPR:
   15351       154095 :     case SPACESHIP_EXPR:
   15352       154095 :     case BIT_AND_EXPR:
   15353       154095 :     case BIT_XOR_EXPR:
   15354       154095 :     case BIT_IOR_EXPR:
   15355       154095 :     case TRUTH_ANDIF_EXPR:
   15356       154095 :     case TRUTH_ORIF_EXPR:
   15357       154095 :     overloadable_expr:
   15358       154095 :       if (!type_dependent_expression_p_push (expr))
   15359              :         return;
   15360       111774 :       info.name = ovl_op_identifier (TREE_CODE (expr));
   15361       111774 :       gcc_checking_assert (tree_operand_length (expr) == 2);
   15362       111774 :       args.safe_push (TREE_OPERAND (expr, 0));
   15363       111774 :       args.safe_push (TREE_OPERAND (expr, 1));
   15364       111774 :       break;
   15365              : 
   15366              :     default:
   15367              :       return;
   15368              :     }
   15369              : 
   15370              :   /* If all arguments are type-dependent we don't need to do
   15371              :      anything further, we won't find new entities.  */
   15372       120744 :   bool all_type_dependent = true;
   15373       538221 :   for (tree arg : args)
   15374       223973 :     if (!type_dependent_expression_p_push (arg))
   15375              :       {
   15376              :         all_type_dependent = false;
   15377              :         break;
   15378              :       }
   15379       120744 :   if (all_type_dependent)
   15380              :     return;
   15381              : 
   15382        47984 :   gcc_checking_assert (!info.args);
   15383        47984 :   info.args = make_tree_vector ();
   15384       243978 :   for (tree arg : args)
   15385       100026 :     vec_safe_push (info.args, arg);
   15386              : 
   15387              :   /* We need to defer name lookup until after walking, otherwise
   15388              :      we get confused by stray TREE_VISITEDs.  */
   15389        47984 :   dep_adl_entity_list.safe_push (info);
   15390      2450726 : }
   15391              : 
   15392              : /* We add the partial & explicit specializations, and the explicit
   15393              :    instantiations.  */
   15394              : 
   15395              : static void
   15396      1131637 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
   15397              : {
   15398      1131637 :   vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
   15399              : 
   15400      1131637 :   if (!decl_p)
   15401              :     {
   15402              :       /* We exclusively use decls to locate things.  Make sure there's
   15403              :          no mismatch between the two specialization tables we keep.
   15404              :          pt.cc optimizes instantiation lookup using a complicated
   15405              :          heuristic.  We don't attempt to replicate that algorithm, but
   15406              :          observe its behaviour and reproduce it upon read back.  */
   15407              : 
   15408       334448 :        gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
   15409              :                            || DECL_CLASS_TEMPLATE_P (entry->tmpl));
   15410              : 
   15411       334448 :        gcc_checking_assert (!match_mergeable_specialization (true, entry));
   15412              :     }
   15413       797189 :   else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
   15414       390701 :     gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
   15415              : 
   15416      1131637 :   data->safe_push (entry);
   15417      1131637 : }
   15418              : 
   15419              : /* Arbitrary stable comparison.  */
   15420              : 
   15421              : static int
   15422     68962422 : specialization_cmp (const void *a_, const void *b_)
   15423              : {
   15424     68962422 :   const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
   15425     68962422 :   const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
   15426              : 
   15427     68962422 :   if (ea == eb)
   15428              :     return 0;
   15429              : 
   15430     68962422 :   tree a = ea->spec;
   15431     68962422 :   tree b = eb->spec;
   15432     68962422 :   if (TYPE_P (a))
   15433              :     {
   15434     19343878 :       a = TYPE_NAME (a);
   15435     19343878 :       b = TYPE_NAME (b);
   15436              :     }
   15437              : 
   15438     68962422 :   if (a == b)
   15439              :     /* This can happen with friend specializations.  Just order by
   15440              :        entry address.  See note in depset_cmp.  */
   15441            0 :     return ea < eb ? -1 : +1;
   15442              : 
   15443     68962422 :   return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
   15444              : }
   15445              : 
   15446              : /* We add all kinds of specialializations.  Implicit specializations
   15447              :    should only streamed and walked if they are reachable from
   15448              :    elsewhere.  Hence the UNREACHED flag.  This is making the
   15449              :    assumption that it is cheaper to reinstantiate them on demand
   15450              :    elsewhere, rather than stream them in when we instantiate their
   15451              :    general template.  Also, if we do stream them, we can only do that
   15452              :    if they are not internal (which they can become if they themselves
   15453              :    touch an internal entity?).  */
   15454              : 
   15455              : void
   15456         5688 : depset::hash::add_specializations (bool decl_p)
   15457              : {
   15458         5688 :   vec<spec_entry *> data;
   15459         5688 :   data.create (100);
   15460         5688 :   walk_specializations (decl_p, specialization_add, &data);
   15461         5688 :   data.qsort (specialization_cmp);
   15462      1137325 :   while (data.length ())
   15463              :     {
   15464      1131637 :       spec_entry *entry = data.pop ();
   15465      1131637 :       tree spec = entry->spec;
   15466      1131637 :       int use_tpl = 0;
   15467      1131637 :       bool is_friend = false;
   15468              : 
   15469      1131637 :       if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
   15470              :         /* A friend of a template.  This is keyed to the
   15471              :            instantiation.  */
   15472              :         is_friend = true;
   15473              : 
   15474      1131637 :       if (decl_p)
   15475              :         {
   15476       797189 :           if (tree ti = DECL_TEMPLATE_INFO (spec))
   15477              :             {
   15478       797189 :               tree tmpl = TI_TEMPLATE (ti);
   15479              : 
   15480       797189 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15481       797189 :               if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15482              :                 {
   15483         5981 :                   spec = tmpl;
   15484         5981 :                   gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
   15485              :                 }
   15486       791208 :               else if (is_friend)
   15487              :                 {
   15488         5759 :                   if (TI_TEMPLATE (ti) != entry->tmpl
   15489         5759 :                       || !template_args_equal (TI_ARGS (ti), entry->tmpl))
   15490         5759 :                     goto template_friend;
   15491              :                 }
   15492              :             }
   15493              :           else
   15494              :             {
   15495            0 :             template_friend:;
   15496         5759 :               gcc_checking_assert (is_friend);
   15497              :               /* This is a friend of a template class, but not the one
   15498              :                  that generated entry->spec itself (i.e. it's an
   15499              :                  equivalent clone).  We do not need to record
   15500              :                  this.  */
   15501         5759 :               continue;
   15502              :             }
   15503              :         }
   15504              :       else
   15505              :         {
   15506       334448 :           if (TREE_CODE (spec) == ENUMERAL_TYPE)
   15507              :             {
   15508         1539 :               tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
   15509              : 
   15510         1539 :               if (TYPE_P (ctx))
   15511         1533 :                 use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
   15512              :               else
   15513            6 :                 use_tpl = DECL_USE_TEMPLATE (ctx);
   15514              :             }
   15515              :           else
   15516       332909 :             use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
   15517              : 
   15518       334448 :           tree ti = TYPE_TEMPLATE_INFO (spec);
   15519       334448 :           tree tmpl = TI_TEMPLATE (ti);
   15520              : 
   15521       334448 :           spec = TYPE_NAME (spec);
   15522       334448 :           if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15523              :             {
   15524         1714 :               spec = tmpl;
   15525         1714 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15526              :             }
   15527              :         }
   15528              : 
   15529      1125878 :       bool needs_reaching = false;
   15530      1125878 :       if (use_tpl == 1)
   15531              :         /* Implicit instantiations only walked if we reach them.  */
   15532              :         needs_reaching = true;
   15533        89542 :       else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
   15534       159864 :                || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
   15535              :         /* Likewise, GMF explicit or partial specializations.  */
   15536              :         needs_reaching = true;
   15537              : 
   15538              : #if false && CHECKING_P
   15539              :       /* The instantiation isn't always on
   15540              :          DECL_TEMPLATE_INSTANTIATIONS, */
   15541              :       // FIXME: we probably need to remember this information?
   15542              :       /* Verify the specialization is on the
   15543              :          DECL_TEMPLATE_INSTANTIATIONS of the template.  */
   15544              :       for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
   15545              :            cons; cons = TREE_CHAIN (cons))
   15546              :         if (TREE_VALUE (cons) == entry->spec)
   15547              :           {
   15548              :             gcc_assert (entry->args == TREE_PURPOSE (cons));
   15549              :             goto have_spec;
   15550              :           }
   15551              :       gcc_unreachable ();
   15552              :     have_spec:;
   15553              : #endif
   15554              : 
   15555              :       /* Make sure nobody left a tree visited lying about.  */
   15556      1125878 :       gcc_checking_assert (!TREE_VISITED (spec));
   15557      1125878 :       depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
   15558      1125878 :       if (dep->is_special ())
   15559            0 :         gcc_unreachable ();
   15560              :       else
   15561              :         {
   15562      1125878 :           if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15563        26603 :             dep = dep->deps[0];
   15564      1099275 :           else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
   15565              :             {
   15566      1099275 :               dep->set_special ();
   15567      1099275 :               dep->deps.safe_push (reinterpret_cast<depset *> (entry));
   15568      1099275 :               if (!decl_p)
   15569       312228 :                 dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
   15570              :             }
   15571              : 
   15572      1125878 :           if (needs_reaching)
   15573      1080401 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15574      1125878 :           if (is_friend)
   15575            0 :             dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
   15576              :         }
   15577              :     }
   15578         5688 :   data.release ();
   15579         5688 : }
   15580              : 
   15581              : /* Add a depset into the mergeable hash.  */
   15582              : 
   15583              : void
   15584      1331526 : depset::hash::add_mergeable (depset *mergeable)
   15585              : {
   15586      1331526 :   gcc_checking_assert (is_key_order ());
   15587      1331526 :   entity_kind ek = mergeable->get_entity_kind ();
   15588      1331526 :   tree decl = mergeable->get_entity ();
   15589      1331526 :   gcc_checking_assert (ek < EK_DIRECT_HWM);
   15590              : 
   15591      1331526 :   depset **slot = entity_slot (decl, true);
   15592      1331526 :   gcc_checking_assert (!*slot);
   15593      1331526 :   depset *dep = make_entity (decl, ek);
   15594      1331526 :   *slot = dep;
   15595              : 
   15596      1331526 :   worklist.safe_push (dep);
   15597              : 
   15598              :   /* So we can locate the mergeable depset this depset refers to,
   15599              :      mark the first dep.  */
   15600      1331526 :   dep->set_special ();
   15601      1331526 :   dep->deps.safe_push (mergeable);
   15602      1331526 : }
   15603              : 
   15604              : /* Find the innermost-namespace scope of DECL, and that
   15605              :    namespace-scope decl.  */
   15606              : 
   15607              : tree
   15608     41688887 : find_pending_key (tree decl, tree *decl_p = nullptr)
   15609              : {
   15610     41688887 :   tree ns = decl;
   15611     50647469 :   do
   15612              :     {
   15613     50647469 :       decl = ns;
   15614     50647469 :       ns = CP_DECL_CONTEXT (ns);
   15615     50647469 :       if (TYPE_P (ns))
   15616      5565112 :         ns = TYPE_NAME (ns);
   15617              :     }
   15618     50647469 :   while (TREE_CODE (ns) != NAMESPACE_DECL);
   15619              : 
   15620     41688887 :   if (decl_p)
   15621     41066656 :     *decl_p = decl;
   15622              : 
   15623     41688887 :   return ns;
   15624              : }
   15625              : 
   15626              : /* Creates bindings and dependencies for all deduction guides of
   15627              :    the given class template DECL as needed.  */
   15628              : 
   15629              : void
   15630        61701 : depset::hash::add_deduction_guides (tree decl)
   15631              : {
   15632              :   /* Alias templates never have deduction guides.  */
   15633        61701 :   if (DECL_ALIAS_TEMPLATE_P (decl))
   15634        60570 :     return;
   15635              : 
   15636              :   /* We don't need to do anything for class-scope deduction guides,
   15637              :      as they will be added as members anyway.  */
   15638        61701 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   15639              :     return;
   15640              : 
   15641        47373 :   tree ns = CP_DECL_CONTEXT (decl);
   15642        47373 :   tree name = dguide_name (decl);
   15643              : 
   15644              :   /* We always add all deduction guides with a given name at once,
   15645              :      so if there's already a binding there's nothing to do.  */
   15646        47373 :   if (find_binding (ns, name))
   15647              :     return;
   15648              : 
   15649        43858 :   tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
   15650              :                                        /*complain=*/false);
   15651        43858 :   if (guides == error_mark_node)
   15652              :     return;
   15653              : 
   15654         1131 :   depset *binding = nullptr;
   15655         5505 :   for (tree t : lkp_range (guides))
   15656              :     {
   15657         3243 :       gcc_checking_assert (!TREE_VISITED (t));
   15658         3243 :       depset *dep = make_dependency (t, EK_FOR_BINDING);
   15659              : 
   15660              :       /* We don't want to create bindings for imported deduction guides, as
   15661              :          this would potentially cause name lookup to return duplicates.  */
   15662         3243 :       if (dep->is_import ())
   15663            6 :         continue;
   15664              : 
   15665         3237 :       if (!binding)
   15666              :         {
   15667              :           /* We have bindings to add.  */
   15668         1125 :           binding = make_binding (ns, name);
   15669         1125 :           add_namespace_context (binding, ns);
   15670              : 
   15671         1125 :           depset **slot = binding_slot (ns, name, /*insert=*/true);
   15672         1125 :           *slot = binding;
   15673              :         }
   15674              : 
   15675         3237 :       binding->deps.safe_push (dep);
   15676         3237 :       dep->deps.safe_push (binding);
   15677         3237 :       dump (dumper::DEPEND)
   15678            0 :         && dump ("Built binding for deduction guide %C:%N",
   15679            0 :                  TREE_CODE (decl), decl);
   15680              :     }
   15681              : }
   15682              : 
   15683              : /* Iteratively find dependencies.  During the walk we may find more
   15684              :    entries on the same binding that need walking.  */
   15685              : 
   15686              : void
   15687       340752 : depset::hash::find_dependencies (module_state *module)
   15688              : {
   15689       340752 :   trees_out walker (NULL, module, *this);
   15690       340752 :   vec<depset *> unreached;
   15691       681504 :   unreached.create (worklist.length ());
   15692              : 
   15693       343090 :   for (;;)
   15694              :     {
   15695       341921 :       reached_unreached = false;
   15696      6123454 :       while (worklist.length ())
   15697              :         {
   15698      5781533 :           depset *item = worklist.pop ();
   15699              : 
   15700      5781533 :           gcc_checking_assert (!item->is_binding ());
   15701      5781533 :           if (item->is_unreached ())
   15702      3070806 :             unreached.quick_push (item);
   15703              :           else
   15704              :             {
   15705      2710727 :               current = item;
   15706      2710727 :               tree decl = current->get_entity ();
   15707      2710727 :               dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
   15708      2712050 :                 && dump ("Dependencies of %s %C:%N",
   15709         1323 :                          is_key_order () ? "key-order"
   15710         1323 :                          : current->entity_kind_name (), TREE_CODE (decl), decl);
   15711      2710727 :               dump.indent ();
   15712      2710727 :               walker.begin ();
   15713      2710727 :               if (current->get_entity_kind () == EK_USING)
   15714        41617 :                 walker.tree_node (OVL_FUNCTION (decl));
   15715      2669110 :               else if (current->get_entity_kind () == EK_TU_LOCAL)
   15716              :                 /* We only stream its name and location.  */
   15717           93 :                 module->note_location (DECL_SOURCE_LOCATION (decl));
   15718      2669017 :               else if (TREE_VISITED (decl))
   15719              :                 /* A global tree.  */;
   15720      2666433 :               else if (current->get_entity_kind () == EK_NAMESPACE)
   15721              :                 {
   15722         2764 :                   module->note_location (DECL_SOURCE_LOCATION (decl));
   15723         2764 :                   add_namespace_context (current, CP_DECL_CONTEXT (decl));
   15724              :                 }
   15725              :               else
   15726              :                 {
   15727      2663669 :                   walker.mark_declaration (decl, current->has_defn ());
   15728              : 
   15729      2663669 :                   if (!is_key_order ()
   15730      2663669 :                       && item->is_pending_entity ())
   15731              :                     {
   15732       622231 :                       tree ns = find_pending_key (decl, nullptr);
   15733       622231 :                       add_namespace_context (item, ns);
   15734              :                     }
   15735              : 
   15736      2663669 :                   auto ovr = make_temp_override
   15737      2663669 :                     (ignore_exposure, item->is_ignored_exposure_context ());
   15738      2663669 :                   walker.decl_value (decl, current);
   15739      2663669 :                   if (current->has_defn ())
   15740       513692 :                     walker.write_definition (decl, current->refs_tu_local ());
   15741      2663669 :                 }
   15742      2710727 :               walker.end ();
   15743              : 
   15744              :               /* If we see either a class template or a deduction guide, make
   15745              :                  sure to add all visible deduction guides.  We need to check
   15746              :                  both in case they have been added in separate modules, or
   15747              :                  one is in the GMF and would have otherwise been discarded.  */
   15748      2710727 :               if (!is_key_order ()
   15749      2710727 :                   && DECL_CLASS_TEMPLATE_P (decl))
   15750        58458 :                 add_deduction_guides (decl);
   15751      2710727 :               if (!is_key_order ()
   15752      2710727 :                   && deduction_guide_p (decl))
   15753         3243 :                 add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
   15754              : 
   15755              :               /* Handle dependent ADL for [module.global.frag] p3.3.  */
   15756      2710727 :               if (!is_key_order () && !dep_adl_entity_list.is_empty ())
   15757              :                 {
   15758        25673 :                   processing_template_decl_sentinel ptds;
   15759        25673 :                   ++processing_template_decl;
   15760        73657 :                   for (auto &info : dep_adl_entity_list)
   15761              :                     {
   15762        47984 :                       tree lookup = lookup_arg_dependent (info.name, NULL_TREE,
   15763              :                                                           info.args, true);
   15764       146428 :                       for (tree fn : lkp_range (lookup))
   15765              :                         /* We don't need to add_dependency, just have
   15766              :                            make_dependency build an ADL binding.  */
   15767        50460 :                         make_dependency (fn, EK_DECL);
   15768              : 
   15769        47984 :                       if (info.rewrite)
   15770              :                         {
   15771        11482 :                           tree rewrite_name = ovl_op_identifier (info.rewrite);
   15772        11482 :                           lookup = lookup_arg_dependent (rewrite_name, NULL_TREE,
   15773              :                                                          info.args, true);
   15774        37798 :                           for (tree fn : lkp_range (lookup))
   15775        14834 :                             make_dependency (fn, EK_DECL);
   15776              :                         }
   15777        47984 :                       release_tree_vector (info.args);
   15778              :                     }
   15779        25673 :                   dep_adl_entity_list.truncate (0);
   15780        25673 :                 }
   15781              : 
   15782      2710727 :               if (!is_key_order ()
   15783      1379201 :                   && TREE_CODE (decl) == TEMPLATE_DECL
   15784      3193945 :                   && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   15785              :                 {
   15786              :                   /* Mark all the explicit & partial specializations as
   15787              :                      reachable.  We search both specialization lists as some
   15788              :                      constrained partial specializations for class types are
   15789              :                      only found in DECL_TEMPLATE_SPECIALIZATIONS.  */
   15790      1389971 :                   auto mark_reached = [this](tree spec)
   15791              :                     {
   15792       916474 :                       if (TYPE_P (spec))
   15793       262853 :                         spec = TYPE_NAME (spec);
   15794       916474 :                       int use_tpl;
   15795       916474 :                       node_template_info (spec, use_tpl);
   15796       916474 :                       if (use_tpl & 2)
   15797              :                         {
   15798        90773 :                           depset *spec_dep = find_dependency (spec);
   15799        90773 :                           if (spec_dep->get_entity_kind () == EK_REDIRECT)
   15800        20479 :                             spec_dep = spec_dep->deps[0];
   15801        90773 :                           if (spec_dep->is_unreached ())
   15802              :                             {
   15803        22415 :                               reached_unreached = true;
   15804        22415 :                               spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   15805        22415 :                               dump (dumper::DEPEND)
   15806            0 :                                 && dump ("Reaching unreached specialization"
   15807            0 :                                          " %C:%N", TREE_CODE (spec), spec);
   15808              :                             }
   15809              :                         }
   15810      1389971 :                     };
   15811              : 
   15812       473497 :                   for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
   15813      1368727 :                        cons; cons = TREE_CHAIN (cons))
   15814       895230 :                     mark_reached (TREE_VALUE (cons));
   15815       473497 :                   for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
   15816       494741 :                        cons; cons = TREE_CHAIN (cons))
   15817        21244 :                     mark_reached (TREE_VALUE (cons));
   15818              :                 }
   15819              : 
   15820      2710727 :               dump.outdent ();
   15821      2710727 :               current = NULL;
   15822              :             }
   15823              :         }
   15824              : 
   15825       341921 :       if (!reached_unreached)
   15826              :         break;
   15827              : 
   15828              :       /* It's possible the we reached the unreached before we
   15829              :          processed it in the above loop, so we'll be doing this an
   15830              :          extra time.  However, to avoid that we have to do some
   15831              :          bit shuffling that also involves a scan of the list.
   15832              :          Swings & roundabouts I guess.  */
   15833         1169 :       std::swap (worklist, unreached);
   15834         1169 :     }
   15835              : 
   15836       340752 :   unreached.release ();
   15837       340752 : }
   15838              : 
   15839              : /* Compare two entries of a single binding.  TYPE_DECL before
   15840              :    non-exported before exported.  */
   15841              : 
   15842              : static int
   15843      1086482 : binding_cmp (const void *a_, const void *b_)
   15844              : {
   15845      1086482 :   depset *a = *(depset *const *)a_;
   15846      1086482 :   depset *b = *(depset *const *)b_;
   15847              : 
   15848      1086482 :   tree a_ent = a->get_entity ();
   15849      1086482 :   tree b_ent = b->get_entity ();
   15850      1086482 :   gcc_checking_assert (a_ent != b_ent
   15851              :                        && !a->is_binding ()
   15852              :                        && !b->is_binding ());
   15853              : 
   15854              :   /* Implicit typedefs come first.  */
   15855      1086482 :   bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
   15856      1086482 :   bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
   15857      1086348 :   if (a_implicit || b_implicit)
   15858              :     {
   15859              :       /* A binding with two implicit type decls?  That's unpossible!  */
   15860          268 :       gcc_checking_assert (!(a_implicit && b_implicit));
   15861          268 :       return a_implicit ? -1 : +1;  /* Implicit first.  */
   15862              :     }
   15863              : 
   15864              :   /* TU-local before non-TU-local.  */
   15865      1086214 :   bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
   15866      1086214 :   bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
   15867      1086214 :   if (a_internal != b_internal)
   15868            0 :     return a_internal ? -1 : +1;  /* Internal first.  */
   15869              : 
   15870              :   /* Hidden before non-hidden.  */
   15871      1086214 :   bool a_hidden = a->is_hidden ();
   15872      1086214 :   bool b_hidden = b->is_hidden ();
   15873      1086214 :   if (a_hidden != b_hidden)
   15874            0 :     return a_hidden ? -1 : +1;
   15875              : 
   15876      1086214 :   bool a_using = a->get_entity_kind () == depset::EK_USING;
   15877      1086214 :   bool a_export;
   15878      1086214 :   if (a_using)
   15879              :     {
   15880       347159 :       a_export = OVL_EXPORT_P (a_ent);
   15881       347159 :       a_ent = OVL_FUNCTION (a_ent);
   15882              :     }
   15883       739055 :   else if (TREE_CODE (a_ent) == CONST_DECL
   15884            0 :            && DECL_LANG_SPECIFIC (a_ent)
   15885       739055 :            && DECL_MODULE_EXPORT_P (a_ent))
   15886              :     a_export = true;
   15887              :   else
   15888       739055 :     a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
   15889              :                                      ? TYPE_NAME (TREE_TYPE (a_ent))
   15890              :                                      : STRIP_TEMPLATE (a_ent));
   15891              : 
   15892      1086214 :   bool b_using = b->get_entity_kind () == depset::EK_USING;
   15893      1086214 :   bool b_export;
   15894      1086214 :   if (b_using)
   15895              :     {
   15896       368376 :       b_export = OVL_EXPORT_P (b_ent);
   15897       368376 :       b_ent = OVL_FUNCTION (b_ent);
   15898              :     }
   15899       717838 :   else if (TREE_CODE (b_ent) == CONST_DECL
   15900            0 :            && DECL_LANG_SPECIFIC (b_ent)
   15901       717838 :            && DECL_MODULE_EXPORT_P (b_ent))
   15902              :     b_export = true;
   15903              :   else
   15904       717838 :     b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
   15905              :                                      ? TYPE_NAME (TREE_TYPE (b_ent))
   15906              :                                      : STRIP_TEMPLATE (b_ent));
   15907              : 
   15908              :   /* Non-exports before exports.  */
   15909      1086214 :   if (a_export != b_export)
   15910       199511 :     return a_export ? +1 : -1;
   15911              : 
   15912              :   /* At this point we don't care, but want a stable sort.  */
   15913              : 
   15914       886703 :   if (a_using != b_using)
   15915              :     /* using first.  */
   15916        16727 :     return a_using? -1 : +1;
   15917              : 
   15918       869976 :   return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
   15919              : }
   15920              : 
   15921              : /* True iff TMPL has an explicit instantiation definition.
   15922              : 
   15923              :    This is local to module.cc because register_specialization skips adding most
   15924              :    instantiations unless module_maybe_has_cmi_p.  */
   15925              : 
   15926              : static bool
   15927           76 : template_has_explicit_inst (tree tmpl)
   15928              : {
   15929           88 :   for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
   15930              :     {
   15931           24 :       tree spec = TREE_VALUE (t);
   15932           24 :       if (DECL_EXPLICIT_INSTANTIATION (spec)
   15933           24 :           && !DECL_REALLY_EXTERN (spec))
   15934              :         return true;
   15935              :     }
   15936              :   return false;
   15937              : }
   15938              : 
   15939              : /* Complain about DEP that exposes a TU-local entity.
   15940              : 
   15941              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15942              :    if we explained anything.  */
   15943              : 
   15944              : bool
   15945          127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
   15946              : {
   15947          127 :   tree decl = dep->get_entity ();
   15948              : 
   15949              :   /* Don't need to walk if we're not going to be emitting
   15950              :      any diagnostics anyway.  */
   15951          148 :   if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15952           21 :                                      OPT_Wexpose_global_module_tu_local))
   15953            0 :     return false;
   15954              : 
   15955          523 :   for (depset *rdep : dep->deps)
   15956          135 :     if (!rdep->is_binding () && rdep->is_tu_local (strict)
   15957          369 :         && !is_exposure_of_member_type (dep, rdep))
   15958              :       {
   15959              :         // FIXME:QOI Better location information?  We're
   15960              :         // losing, so it doesn't matter about efficiency.
   15961          118 :         tree exposed = rdep->get_entity ();
   15962          118 :         auto_diagnostic_group d;
   15963          118 :         if (strict)
   15964              :           {
   15965              :             /* Allow suppressing the warning from the point of declaration
   15966              :                of the otherwise-exposed decl, for cases we know that
   15967              :                exposures will never be 'bad'.  */
   15968           27 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
   15969           27 :                                     OPT_Wexpose_global_module_tu_local)
   15970           45 :                 && pedwarn (DECL_SOURCE_LOCATION (decl),
   15971           18 :                             OPT_Wexpose_global_module_tu_local,
   15972              :                             "%qD exposes TU-local entity %qD", decl, exposed))
   15973              :               {
   15974           18 :                 bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15975           18 :                 gcc_checking_assert (informed);
   15976              :                 return true;
   15977              :               }
   15978              :           }
   15979              :         else
   15980              :           {
   15981           91 :             error_at (DECL_SOURCE_LOCATION (decl),
   15982              :                       "%qD exposes TU-local entity %qD", decl, exposed);
   15983           91 :             bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15984           91 :             gcc_checking_assert (informed);
   15985           91 :             if (dep->is_tu_local (/*strict=*/true))
   15986            3 :               inform (DECL_SOURCE_LOCATION (decl),
   15987              :                       "%qD is also TU-local but has been exposed elsewhere",
   15988              :                       decl);
   15989              :             return true;
   15990              :           }
   15991          118 :       }
   15992              : 
   15993              :   return false;
   15994              : }
   15995              : 
   15996              : /* Warn about a template DEP that references a TU-local entity.
   15997              : 
   15998              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15999              :    if we explained anything.  */
   16000              : 
   16001              : bool
   16002           94 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
   16003              : {
   16004           94 :   tree decl = dep->get_entity ();
   16005              : 
   16006              :   /* Don't bother walking if we know we won't be emitting anything.  */
   16007           94 :   if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   16008           94 :                            OPT_Wtemplate_names_tu_local)
   16009              :       /* Only warn strictly if users haven't silenced this warning here.  */
   16010          121 :       || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   16011           27 :                                          OPT_Wexpose_global_module_tu_local)))
   16012              :     return false;
   16013              : 
   16014              :   /* Friend decls in a class body are ignored, but this is harmless:
   16015              :      it should not impact any consumers.  */
   16016           94 :   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
   16017              :     return false;
   16018              : 
   16019              :   /* We should now only be warning about templates.  */
   16020           76 :   gcc_checking_assert
   16021              :     (TREE_CODE (decl) == TEMPLATE_DECL
   16022              :      && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
   16023              : 
   16024              :   /* Don't warn if we've seen any explicit instantiation definitions,
   16025              :      the intent might be for importers to only use those.  */
   16026           76 :   if (template_has_explicit_inst (decl))
   16027              :     return false;
   16028              : 
   16029          268 :   for (depset *rdep : dep->deps)
   16030          134 :     if (!rdep->is_binding () && rdep->is_tu_local (strict))
   16031              :       {
   16032           67 :         tree ref = rdep->get_entity ();
   16033           67 :         auto_diagnostic_group d;
   16034           67 :         if (strict)
   16035              :           {
   16036           15 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
   16037           15 :                                     OPT_Wexpose_global_module_tu_local)
   16038           21 :                 && warning_at (DECL_SOURCE_LOCATION (decl),
   16039            6 :                                OPT_Wtemplate_names_tu_local,
   16040              :                                "%qD refers to TU-local entity %qD, which may "
   16041              :                                "cause issues when instantiating in other TUs",
   16042              :                                decl, ref))
   16043              :               {
   16044            6 :                 is_tu_local_entity (ref, /*explain=*/true);
   16045            6 :                 return true;
   16046              :               }
   16047              :           }
   16048           52 :         else if (warning_at (DECL_SOURCE_LOCATION (decl),
   16049           52 :                              OPT_Wtemplate_names_tu_local,
   16050              :                              "%qD refers to TU-local entity %qD and cannot "
   16051              :                              "be instantiated in other TUs", decl, ref))
   16052              :           {
   16053           52 :             is_tu_local_entity (ref, /*explain=*/true);
   16054           52 :             return true;
   16055              :           }
   16056           67 :       }
   16057              : 
   16058              :   return false;
   16059              : }
   16060              : 
   16061              : /* Sort the bindings, issue errors about bad internal refs.  */
   16062              : 
   16063              : bool
   16064         2844 : depset::hash::finalize_dependencies ()
   16065              : {
   16066         2844 :   bool ok = true;
   16067      2119259 :   for (depset *dep : *this)
   16068              :     {
   16069      2116415 :       if (dep->is_binding ())
   16070              :         {
   16071              :           /* Keep the containing namespace dep first.  */
   16072       164005 :           gcc_checking_assert (dep->deps.length () > 1
   16073              :                                && (dep->deps[0]->get_entity_kind ()
   16074              :                                    == EK_NAMESPACE)
   16075              :                                && (dep->deps[0]->get_entity ()
   16076              :                                    == dep->get_entity ()));
   16077       164005 :           if (dep->deps.length () > 2)
   16078        16683 :             gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
   16079              :                        sizeof (dep->deps[1]), binding_cmp);
   16080              : 
   16081              :           /* Bindings shouldn't refer to imported entities.  */
   16082       164005 :           if (CHECKING_P)
   16083       888198 :             for (depset *entity : dep->deps)
   16084       396183 :               gcc_checking_assert (!entity->is_import ());
   16085       164005 :           continue;
   16086       164005 :         }
   16087              : 
   16088              :       /* Otherwise, we'll check for bad internal refs.
   16089              :          Don't complain about any references from TU-local entities.  */
   16090      1952410 :       if (dep->is_tu_local ())
   16091          264 :         continue;
   16092              : 
   16093              :       /* We already complained about usings of non-external entities in
   16094              :          check_can_export_using_decl, don't do it again here.  */
   16095      1952146 :       if (dep->get_entity_kind () == EK_USING)
   16096        41617 :         continue;
   16097              : 
   16098      1910529 :       if (dep->is_exposure ())
   16099              :         {
   16100          106 :           bool explained = diagnose_bad_internal_ref (dep);
   16101              : 
   16102              :           /* A TU-local variable will always be considered an exposure,
   16103              :              so we don't have to worry about strict-only handling.  */
   16104          106 :           tree decl = dep->get_entity ();
   16105          106 :           if (!explained
   16106           15 :               && VAR_P (decl)
   16107          121 :               && (DECL_DECLARED_CONSTEXPR_P (decl)
   16108            6 :                   || DECL_INLINE_VAR_P (decl)))
   16109              :             {
   16110           15 :               auto_diagnostic_group d;
   16111           15 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   16112            9 :                 error_at (DECL_SOURCE_LOCATION (decl),
   16113              :                           "%qD is declared %<constexpr%> and is initialized to "
   16114              :                           "a TU-local value", decl);
   16115              :               else
   16116              :                 {
   16117              :                   /* This can only occur with references.  */
   16118            6 :                   gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
   16119            6 :                   error_at (DECL_SOURCE_LOCATION (decl),
   16120              :                             "%qD is a reference declared %<inline%> and is "
   16121              :                             "constant-initialized to a TU-local value", decl);
   16122              :                 }
   16123           15 :               bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
   16124              :                                                  /*explain=*/true);
   16125           15 :               gcc_checking_assert (informed);
   16126           15 :               explained = true;
   16127           15 :             }
   16128              : 
   16129              :           /* We should have emitted an error above, unless the warning was
   16130              :              silenced.  */
   16131          106 :           gcc_checking_assert (explained);
   16132          106 :           ok = false;
   16133          106 :           continue;
   16134          106 :         }
   16135              : 
   16136              :       /* In all other cases, we're just warning (rather than erroring).
   16137              :          We don't want to do too much warning, so let's just bail after
   16138              :          the first warning we successfully emit.  */
   16139      1910441 :       if (warn_expose_global_module_tu_local
   16140      1910423 :           && !dep->is_tu_local (/*strict=*/true)
   16141      1910381 :           && dep->is_exposure (/*strict=*/true)
   16142      1910444 :           && diagnose_bad_internal_ref (dep, /*strict=*/true))
   16143           18 :         continue;
   16144              : 
   16145      1910457 :       if (warn_template_names_tu_local
   16146      1910420 :           && dep->refs_tu_local ()
   16147      1910472 :           && diagnose_template_names_tu_local (dep))
   16148           52 :         continue;
   16149              : 
   16150      1910353 :       if (warn_template_names_tu_local
   16151       272951 :           && warn_expose_global_module_tu_local
   16152       272951 :           && !dep->is_tu_local (/*strict=*/true)
   16153       272927 :           && dep->refs_tu_local (/*strict=*/true)
   16154           30 :           && !dep->is_exposure (/*strict=*/true)
   16155      1910380 :           && diagnose_template_names_tu_local (dep, /*strict=*/true))
   16156              :         continue;
   16157              :     }
   16158              : 
   16159         2844 :   return ok;
   16160              : }
   16161              : 
   16162              : /* Core of TARJAN's algorithm to find Strongly Connected Components
   16163              :    within a graph.  See https://en.wikipedia.org/wiki/
   16164              :    Tarjan%27s_strongly_connected_components_algorithm for details.
   16165              : 
   16166              :    We use depset::section as lowlink.  Completed nodes have
   16167              :    depset::cluster containing the cluster number, with the top
   16168              :    bit set.
   16169              : 
   16170              :    A useful property is that the output vector is a reverse
   16171              :    topological sort of the resulting DAG.  In our case that means
   16172              :    dependent SCCs are found before their dependers.  We make use of
   16173              :    that property.  */
   16174              : 
   16175              : void
   16176      2873715 : depset::tarjan::connect (depset *v)
   16177              : {
   16178      2873715 :   gcc_checking_assert (v->is_binding ()
   16179              :                        || !(v->is_tu_local ()
   16180              :                             || v->is_unreached ()
   16181              :                             || v->is_import ()));
   16182              : 
   16183      2873715 :   v->cluster = v->section = ++index;
   16184      2873715 :   stack.safe_push (v);
   16185              : 
   16186              :   /* Walk all our dependencies, ignore a first marked slot  */
   16187     25443249 :   for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
   16188              :     {
   16189      9853627 :       depset *dep = v->deps[ix];
   16190              : 
   16191      9853627 :       if (dep->is_binding ()
   16192     19475381 :           || !(dep->is_import () || dep->is_tu_local ()))
   16193              :         {
   16194      9848883 :           unsigned lwm = dep->cluster;
   16195              : 
   16196      9848883 :           if (!dep->cluster)
   16197              :             {
   16198              :               /* A new node.  Connect it.  */
   16199      1610690 :               connect (dep);
   16200      1610690 :               lwm = dep->section;
   16201              :             }
   16202              : 
   16203      9848883 :           if (dep->section && v->section > lwm)
   16204      1532757 :             v->section = lwm;
   16205              :         }
   16206              :     }
   16207              : 
   16208      2873715 :   if (v->section == v->cluster)
   16209              :     {
   16210              :       /* Root of a new SCC.  Push all the members onto the result list. */
   16211              :       unsigned num = v->cluster;
   16212      2873715 :       depset *p;
   16213      2873715 :       do
   16214              :         {
   16215      2873715 :           p = stack.pop ();
   16216      2873715 :           p->cluster = num;
   16217      2873715 :           p->section = 0;
   16218      2873715 :           result.quick_push (p);
   16219              :         }
   16220      2873715 :       while (p != v);
   16221              :     }
   16222      2873715 : }
   16223              : 
   16224              : /* Compare two depsets.  The specific ordering is unimportant, we're
   16225              :    just trying to get consistency.  */
   16226              : 
   16227              : static int
   16228    138917815 : depset_cmp (const void *a_, const void *b_)
   16229              : {
   16230    138917815 :   depset *a = *(depset *const *)a_;
   16231    138917815 :   depset *b = *(depset *const *)b_;
   16232              : 
   16233    138917815 :   depset::entity_kind a_kind = a->get_entity_kind ();
   16234    138917815 :   depset::entity_kind b_kind = b->get_entity_kind ();
   16235              : 
   16236    138917815 :   if  (a_kind != b_kind)
   16237              :     /* Different entity kinds, order by that.  */
   16238      4915090 :     return a_kind < b_kind ? -1 : +1;
   16239              : 
   16240    134002725 :   tree a_decl = a->get_entity ();
   16241    134002725 :   tree b_decl = b->get_entity ();
   16242    134002725 :   if (a_kind == depset::EK_USING)
   16243              :     {
   16244              :       /* If one is a using, the other must be too.  */
   16245      2399013 :       a_decl = OVL_FUNCTION (a_decl);
   16246      2399013 :       b_decl = OVL_FUNCTION (b_decl);
   16247              :     }
   16248              : 
   16249    134002725 :   if (a_decl != b_decl)
   16250              :     /* Different entities, order by their UID.  */
   16251    125936843 :     return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
   16252              : 
   16253      8065882 :   if (a_kind == depset::EK_BINDING)
   16254              :     {
   16255              :       /* Both are bindings.  Order by identifier hash.  */
   16256      8062599 :       gcc_checking_assert (a->get_name () != b->get_name ());
   16257      8062599 :       hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
   16258      8062599 :       hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
   16259      8062599 :       return (ah == bh ? 0 : ah < bh ? -1 : +1);
   16260              :     }
   16261              : 
   16262              :   /* They are the same decl.  This can happen with two using decls
   16263              :      pointing to the same target.  The best we can aim for is
   16264              :      consistently telling qsort how to order them.  Hopefully we'll
   16265              :      never have to debug a case that depends on this.  Oh, who am I
   16266              :      kidding?  Good luck.  */
   16267         3283 :   gcc_checking_assert (a_kind == depset::EK_USING);
   16268              : 
   16269              :   /* Order by depset address.  Not the best, but it is something.  */
   16270         3283 :   return a < b ? -1 : +1;
   16271              : }
   16272              : 
   16273              : /* Sort the clusters in SCC such that those that depend on one another
   16274              :    are placed later.   */
   16275              : 
   16276              : // FIXME: I am not convinced this is needed and, if needed,
   16277              : // sufficient.  We emit the decls in this order but that emission
   16278              : // could walk into later decls (from the body of the decl, or default
   16279              : // arg-like things).  Why doesn't that walk do the right thing?  And
   16280              : // if it DTRT why do we need to sort here -- won't things naturally
   16281              : // work?  I think part of the issue is that when we're going to refer
   16282              : // to an entity by name, and that entity is in the same cluster as us,
   16283              : // we need to actually walk that entity, if we've not already walked
   16284              : // it.
   16285              : static void
   16286       337908 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   16287              : {
   16288       337908 :   depset::hash table (size, original);
   16289              : 
   16290       337908 :   dump.indent ();
   16291              : 
   16292              :   /* Place bindings last, usings before that.  It's not strictly
   16293              :      necessary, but it does make things neater.  Says Mr OCD.  */
   16294       337908 :   unsigned bind_lwm = size;
   16295       337908 :   unsigned use_lwm = size;
   16296      1874796 :   for (unsigned ix = 0; ix != use_lwm;)
   16297              :     {
   16298      1536888 :       depset *dep = scc[ix];
   16299      1536888 :       switch (dep->get_entity_kind ())
   16300              :         {
   16301       163748 :         case depset::EK_BINDING:
   16302              :           /* Move to end.  No increment.  Notice this could be moving
   16303              :              a using decl, which we'll then move again.  */
   16304       163748 :           if (--bind_lwm != ix)
   16305              :             {
   16306        94866 :               scc[ix] = scc[bind_lwm];
   16307        94866 :               scc[bind_lwm] = dep;
   16308              :             }
   16309       163748 :           if (use_lwm > bind_lwm)
   16310              :             {
   16311       130375 :               use_lwm--;
   16312       130375 :               break;
   16313              :             }
   16314              :           /* We must have copied a using or TU-local, so move it too.  */
   16315        33373 :           dep = scc[ix];
   16316        33373 :           gcc_checking_assert
   16317              :             (dep->get_entity_kind () == depset::EK_USING
   16318              :              || dep->get_entity_kind () == depset::EK_TU_LOCAL);
   16319              :           /* FALLTHROUGH  */
   16320              : 
   16321        74987 :         case depset::EK_USING:
   16322        74987 :         case depset::EK_TU_LOCAL:
   16323        74987 :           if (--use_lwm != ix)
   16324              :             {
   16325        58566 :               scc[ix] = scc[use_lwm];
   16326        58566 :               scc[use_lwm] = dep;
   16327              :             }
   16328              :           break;
   16329              : 
   16330      1331526 :         case depset::EK_DECL:
   16331      1331526 :         case depset::EK_SPECIALIZATION:
   16332      1331526 :         case depset::EK_PARTIAL:
   16333      1331526 :           table.add_mergeable (dep);
   16334      1331526 :           ix++;
   16335      1331526 :           break;
   16336              : 
   16337            0 :         default:
   16338            0 :           gcc_unreachable ();
   16339              :         }
   16340              :     }
   16341              : 
   16342       337908 :   gcc_checking_assert (use_lwm <= bind_lwm);
   16343       338196 :   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
   16344              : 
   16345       337908 :   table.find_dependencies (nullptr);
   16346              : 
   16347       337908 :   auto_vec<depset *> order = table.connect ();
   16348       675816 :   gcc_checking_assert (order.length () == use_lwm);
   16349              : 
   16350              :   /* Now rewrite entries [0,lwm), in the dependency order we
   16351              :      discovered.  Usually each entity is in its own cluster.  Rarely,
   16352              :      we can get multi-entity clusters, in which case all but one must
   16353              :      only be reached from within the cluster.  This happens for
   16354              :      something like:
   16355              : 
   16356              :      template<typename T>
   16357              :      auto Foo (const T &arg) -> TPL<decltype (arg)>;
   16358              : 
   16359              :      The instantiation of TPL will be in the specialization table, and
   16360              :      refer to Foo via arg.  But we can only get to that specialization
   16361              :      from Foo's declaration, so we only need to treat Foo as mergeable
   16362              :      (We'll do structural comparison of TPL<decltype (arg)>).
   16363              : 
   16364              :      We approximate finding the single cluster entry dep by checking for
   16365              :      entities recursively depending on a dep first seen when streaming
   16366              :      its own merge key; the first dep we see in such a cluster should be
   16367              :      the first one streamed.  */
   16368              :   unsigned entry_pos = ~0u;
   16369              :   unsigned cluster = ~0u;
   16370      3338868 :   for (unsigned ix = 0; ix != order.length (); ix++)
   16371              :     {
   16372      1331526 :       gcc_checking_assert (order[ix]->is_special ());
   16373      1331526 :       bool tight = order[ix]->cluster == cluster;
   16374      1331526 :       depset *dep = order[ix]->deps[0];
   16375      1332519 :       dump (dumper::MERGE)
   16376         1983 :         && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
   16377          993 :                  tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
   16378      1331526 :       scc[ix] = dep;
   16379      1331526 :       if (tight)
   16380              :         {
   16381          134 :           gcc_checking_assert (dep->is_maybe_recursive ());
   16382          134 :           if (dep->is_entry ())
   16383              :             {
   16384              :               /* There should only be one entry dep in a cluster.  */
   16385            9 :               gcc_checking_assert (!scc[entry_pos]->is_entry ());
   16386            9 :               gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
   16387            9 :               scc[ix] = scc[entry_pos];
   16388            9 :               scc[entry_pos] = dep;
   16389              :             }
   16390              :         }
   16391              :       else
   16392              :         entry_pos = ix;
   16393      1331526 :       cluster = order[ix]->cluster;
   16394              :     }
   16395              : 
   16396       338196 :   dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
   16397       337908 :   dump.outdent ();
   16398       337908 : }
   16399              : 
   16400              : /* Reduce graph to SCCS clusters.  SCCS will be populated with the
   16401              :    depsets in dependency order.  Each depset's CLUSTER field contains
   16402              :    its cluster number.  Each SCC has a unique cluster number, and are
   16403              :    contiguous in SCCS. Cluster numbers are otherwise arbitrary.  */
   16404              : 
   16405              : vec<depset *>
   16406       340723 : depset::hash::connect ()
   16407              : {
   16408       340723 :   tarjan connector (size ());
   16409       340723 :   vec<depset *> deps;
   16410       340723 :   deps.create (size ());
   16411      3787741 :   for (depset *item : *this)
   16412              :     {
   16413      3447018 :       entity_kind kind = item->get_entity_kind ();
   16414      3283270 :       if (kind == EK_BINDING
   16415      3283270 :           || !(kind == EK_REDIRECT
   16416      3255718 :                || item->is_tu_local ()
   16417      3255587 :                || item->is_unreached ()
   16418      2714225 :                || item->is_import ()))
   16419      2873715 :         deps.quick_push (item);
   16420              :     }
   16421              : 
   16422              :   /* Iteration over the hash table is an unspecified ordering.  While
   16423              :      that has advantages, it causes 2 problems.  Firstly repeatable
   16424              :      builds are tricky.  Secondly creating testcases that check
   16425              :      dependencies are correct by making sure a bad ordering would
   16426              :      happen if that was wrong.  */
   16427       340723 :   deps.qsort (depset_cmp);
   16428              : 
   16429      3555161 :   while (deps.length ())
   16430              :     {
   16431      2873715 :       depset *v = deps.pop ();
   16432      2873715 :       dump (dumper::CLUSTER) &&
   16433         1800 :         (v->is_binding ()
   16434          210 :          ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
   16435         1590 :          : dump ("Connecting %s %s %C:%N",
   16436         1590 :                  is_key_order () ? "key-order"
   16437          870 :                  : !v->has_defn () ? "declaration" : "definition",
   16438         1590 :                  v->entity_kind_name (), TREE_CODE (v->get_entity ()),
   16439              :                  v->get_entity ()));
   16440      2873715 :       if (!v->cluster)
   16441      1263025 :         connector.connect (v);
   16442              :     }
   16443              : 
   16444       340723 :   deps.release ();
   16445       681446 :   return connector.result;
   16446       340723 : }
   16447              : 
   16448              : /* Initialize location spans.  */
   16449              : 
   16450              : void
   16451         5033 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
   16452              : {
   16453         5033 :   gcc_checking_assert (!init_p ());
   16454         5033 :   spans = new vec<span> ();
   16455         5033 :   spans->reserve (20);
   16456              : 
   16457         5033 :   span interval;
   16458         5033 :   interval.ordinary.first = 0;
   16459         5033 :   interval.macro.second = MAX_LOCATION_T + 1;
   16460         5033 :   interval.ordinary_delta = interval.macro_delta = 0;
   16461              : 
   16462              :   /* A span for reserved fixed locs.  */
   16463         5033 :   interval.ordinary.second
   16464         5033 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   16465         5033 :   interval.macro.first = interval.macro.second;
   16466         5033 :   dump (dumper::LOCATION)
   16467           42 :     && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16468              :              interval.ordinary.first, interval.ordinary.second,
   16469              :              interval.macro.first, interval.macro.second);
   16470         5033 :   spans->quick_push (interval);
   16471              : 
   16472              :   /* A span for command line & forced headers.  */
   16473         5033 :   interval.ordinary.first = interval.ordinary.second;
   16474         5033 :   interval.macro.second = interval.macro.first;
   16475         5033 :   if (map)
   16476              :     {
   16477         5027 :       interval.ordinary.second = map->start_location;
   16478         5027 :       interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
   16479              :     }
   16480         5033 :   dump (dumper::LOCATION)
   16481           21 :     && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16482              :              interval.ordinary.first, interval.ordinary.second,
   16483              :              interval.macro.first, interval.macro.second);
   16484         5033 :   spans->quick_push (interval);
   16485              : 
   16486              :   /* Start an interval for the main file.  */
   16487         5033 :   interval.ordinary.first = interval.ordinary.second;
   16488         5033 :   interval.macro.second = interval.macro.first;
   16489         5033 :   dump (dumper::LOCATION)
   16490           21 :     && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
   16491              :              interval.ordinary.first, interval.macro.second);
   16492         5033 :   spans->quick_push (interval);
   16493         5033 : }
   16494              : 
   16495              : /* Reopen the span, if we want the about-to-be-inserted set of maps to
   16496              :    be propagated in our own location table.  I.e. we are the primary
   16497              :    interface and we're importing a partition.  */
   16498              : 
   16499              : bool
   16500         3123 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
   16501              : {
   16502         3123 :   bool opened = (module_interface_p () && !module_partition_p ()
   16503         3591 :                  && import->is_partition ());
   16504          178 :   if (opened)
   16505          178 :     open (hwm);
   16506         3123 :   return opened;
   16507              : }
   16508              : 
   16509              : /* Open a new linemap interval.  The just-created ordinary map is the
   16510              :    first map of the interval.  */
   16511              : 
   16512              : void
   16513         1087 : loc_spans::open (location_t hwm)
   16514              : {
   16515         1087 :   span interval;
   16516         1087 :   interval.ordinary.first = interval.ordinary.second = hwm;
   16517         2174 :   interval.macro.first = interval.macro.second
   16518         1087 :     = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16519         1087 :   interval.ordinary_delta = interval.macro_delta = 0;
   16520         1087 :   dump (dumper::LOCATION)
   16521            0 :     && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
   16522            0 :              spans->length (), interval.ordinary.first,
   16523              :              interval.macro.second);
   16524         1087 :   if (spans->length ())
   16525              :     {
   16526              :       /* No overlapping!  */
   16527         1087 :       auto &last = spans->last ();
   16528         1087 :       gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
   16529         1087 :       gcc_checking_assert (interval.macro.second <= last.macro.first);
   16530              :     }
   16531         1087 :   spans->safe_push (interval);
   16532         1087 : }
   16533              : 
   16534              : /* Close out the current linemap interval.  The last maps are within
   16535              :    the interval.  */
   16536              : 
   16537              : void
   16538         6117 : loc_spans::close ()
   16539              : {
   16540         6117 :   span &interval = spans->last ();
   16541              : 
   16542         6117 :   interval.ordinary.second
   16543         6117 :     = ((line_table->highest_location
   16544         6117 :         + (loc_one << line_table->default_range_bits))
   16545         6117 :        & ~((loc_one << line_table->default_range_bits) - 1));
   16546         6117 :   interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16547         6117 :   dump (dumper::LOCATION)
   16548           21 :     && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
   16549           21 :              spans->length () - 1,
   16550              :              interval.ordinary.first,interval.ordinary.second,
   16551              :              interval.macro.first, interval.macro.second);
   16552         6117 : }
   16553              : 
   16554              : /* Given an ordinary location LOC, return the lmap_interval it resides
   16555              :    in.  NULL if it is not in an interval.  */
   16556              : 
   16557              : const loc_spans::span *
   16558     44368091 : loc_spans::ordinary (location_t loc)
   16559              : {
   16560     44368091 :   unsigned len = spans->length ();
   16561     44368091 :   unsigned pos = 0;
   16562    132982181 :   while (len)
   16563              :     {
   16564     88604301 :       unsigned half = len / 2;
   16565     88604301 :       const span &probe = (*spans)[pos + half];
   16566     88604301 :       if (loc < probe.ordinary.first)
   16567              :         len = half;
   16568     88596796 :       else if (loc < probe.ordinary.second)
   16569              :         return &probe;
   16570              :       else
   16571              :         {
   16572     44238494 :           pos += half + 1;
   16573     44238494 :           len = len - (half + 1);
   16574              :         }
   16575              :     }
   16576              :   return NULL;
   16577              : }
   16578              : 
   16579              : /* Likewise, given a macro location LOC, return the lmap interval it
   16580              :    resides in.   */
   16581              : 
   16582              : const loc_spans::span *
   16583      3337292 : loc_spans::macro (location_t loc)
   16584              : {
   16585      3337292 :   unsigned len = spans->length ();
   16586      3337292 :   unsigned pos = 0;
   16587     10008828 :   while (len)
   16588              :     {
   16589      6671524 :       unsigned half = len / 2;
   16590      6671524 :       const span &probe = (*spans)[pos + half];
   16591      6671524 :       if (loc >= probe.macro.second)
   16592              :         len = half;
   16593      6671524 :       else if (loc >= probe.macro.first)
   16594              :         return &probe;
   16595              :       else
   16596              :         {
   16597      3334244 :           pos += half + 1;
   16598      3334244 :           len = len - (half + 1);
   16599              :         }
   16600              :     }
   16601              :   return NULL;
   16602              : }
   16603              : 
   16604              : /* Return the ordinary location closest to FROM.  */
   16605              : 
   16606              : static location_t
   16607         7003 : ordinary_loc_of (line_maps *lmaps, location_t from)
   16608              : {
   16609        14009 :   while (!IS_ORDINARY_LOC (from))
   16610              :     {
   16611            3 :       if (IS_ADHOC_LOC (from))
   16612            3 :         from = get_location_from_adhoc_loc (lmaps, from);
   16613            3 :       if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
   16614              :         {
   16615              :           /* Find the ordinary location nearest FROM.  */
   16616            0 :           const line_map *map = linemap_lookup (lmaps, from);
   16617            0 :           const line_map_macro *mac_map = linemap_check_macro (map);
   16618            0 :           from = mac_map->get_expansion_point_location ();
   16619              :         }
   16620              :     }
   16621         7003 :   return from;
   16622              : }
   16623              : 
   16624              : static module_state **
   16625        12597 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
   16626              : {
   16627        12597 :   module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
   16628        12597 :   hashval_t hv = module_state_hash::hash (ct);
   16629              : 
   16630        12597 :   return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
   16631              : }
   16632              : 
   16633              : static module_state *
   16634       173461 : get_primary (module_state *parent)
   16635              : {
   16636       177584 :   while (parent->is_partition ())
   16637          691 :     parent = parent->parent;
   16638              : 
   16639       176893 :   if (!parent->name)
   16640              :     // Implementation unit has null name
   16641       109107 :     parent = parent->parent;
   16642              : 
   16643       164382 :   return parent;
   16644              : }
   16645              : 
   16646              : /* Find or create module NAME & PARENT in the hash table.  */
   16647              : 
   16648              : module_state *
   16649        12597 : get_module (tree name, module_state *parent, bool partition)
   16650              : {
   16651              :   /* We might be given an empty NAME if preprocessing fails to handle
   16652              :      a header-name token.  */
   16653        12597 :   if (name && TREE_CODE (name) == STRING_CST
   16654        15491 :       && TREE_STRING_LENGTH (name) == 0)
   16655              :     return nullptr;
   16656              : 
   16657        12597 :   if (partition)
   16658              :     {
   16659         1123 :       if (!parent)
   16660          238 :         parent = get_primary (this_module ());
   16661              : 
   16662         1123 :       if (!parent->is_partition () && !parent->flatname)
   16663          265 :         parent->set_flatname ();
   16664              :     }
   16665              : 
   16666        12597 :   module_state **slot = get_module_slot (name, parent, partition, true);
   16667        12597 :   module_state *state = *slot;
   16668        12597 :   if (!state)
   16669              :     {
   16670         6817 :       state = (new (ggc_alloc<module_state> ())
   16671         6817 :                module_state (name, parent, partition));
   16672         6817 :       *slot = state;
   16673              :     }
   16674              :   return state;
   16675              : }
   16676              : 
   16677              : /* Process string name PTR into a module_state.  */
   16678              : 
   16679              : static module_state *
   16680          454 : get_module (const char *ptr)
   16681              : {
   16682              :   /* On DOS based file systems, there is an ambiguity with A:B which can be
   16683              :      interpreted as a module Module:Partition or Drive:PATH.  Interpret strings
   16684              :      which clearly starts as pathnames as header-names and everything else is
   16685              :      treated as a (possibly malformed) named moduled.  */
   16686          454 :   if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
   16687              : #if HAVE_DOS_BASED_FILE_SYSTEM
   16688              :       || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
   16689              : #endif
   16690              :       || false)
   16691              :     /* A header name.  */
   16692          109 :     return get_module (build_string (strlen (ptr), ptr));
   16693              : 
   16694              :   bool partition = false;
   16695              :   module_state *mod = NULL;
   16696              : 
   16697         1357 :   for (const char *probe = ptr;; probe++)
   16698         1702 :     if (!*probe || *probe == '.' || *probe == ':')
   16699              :       {
   16700          435 :         if (probe == ptr)
   16701              :           return NULL;
   16702              : 
   16703          435 :         mod = get_module (get_identifier_with_length (ptr, probe - ptr),
   16704              :                           mod, partition);
   16705          435 :         ptr = probe;
   16706          435 :         if (*ptr == ':')
   16707              :           {
   16708           87 :             if (partition)
   16709              :               return NULL;
   16710              :             partition = true;
   16711              :           }
   16712              : 
   16713          435 :         if (!*ptr++)
   16714              :           break;
   16715              :       }
   16716         1267 :     else if (!(ISALPHA (*probe) || *probe == '_'
   16717           18 :                || (probe != ptr && ISDIGIT (*probe))))
   16718              :       return NULL;
   16719              : 
   16720              :   return mod;
   16721              : }
   16722              : 
   16723              : /* Create a new mapper connecting to OPTION.  */
   16724              : 
   16725              : module_client *
   16726         5033 : make_mapper (location_t loc, class mkdeps *deps)
   16727              : {
   16728         5033 :   timevar_start (TV_MODULE_MAPPER);
   16729         5033 :   const char *option = module_mapper_name;
   16730         5033 :   if (!option)
   16731         4985 :     option = getenv ("CXX_MODULE_MAPPER");
   16732              : 
   16733        10066 :   mapper = module_client::open_module_client
   16734         5033 :     (loc, option, deps, &set_cmi_repo,
   16735         5033 :      (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
   16736         5033 :      && save_decoded_options[0].arg != progname
   16737              :      ? save_decoded_options[0].arg : nullptr);
   16738              : 
   16739         5033 :   timevar_stop (TV_MODULE_MAPPER);
   16740              : 
   16741         5033 :   return mapper;
   16742              : }
   16743              : 
   16744              : static unsigned lazy_snum;
   16745              : 
   16746              : static bool
   16747        11872 : recursive_lazy (unsigned snum = ~0u)
   16748              : {
   16749        11872 :   if (lazy_snum)
   16750              :     {
   16751            0 :       error_at (input_location, "recursive lazy load");
   16752            0 :       return true;
   16753              :     }
   16754              : 
   16755        11872 :   lazy_snum = snum;
   16756        11872 :   return false;
   16757              : }
   16758              : 
   16759              : /* If THIS has an interface dependency on itself, report an error and
   16760              :    return false.  */
   16761              : 
   16762              : bool
   16763         2967 : module_state::check_circular_import (location_t from)
   16764              : {
   16765         2967 :   if (this == this_module ())
   16766              :     {
   16767              :       /* Cannot import the current module.  */
   16768            9 :       auto_diagnostic_group d;
   16769            9 :       error_at (from, "module %qs depends on itself", get_flatname ());
   16770            9 :       if (!header_module_p ())
   16771            6 :         inform (loc, "module %qs declared here", get_flatname ());
   16772            9 :       return false;
   16773            9 :     }
   16774              :   return true;
   16775              : }
   16776              : 
   16777              : /* Module name substitutions.  */
   16778              : static vec<module_state *,va_heap> substs;
   16779              : 
   16780              : void
   16781         9232 : module_state::mangle (bool include_partition)
   16782              : {
   16783         9232 :   if (subst)
   16784          425 :     mangle_module_substitution (subst);
   16785              :   else
   16786              :     {
   16787         8807 :       if (parent)
   16788          888 :         parent->mangle (include_partition);
   16789         8807 :       if (include_partition || !is_partition ())
   16790              :         {
   16791              :           // Partitions are significant for global initializer
   16792              :           // functions
   16793         8601 :           bool partition = is_partition () && !parent->is_partition ();
   16794         8601 :           subst = mangle_module_component (name, partition);
   16795         8601 :           substs.safe_push (this);
   16796              :         }
   16797              :     }
   16798         9232 : }
   16799              : 
   16800              : void
   16801         8344 : mangle_module (int mod, bool include_partition)
   16802              : {
   16803         8344 :   module_state *imp = (*modules)[mod];
   16804              : 
   16805         8344 :   gcc_checking_assert (!imp->is_header ());
   16806              : 
   16807         8344 :   if (!imp->name)
   16808              :     /* Set when importing the primary module interface.  */
   16809          223 :     imp = imp->parent;
   16810              : 
   16811              :   /* Ensure this is actually a module unit.  */
   16812          223 :   gcc_checking_assert (imp);
   16813              : 
   16814         8344 :   imp->mangle (include_partition);
   16815         8344 : }
   16816              : 
   16817              : /* Clean up substitutions.  */
   16818              : void
   16819         7873 : mangle_module_fini ()
   16820              : {
   16821        16474 :   while (substs.length ())
   16822         8601 :     substs.pop ()->subst = 0;
   16823         7873 : }
   16824              : 
   16825              : /* Announce WHAT about the module.  */
   16826              : 
   16827              : void
   16828        12927 : module_state::announce (const char *what) const
   16829              : {
   16830        12927 :   if (noisy_p ())
   16831              :     {
   16832            0 :       fprintf (stderr, " %s:%s", what, get_flatname ());
   16833            0 :       fflush (stderr);
   16834              :     }
   16835        12927 : }
   16836              : 
   16837              : /* A human-readable README section.  The contents of this section to
   16838              :    not contribute to the CRC, so the contents can change per
   16839              :    compilation.  That allows us to embed CWD, hostname, build time and
   16840              :    what not.  It is a STRTAB that may be extracted with:
   16841              :      readelf -pgnu.c++.README $(module).gcm */
   16842              : 
   16843              : void
   16844         2815 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
   16845              : {
   16846         2815 :   bytes_out readme (to);
   16847              : 
   16848         2815 :   readme.begin (false);
   16849              : 
   16850         2815 :   readme.printf ("GNU C++ %s",
   16851         2815 :                  is_header () ? "header unit"
   16852         1906 :                  : !is_partition () ? "primary interface"
   16853          208 :                  : is_interface () ? "interface partition"
   16854              :                  : "internal partition");
   16855              : 
   16856              :   /* Compiler's version.  */
   16857         2815 :   readme.printf ("compiler: %s", version_string);
   16858              : 
   16859              :   /* Module format version.  */
   16860         2815 :   verstr_t string;
   16861         2815 :   version2string (MODULE_VERSION, string);
   16862         2815 :   readme.printf ("version: %s", string);
   16863              : 
   16864              :   /* Module information.  */
   16865         2815 :   readme.printf ("module: %s", get_flatname ());
   16866         2815 :   readme.printf ("source: %s", main_input_filename);
   16867         2815 :   readme.printf ("dialect: %s", dialect);
   16868         2815 :   if (extensions)
   16869           30 :     readme.printf ("extensions: %s%s%s",
   16870              :                    extensions & SE_OPENMP ? "-fopenmp"
   16871            6 :                    : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
   16872              :                    (extensions & SE_OPENACC)
   16873            3 :                    && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
   16874              :                    ? " " : "",
   16875           12 :                    extensions & SE_OPENACC ? "-fopenacc" : "");
   16876              : 
   16877              :   /* The following fields could be expected to change between
   16878              :      otherwise identical compilations.  Consider a distributed build
   16879              :      system.  We should have a way of overriding that.  */
   16880         2815 :   if (char *cwd = getcwd (NULL, 0))
   16881              :     {
   16882         2815 :       readme.printf ("cwd: %s", cwd);
   16883         2815 :       free (cwd);
   16884              :     }
   16885         5630 :   readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
   16886              : #if NETWORKING
   16887              :   {
   16888              :     char hostname[64];
   16889              :     if (!gethostname (hostname, sizeof (hostname)))
   16890              :       readme.printf ("host: %s", hostname);
   16891              :   }
   16892              : #endif
   16893         2815 :   {
   16894              :     /* This of course will change!  */
   16895         2815 :     time_t stampy;
   16896         2815 :     auto kind = cpp_get_date (reader, &stampy);
   16897         2815 :     if (kind != CPP_time_kind::UNKNOWN)
   16898              :       {
   16899         2815 :         struct tm *time;
   16900              : 
   16901         2815 :         time = gmtime (&stampy);
   16902         2815 :         readme.print_time ("build", time, "UTC");
   16903              : 
   16904         2815 :         if (kind == CPP_time_kind::DYNAMIC)
   16905              :           {
   16906         2815 :             time = localtime (&stampy);
   16907         2815 :             readme.print_time ("local", time,
   16908              : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way?  */
   16909              :                                time->tm_zone
   16910              : #else
   16911              :                                ""
   16912              : #endif
   16913              :                                );
   16914              :           }
   16915              :       }
   16916              :   }
   16917              : 
   16918              :   /* Its direct imports.  */
   16919         3493 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16920              :     {
   16921          678 :       module_state *state = (*modules)[ix];
   16922              : 
   16923          678 :       if (state->is_direct ())
   16924          997 :         readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
   16925              :                        state->get_flatname (), state->filename);
   16926              :     }
   16927              : 
   16928         2815 :   readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
   16929         2815 : }
   16930              : 
   16931              : /* Sort environment var names in reverse order.  */
   16932              : 
   16933              : static int
   16934            0 : env_var_cmp (const void *a_, const void *b_)
   16935              : {
   16936            0 :   const unsigned char *a = *(const unsigned char *const *)a_;
   16937            0 :   const unsigned char *b = *(const unsigned char *const *)b_;
   16938              : 
   16939            0 :   for (unsigned ix = 0; ; ix++)
   16940              :     {
   16941            0 :       bool a_end = !a[ix] || a[ix] == '=';
   16942            0 :       if (a[ix] == b[ix])
   16943              :         {
   16944            0 :           if (a_end)
   16945              :             break;
   16946              :         }
   16947              :       else
   16948              :         {
   16949            0 :           bool b_end = !b[ix] || b[ix] == '=';
   16950              : 
   16951            0 :           if (!a_end && !b_end)
   16952            0 :             return a[ix] < b[ix] ? +1 : -1;
   16953            0 :           if (a_end && b_end)
   16954              :             break;
   16955            0 :           return a_end ? +1 : -1;
   16956              :         }
   16957            0 :     }
   16958              : 
   16959              :   return 0;
   16960              : }
   16961              : 
   16962              : /* Write the environment. It is a STRTAB that may be extracted with:
   16963              :      readelf -pgnu.c++.ENV $(module).gcm */
   16964              : 
   16965              : void
   16966            0 : module_state::write_env (elf_out *to)
   16967              : {
   16968            0 :   vec<const char *> vars;
   16969            0 :   vars.create (20);
   16970              : 
   16971            0 :   extern char **environ;
   16972            0 :   while (const char *var = environ[vars.length ()])
   16973            0 :     vars.safe_push (var);
   16974            0 :   vars.qsort (env_var_cmp);
   16975              : 
   16976            0 :   bytes_out env (to);
   16977            0 :   env.begin (false);
   16978            0 :   while (vars.length ())
   16979            0 :     env.printf ("%s", vars.pop ());
   16980            0 :   env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
   16981              : 
   16982            0 :   vars.release ();
   16983            0 : }
   16984              : 
   16985              : /* Write the direct or indirect imports.
   16986              :    u:N
   16987              :    {
   16988              :      u:index
   16989              :      s:name
   16990              :      u32:crc
   16991              :      s:filename (direct)
   16992              :      u:exported (direct)
   16993              :    } imports[N]
   16994              :  */
   16995              : 
   16996              : void
   16997          916 : module_state::write_imports (bytes_out &sec, bool direct)
   16998              : {
   16999          916 :   unsigned count = 0;
   17000              : 
   17001         1934 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   17002              :     {
   17003         1018 :       module_state *imp = (*modules)[ix];
   17004              : 
   17005         1018 :       if (imp->remap && imp->is_direct () == direct)
   17006          488 :         count++;
   17007              :     }
   17008              : 
   17009          916 :   gcc_assert (!direct || count);
   17010              : 
   17011          916 :   sec.u (count);
   17012         2850 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   17013              :     {
   17014         1018 :       module_state *imp = (*modules)[ix];
   17015              : 
   17016         1018 :       if (imp->remap && imp->is_direct () == direct)
   17017              :         {
   17018          650 :           dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
   17019              :                            !direct ? "indirect "
   17020           81 :                            : imp->exported_p ? "exported " : "",
   17021              :                            ix, imp->remap, imp, imp->crc);
   17022          488 :           sec.u (imp->remap);
   17023          488 :           sec.str (imp->get_flatname ());
   17024          488 :           sec.u32 (imp->crc);
   17025          488 :           if (direct)
   17026              :             {
   17027          480 :               write_location (sec, imp->imported_from ());
   17028          480 :               sec.str (imp->filename);
   17029          480 :               int exportedness = 0;
   17030          480 :               if (imp->exported_p)
   17031              :                 exportedness = +1;
   17032          286 :               else if (!imp->is_purview_direct ())
   17033           12 :                 exportedness = -1;
   17034          480 :               sec.i (exportedness);
   17035              :             }
   17036              :         }
   17037              :     }
   17038          916 : }
   17039              : 
   17040              : /* READER, LMAPS  != NULL == direct imports,
   17041              :    == NUL == indirect imports.  */
   17042              : 
   17043              : unsigned
   17044          776 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
   17045              : {
   17046          776 :   unsigned count = sec.u ();
   17047          776 :   unsigned loaded = 0;
   17048              : 
   17049         1961 :   while (count--)
   17050              :     {
   17051          409 :       unsigned ix = sec.u ();
   17052          409 :       if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
   17053              :         {
   17054            0 :           sec.set_overrun ();
   17055            0 :           break;
   17056              :         }
   17057              : 
   17058          409 :       const char *name = sec.str (NULL);
   17059          409 :       module_state *imp = get_module (name);
   17060          409 :       unsigned crc = sec.u32 ();
   17061          409 :       int exportedness = 0;
   17062              : 
   17063              :       /* If the import is a partition, it must be the same primary
   17064              :          module as this TU.  */
   17065          409 :       if (imp && imp->is_partition () &&
   17066              :           (!named_module_p ()
   17067          144 :            || (get_primary (this_module ()) != get_primary (imp))))
   17068              :         imp = NULL;
   17069              : 
   17070          409 :       if (!imp)
   17071            0 :         sec.set_overrun ();
   17072          409 :       if (sec.get_overrun ())
   17073              :         break;
   17074              : 
   17075          409 :       if (lmaps)
   17076              :         {
   17077              :           /* A direct import, maybe load it.  */
   17078          406 :           location_t floc = read_location (sec);
   17079          406 :           const char *fname = sec.str (NULL);
   17080          406 :           exportedness = sec.i ();
   17081              : 
   17082          406 :           if (sec.get_overrun ())
   17083              :             break;
   17084              : 
   17085          406 :           if (!imp->check_circular_import (floc))
   17086            3 :             continue;
   17087              : 
   17088          403 :           if (imp->loadedness == ML_NONE)
   17089              :             {
   17090          316 :               imp->loc = floc;
   17091          316 :               imp->crc = crc;
   17092          316 :               if (!imp->get_flatname ())
   17093          274 :                 imp->set_flatname ();
   17094              : 
   17095          316 :               unsigned n = dump.push (imp);
   17096              : 
   17097          316 :               if (!imp->filename && fname)
   17098          274 :                 imp->filename = xstrdup (fname);
   17099              : 
   17100          316 :               if (imp->is_partition ())
   17101           33 :                 dump () && dump ("Importing elided partition %M", imp);
   17102              : 
   17103          316 :               if (!imp->do_import (reader, false))
   17104            3 :                 imp = NULL;
   17105          316 :               dump.pop (n);
   17106          316 :               if (!imp)
   17107            3 :                 continue;
   17108              :             }
   17109              : 
   17110          400 :           if (is_partition ())
   17111              :             {
   17112           69 :               if (!imp->is_direct () && !imp->is_partition_direct ())
   17113              :                 {
   17114           30 :                   imp->directness = MD_PARTITION_DIRECT;
   17115           30 :                   linemap_module_reparent (line_table, imp->loc, floc);
   17116              :                 }
   17117           69 :               if (exportedness > 0)
   17118            6 :                 imp->exported_p = true;
   17119              :             }
   17120              :         }
   17121              :       else
   17122              :         {
   17123              :           /* An indirect import, find it, it should already be here.  */
   17124            3 :           if (imp->loadedness == ML_NONE)
   17125              :             {
   17126            0 :               error_at (loc, "indirect import %qs is not already loaded", name);
   17127            0 :               continue;
   17128              :             }
   17129              :         }
   17130              : 
   17131          403 :       if (imp->crc != crc)
   17132            0 :         error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
   17133              : 
   17134          403 :       (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
   17135              : 
   17136          403 :       if (lmaps && exportedness >= 0)
   17137          388 :         set_import (imp, bool (exportedness));
   17138          583 :       dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
   17139           90 :                        : exportedness > 0 ? "exported "
   17140           51 :                        : exportedness < 0 ? "gmf" : "", ix, imp,
   17141              :                        imp->mod);
   17142          403 :       loaded++;
   17143              :     }
   17144              : 
   17145          776 :   return loaded;
   17146              : }
   17147              : 
   17148              : /* Write the import table to MOD_SNAME_PFX.imp.  */
   17149              : 
   17150              : void
   17151          458 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
   17152              : {
   17153          536 :   dump () && dump ("Writing imports");
   17154          458 :   dump.indent ();
   17155              : 
   17156          458 :   bytes_out sec (to);
   17157          458 :   sec.begin ();
   17158              : 
   17159          458 :   write_imports (sec, true);
   17160          458 :   write_imports (sec, false);
   17161              : 
   17162          458 :   sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
   17163          458 :   dump.outdent ();
   17164          458 : }
   17165              : 
   17166              : bool
   17167          388 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
   17168              : {
   17169          388 :   bytes_in sec;
   17170              : 
   17171          388 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
   17172              :     return false;
   17173              : 
   17174          475 :   dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
   17175          388 :   dump.indent ();
   17176              : 
   17177              :   /* Read the imports.  */
   17178          388 :   unsigned direct = read_imports (sec, reader, lmaps);
   17179          388 :   unsigned indirect = read_imports (sec, NULL, NULL);
   17180          388 :   if (direct + indirect + 1 != slurp->remap->length ())
   17181            6 :     from ()->set_error (elf::E_BAD_IMPORT);
   17182              : 
   17183          388 :   dump.outdent ();
   17184          388 :   if (!sec.end (from ()))
   17185              :     return false;
   17186              :   return true;
   17187          388 : }
   17188              : 
   17189              : /* We're the primary module interface, but have partitions.  Document
   17190              :    them so that non-partition module implementation units know which
   17191              :    have already been loaded.  */
   17192              : 
   17193              : void
   17194          136 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
   17195              : {
   17196          160 :   dump () && dump ("Writing %u elided partitions", count);
   17197          136 :   dump.indent ();
   17198              : 
   17199          136 :   bytes_out sec (to);
   17200          136 :   sec.begin ();
   17201              : 
   17202          486 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   17203              :     {
   17204          214 :       module_state *imp = (*modules)[ix];
   17205          214 :       if (imp->is_partition ())
   17206              :         {
   17207          229 :           dump () && dump ("Writing elided partition %M (crc=%x)",
   17208              :                            imp, imp->crc);
   17209          190 :           sec.str (imp->get_flatname ());
   17210          190 :           sec.u32 (imp->crc);
   17211          371 :           write_location (sec, imp->is_direct ()
   17212          181 :                           ? imp->imported_from () : UNKNOWN_LOCATION);
   17213          190 :           sec.str (imp->filename);
   17214              :         }
   17215              :     }
   17216              : 
   17217          136 :   sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
   17218          136 :   dump.outdent ();
   17219          136 : }
   17220              : 
   17221              : bool
   17222           27 : module_state::read_partitions (unsigned count)
   17223              : {
   17224           27 :   bytes_in sec;
   17225           27 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
   17226              :     return false;
   17227              : 
   17228           33 :   dump () && dump ("Reading %u elided partitions", count);
   17229           27 :   dump.indent ();
   17230              : 
   17231           66 :   while (count--)
   17232              :     {
   17233           39 :       const char *name = sec.str (NULL);
   17234           39 :       unsigned crc = sec.u32 ();
   17235           39 :       location_t floc = read_location (sec);
   17236           39 :       const char *fname = sec.str (NULL);
   17237              : 
   17238           39 :       if (sec.get_overrun ())
   17239              :         break;
   17240              : 
   17241           48 :       dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
   17242              : 
   17243           39 :       module_state *imp = get_module (name);
   17244           39 :       if (!imp  /* Partition should be ...  */
   17245           39 :           || !imp->is_partition () /* a partition ...  */
   17246           39 :           || imp->loadedness != ML_NONE  /* that is not yet loaded ...  */
   17247           78 :           || get_primary (imp) != this) /* whose primary is this.  */
   17248              :         {
   17249            0 :           sec.set_overrun ();
   17250            0 :           break;
   17251              :         }
   17252              : 
   17253           39 :       if (!imp->has_location ())
   17254           30 :         imp->loc = floc;
   17255           39 :       imp->crc = crc;
   17256           39 :       if (!imp->filename && fname[0])
   17257           30 :         imp->filename = xstrdup (fname);
   17258              :     }
   17259              : 
   17260           27 :   dump.outdent ();
   17261           27 :   if (!sec.end (from ()))
   17262              :     return false;
   17263              :   return true;
   17264           27 : }
   17265              : 
   17266              : /* Data for config reading and writing.  */
   17267              : struct module_state_config {
   17268              :   const char *dialect_str = get_dialect ();
   17269              :   line_map_uint_t ordinary_locs = 0;
   17270              :   line_map_uint_t macro_locs = 0;
   17271              :   unsigned num_imports = 0;
   17272              :   unsigned num_partitions = 0;
   17273              :   unsigned num_entities = 0;
   17274              :   unsigned loc_range_bits = 0;
   17275              :   unsigned active_init = 0;
   17276              : 
   17277        99779 :   static void release ()
   17278              :   {
   17279        99779 :     XDELETEVEC (dialect);
   17280        99779 :     dialect = NULL;
   17281              :   }
   17282              : 
   17283              : private:
   17284              :   static const char *get_dialect ();
   17285              :   static char *dialect;
   17286              : };
   17287              : 
   17288              : char *module_state_config::dialect;
   17289              : 
   17290              : /* Generate a string of the significant compilation options.
   17291              :    Generally assume the user knows what they're doing, in the same way
   17292              :    that object files can be mixed.  */
   17293              : 
   17294              : const char *
   17295         6070 : module_state_config::get_dialect ()
   17296              : {
   17297         6070 :   if (!dialect)
   17298         9634 :     dialect = concat (get_cxx_dialect_name (cxx_dialect),
   17299              :                       /* C++ implies these, only show if disabled.  */
   17300         4817 :                       flag_exceptions ? "" : "/no-exceptions",
   17301         4817 :                       flag_rtti ? "" : "/no-rtti",
   17302         4817 :                       flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
   17303              :                       /* C++ 20 implies concepts and coroutines.  */
   17304         1505 :                       cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
   17305         4817 :                       (cxx_dialect < cxx20 && flag_coroutines
   17306              :                        ? "/coroutines" : ""),
   17307         4817 :                       flag_module_implicit_inline ? "/implicit-inline" : "",
   17308         4817 :                       flag_contracts ? "/contracts" : "",
   17309         4817 :                       flag_reflection ? "/reflection" : "",
   17310              :                       NULL);
   17311              : 
   17312         6070 :   return dialect;
   17313              : }
   17314              : 
   17315              : /* Contents of a cluster.  */
   17316              : enum cluster_tag {
   17317              :   ct_decl,      /* A decl.  */
   17318              :   ct_defn,      /* A definition.  */
   17319              :   ct_bind,      /* A binding.  */
   17320              :   ct_hwm
   17321              : };
   17322              : 
   17323              : /* Binding modifiers.  */
   17324              : enum ct_bind_flags
   17325              : {
   17326              :   cbf_export = 0x1,     /* An exported decl.  */
   17327              :   cbf_hidden = 0x2,     /* A hidden (friend) decl.  */
   17328              :   cbf_using = 0x4,      /* A using decl.  */
   17329              :   cbf_internal = 0x8,   /* A TU-local decl.  */
   17330              : };
   17331              : 
   17332              : /* DEP belongs to a different cluster, seed it to prevent
   17333              :    unfortunately timed duplicate import.  */
   17334              : // FIXME: QOI For inter-cluster references we could just only pick
   17335              : // one entity from an earlier cluster.  Even better track
   17336              : // dependencies between earlier clusters
   17337              : 
   17338              : void
   17339      9967251 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
   17340              : {
   17341      9967251 :   if (dep->is_tu_local ())
   17342              :     /* We only stream placeholders for TU-local entities anyway.  */;
   17343      9967042 :   else if (dep->is_import () || dep->cluster < index_hwm)
   17344              :     {
   17345      4296767 :       tree ent = dep->get_entity ();
   17346      4296767 :       if (!TREE_VISITED (ent))
   17347              :         {
   17348      1844203 :           sec.tree_node (ent);
   17349      1844653 :           dump (dumper::CLUSTER)
   17350          450 :             && dump ("Seeded %s %N",
   17351          450 :                      dep->is_import () ? "import" : "intercluster", ent);
   17352              :         }
   17353              :     }
   17354      9967251 : }
   17355              : 
   17356              : /* Write the cluster of depsets in SCC[0-SIZE).
   17357              :    dep->section -> section number
   17358              :    dep->cluster -> entity number
   17359              :  */
   17360              : 
   17361              : unsigned
   17362       337908 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
   17363              :                              depset::hash &table, unsigned *counts,
   17364              :                              unsigned *crc_ptr)
   17365              : {
   17366       339442 :   dump () && dump ("Writing section:%u %u depsets", table.section, size);
   17367       337908 :   dump.indent ();
   17368              : 
   17369       337908 :   trees_out sec (to, this, table, table.section);
   17370       337908 :   sec.begin ();
   17371       337908 :   unsigned index_lwm = counts[MSC_entities];
   17372              : 
   17373              :   /* Determine entity numbers, mark for writing.   */
   17374       338217 :   dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
   17375      1874796 :   for (unsigned ix = 0; ix != size; ix++)
   17376              :     {
   17377      1536888 :       depset *b = scc[ix];
   17378              : 
   17379      1536888 :       switch (b->get_entity_kind ())
   17380              :         {
   17381            0 :         default:
   17382            0 :           gcc_unreachable ();
   17383              : 
   17384       163748 :         case depset::EK_BINDING:
   17385       163748 :           {
   17386       163748 :             dump (dumper::CLUSTER)
   17387          210 :               && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
   17388              :                        b->get_entity (), b->get_name ());
   17389       163748 :             depset *ns_dep = b->deps[0];
   17390       163748 :             gcc_checking_assert (ns_dep->get_entity_kind ()
   17391              :                                  == depset::EK_NAMESPACE
   17392              :                                  && ns_dep->get_entity () == b->get_entity ());
   17393       395669 :             for (unsigned jx = b->deps.length (); --jx;)
   17394              :               {
   17395       231921 :                 depset *dep = b->deps[jx];
   17396              :                 // We could be declaring something that is also a
   17397              :                 // (merged) import
   17398       273583 :                 gcc_checking_assert (dep->is_import ()
   17399              :                                      || TREE_VISITED (dep->get_entity ())
   17400              :                                      || (dep->get_entity_kind ()
   17401              :                                          == depset::EK_USING)
   17402              :                                      || (dep->get_entity_kind ()
   17403              :                                          == depset::EK_TU_LOCAL));
   17404              :               }
   17405              :           }
   17406              :           break;
   17407              : 
   17408      1331526 :         case depset::EK_DECL:
   17409      1331526 :         case depset::EK_SPECIALIZATION:
   17410      1331526 :         case depset::EK_PARTIAL:
   17411      1331526 :           b->cluster = counts[MSC_entities]++;
   17412      1331526 :           sec.mark_declaration (b->get_entity (), b->has_defn ());
   17413              :           /* FALLTHROUGH  */
   17414              : 
   17415      1373140 :         case depset::EK_USING:
   17416      1373140 :         case depset::EK_TU_LOCAL:
   17417      2746280 :           gcc_checking_assert (!b->is_import ()
   17418              :                                && !b->is_unreached ());
   17419      1540062 :           dump (dumper::CLUSTER)
   17420         1161 :             && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
   17421          729 :                      b->has_defn () ? "definition" : "declaration",
   17422              :                      b->get_entity ());
   17423              :           break;
   17424              :         }
   17425              :     }
   17426       338217 :   dump (dumper::CLUSTER) && (dump.outdent (), true);
   17427              : 
   17428              :   /* Ensure every out-of-cluster decl is referenced before we start
   17429              :      streaming.  We must do both imports *and* earlier clusters,
   17430              :      because the latter could reach into the former and cause a
   17431              :      duplicate loop.   */
   17432       337908 :   sec.set_importing (+1);
   17433      1874796 :   for (unsigned ix = 0; ix != size; ix++)
   17434              :     {
   17435      1536888 :       depset *b = scc[ix];
   17436     19780732 :       for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
   17437              :         {
   17438      8357918 :           depset *dep = b->deps[jx];
   17439              : 
   17440      8357918 :           if (dep->is_binding ())
   17441              :             {
   17442      2073079 :               for (unsigned ix = dep->deps.length (); --ix;)
   17443              :                 {
   17444      1609333 :                   depset *bind = dep->deps[ix];
   17445      1609333 :                   if (bind->get_entity_kind () == depset::EK_USING)
   17446       576718 :                     bind = bind->deps[1];
   17447              : 
   17448      1609333 :                   intercluster_seed (sec, index_lwm, bind);
   17449              :                 }
   17450              :               /* Also check the namespace itself.  */
   17451       231873 :               dep = dep->deps[0];
   17452              :             }
   17453              : 
   17454      8357918 :           intercluster_seed (sec, index_lwm, dep);
   17455              :         }
   17456              :     }
   17457       337908 :   sec.tree_node (NULL_TREE);
   17458              :   /* We're done importing now.  */
   17459       337908 :   sec.set_importing (-1);
   17460              : 
   17461              :   /* Write non-definitions.  */
   17462      1874796 :   for (unsigned ix = 0; ix != size; ix++)
   17463              :     {
   17464      1536888 :       depset *b = scc[ix];
   17465      1536888 :       tree decl = b->get_entity ();
   17466      1536888 :       switch (b->get_entity_kind ())
   17467              :         {
   17468            0 :         default:
   17469            0 :           gcc_unreachable ();
   17470       163748 :           break;
   17471              : 
   17472       163748 :         case depset::EK_BINDING:
   17473       163748 :           {
   17474       163748 :             gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
   17475       164946 :             dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
   17476              :                              decl, b->get_name ());
   17477       163748 :             sec.u (ct_bind);
   17478       163748 :             sec.tree_node (decl);
   17479       163748 :             sec.tree_node (b->get_name ());
   17480              : 
   17481              :             /* Write in reverse order, so reading will see the exports
   17482              :                first, thus building the overload chain will be
   17483              :                optimized.  */
   17484       559417 :             for (unsigned jx = b->deps.length (); --jx;)
   17485              :               {
   17486       231921 :                 depset *dep = b->deps[jx];
   17487       231921 :                 tree bound = dep->get_entity ();
   17488       231921 :                 unsigned flags = 0;
   17489       231921 :                 if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
   17490              :                   flags |= cbf_internal;
   17491       231873 :                 else if (dep->get_entity_kind () == depset::EK_USING)
   17492              :                   {
   17493        41614 :                     tree ovl = bound;
   17494        41614 :                     bound = OVL_FUNCTION (bound);
   17495        41614 :                     if (!(TREE_CODE (bound) == CONST_DECL
   17496         5062 :                           && UNSCOPED_ENUM_P (TREE_TYPE (bound))
   17497         4220 :                           && decl == TYPE_NAME (TREE_TYPE (bound))))
   17498              :                       /* An unscoped enumerator in its enumeration's
   17499              :                          scope is not a using.  */
   17500              :                       flags |= cbf_using;
   17501        41614 :                     if (OVL_EXPORT_P (ovl))
   17502        40417 :                       flags |= cbf_export;
   17503              :                   }
   17504              :                 else
   17505              :                   {
   17506              :                     /* An implicit typedef must be at one.  */
   17507       190259 :                     gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
   17508       190259 :                     if (dep->is_hidden ())
   17509              :                       flags |= cbf_hidden;
   17510       190170 :                     else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
   17511       144719 :                       flags |= cbf_export;
   17512              :                   }
   17513              : 
   17514       231921 :                 gcc_checking_assert (DECL_P (bound));
   17515              : 
   17516       231921 :                 sec.i (flags);
   17517       231921 :                 if (flags & cbf_internal)
   17518              :                   {
   17519           48 :                     sec.tree_node (name_for_tu_local_decl (bound));
   17520           48 :                     write_location (sec, DECL_SOURCE_LOCATION (bound));
   17521              :                   }
   17522              :                 else
   17523       231873 :                   sec.tree_node (bound);
   17524              :               }
   17525              : 
   17526              :             /* Terminate the list.  */
   17527       163748 :             sec.i (-1);
   17528              :           }
   17529       163748 :           break;
   17530              : 
   17531        41614 :         case depset::EK_USING:
   17532        41614 :         case depset::EK_TU_LOCAL:
   17533        41641 :           dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
   17534           27 :                            TREE_CODE (decl), decl);
   17535              :           break;
   17536              : 
   17537      1331526 :         case depset::EK_SPECIALIZATION:
   17538      1331526 :         case depset::EK_PARTIAL:
   17539      1331526 :         case depset::EK_DECL:
   17540      1334673 :           dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
   17541              :                            b->entity_kind_name (), b->cluster,
   17542         3147 :                            TREE_CODE (decl), decl);
   17543              : 
   17544      1331526 :           sec.u (ct_decl);
   17545      1331526 :           sec.tree_node (decl);
   17546              : 
   17547      1540035 :           dump () && dump ("Wrote declaration entity:%u %C:%N",
   17548         3147 :                            b->cluster, TREE_CODE (decl), decl);
   17549              :           break;
   17550              :         }
   17551              :     }
   17552              : 
   17553              :   depset *namer = NULL;
   17554              : 
   17555              :   /* Write out definitions  */
   17556      1874796 :   for (unsigned ix = 0; ix != size; ix++)
   17557              :     {
   17558      1536888 :       depset *b = scc[ix];
   17559      1536888 :       tree decl = b->get_entity ();
   17560      1536888 :       switch (b->get_entity_kind ())
   17561              :         {
   17562              :         default:
   17563              :           break;
   17564              : 
   17565      1331526 :         case depset::EK_SPECIALIZATION:
   17566      1331526 :         case depset::EK_PARTIAL:
   17567      1331526 :         case depset::EK_DECL:
   17568      1331526 :           if (!namer)
   17569       323786 :             namer = b;
   17570              : 
   17571      1331526 :           if (b->has_defn ())
   17572              :             {
   17573       513511 :               sec.u (ct_defn);
   17574       513511 :               sec.tree_node (decl);
   17575       514454 :               dump () && dump ("Writing definition %N", decl);
   17576       513511 :               sec.write_definition (decl, b->refs_tu_local ());
   17577              : 
   17578       513511 :               if (!namer->has_defn ())
   17579      1536888 :                 namer = b;
   17580              :             }
   17581              :           break;
   17582              :         }
   17583              :     }
   17584              : 
   17585              :   /* We don't find the section by name.  Use depset's decl's name for
   17586              :      human friendliness.  */
   17587       337908 :   unsigned name = 0;
   17588       337908 :   tree naming_decl = NULL_TREE;
   17589       337908 :   if (namer)
   17590              :     {
   17591       323786 :       naming_decl = namer->get_entity ();
   17592       323786 :       if (namer->get_entity_kind () == depset::EK_USING)
   17593              :         /* This unfortunately names the section from the target of the
   17594              :            using decl.  But the name is only a guide, so Do Not Care.  */
   17595            0 :         naming_decl = OVL_FUNCTION (naming_decl);
   17596       323786 :       if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
   17597              :         /* Lose any anonymousness.  */
   17598       114982 :         naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
   17599       323786 :       name = to->qualified_name (naming_decl, namer->has_defn ());
   17600              :     }
   17601              : 
   17602       337908 :   unsigned bytes = sec.pos;
   17603       337908 :   unsigned snum = sec.end (to, name, crc_ptr);
   17604              : 
   17605      2212704 :   for (unsigned ix = size; ix--;)
   17606      1536888 :     gcc_checking_assert (scc[ix]->section == snum);
   17607              : 
   17608       337908 :   dump.outdent ();
   17609       339442 :   dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
   17610              : 
   17611       337908 :   return bytes;
   17612       337908 : }
   17613              : 
   17614              : /* Read a cluster from section SNUM.  */
   17615              : 
   17616              : bool
   17617       215863 : module_state::read_cluster (unsigned snum)
   17618              : {
   17619       215863 :   trees_in sec (this);
   17620              : 
   17621       215863 :   if (!sec.begin (loc, from (), snum))
   17622              :     return false;
   17623              : 
   17624       217181 :   dump () && dump ("Reading section:%u", snum);
   17625       215863 :   dump.indent ();
   17626              : 
   17627              :   /* We care about structural equality.  */
   17628       215863 :   comparing_dependent_aliases++;
   17629              : 
   17630              :   /* First seed the imports.  */
   17631      1347183 :   while (tree import = sec.tree_node ())
   17632      1132945 :     dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
   17633              : 
   17634      1552565 :   while (!sec.get_overrun () && sec.more_p ())
   17635              :     {
   17636      1336702 :       unsigned ct = sec.u ();
   17637      1336702 :       switch (ct)
   17638              :         {
   17639            0 :         default:
   17640            0 :           sec.set_overrun ();
   17641            0 :           break;
   17642              : 
   17643       107042 :         case ct_bind:
   17644              :           /* A set of namespace bindings.  */
   17645       107042 :           {
   17646       107042 :             tree ns = sec.tree_node ();
   17647       107042 :             tree name = sec.tree_node ();
   17648       107042 :             tree decls = NULL_TREE;
   17649       107042 :             tree visible = NULL_TREE;
   17650       107042 :             tree internal = NULL_TREE;
   17651       107042 :             tree type = NULL_TREE;
   17652       107042 :             bool dedup = false;
   17653       107042 :             bool global_p = is_header ();
   17654              : 
   17655              :             /* We rely on the bindings being in the reverse order of
   17656              :                the resulting overload set.  */
   17657       256540 :             for (;;)
   17658              :               {
   17659       256540 :                 int flags = sec.i ();
   17660       256540 :                 if (flags < 0)
   17661              :                   break;
   17662              : 
   17663       149498 :                 if ((flags & cbf_hidden)
   17664           56 :                     && (flags & (cbf_using | cbf_export)))
   17665            0 :                   sec.set_overrun ();
   17666       149498 :                 if ((flags & cbf_internal)
   17667           15 :                     && flags != cbf_internal)
   17668            0 :                   sec.set_overrun ();
   17669              : 
   17670            0 :                 if (flags & cbf_internal)
   17671              :                   {
   17672           15 :                     tree name = sec.tree_node ();
   17673           15 :                     location_t loc = read_location (sec);
   17674           15 :                     if (sec.get_overrun ())
   17675              :                       break;
   17676              : 
   17677           15 :                     tree decl = make_node (TU_LOCAL_ENTITY);
   17678           15 :                     TU_LOCAL_ENTITY_NAME (decl) = name;
   17679           15 :                     TU_LOCAL_ENTITY_LOCATION (decl) = loc;
   17680           15 :                     internal = tree_cons (NULL_TREE, decl, internal);
   17681           15 :                     continue;
   17682           15 :                   }
   17683              : 
   17684       149483 :                 tree decl = sec.tree_node ();
   17685       149483 :                 if (sec.get_overrun ())
   17686              :                   break;
   17687              : 
   17688       149483 :                 if (!global_p)
   17689              :                   {
   17690              :                     /* Check if the decl could require GM merging.  */
   17691        17214 :                     tree orig = get_originating_module_decl (decl);
   17692        17214 :                     tree inner = STRIP_TEMPLATE (orig);
   17693        17214 :                     if (!DECL_LANG_SPECIFIC (inner)
   17694        34428 :                         || !DECL_MODULE_ATTACH_P (inner))
   17695              :                       global_p = true;
   17696              :                   }
   17697              : 
   17698       149483 :                 if (decls && TREE_CODE (decl) == TYPE_DECL)
   17699              :                   {
   17700              :                     /* Stat hack.  */
   17701           54 :                     if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
   17702            0 :                       sec.set_overrun ();
   17703              : 
   17704           54 :                     if (flags & cbf_using)
   17705              :                       {
   17706            3 :                         type = build_lang_decl_loc (UNKNOWN_LOCATION,
   17707              :                                                     USING_DECL,
   17708            3 :                                                     DECL_NAME (decl),
   17709              :                                                     NULL_TREE);
   17710            3 :                         USING_DECL_DECLS (type) = decl;
   17711            3 :                         USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
   17712            3 :                         DECL_CONTEXT (type) = ns;
   17713              : 
   17714            3 :                         DECL_MODULE_PURVIEW_P (type) = true;
   17715            3 :                         if (flags & cbf_export)
   17716            3 :                           DECL_MODULE_EXPORT_P (type) = true;
   17717              :                       }
   17718              :                     else
   17719              :                       type = decl;
   17720              :                   }
   17721              :                 else
   17722              :                   {
   17723       149429 :                     if ((flags & cbf_using) &&
   17724        21878 :                         !DECL_DECLARES_FUNCTION_P (decl))
   17725              :                       {
   17726              :                         /* We should only see a single non-function using-decl
   17727              :                            for a binding; more than that would clash.  */
   17728         4684 :                         if (decls)
   17729            0 :                           sec.set_overrun ();
   17730              : 
   17731              :                         /* FIXME: Propagate the location of the using-decl
   17732              :                            for use in diagnostics.  */
   17733         4684 :                         decls = build_lang_decl_loc (UNKNOWN_LOCATION,
   17734              :                                                      USING_DECL,
   17735         4684 :                                                      DECL_NAME (decl),
   17736              :                                                      NULL_TREE);
   17737         4684 :                         USING_DECL_DECLS (decls) = decl;
   17738              :                         /* We don't currently record the actual scope of the
   17739              :                            using-declaration, but this approximation should
   17740              :                            generally be good enough.  */
   17741         4684 :                         USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
   17742         4684 :                         DECL_CONTEXT (decls) = ns;
   17743              : 
   17744         4684 :                         DECL_MODULE_PURVIEW_P (decls) = true;
   17745         4684 :                         if (flags & cbf_export)
   17746         4562 :                           DECL_MODULE_EXPORT_P (decls) = true;
   17747              :                       }
   17748       144745 :                     else if (decls
   17749       102343 :                              || (flags & (cbf_hidden | cbf_using))
   17750       238968 :                              || DECL_FUNCTION_TEMPLATE_P (decl))
   17751              :                       {
   17752        66182 :                         decls = ovl_make (decl, decls);
   17753        66182 :                         if (flags & cbf_using)
   17754              :                           {
   17755        17194 :                             dedup = true;
   17756        17194 :                             OVL_USING_P (decls) = true;
   17757        17194 :                             OVL_PURVIEW_P (decls) = true;
   17758        17194 :                             if (flags & cbf_export)
   17759        17179 :                               OVL_EXPORT_P (decls) = true;
   17760              :                           }
   17761              : 
   17762        66182 :                         if (flags & cbf_hidden)
   17763           56 :                           OVL_HIDDEN_P (decls) = true;
   17764        66126 :                         else if (dedup)
   17765        25210 :                           OVL_DEDUP_P (decls) = true;
   17766              :                       }
   17767              :                     else
   17768              :                       decls = decl;
   17769              : 
   17770       149307 :                     if (flags & cbf_export
   17771       149429 :                         || (!(flags & cbf_hidden)
   17772        18471 :                             && (is_module () || is_partition ())))
   17773       149498 :                       visible = decls;
   17774              :                   }
   17775              :               }
   17776              : 
   17777       107042 :             if (!decls && !internal)
   17778            0 :               sec.set_overrun ();
   17779              : 
   17780       107042 :             if (sec.get_overrun ())
   17781              :               break; /* Bail.  */
   17782              : 
   17783       107954 :             dump () && dump ("Binding of %P", ns, name);
   17784       107042 :             if (!set_module_binding (ns, name, mod, global_p,
   17785       107042 :                                      is_module () || is_partition (),
   17786              :                                      decls, type, visible, internal))
   17787            0 :               sec.set_overrun ();
   17788              :           }
   17789              :           break;
   17790              : 
   17791       886523 :         case ct_decl:
   17792              :           /* A decl.  */
   17793       886523 :           {
   17794       886523 :             tree decl = sec.tree_node ();
   17795      1340510 :             dump () && dump ("Read declaration of %N", decl);
   17796              :           }
   17797              :           break;
   17798              : 
   17799       343137 :         case ct_defn:
   17800       343137 :           {
   17801       343137 :             tree decl = sec.tree_node ();
   17802       344477 :             dump () && dump ("Reading definition of %N", decl);
   17803       343137 :             sec.read_definition (decl);
   17804              :           }
   17805       343137 :           break;
   17806              :         }
   17807              :     }
   17808              : 
   17809              :   /* When lazy loading is in effect, we can be in the middle of
   17810              :      parsing or instantiating a function.  Save it away.
   17811              :      push_function_context does too much work.   */
   17812       215863 :   tree old_cfd = current_function_decl;
   17813       215863 :   struct function *old_cfun = cfun;
   17814       405759 :   for (const post_process_data& pdata : sec.post_process ())
   17815              :     {
   17816       141758 :       tree decl = pdata.decl;
   17817              : 
   17818       141758 :       bool abstract = false;
   17819       141758 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   17820              :         {
   17821        87259 :           abstract = true;
   17822        87259 :           decl = DECL_TEMPLATE_RESULT (decl);
   17823              :         }
   17824              : 
   17825       141758 :       current_function_decl = decl;
   17826       141758 :       allocate_struct_function (decl, abstract);
   17827       141758 :       cfun->language = ggc_cleared_alloc<language_function> ();
   17828       141758 :       cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
   17829       141758 :       cfun->function_start_locus = pdata.start_locus;
   17830       141758 :       cfun->function_end_locus = pdata.end_locus;
   17831       141758 :       cfun->language->returns_value = pdata.returns_value;
   17832       141758 :       cfun->language->returns_null = pdata.returns_null;
   17833       141758 :       cfun->language->returns_abnormally = pdata.returns_abnormally;
   17834       141758 :       cfun->language->infinite_loop = pdata.infinite_loop;
   17835       141758 :       cfun->coroutine_component = DECL_COROUTINE_P (decl);
   17836              : 
   17837              :       /* Make sure we emit explicit instantiations.
   17838              :          FIXME do we want to do this in expand_or_defer_fn instead?  */
   17839       141758 :       if (DECL_EXPLICIT_INSTANTIATION (decl)
   17840       141758 :           && !DECL_EXTERNAL (decl))
   17841           27 :         setup_explicit_instantiation_definition_linkage (decl);
   17842              : 
   17843       141758 :       if (abstract)
   17844              :         ;
   17845        54499 :       else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
   17846         8959 :         vec_safe_push (post_load_decls, decl);
   17847              :       else
   17848              :         {
   17849        45540 :           bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
   17850              : #ifdef PCC_STATIC_STRUCT_RETURN
   17851              :           cfun->returns_pcc_struct = aggr;
   17852              : #endif
   17853        45540 :           cfun->returns_struct = aggr;
   17854        45540 :           expand_or_defer_fn (decl);
   17855              : 
   17856              :           /* If we first see this function after at_eof, it doesn't get
   17857              :              note_vague_linkage_fn from tentative_decl_linkage, so the loop in
   17858              :              c_parse_final_cleanups won't consider it.  But with DECL_COMDAT we
   17859              :              can just clear DECL_EXTERNAL and let cgraph decide.
   17860              :              FIXME handle this outside module.cc after GCC 15.  */
   17861         1884 :           if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   17862        45707 :               && DECL_NOT_REALLY_EXTERN (decl))
   17863          131 :             DECL_EXTERNAL (decl) = false;
   17864              :         }
   17865              : 
   17866              :     }
   17867       215931 :   for (const tree& type : sec.post_process_type ())
   17868              :     {
   17869              :       /* Attempt to complete an array type now in case its element type
   17870              :          had a definition streamed later in the cluster.  */
   17871           32 :       gcc_checking_assert (TREE_CODE (type) == ARRAY_TYPE);
   17872           32 :       complete_type (type);
   17873              :     }
   17874       215863 :   set_cfun (old_cfun);
   17875       215863 :   current_function_decl = old_cfd;
   17876       215863 :   comparing_dependent_aliases--;
   17877              : 
   17878       215863 :   dump.outdent ();
   17879       217181 :   dump () && dump ("Read section:%u", snum);
   17880              : 
   17881       215863 :   loaded_clusters++;
   17882              : 
   17883       215863 :   if (!sec.end (from ()))
   17884              :     return false;
   17885              : 
   17886              :   return true;
   17887       215863 : }
   17888              : 
   17889              : void
   17890       166816 : module_state::write_namespace (bytes_out &sec, depset *dep)
   17891              : {
   17892       166816 :   unsigned ns_num = dep->cluster;
   17893       166816 :   unsigned ns_import = 0;
   17894              : 
   17895       166816 :   if (dep->is_import ())
   17896            0 :     ns_import = dep->section;
   17897       166816 :   else if (dep->get_entity () != global_namespace)
   17898       110487 :     ns_num++;
   17899              : 
   17900       166816 :   sec.u (ns_import);
   17901       166816 :   sec.u (ns_num);
   17902       166816 : }
   17903              : 
   17904              : tree
   17905       175743 : module_state::read_namespace (bytes_in &sec)
   17906              : {
   17907       175743 :   unsigned ns_import = sec.u ();
   17908       175743 :   unsigned ns_num = sec.u ();
   17909       175743 :   tree ns = NULL_TREE;
   17910              : 
   17911       175743 :   if (ns_import || ns_num)
   17912              :     {
   17913       113719 :       if (!ns_import)
   17914       113719 :         ns_num--;
   17915              : 
   17916       113719 :       if (unsigned origin = slurp->remap_module (ns_import))
   17917              :         {
   17918       113719 :           module_state *from = (*modules)[origin];
   17919       113719 :           if (ns_num < from->entity_num)
   17920              :             {
   17921       113719 :               binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
   17922              : 
   17923       113719 :               if (!slot.is_lazy ())
   17924       113719 :                 ns = slot;
   17925              :             }
   17926              :         }
   17927              :       else
   17928            0 :         sec.set_overrun ();
   17929              :     }
   17930              :   else
   17931        62024 :     ns = global_namespace;
   17932              : 
   17933       175743 :   return ns;
   17934              : }
   17935              : 
   17936              : /* SPACES is a sorted vector of namespaces.  Write out the namespaces
   17937              :    to MOD_SNAME_PFX.nms section.   */
   17938              : 
   17939              : void
   17940          597 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
   17941              :                                 unsigned num, unsigned *crc_p)
   17942              : {
   17943          655 :   dump () && dump ("Writing namespaces");
   17944          597 :   dump.indent ();
   17945              : 
   17946          597 :   bytes_out sec (to);
   17947          597 :   sec.begin ();
   17948              : 
   17949         3940 :   for (unsigned ix = 0; ix != num; ix++)
   17950              :     {
   17951         2746 :       depset *b = spaces[ix];
   17952         2746 :       tree ns = b->get_entity ();
   17953              : 
   17954              :       /* This could be an anonymous namespace even for a named module,
   17955              :          since we can still emit no-linkage decls.  */
   17956         2746 :       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
   17957              : 
   17958         2746 :       unsigned flags = 0;
   17959         2746 :       if (TREE_PUBLIC (ns))
   17960         2685 :         flags |= 1;
   17961         2746 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17962          524 :         flags |= 2;
   17963         2746 :       if (DECL_MODULE_PURVIEW_P (ns))
   17964         2089 :         flags |= 4;
   17965         2746 :       if (DECL_MODULE_EXPORT_P (ns))
   17966         1866 :         flags |= 8;
   17967         2746 :       if (TREE_DEPRECATED (ns))
   17968           18 :         flags |= 16;
   17969              : 
   17970         2886 :       dump () && dump ("Writing namespace:%u %N%s%s%s%s",
   17971              :                        b->cluster, ns,
   17972          140 :                        flags & 1 ? ", public" : "",
   17973          140 :                        flags & 2 ? ", inline" : "",
   17974          140 :                        flags & 4 ? ", purview" : "",
   17975          140 :                        flags & 8 ? ", export" : "",
   17976          140 :                        flags & 16 ? ", deprecated" : "");
   17977         2746 :       sec.u (b->cluster);
   17978         2746 :       sec.u (to->name (DECL_NAME (ns)));
   17979         2746 :       write_namespace (sec, b->deps[0]);
   17980              : 
   17981         2746 :       sec.u (flags);
   17982         2746 :       write_location (sec, DECL_SOURCE_LOCATION (ns));
   17983              : 
   17984         2746 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17985              :         {
   17986          524 :           if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
   17987              :             {
   17988          131 :               tree tags = TREE_VALUE (attr);
   17989          131 :               sec.u (list_length (tags));
   17990          396 :               for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
   17991          134 :                 sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
   17992              :             }
   17993              :           else
   17994          393 :             sec.u (0);
   17995              :         }
   17996              :     }
   17997              : 
   17998          597 :   sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
   17999          597 :   dump.outdent ();
   18000          597 : }
   18001              : 
   18002              : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace.  Fill in
   18003              :    SPACES from that data.  */
   18004              : 
   18005              : bool
   18006          612 : module_state::read_namespaces (unsigned num)
   18007              : {
   18008          612 :   bytes_in sec;
   18009              : 
   18010          612 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
   18011              :     return false;
   18012              : 
   18013          685 :   dump () && dump ("Reading namespaces");
   18014          612 :   dump.indent ();
   18015              : 
   18016         3412 :   for (unsigned ix = 0; ix != num; ix++)
   18017              :     {
   18018         2800 :       unsigned entity_index = sec.u ();
   18019         2800 :       unsigned name = sec.u ();
   18020              : 
   18021         2800 :       tree parent = read_namespace (sec);
   18022              : 
   18023              :       /* See comment in write_namespace about why not bits.  */
   18024         2800 :       unsigned flags = sec.u ();
   18025         2800 :       location_t src_loc = read_location (sec);
   18026         2800 :       unsigned tags_count = (flags & 2) ? sec.u () : 0;
   18027              : 
   18028         2800 :       if (entity_index >= entity_num
   18029         2800 :           || !parent
   18030         2800 :           || (flags & 0xc) == 0x8)
   18031            0 :         sec.set_overrun ();
   18032              : 
   18033         2800 :       tree tags = NULL_TREE;
   18034         2939 :       while (tags_count--)
   18035              :         {
   18036          139 :           size_t len;
   18037          139 :           const char *str = sec.str (&len);
   18038          139 :           tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
   18039          139 :           tags = nreverse (tags);
   18040              :         }
   18041              : 
   18042         2800 :       if (sec.get_overrun ())
   18043              :         break;
   18044              : 
   18045         5536 :       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
   18046              : 
   18047         3020 :       dump () && dump ("Read namespace:%u %P%s%s%s%s",
   18048              :                        entity_index, parent, id,
   18049          110 :                        flags & 1 ? ", public" : "",
   18050              :                        flags & 2 ? ", inline" : "",
   18051          110 :                        flags & 4 ? ", purview" : "",
   18052          110 :                        flags & 8 ? ", export" : "",
   18053          110 :                        flags & 16 ? ", deprecated" : "");
   18054         2800 :       bool visible_p = ((flags & 8)
   18055         2800 :                         || ((flags & 1)
   18056          636 :                             && (flags & 4)
   18057          101 :                             && (is_partition () || is_module ())));
   18058         2800 :       tree inner = add_imported_namespace (parent, id, src_loc, mod,
   18059              :                                            bool (flags & 2), visible_p);
   18060         2800 :       if (!inner)
   18061              :         {
   18062            0 :           sec.set_overrun ();
   18063            0 :           break;
   18064              :         }
   18065              : 
   18066         2800 :       if (is_partition ())
   18067              :         {
   18068           54 :           if (flags & 4)
   18069           48 :             DECL_MODULE_PURVIEW_P (inner) = true;
   18070           54 :           if (flags & 8)
   18071           27 :             DECL_MODULE_EXPORT_P (inner) = true;
   18072              :         }
   18073              : 
   18074         2800 :       if (flags & 16)
   18075           16 :         TREE_DEPRECATED (inner) = true;
   18076              : 
   18077         2800 :       if (tags)
   18078          136 :         DECL_ATTRIBUTES (inner)
   18079          272 :           = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
   18080              : 
   18081              :       /* Install the namespace.  */
   18082         2800 :       (*entity_ary)[entity_lwm + entity_index] = inner;
   18083         2800 :       if (DECL_MODULE_IMPORT_P (inner))
   18084              :         {
   18085            0 :           bool existed;
   18086            0 :           unsigned *slot = &entity_map->get_or_insert
   18087            0 :             (DECL_UID (inner), &existed);
   18088            0 :           if (existed)
   18089              :             /* If it existed, it should match.  */
   18090            0 :             gcc_checking_assert (inner == (*entity_ary)[*slot]);
   18091              :           else
   18092            0 :             *slot = entity_lwm + entity_index;
   18093              :         }
   18094              :     }
   18095              : 
   18096          612 :   dump.outdent ();
   18097          612 :   if (!sec.end (from ()))
   18098              :     return false;
   18099              :   return true;
   18100          612 : }
   18101              : 
   18102              : unsigned
   18103          597 : module_state::write_using_directives (elf_out *to, depset::hash &table,
   18104              :                                       vec<depset *> spaces, unsigned *crc_p)
   18105              : {
   18106          655 :   dump () && dump ("Writing using-directives");
   18107          597 :   dump.indent ();
   18108              : 
   18109          597 :   bytes_out sec (to);
   18110          597 :   sec.begin ();
   18111              : 
   18112          597 :   unsigned num = 0;
   18113         3940 :   auto emit_one_ns = [&](depset *parent_dep)
   18114              :     {
   18115         3343 :       tree parent = parent_dep->get_entity ();
   18116         3889 :       for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
   18117              :         {
   18118          186 :           if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
   18119           19 :             continue;
   18120          167 :           bool exported = DECL_MODULE_EXPORT_P (udir);
   18121          167 :           tree target = USING_DECL_DECLS (udir);
   18122          167 :           depset *target_dep = table.find_dependency (target);
   18123              : 
   18124              :           /* An using-directive imported from a different module might not
   18125              :              have been walked earlier (PR c++/122915).  But importers will
   18126              :              be able to just refer to the decl in that module unless it was
   18127              :              a partition anyway, so we don't have anything to do here.  */
   18128          167 :           if (!target_dep)
   18129              :             {
   18130            6 :               gcc_checking_assert (DECL_MODULE_IMPORT_P (udir));
   18131            6 :               continue;
   18132              :             }
   18133              : 
   18134          185 :           dump () && dump ("Writing using-directive in %N for %N",
   18135              :                            parent, target);
   18136          161 :           sec.u (exported);
   18137          161 :           write_namespace (sec, parent_dep);
   18138          161 :           write_namespace (sec, target_dep);
   18139          161 :           ++num;
   18140              :         }
   18141         3940 :     };
   18142              : 
   18143          597 :   emit_one_ns (table.find_dependency (global_namespace));
   18144         4537 :   for (depset *parent_dep : spaces)
   18145         2746 :     emit_one_ns (parent_dep);
   18146              : 
   18147          597 :   sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
   18148          597 :   dump.outdent ();
   18149              : 
   18150          597 :   return num;
   18151          597 : }
   18152              : 
   18153              : bool
   18154          162 : module_state::read_using_directives (unsigned num)
   18155              : {
   18156          162 :   if (!bitmap_bit_p (this_module ()->imports, mod))
   18157              :     {
   18158           11 :       dump () && dump ("Ignoring using-directives because module %M "
   18159              :                        "is not visible in this TU", this);
   18160              :       return true;
   18161              :     }
   18162              : 
   18163          154 :   bytes_in sec;
   18164              : 
   18165          154 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
   18166              :     return false;
   18167              : 
   18168          166 :   dump () && dump ("Reading using-directives");
   18169          154 :   dump.indent ();
   18170              : 
   18171          335 :   for (unsigned ix = 0; ix != num; ++ix)
   18172              :     {
   18173          181 :       bool exported = sec.u ();
   18174          181 :       tree parent = read_namespace (sec);
   18175          181 :       tree target = read_namespace (sec);
   18176          181 :       if (sec.get_overrun ())
   18177              :         break;
   18178              : 
   18179          193 :       dump () && dump ("Read using-directive in %N for %N", parent, target);
   18180          181 :       if (exported || is_module () || is_partition ())
   18181          154 :         add_imported_using_namespace (parent, target);
   18182              :     }
   18183              : 
   18184          154 :   dump.outdent ();
   18185          154 :   if (!sec.end (from ()))
   18186              :     return false;
   18187              :   return true;
   18188          154 : }
   18189              : 
   18190              : /* Write the binding TABLE to MOD_SNAME_PFX.bnd   */
   18191              : 
   18192              : unsigned
   18193         2815 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
   18194              : {
   18195         3115 :   dump () && dump ("Writing binding table");
   18196         2815 :   dump.indent ();
   18197              : 
   18198         2815 :   unsigned num = 0;
   18199         2815 :   bytes_out sec (to);
   18200         2815 :   sec.begin ();
   18201              : 
   18202      3092823 :   for (unsigned ix = 0; ix != sccs.length (); ix++)
   18203              :     {
   18204      1542189 :       depset *b = sccs[ix];
   18205      1542189 :       if (b->is_binding ())
   18206              :         {
   18207       163748 :           tree ns = b->get_entity ();
   18208       164946 :           dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
   18209              :                            b->section);
   18210       163748 :           sec.u (to->name (b->get_name ()));
   18211       163748 :           write_namespace (sec, b->deps[0]);
   18212       163748 :           sec.u (b->section);
   18213       163748 :           num++;
   18214              :         }
   18215              :     }
   18216              : 
   18217         2815 :   sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
   18218         2815 :   dump.outdent ();
   18219              : 
   18220         2815 :   return num;
   18221         2815 : }
   18222              : 
   18223              : /* Read the binding table from MOD_SNAME_PFX.bind.  */
   18224              : 
   18225              : bool
   18226         2997 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
   18227              : {
   18228         2997 :   bytes_in sec;
   18229              : 
   18230         2997 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
   18231              :     return false;
   18232              : 
   18233         3529 :   dump () && dump ("Reading binding table");
   18234         2997 :   dump.indent ();
   18235       175578 :   for (; !sec.get_overrun () && num--;)
   18236              :     {
   18237       172581 :       const char *name = from ()->name (sec.u ());
   18238       172581 :       tree ns = read_namespace (sec);
   18239       172581 :       unsigned snum = sec.u ();
   18240              : 
   18241       172581 :       if (!ns || !name || (snum - lwm) >= (hwm - lwm))
   18242            0 :         sec.set_overrun ();
   18243       172581 :       if (!sec.get_overrun ())
   18244              :         {
   18245       172581 :           tree id = get_identifier (name);
   18246       174131 :           dump () && dump ("Bindings %P section:%u", ns, id, snum);
   18247       172581 :           if (mod && !import_module_binding (ns, id, mod, snum))
   18248              :             break;
   18249              :         }
   18250              :     }
   18251              : 
   18252         2997 :   dump.outdent ();
   18253         2997 :   if (!sec.end (from ()))
   18254              :     return false;
   18255              :   return true;
   18256         2997 : }
   18257              : 
   18258              : /* Write the entity table to MOD_SNAME_PFX.ent
   18259              : 
   18260              :    Each entry is a section number.  */
   18261              : 
   18262              : void
   18263         2549 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
   18264              :                               unsigned count, unsigned *crc_p)
   18265              : {
   18266         2796 :   dump () && dump ("Writing entities");
   18267         2549 :   dump.indent ();
   18268              : 
   18269         2549 :   bytes_out sec (to);
   18270         2549 :   sec.begin ();
   18271              : 
   18272         2549 :   unsigned current = 0;
   18273      1547266 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18274              :     {
   18275      1542168 :       depset *d = depsets[ix];
   18276              : 
   18277      2878989 :       switch (d->get_entity_kind ())
   18278              :         {
   18279              :         default:
   18280              :           break;
   18281              : 
   18282         5295 :         case depset::EK_NAMESPACE:
   18283         5295 :           if (!d->is_import () && d->get_entity () != global_namespace)
   18284              :             {
   18285         2746 :               gcc_checking_assert (d->cluster == current);
   18286         2746 :               current++;
   18287         2746 :               sec.u (0);
   18288              :             }
   18289              :           break;
   18290              : 
   18291      1331526 :         case depset::EK_DECL:
   18292      1331526 :         case depset::EK_SPECIALIZATION:
   18293      1331526 :         case depset::EK_PARTIAL:
   18294      2663052 :           gcc_checking_assert (!d->is_unreached ()
   18295              :                                && !d->is_import ()
   18296              :                                && d->cluster == current
   18297              :                                && d->section);
   18298      1331526 :           current++;
   18299      1331526 :           sec.u (d->section);
   18300      1331526 :           break;
   18301              :         }
   18302              :     }
   18303         2549 :   gcc_assert (count == current);
   18304         2549 :   sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
   18305         2549 :   dump.outdent ();
   18306         2549 : }
   18307              : 
   18308              : bool
   18309         2768 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
   18310              : {
   18311         2768 :   trees_in sec (this);
   18312              : 
   18313         2768 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
   18314              :     return false;
   18315              : 
   18316         3246 :   dump () && dump ("Reading entities");
   18317         2768 :   dump.indent ();
   18318              : 
   18319      1277558 :   for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
   18320              :     {
   18321      1274790 :       unsigned snum = sec.u ();
   18322      1274790 :       if (snum && (snum - lwm) >= (hwm - lwm))
   18323            0 :         sec.set_overrun ();
   18324      1274790 :       if (sec.get_overrun ())
   18325              :         break;
   18326              : 
   18327      1274790 :       if (snum)
   18328      1271990 :         slot->set_lazy (snum << 2);
   18329              :     }
   18330              : 
   18331         2768 :   dump.outdent ();
   18332         2768 :   if (!sec.end (from ()))
   18333              :     return false;
   18334              :   return true;
   18335         2768 : }
   18336              : 
   18337              : /* Write the pending table to MOD_SNAME_PFX.pnd
   18338              : 
   18339              :    The pending table holds information about clusters that need to be
   18340              :    loaded because they contain information about something that is not
   18341              :    found by namespace-scope lookup.
   18342              : 
   18343              :    The three cases are:
   18344              : 
   18345              :    (a) Template (maybe-partial) specializations that we have
   18346              :    instantiated or defined.  When an importer needs to instantiate
   18347              :    that template, they /must have/ the partial, explicit & extern
   18348              :    specializations available.  If they have the other specializations
   18349              :    available, they'll have less work to do.  Thus, when we're about to
   18350              :    instantiate FOO, we have to be able to ask 'are there any
   18351              :    specialization of FOO in our imports?'.
   18352              : 
   18353              :    (b) (Maybe-implicit) member functions definitions.  A class could
   18354              :    be defined in one header, and an inline member defined in a
   18355              :    different header (this occurs in the STL).  Similarly, like the
   18356              :    specialization case, an implicit member function could have been
   18357              :    'instantiated' in one module, and it'd be nice to not have to
   18358              :    reinstantiate it in another.
   18359              : 
   18360              :    (c) Classes completed elsewhere.  A class could be declared in one
   18361              :    header and defined in another.  We need to know to load the class
   18362              :    definition before looking in it.  It does highlight an issue --
   18363              :    there could be an intermediate import between the outermost containing
   18364              :    namespace-scope class and the innermost being-defined class.  This is
   18365              :    actually possible with all of these cases, so be aware -- we're not
   18366              :    just talking of one level of import to get to the innermost namespace.
   18367              : 
   18368              :    This gets complicated fast, it took me multiple attempts to even
   18369              :    get something remotely working.  Partially because I focussed on
   18370              :    optimizing what I think turns out to be a smaller problem, given
   18371              :    the known need to do the more general case *anyway*.  I document
   18372              :    the smaller problem, because it does appear to be the natural way
   18373              :    to do it.  It's trap!
   18374              : 
   18375              :    **** THE TRAP
   18376              : 
   18377              :    Let's refer to the primary template or the containing class as the
   18378              :    KEY.  And the specialization or member as the PENDING-ENTITY.  (To
   18379              :    avoid having to say those mouthfuls all the time.)
   18380              : 
   18381              :    In either case, we have an entity and we need some way of mapping
   18382              :    that to a set of entities that need to be loaded before we can
   18383              :    proceed with whatever processing of the entity we were going to do.
   18384              : 
   18385              :    We need to link the key to the pending-entity in some way.  Given a
   18386              :    key, tell me the pending-entities I need to have loaded.  However
   18387              :    we tie the key to the pending-entity must not rely on the key being
   18388              :    loaded -- that'd defeat the lazy loading scheme.
   18389              : 
   18390              :    As the key will be an import in we know its entity number (either
   18391              :    because we imported it, or we're writing it out too).  Thus we can
   18392              :    generate a map of key-indices to pending-entities.  The
   18393              :    pending-entity indices will be into our span of the entity table,
   18394              :    and thus allow them to be lazily loaded.  The key index will be
   18395              :    into another slot of the entity table.  Notice that this checking
   18396              :    could be expensive, we don't want to iterate over a bunch of
   18397              :    pending-entity indices (across multiple imports), every time we're
   18398              :    about do to the thing with the key.  We need to quickly determine
   18399              :    'definitely nothing needed'.
   18400              : 
   18401              :    That's almost good enough, except that key indices are not unique
   18402              :    in a couple of cases :( Specifically the Global Module or a module
   18403              :    partition can result in multiple modules assigning an entity index
   18404              :    for the key.  The decl-merging on loading will detect that so we
   18405              :    only have one Key loaded, and in the entity hash it'll indicate the
   18406              :    entity index of first load.  Which might be different to how we
   18407              :    know it.  Notice this is restricted to GM entities or this-module
   18408              :    entities.  Foreign imports cannot have this.
   18409              : 
   18410              :    We can simply resolve this in the direction of how this module
   18411              :    referred to the key to how the importer knows it.  Look in the
   18412              :    entity table slot that we nominate, maybe lazy load it, and then
   18413              :    lookup the resultant entity in the entity hash to learn how the
   18414              :    importer knows it.
   18415              : 
   18416              :    But we need to go in the other direction :( Given the key, find all
   18417              :    the index-aliases of that key.  We can partially solve that by
   18418              :    adding an alias hash table.  Whenever we load a merged decl, add or
   18419              :    augment a mapping from the entity (or its entity-index) to the
   18420              :    newly-discovered index.  Then when we look for pending entities of
   18421              :    a key, we also iterate over this aliases this mapping provides.
   18422              : 
   18423              :    But that requires the alias to be loaded.  And that's not
   18424              :    necessarily true.
   18425              : 
   18426              :    *** THE SIMPLER WAY
   18427              : 
   18428              :    The remaining fixed thing we have is the innermost namespace
   18429              :    containing the ultimate namespace-scope container of the key and
   18430              :    the name of that container (which might be the key itself).  I.e. a
   18431              :    namespace-decl/identifier/module tuple.  Let's call this the
   18432              :    top-key.  We'll discover that the module is not important here,
   18433              :    because of cross-module possibilities mentioned in case #c above.
   18434              :    We can't markup namespace-binding slots.  The best we can do is
   18435              :    mark the binding vector with 'there's something here', and have
   18436              :    another map from namespace/identifier pairs to a vector of pending
   18437              :    entity indices.
   18438              : 
   18439              :    Maintain a pending-entity map.  This is keyed by top-key, and
   18440              :    maps to a vector of pending-entity indices.  On the binding vector
   18441              :    have flags saying whether the pending-name-entity map has contents.
   18442              :    (We might want to further extend the key to be GM-vs-Partition and
   18443              :    specialization-vs-member, but let's not get ahead of ourselves.)
   18444              : 
   18445              :    For every key-like entity, find the outermost namespace-scope
   18446              :    name.  Use that to lookup in the pending-entity map and then make
   18447              :    sure the specified entities are loaded.
   18448              : 
   18449              :    An optimization might be to have a flag in each key-entity saying
   18450              :    that its top key might be in the entity table.  It's not clear to
   18451              :    me how to set that flag cheaply -- cheaper than just looking.
   18452              : 
   18453              :    FIXME: It'd be nice to have a bit in decls to tell us whether to
   18454              :    even try this.  We can have a 'already done' flag, that we set when
   18455              :    we've done KLASS's lazy pendings.  When we import a module that
   18456              :    registers pendings on the same top-key as KLASS we need to clear
   18457              :    the flag.  A recursive walk of the top-key clearing the bit will
   18458              :    suffice.  Plus we only need to recurse on classes that have the bit
   18459              :    set.  (That means we need to set the bit on parents of KLASS here,
   18460              :    don't forget.)  However, first: correctness, second: efficiency.  */
   18461              : 
   18462              : unsigned
   18463         2815 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
   18464              :                               depset::hash &table, unsigned *crc_p)
   18465              : {
   18466         3115 :   dump () && dump ("Writing pending-entities");
   18467         2815 :   dump.indent ();
   18468              : 
   18469         2815 :   trees_out sec (to, this, table);
   18470         2815 :   sec.begin ();
   18471              : 
   18472         2815 :   unsigned count = 0;
   18473         2815 :   tree cache_ns = NULL_TREE;
   18474         2815 :   tree cache_id = NULL_TREE;
   18475         2815 :   unsigned cache_section = ~0;
   18476      1547819 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18477              :     {
   18478      1542189 :       depset *d = depsets[ix];
   18479              : 
   18480      1542189 :       if (d->is_binding ())
   18481       920028 :         continue;
   18482              : 
   18483      1378441 :       if (d->is_import ())
   18484            0 :         continue;
   18485              : 
   18486      1378441 :       if (!d->is_pending_entity ())
   18487       756280 :         continue;
   18488              : 
   18489       622161 :       tree key_decl = nullptr;
   18490       622161 :       tree key_ns = find_pending_key (d->get_entity (), &key_decl);
   18491       622161 :       tree key_name = DECL_NAME (key_decl);
   18492              : 
   18493       622161 :       if (IDENTIFIER_ANON_P (key_name))
   18494              :         {
   18495            6 :           gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
   18496           12 :           if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
   18497            6 :             key_name = DECL_NAME (attached);
   18498              :           else
   18499              :             {
   18500              :               /* There's nothing to attach it to.  Must
   18501              :                  always reinstantiate.  */
   18502            0 :               dump ()
   18503            0 :                 && dump ("Unattached lambda %N[%u] section:%u",
   18504            0 :                          d->get_entity_kind () == depset::EK_DECL
   18505              :                          ? "Member" : "Specialization", d->get_entity (),
   18506              :                          d->cluster, d->section);
   18507            0 :               continue;
   18508              :             }
   18509              :         }
   18510              : 
   18511       622161 :       char const *also = "";
   18512       622161 :       if (d->section == cache_section
   18513       417321 :           && key_ns == cache_ns
   18514       417321 :           && key_name == cache_id)
   18515              :         /* Same section & key as previous, no need to repeat ourselves.  */
   18516              :         also = "also ";
   18517              :       else
   18518              :         {
   18519       279148 :           cache_ns = key_ns;
   18520       279148 :           cache_id = key_name;
   18521       279148 :           cache_section = d->section;
   18522       279148 :           gcc_checking_assert (table.find_dependency (cache_ns));
   18523       279148 :           sec.tree_node (cache_ns);
   18524       279148 :           sec.tree_node (cache_id);
   18525       279148 :           sec.u (d->cluster);
   18526       279148 :           count++;
   18527              :         }
   18528       623617 :       dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
   18529          728 :                        d->get_entity_kind () == depset::EK_DECL
   18530              :                        ? "member" : "specialization", d->get_entity (),
   18531              :                        d->cluster, cache_section, also, cache_ns, cache_id);
   18532              :       }
   18533         2815 :   sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
   18534         2815 :   dump.outdent ();
   18535              : 
   18536         2815 :   return count;
   18537         2815 : }
   18538              : 
   18539              : bool
   18540         1604 : module_state::read_pendings (unsigned count)
   18541              : {
   18542         1604 :   trees_in sec (this);
   18543              : 
   18544         1604 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
   18545              :     return false;
   18546              : 
   18547         1926 :   dump () && dump ("Reading %u pendings", count);
   18548         1604 :   dump.indent ();
   18549              : 
   18550       267812 :   for (unsigned ix = 0; ix != count; ix++)
   18551              :     {
   18552       266208 :       pending_key key;
   18553       266208 :       unsigned index;
   18554              : 
   18555       266208 :       key.ns = sec.tree_node ();
   18556       266208 :       key.id = sec.tree_node ();
   18557       266208 :       index = sec.u ();
   18558              : 
   18559       266208 :       if (!key.ns || !key.id
   18560       266208 :           || !(TREE_CODE (key.ns) == NAMESPACE_DECL
   18561       266208 :                && !DECL_NAMESPACE_ALIAS (key.ns))
   18562       266208 :           || !identifier_p (key.id)
   18563       532416 :           || index >= entity_num)
   18564            0 :         sec.set_overrun ();
   18565              : 
   18566       266208 :       if (sec.get_overrun ())
   18567              :         break;
   18568              : 
   18569       267017 :       dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
   18570              : 
   18571       266208 :       index += entity_lwm;
   18572       266208 :       auto &vec = pending_table->get_or_insert (key);
   18573       266208 :       vec.safe_push (index);
   18574              :     }
   18575              : 
   18576         1604 :   dump.outdent ();
   18577         1604 :   if (!sec.end (from ()))
   18578              :     return false;
   18579              :   return true;
   18580         1604 : }
   18581              : 
   18582              : /* Read & write locations.  */
   18583              : enum loc_kind {
   18584              :   LK_ORDINARY,
   18585              :   LK_MACRO,
   18586              :   LK_IMPORT_ORDINARY,
   18587              :   LK_IMPORT_MACRO,
   18588              :   LK_ADHOC,
   18589              :   LK_RESERVED,
   18590              : };
   18591              : 
   18592              : static const module_state *
   18593         4892 : module_for_ordinary_loc (location_t loc)
   18594              : {
   18595         4892 :   unsigned pos = 0;
   18596         4892 :   unsigned len = ool->length () - pos;
   18597              : 
   18598         9787 :   while (len)
   18599              :     {
   18600         4895 :       unsigned half = len / 2;
   18601         4895 :       module_state *probe = (*ool)[pos + half];
   18602         4895 :       if (loc < probe->ordinary_locs.first)
   18603              :         len = half;
   18604         4892 :       else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
   18605              :         return probe;
   18606              :       else
   18607              :         {
   18608            0 :           pos += half + 1;
   18609            0 :           len = len - (half + 1);
   18610              :         }
   18611              :     }
   18612              : 
   18613              :   return nullptr;
   18614              : }
   18615              : 
   18616              : static const module_state *
   18617            6 : module_for_macro_loc (location_t loc)
   18618              : {
   18619            6 :   unsigned pos = 1;
   18620            6 :   unsigned len = modules->length () - pos;
   18621              : 
   18622            6 :   while (len)
   18623              :     {
   18624            6 :       unsigned half = len / 2;
   18625            6 :       module_state *probe = (*modules)[pos + half];
   18626            6 :       if (loc < probe->macro_locs.first)
   18627              :         {
   18628            0 :           pos += half + 1;
   18629            0 :           len = len - (half + 1);
   18630              :         }
   18631            6 :       else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
   18632              :         len = half;
   18633              :       else
   18634              :         return probe;
   18635              :     }
   18636              : 
   18637              :   return NULL;
   18638              : }
   18639              : 
   18640              : location_t
   18641         1329 : module_state::imported_from () const
   18642              : {
   18643         1329 :   location_t from = loc;
   18644         1329 :   line_map_ordinary const *fmap
   18645         1329 :     = linemap_check_ordinary (linemap_lookup (line_table, from));
   18646              : 
   18647         1329 :   if (MAP_MODULE_P (fmap))
   18648         1329 :     from = linemap_included_from (fmap);
   18649              : 
   18650         1329 :   return from;
   18651              : }
   18652              : 
   18653              : /* Note that LOC will need writing.  This allows us to prune locations
   18654              :    that are not needed.  */
   18655              : 
   18656              : bool
   18657     30671620 : module_state::note_location (location_t loc)
   18658              : {
   18659     30671620 :   bool added = false;
   18660     30671620 :   if (!macro_loc_table && !ord_loc_table)
   18661              :     ;
   18662     30671620 :   else if (loc < RESERVED_LOCATION_COUNT)
   18663              :     ;
   18664     27928502 :   else if (IS_ADHOC_LOC (loc))
   18665              :     {
   18666      3537241 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18667      3537241 :       note_location (locus);
   18668      3537241 :       source_range range = get_range_from_loc (line_table, loc);
   18669      3537241 :       if (range.m_start != locus)
   18670      3410051 :         note_location (range.m_start);
   18671      3537241 :       note_location (range.m_finish);
   18672              :     }
   18673     24391261 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18674              :     {
   18675      1673733 :       if (spans.macro (loc))
   18676              :         {
   18677      1673727 :           const line_map *map = linemap_lookup (line_table, loc);
   18678      1673727 :           const line_map_macro *mac_map = linemap_check_macro (map);
   18679      1673727 :           hashval_t hv = macro_loc_traits::hash (mac_map);
   18680      1673727 :           macro_loc_info *slot
   18681      1673727 :             = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
   18682      1673727 :           if (!slot->src)
   18683              :             {
   18684       172577 :               slot->src = mac_map;
   18685       172577 :               slot->remap = 0;
   18686              :               // Expansion locations could themselves be from a
   18687              :               // macro, we need to note them all.
   18688       172577 :               note_location (mac_map->m_expansion);
   18689       172577 :               gcc_checking_assert (mac_map->n_tokens);
   18690       172577 :               location_t tloc = UNKNOWN_LOCATION;
   18691      6564109 :               for (unsigned ix = mac_map->n_tokens * 2; ix--;)
   18692      6391532 :                 if (mac_map->macro_locations[ix] != tloc)
   18693              :                   {
   18694      3372507 :                     tloc = mac_map->macro_locations[ix];
   18695      3372507 :                     note_location (tloc);
   18696              :                   }
   18697              :               added = true;
   18698              :             }
   18699              :         }
   18700              :     }
   18701     22717528 :   else if (IS_ORDINARY_LOC (loc))
   18702              :     {
   18703     22717528 :       if (spans.ordinary (loc))
   18704              :         {
   18705     22712631 :           const line_map *map = linemap_lookup (line_table, loc);
   18706     22712631 :           const line_map_ordinary *ord_map = linemap_check_ordinary (map);
   18707     22712631 :           ord_loc_info lkup;
   18708     22712631 :           lkup.src = ord_map;
   18709     22712631 :           lkup.span = loc_one << ord_map->m_column_and_range_bits;
   18710     22712631 :           lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
   18711     22712631 :           lkup.remap = 0;
   18712     22712631 :           ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
   18713     22712631 :                                 (lkup, ord_loc_traits::hash (lkup), INSERT));
   18714     22712631 :           if (!slot->src)
   18715              :             {
   18716      2556689 :               *slot = lkup;
   18717      2556689 :               added = true;
   18718              :             }
   18719              :         }
   18720              :     }
   18721              :   else
   18722            0 :     gcc_unreachable ();
   18723     30671620 :   return added;
   18724              : }
   18725              : 
   18726              : /* If we're not streaming, record that we need location LOC.
   18727              :    Otherwise stream it.  */
   18728              : 
   18729              : void
   18730     45943035 : module_state::write_location (bytes_out &sec, location_t loc)
   18731              : {
   18732     45943035 :   if (!sec.streaming_p ())
   18733              :     {
   18734     16353812 :       note_location (loc);
   18735     16353812 :       return;
   18736              :     }
   18737              : 
   18738     29589223 :   if (loc < RESERVED_LOCATION_COUNT)
   18739              :     {
   18740      2821463 :       dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
   18741      2821445 :       sec.loc (LK_RESERVED + loc);
   18742              :     }
   18743     26767778 :   else if (IS_ADHOC_LOC (loc))
   18744              :     {
   18745      3453659 :       dump (dumper::LOCATION) && dump ("Adhoc location");
   18746      3453656 :       sec.u (LK_ADHOC);
   18747      3453656 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18748      3453656 :       write_location (sec, locus);
   18749      3453656 :       source_range range = get_range_from_loc (line_table, loc);
   18750      3453656 :       if (range.m_start == locus)
   18751              :         /* Compress.  */
   18752       119576 :         range.m_start = UNKNOWN_LOCATION;
   18753      3453656 :       write_location (sec, range.m_start);
   18754      3453656 :       write_location (sec, range.m_finish);
   18755      3453656 :       unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
   18756      3453656 :       sec.u (discriminator);
   18757              :     }
   18758     23314122 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18759              :     {
   18760      1663559 :       const macro_loc_info *info = nullptr;
   18761      1663559 :       line_map_uint_t offset = 0;
   18762      1663559 :       if (unsigned hwm = macro_loc_remap->length ())
   18763              :         {
   18764      1663553 :           info = macro_loc_remap->begin ();
   18765     24486480 :           while (hwm != 1)
   18766              :             {
   18767     21159374 :               unsigned mid = hwm / 2;
   18768     21159374 :               if (MAP_START_LOCATION (info[mid].src) <= loc)
   18769              :                 {
   18770     10681754 :                   info += mid;
   18771     10681754 :                   hwm -= mid;
   18772              :                 }
   18773              :               else
   18774              :                 hwm = mid;
   18775              :             }
   18776      1663553 :           offset = loc - MAP_START_LOCATION (info->src);
   18777      1663553 :           if (offset > info->src->n_tokens)
   18778            0 :             info = nullptr;
   18779              :         }
   18780              : 
   18781      1663559 :       gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
   18782              : 
   18783      1663559 :       if (info)
   18784              :         {
   18785      1663553 :           offset += info->remap;
   18786      1663553 :           sec.u (LK_MACRO);
   18787      1663553 :           sec.loc (offset);
   18788      1663553 :           dump (dumper::LOCATION)
   18789            9 :             && dump ("Macro location %K output %K", loc, offset);
   18790              :         }
   18791            6 :       else if (const module_state *import = module_for_macro_loc (loc))
   18792              :         {
   18793            6 :           auto off = loc - import->macro_locs.first;
   18794            6 :           sec.u (LK_IMPORT_MACRO);
   18795            6 :           sec.u (import->remap);
   18796            6 :           sec.loc (off);
   18797            6 :           dump (dumper::LOCATION)
   18798            0 :             && dump ("Imported macro location %K output %u:%K",
   18799            0 :                      loc, import->remap, off);
   18800              :         }
   18801              :       else
   18802            0 :         gcc_unreachable ();
   18803              :     }
   18804     21650563 :   else if (IS_ORDINARY_LOC (loc))
   18805              :     {
   18806              :       /* If we ran out of locations for imported decls, this location could
   18807              :          be a module unit's location.  In that case, remap the location
   18808              :          to be where we imported the module from.  */
   18809     21650563 :       if (spans.locations_exhausted_p () || CHECKING_P)
   18810              :         {
   18811     21650563 :           const line_map_ordinary *map
   18812     21650563 :             = linemap_check_ordinary (linemap_lookup (line_table, loc));
   18813     21650563 :           if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
   18814              :             {
   18815            0 :               gcc_checking_assert (spans.locations_exhausted_p ());
   18816            0 :               write_location (sec, linemap_included_from (map));
   18817            0 :               return;
   18818              :             }
   18819              :         }
   18820              : 
   18821     21650563 :       const ord_loc_info *info = nullptr;
   18822     21650563 :       line_map_uint_t offset = 0;
   18823     21650563 :       if (line_map_uint_t hwm = ord_loc_remap->length ())
   18824              :         {
   18825     21650563 :           info = ord_loc_remap->begin ();
   18826    323113877 :           while (hwm != 1)
   18827              :             {
   18828    279812751 :               auto mid = hwm / 2;
   18829    279812751 :               if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
   18830              :                 {
   18831    146673603 :                   info += mid;
   18832    146673603 :                   hwm -= mid;
   18833              :                 }
   18834              :               else
   18835              :                 hwm = mid;
   18836              :             }
   18837     21650563 :           offset = loc - MAP_START_LOCATION (info->src) - info->offset;
   18838     21650563 :           if (offset > info->span)
   18839         4892 :             info = nullptr;
   18840              :         }
   18841              : 
   18842     21650563 :       gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
   18843              : 
   18844     21650563 :       if (info)
   18845              :         {
   18846     21645671 :           offset += info->remap;
   18847     21645671 :           sec.u (LK_ORDINARY);
   18848     21645671 :           sec.loc (offset);
   18849              : 
   18850     21645671 :           dump (dumper::LOCATION)
   18851           78 :             && dump ("Ordinary location %K output %K", loc, offset);
   18852              :         }
   18853         4892 :       else if (const module_state *import = module_for_ordinary_loc (loc))
   18854              :         {
   18855         4892 :           auto off = loc - import->ordinary_locs.first;
   18856         4892 :           sec.u (LK_IMPORT_ORDINARY);
   18857         4892 :           sec.u (import->remap);
   18858         4892 :           sec.loc (off);
   18859         4892 :           dump (dumper::LOCATION)
   18860            0 :             && dump ("Imported ordinary location %K output %u:%K",
   18861            0 :                      loc, import->remap, off);
   18862              :         }
   18863              :       else
   18864            0 :         gcc_unreachable ();
   18865              :     }
   18866              :   else
   18867            0 :     gcc_unreachable ();
   18868              : }
   18869              : 
   18870              : location_t
   18871     21243782 : module_state::read_location (bytes_in &sec) const
   18872              : {
   18873     21243782 :   location_t locus = UNKNOWN_LOCATION;
   18874     21243782 :   unsigned kind = sec.u ();
   18875     21243782 :   switch (kind)
   18876              :      {
   18877      1960331 :     default:
   18878      1960331 :       {
   18879      1960331 :         if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
   18880      1960331 :           locus = location_t (kind - LK_RESERVED);
   18881              :         else
   18882            0 :           sec.set_overrun ();
   18883      1960331 :         dump (dumper::LOCATION)
   18884            0 :           && dump ("Reserved location %K", locus);
   18885              :       }
   18886              :       break;
   18887              : 
   18888      2379743 :      case LK_ADHOC:
   18889      2379743 :       {
   18890      2379743 :         dump (dumper::LOCATION) && dump ("Adhoc location");
   18891      2379743 :         locus = read_location (sec);
   18892      2379743 :         source_range range;
   18893      2379743 :         range.m_start = read_location (sec);
   18894      2379743 :         if (range.m_start == UNKNOWN_LOCATION)
   18895        76260 :           range.m_start = locus;
   18896      2379743 :         range.m_finish = read_location (sec);
   18897      2379743 :         unsigned discriminator = sec.u ();
   18898      2379743 :         if (locus != loc && range.m_start != loc && range.m_finish != loc)
   18899      2379743 :           locus = line_table->get_or_create_combined_loc (locus, range,
   18900              :                                                           nullptr, discriminator);
   18901              :       }
   18902              :       break;
   18903              : 
   18904      1403505 :     case LK_MACRO:
   18905      1403505 :       {
   18906      1403505 :         auto off = sec.loc ();
   18907              : 
   18908      1403505 :         if (macro_locs.second)
   18909              :           {
   18910      1403505 :             if (off < macro_locs.second)
   18911      1403505 :               locus = off + macro_locs.first;
   18912              :             else
   18913            0 :               sec.set_overrun ();
   18914              :           }
   18915              :         else
   18916            0 :           locus = loc;
   18917      1403505 :         dump (dumper::LOCATION)
   18918            0 :           && dump ("Macro %K becoming %K", off, locus);
   18919              :       }
   18920              :       break;
   18921              : 
   18922     15498228 :     case LK_ORDINARY:
   18923     15498228 :       {
   18924     15498228 :         auto off = sec.loc ();
   18925     15498228 :         if (ordinary_locs.second)
   18926              :           {
   18927     15498228 :             if (off < ordinary_locs.second)
   18928     15498228 :               locus = off + ordinary_locs.first;
   18929              :             else
   18930            0 :               sec.set_overrun ();
   18931              :           }
   18932              :         else
   18933            0 :           locus = loc;
   18934              : 
   18935     15498228 :         dump (dumper::LOCATION)
   18936            0 :           && dump ("Ordinary location %K becoming %K", off, locus);
   18937              :       }
   18938              :       break;
   18939              : 
   18940         1975 :      case LK_IMPORT_MACRO:
   18941         1975 :      case LK_IMPORT_ORDINARY:
   18942         1975 :        {
   18943         1975 :          unsigned mod = sec.u ();
   18944         1975 :          location_t off = sec.loc ();
   18945         1975 :          const module_state *import = NULL;
   18946              : 
   18947         1975 :          if (!mod && !slurp->remap)
   18948              :            /* This is an early read of a partition location during the
   18949              :               read of our ordinary location map.  */
   18950              :            import = this;
   18951              :          else
   18952              :            {
   18953         1975 :              mod = slurp->remap_module (mod);
   18954         1975 :              if (!mod)
   18955            0 :                sec.set_overrun ();
   18956              :              else
   18957         1975 :                import = (*modules)[mod];
   18958              :            }
   18959              : 
   18960         1975 :          if (import)
   18961              :            {
   18962         1975 :              if (kind == LK_IMPORT_MACRO)
   18963              :                {
   18964            6 :                  if (!import->macro_locs.second)
   18965            0 :                    locus = import->loc;
   18966            6 :                  else if (off < import->macro_locs.second)
   18967            6 :                    locus = off + import->macro_locs.first;
   18968              :                  else
   18969            0 :                    sec.set_overrun ();
   18970              :                }
   18971              :              else
   18972              :                {
   18973         1969 :                  if (!import->ordinary_locs.second)
   18974            0 :                    locus = import->loc;
   18975         1969 :                  else if (off < import->ordinary_locs.second)
   18976         1969 :                    locus = import->ordinary_locs.first + off;
   18977              :                  else
   18978            0 :                    sec.set_overrun ();
   18979              :                }
   18980              :            }
   18981              :        }
   18982              :        break;
   18983              :     }
   18984              : 
   18985     21243782 :   return locus;
   18986              : }
   18987              : 
   18988              : /* Allocate hash tables to record needed locations.  */
   18989              : 
   18990              : void
   18991         2844 : module_state::write_init_maps ()
   18992              : {
   18993         2844 :   macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
   18994         2844 :   ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
   18995         2844 : }
   18996              : 
   18997              : /* Prepare the span adjustments.  We prune unneeded locations -- at
   18998              :    this point every needed location must have been seen by
   18999              :    note_location.  */
   19000              : 
   19001              : range_t
   19002         2815 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
   19003              : {
   19004         3115 :   dump () && dump ("Preparing locations");
   19005         2815 :   dump.indent ();
   19006              : 
   19007         3115 :   dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
   19008          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.first,
   19009          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.second,
   19010          300 :                    spans[loc_spans::SPAN_RESERVED].macro.first,
   19011          300 :                    spans[loc_spans::SPAN_RESERVED].macro.second);
   19012              : 
   19013         2815 :   range_t info {0, 0};
   19014              : 
   19015              :   // Sort the noted lines.
   19016         2815 :   vec_alloc (ord_loc_remap, ord_loc_table->size ());
   19017         2815 :   for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   19018      2544482 :        iter != end; ++iter)
   19019      2541667 :     ord_loc_remap->quick_push (*iter);
   19020         2815 :   ord_loc_remap->qsort (&ord_loc_info::compare);
   19021              : 
   19022              :   // Note included-from maps.
   19023         2815 :   bool added = false;
   19024         2815 :   const line_map_ordinary *current = nullptr;
   19025      2550112 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19026      2544482 :        iter != end; ++iter)
   19027      2541667 :     if (iter->src != current)
   19028              :       {
   19029        36024 :         current = iter->src;
   19030        14540 :         for (auto probe = current;
   19031        36024 :              auto from = linemap_included_from (probe);
   19032        14540 :              probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
   19033              :           {
   19034        32817 :             if (has_partitions)
   19035              :               {
   19036              :                 // Partition locations need to elide their module map
   19037              :                 // entry.
   19038          223 :                 probe
   19039          223 :                   = linemap_check_ordinary (linemap_lookup (line_table, from));
   19040          223 :                 if (MAP_MODULE_P (probe))
   19041          190 :                   from = linemap_included_from (probe);
   19042              :               }
   19043              : 
   19044        32817 :             if (!note_location (from))
   19045              :               break;
   19046        14540 :             added = true;
   19047        14540 :           }
   19048              :       }
   19049         2815 :   if (added)
   19050              :     {
   19051              :       // Reconstruct the line array as we added items to the hash table.
   19052          529 :       vec_free (ord_loc_remap);
   19053          529 :       vec_alloc (ord_loc_remap, ord_loc_table->size ());
   19054          529 :       for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   19055      2544684 :            iter != end; ++iter)
   19056      2544155 :         ord_loc_remap->quick_push (*iter);
   19057          529 :       ord_loc_remap->qsort (&ord_loc_info::compare);
   19058              :     }
   19059         2815 :   delete ord_loc_table;
   19060         2815 :   ord_loc_table = nullptr;
   19061              : 
   19062              :   // Merge (sufficiently) adjacent spans, and calculate remapping.
   19063         2815 :   constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
   19064         5630 :   auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19065         2815 :   auto dst = begin;
   19066         2815 :   line_map_uint_t offset = 0;
   19067         2815 :   unsigned range_bits = 0;
   19068         2815 :   ord_loc_info *base = nullptr;
   19069      2559022 :   for (auto iter = begin; iter != end; ++iter)
   19070              :     {
   19071      2556207 :       if (base && iter->src == base->src)
   19072              :         {
   19073      4798237 :           if (base->offset + base->span +
   19074      2525067 :               ((adjacency << base->src->m_column_and_range_bits)
   19075              :                // If there are few c&r bits, allow further separation.
   19076      2525067 :                | (adjacency << 4))
   19077      2525067 :               >= iter->offset)
   19078              :             {
   19079              :               // Merge.
   19080      2273170 :               offset -= base->span;
   19081      2273170 :               base->span = iter->offset + iter->span - base->offset;
   19082      2273170 :               offset += base->span;
   19083      2273170 :               continue;
   19084              :             }
   19085              :         }
   19086        31140 :       else if (range_bits < iter->src->m_range_bits)
   19087         2716 :         range_bits = iter->src->m_range_bits;
   19088              : 
   19089       283037 :       offset += ((loc_one << iter->src->m_range_bits) - 1);
   19090       283037 :       offset &= ~((loc_one << iter->src->m_range_bits) - 1);
   19091       283037 :       iter->remap = offset;
   19092       283037 :       offset += iter->span;
   19093       283037 :       base = dst;
   19094       283037 :       *dst++ = *iter;
   19095              :     }
   19096         2815 :   ord_loc_remap->truncate (dst - begin);
   19097              : 
   19098         2815 :   info.first = ord_loc_remap->length ();
   19099         2815 :   cfg->ordinary_locs = offset;
   19100         2815 :   cfg->loc_range_bits = range_bits;
   19101         3115 :   dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
   19102              :                    info.first,
   19103              :                    cfg->ordinary_locs,
   19104              :                    cfg->loc_range_bits);
   19105              : 
   19106              :   // Remap the macro locations.
   19107         2815 :   vec_alloc (macro_loc_remap, macro_loc_table->size ());
   19108         2815 :   for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
   19109       175392 :        iter != end; ++iter)
   19110       172577 :     macro_loc_remap->quick_push (*iter);
   19111         2815 :   delete macro_loc_table;
   19112         2815 :   macro_loc_table = nullptr;
   19113              : 
   19114         2815 :   macro_loc_remap->qsort (&macro_loc_info::compare);
   19115         2815 :   offset = 0;
   19116         8445 :   for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
   19117       175392 :        iter != end; ++iter)
   19118              :     {
   19119       172577 :       auto mac = iter->src;
   19120       172577 :       iter->remap = offset;
   19121       172577 :       offset += mac->n_tokens;
   19122              :     }
   19123         2815 :   info.second = macro_loc_remap->length ();
   19124         2815 :   cfg->macro_locs = offset;
   19125              : 
   19126         3115 :   dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
   19127              : 
   19128         2815 :   dump.outdent ();
   19129              : 
   19130              :   // If we have no ordinary locs, we must also have no macro locs.
   19131         2815 :   gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
   19132              : 
   19133         2815 :   return info;
   19134              : }
   19135              : 
   19136              : bool
   19137         3048 : module_state::read_prepare_maps (const module_state_config *cfg)
   19138              : {
   19139         3048 :   location_t ordinary = line_table->highest_location + 1;
   19140         3048 :   ordinary += cfg->ordinary_locs;
   19141              : 
   19142         3048 :   location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19143         3048 :   macro -= cfg->macro_locs;
   19144              : 
   19145         3048 :   if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
   19146         3048 :       && macro >= LINE_MAP_MAX_LOCATION)
   19147              :     /* OK, we have enough locations.  */
   19148              :     return true;
   19149              : 
   19150            0 :   ordinary_locs.first = ordinary_locs.second = 0;
   19151            0 :   macro_locs.first = macro_locs.second = 0;
   19152              : 
   19153            0 :   spans.report_location_exhaustion (loc);
   19154              : 
   19155              :   return false;
   19156              : }
   19157              : 
   19158              : /* Write & read the location maps. Not called if there are no
   19159              :    locations.   */
   19160              : 
   19161              : void
   19162         2716 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
   19163              :                                    bool has_partitions, unsigned *crc_p)
   19164              : {
   19165         2994 :   dump () && dump ("Writing ordinary location maps");
   19166         2716 :   dump.indent ();
   19167              : 
   19168         2716 :   vec<const char *> filenames;
   19169         2716 :   filenames.create (20);
   19170              : 
   19171              :   /* Determine the unique filenames.  */
   19172         2716 :   const line_map_ordinary *current = nullptr;
   19173       291185 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19174       285753 :        iter != end; ++iter)
   19175       283037 :     if (iter->src != current)
   19176              :       {
   19177        31140 :         current = iter->src;
   19178        31140 :         const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19179              : 
   19180              :         /* We should never find a module linemap in an interval.  */
   19181        31140 :         gcc_checking_assert (!MAP_MODULE_P (iter->src));
   19182              : 
   19183              :         /* We expect very few filenames, so just an array.
   19184              :            (Not true when headers are still in play :()  */
   19185      2373076 :         for (unsigned jx = filenames.length (); jx--;)
   19186              :           {
   19187      2325575 :             const char *name = filenames[jx];
   19188      2325575 :             if (0 == strcmp (name, fname))
   19189              :               {
   19190              :                 /* Reset the linemap's name, because for things like
   19191              :                    preprocessed input we could have multiple instances
   19192              :                    of the same name, and we'd rather not percolate
   19193              :                    that.  */
   19194        14779 :                 const_cast<line_map_ordinary *> (iter->src)->to_file = name;
   19195        14779 :                 fname = NULL;
   19196        14779 :                 break;
   19197              :               }
   19198              :           }
   19199        31140 :         if (fname)
   19200        16361 :           filenames.safe_push (fname);
   19201              :       }
   19202              : 
   19203         2716 :   bytes_out sec (to);
   19204         2716 :   sec.begin ();
   19205              : 
   19206              :   /* Write the filenames.  */
   19207         2716 :   unsigned len = filenames.length ();
   19208         2716 :   sec.u (len);
   19209         2994 :   dump () && dump ("%u source file names", len);
   19210        19077 :   for (unsigned ix = 0; ix != len; ix++)
   19211              :     {
   19212        16361 :       const char *fname = filenames[ix];
   19213        16376 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19214        16361 :       sec.str (fname);
   19215              :     }
   19216              : 
   19217         2716 :   sec.loc (info.first); /* Num maps.  */
   19218         2716 :   const ord_loc_info *base = nullptr;
   19219       291185 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19220       285753 :        iter != end; ++iter)
   19221              :     {
   19222       283037 :       dump (dumper::LOCATION)
   19223           36 :         && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
   19224           36 :                  (location_t) (iter - ord_loc_remap->begin ()),
   19225           18 :                  MAP_START_LOCATION (iter->src),
   19226              :                  iter->offset, iter->span, iter->remap,
   19227              :                  iter->span);
   19228              : 
   19229       283037 :       if (!base || iter->src != base->src)
   19230        31140 :         base = iter;
   19231       283037 :       sec.loc (iter->offset - base->offset);
   19232       283037 :       if (base == iter)
   19233              :         {
   19234        31140 :           sec.u (iter->src->sysp);
   19235        31140 :           sec.u (iter->src->m_range_bits);
   19236        31140 :           sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
   19237              : 
   19238        31140 :           const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19239      7155004 :           for (unsigned ix = 0; ix != filenames.length (); ix++)
   19240      3577502 :             if (filenames[ix] == fname)
   19241              :               {
   19242        31140 :                 sec.u (ix);
   19243        31140 :                 break;
   19244              :               }
   19245        31140 :           unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
   19246        31140 :           line += iter->offset >> iter->src->m_column_and_range_bits;
   19247        31140 :           sec.u (line);
   19248              :         }
   19249       283037 :       sec.loc (iter->remap);
   19250       283037 :       if (base == iter)
   19251              :         {
   19252              :           /* Write the included from location, which means reading it
   19253              :              while reading in the ordinary maps.  So we'd better not
   19254              :              be getting ahead of ourselves.  */
   19255        31140 :           location_t from = linemap_included_from (iter->src);
   19256        31140 :           gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
   19257        31140 :           if (from != UNKNOWN_LOCATION && has_partitions)
   19258              :             {
   19259              :               /* A partition's span will have a from pointing at a
   19260              :                  MODULE_INC.  Find that map's from.  */
   19261          217 :               line_map_ordinary const *fmap
   19262          217 :                 = linemap_check_ordinary (linemap_lookup (line_table, from));
   19263          217 :               if (MAP_MODULE_P (fmap))
   19264          184 :                 from = linemap_included_from (fmap);
   19265              :             }
   19266        31140 :           write_location (sec, from);
   19267              :         }
   19268              :     }
   19269              : 
   19270         2716 :   filenames.release ();
   19271              : 
   19272         2716 :   sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
   19273         2716 :   dump.outdent ();
   19274         2716 : }
   19275              : 
   19276              : /* Return the prefix to use for dumping a #pragma diagnostic change to DK.  */
   19277              : 
   19278              : static const char *
   19279         1036 : dk_string (enum diagnostics::kind dk)
   19280              : {
   19281         1036 :   gcc_assert (dk > diagnostics::kind::unspecified
   19282              :               && dk < diagnostics::kind::last_diagnostic_kind);
   19283         1036 :   if (dk == diagnostics::kind::ignored)
   19284              :     /* diagnostics/kinds.def has an empty string for ignored.  */
   19285              :     return "ignored: ";
   19286              :   else
   19287            0 :     return diagnostics::get_text_for_kind (dk);
   19288              : }
   19289              : 
   19290              : /* Dump one #pragma GCC diagnostic entry.  */
   19291              : 
   19292              : static bool
   19293         2116 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
   19294              : {
   19295         2116 :   if (dk == diagnostics::kind::pop)
   19296         1080 :     return dump (" Index %u: pop from %d", index, opt);
   19297              :   else
   19298         1036 :     return dump (" Index %u: %s%s", index, dk_string (dk),
   19299         2072 :                  cl_options[opt].opt_text);
   19300              : }
   19301              : 
   19302              : /* Write out any #pragma GCC diagnostic info to the .dgc section.  */
   19303              : 
   19304              : void
   19305         5531 : module_state::write_diagnostic_classification (elf_out *to,
   19306              :                                                diagnostics::context *dc,
   19307              :                                                unsigned *crc_p)
   19308              : {
   19309         5531 :   auto &changes = dc->get_classification_history ();
   19310              : 
   19311         5531 :   bytes_out sec (to);
   19312         5531 :   if (sec.streaming_p ())
   19313              :     {
   19314         2716 :       sec.begin ();
   19315         2994 :       dump () && dump ("Writing diagnostic change locations");
   19316         2716 :       dump.indent ();
   19317              :     }
   19318              : 
   19319         5531 :   unsigned len = changes.length ();
   19320              : 
   19321              :   /* We don't want to write out any entries that came from one of our imports.
   19322              :      But then we need to adjust the total, and change diagnostics::kind::pop
   19323              :      targets to match the index in our actual output.  So remember how many
   19324              :      lines we had skipped at each step, where -1 means this line itself
   19325              :      is skipped.  */
   19326         5531 :   int skips = 0;
   19327         5531 :   auto_vec<int> skips_at (len);
   19328         5531 :   skips_at.safe_grow (len);
   19329              : 
   19330        79042 :   for (unsigned i = 0; i < len; ++i)
   19331              :     {
   19332        67980 :       const auto &c = changes[i];
   19333        67980 :       skips_at[i] = skips;
   19334        67980 :       if (linemap_location_from_module_p (line_table, c.location))
   19335              :         {
   19336        13206 :           ++skips;
   19337        13206 :           skips_at[i] = -1;
   19338        13206 :           continue;
   19339              :         }
   19340              :     }
   19341              : 
   19342         5531 :   if (sec.streaming_p ())
   19343              :     {
   19344         2716 :       sec.u (len - skips);
   19345         2994 :       dump () && dump ("Diagnostic changes: %u", len - skips);
   19346              :     }
   19347              : 
   19348        73511 :   for (unsigned i = 0; i < len; ++i)
   19349              :     {
   19350        67980 :       if (skips_at[i] == -1)
   19351        13206 :         continue;
   19352              : 
   19353        54774 :       const auto &c = changes[i];
   19354        54774 :       write_location (sec, c.location);
   19355        54774 :       if (sec.streaming_p ())
   19356              :         {
   19357        27387 :           unsigned opt = c.option;
   19358        27387 :           if (c.kind == diagnostics::kind::pop)
   19359        14091 :             opt -= skips_at[opt];
   19360        27387 :           sec.u (opt);
   19361        27387 :           sec.u (static_cast<unsigned> (c.kind));
   19362        70029 :           dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
   19363              :         }
   19364              :     }
   19365              : 
   19366         5531 :   if (sec.streaming_p ())
   19367              :     {
   19368         2716 :       sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
   19369         2716 :       dump.outdent ();
   19370              :     }
   19371         5531 : }
   19372              : 
   19373              : /* Read any #pragma GCC diagnostic info from the .dgc section.  */
   19374              : 
   19375              : bool
   19376         2990 : module_state::read_diagnostic_classification (diagnostics::context *dc)
   19377              : {
   19378         2990 :   bytes_in sec;
   19379              : 
   19380         2990 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
   19381              :     return false;
   19382              : 
   19383         3510 :   dump () && dump ("Reading diagnostic change locations");
   19384         2990 :   dump.indent ();
   19385              : 
   19386         2990 :   unsigned len = sec.u ();
   19387         3510 :   dump () && dump ("Diagnostic changes: %u", len);
   19388              : 
   19389         2990 :   auto &changes = dc->get_classification_history ();
   19390         2990 :   int offset = changes.length ();
   19391         2990 :   changes.reserve (len + 1);
   19392        30792 :   for (unsigned i = 0; i < len; ++i)
   19393              :     {
   19394        24812 :       location_t loc = read_location (sec);
   19395        24812 :       int opt = sec.u ();
   19396        24812 :       enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
   19397        24812 :       if (kind == diagnostics::kind::pop)
   19398              :         /* For a pop, opt is the 'changes' index to return to.  */
   19399        12757 :         opt += offset;
   19400        24812 :       changes.quick_push ({ loc, opt, kind });
   19401        24879 :       dump () && dump_dc_change (changes.length () - 1, opt, kind);
   19402              :     }
   19403              : 
   19404              :   /* Did the import pop all its diagnostic changes?  */
   19405         2990 :   bool last_was_reset = (len == 0);
   19406         2990 :   if (len)
   19407          232 :     for (int i = changes.length () - 1; ; --i)
   19408              :       {
   19409        11060 :         gcc_checking_assert (i >= offset);
   19410              : 
   19411        11060 :         const auto &c = changes[i];
   19412        11060 :         if (c.kind != diagnostics::kind::pop)
   19413              :           break;
   19414        11051 :         else if (c.option == offset)
   19415              :           {
   19416              :             last_was_reset = true;
   19417              :             break;
   19418              :           }
   19419              :         else
   19420              :           /* As in update_effective_level_from_pragmas, the loop will decrement
   19421              :              i so we actually jump to c.option - 1.  */
   19422        10944 :           i = c.option;
   19423        10944 :       }
   19424         2990 :   if (!last_was_reset)
   19425              :     {
   19426              :       /* It didn't, so add a pop at its last location to avoid affecting later
   19427              :          imports.  */
   19428            9 :       location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
   19429            9 :       changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
   19430           15 :       dump () && dump (" Adding final pop from index %d", offset);
   19431              :     }
   19432              : 
   19433         2990 :   dump.outdent ();
   19434         2990 :   if (!sec.end (from ()))
   19435              :     return false;
   19436              : 
   19437              :   return true;
   19438         2990 : }
   19439              : 
   19440              : void
   19441          128 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
   19442              : {
   19443          140 :   dump () && dump ("Writing macro location maps");
   19444          128 :   dump.indent ();
   19445              : 
   19446          128 :   bytes_out sec (to);
   19447          128 :   sec.begin ();
   19448              : 
   19449          140 :   dump () && dump ("Macro maps:%K", info.second);
   19450          128 :   sec.loc (info.second);
   19451              : 
   19452          128 :   line_map_uint_t macro_num = 0;
   19453          256 :   for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
   19454       172705 :        iter-- != begin;)
   19455              :     {
   19456       172577 :       auto mac = iter->src;
   19457       172577 :       sec.loc (iter->remap);
   19458       172577 :       sec.u (mac->n_tokens);
   19459       172577 :       sec.cpp_node (mac->macro);
   19460       172577 :       write_location (sec, mac->m_expansion);
   19461       172577 :       const location_t *locs = mac->macro_locations;
   19462              :       /* There are lots of identical runs.  */
   19463       172577 :       location_t prev = UNKNOWN_LOCATION;
   19464       172577 :       unsigned count = 0;
   19465       172577 :       unsigned runs = 0;
   19466      6564109 :       for (unsigned jx = mac->n_tokens * 2; jx--;)
   19467              :         {
   19468      6391532 :           location_t tok_loc = locs[jx];
   19469      6391532 :           if (tok_loc == prev)
   19470              :             {
   19471      3019025 :               count++;
   19472      3019025 :               continue;
   19473              :             }
   19474      3372507 :           runs++;
   19475      3372507 :           sec.u (count);
   19476      3372507 :           count = 1;
   19477      3372507 :           prev = tok_loc;
   19478      3372507 :           write_location (sec, tok_loc);
   19479              :         }
   19480       172577 :       sec.u (count);
   19481       172577 :       dump (dumper::LOCATION)
   19482            9 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
   19483            9 :                  macro_num, identifier (mac->macro),
   19484              :                  runs, mac->n_tokens,
   19485              :                  MAP_START_LOCATION (mac),
   19486            9 :                  MAP_START_LOCATION (mac) + mac->n_tokens,
   19487              :                  iter->remap);
   19488       172577 :       macro_num++;
   19489              :     }
   19490          128 :   gcc_assert (macro_num == info.second);
   19491              : 
   19492          128 :   sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
   19493          128 :   dump.outdent ();
   19494          128 : }
   19495              : 
   19496              : bool
   19497         2990 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
   19498              :                                   unsigned range_bits)
   19499              : {
   19500         2990 :   bytes_in sec;
   19501              : 
   19502         2990 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
   19503              :     return false;
   19504         3510 :   dump () && dump ("Reading ordinary location maps");
   19505         2990 :   dump.indent ();
   19506              : 
   19507              :   /* Read the filename table.  */
   19508         2990 :   unsigned len = sec.u ();
   19509         3510 :   dump () && dump ("%u source file names", len);
   19510         2990 :   vec<const char *> filenames;
   19511         2990 :   filenames.create (len);
   19512        22452 :   for (unsigned ix = 0; ix != len; ix++)
   19513              :     {
   19514        16472 :       size_t l;
   19515        16472 :       const char *buf = sec.str (&l);
   19516        16472 :       char *fname = XNEWVEC (char, l + 1);
   19517        16472 :       memcpy (fname, buf, l + 1);
   19518        16472 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19519              :       /* We leak these names into the line-map table.  But it
   19520              :          doesn't own them.  */
   19521        16472 :       filenames.quick_push (fname);
   19522              :     }
   19523              : 
   19524         2990 :   line_map_uint_t num_ordinary = sec.loc ();
   19525         3510 :   dump () && dump ("Ordinary maps:%K, range_bits:%u",
   19526              :                    num_ordinary, range_bits);
   19527              : 
   19528         2990 :   location_t offset = line_table->highest_location + 1;
   19529         2990 :   offset += ((loc_one << range_bits) - 1);
   19530         2990 :   offset &= ~((loc_one << range_bits) - 1);
   19531         2990 :   ordinary_locs.first = offset;
   19532              : 
   19533         2990 :   bool propagated = spans.maybe_propagate (this, offset);
   19534         2990 :   line_map_ordinary *maps = static_cast<line_map_ordinary *>
   19535         2990 :     (line_map_new_raw (line_table, false, num_ordinary));
   19536              : 
   19537         2990 :   const line_map_ordinary *base = nullptr;
   19538       293816 :   for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
   19539              :     {
   19540       287836 :       line_map_ordinary *map = &maps[ix];
   19541              : 
   19542       287836 :       location_t offset = sec.loc ();
   19543       287836 :       if (!offset)
   19544              :         {
   19545        31660 :           map->reason = LC_RENAME;
   19546        31660 :           map->sysp = sec.u ();
   19547        31660 :           map->m_range_bits = sec.u ();
   19548        31660 :           map->m_column_and_range_bits = sec.u () + map->m_range_bits;
   19549        31660 :           unsigned fnum = sec.u ();
   19550        63320 :           map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
   19551        31660 :           map->to_line = sec.u ();
   19552        31660 :           base = map;
   19553              :         }
   19554              :       else
   19555              :         {
   19556       256176 :           *map = *base;
   19557       256176 :           map->to_line += offset >> map->m_column_and_range_bits;
   19558              :         }
   19559       287836 :       location_t remap = sec.loc ();
   19560       287836 :       map->start_location = remap + ordinary_locs.first;
   19561       287836 :       if (base == map)
   19562              :         {
   19563              :           /* Root the outermost map at our location.  */
   19564        31660 :           ordinary_locs.second = remap;
   19565        31660 :           location_t from = read_location (sec);
   19566        31660 :           map->included_from = from != UNKNOWN_LOCATION ? from : loc;
   19567              :         }
   19568              :     }
   19569              : 
   19570         2990 :   ordinary_locs.second = num_ord_locs;
   19571              :   /* highest_location is the one handed out, not the next one to
   19572              :      hand out.  */
   19573         2990 :   line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
   19574              : 
   19575         2990 :   if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
   19576              :     /* We shouldn't run out of locations, as we checked before
   19577              :        starting.  */
   19578            0 :     sec.set_overrun ();
   19579         3510 :   dump () && dump ("Ordinary location [%K,+%K)",
   19580              :                    ordinary_locs.first, ordinary_locs.second);
   19581              : 
   19582         2990 :   if (propagated)
   19583          175 :     spans.close ();
   19584              : 
   19585         2990 :   filenames.release ();
   19586              : 
   19587         2990 :   dump.outdent ();
   19588         2990 :   if (!sec.end (from ()))
   19589              :     return false;
   19590              : 
   19591              :   return true;
   19592         2990 : }
   19593              : 
   19594              : bool
   19595          133 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
   19596              : {
   19597          133 :   bytes_in sec;
   19598              : 
   19599          133 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
   19600              :     return false;
   19601          142 :   dump () && dump ("Reading macro location maps");
   19602          133 :   dump.indent ();
   19603              : 
   19604          133 :   line_map_uint_t num_macros = sec.loc ();
   19605          142 :   dump () && dump ("Macro maps:%K locs:%K",
   19606              :                    num_macros, num_macro_locs);
   19607              : 
   19608          266 :   bool propagated = spans.maybe_propagate (this,
   19609          133 :                                            line_table->highest_location + 1);
   19610              : 
   19611          133 :   location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19612          133 :   macro_locs.second = num_macro_locs;
   19613          133 :   macro_locs.first = offset - num_macro_locs;
   19614              : 
   19615          142 :   dump () && dump ("Macro loc delta %K", offset);
   19616          142 :   dump () && dump ("Macro locations [%K,%K)",
   19617              :                    macro_locs.first, macro_locs.second);
   19618              : 
   19619       175184 :   for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
   19620              :     {
   19621       175051 :       location_t offset = sec.loc ();
   19622       175051 :       unsigned n_tokens = sec.u ();
   19623       175051 :       cpp_hashnode *node = sec.cpp_node ();
   19624       175051 :       location_t exp_loc = read_location (sec);
   19625              : 
   19626       175051 :       const line_map_macro *macro
   19627       175051 :         = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
   19628       175051 :       if (!macro)
   19629              :         /* We shouldn't run out of locations, as we checked that we
   19630              :            had enough before starting.  */
   19631              :         break;
   19632       175051 :       gcc_checking_assert (MAP_START_LOCATION (macro)
   19633              :                            == offset + macro_locs.first);
   19634              : 
   19635       175051 :       location_t *locs = macro->macro_locations;
   19636       175051 :       location_t tok_loc = UNKNOWN_LOCATION;
   19637       175051 :       unsigned count = sec.u ();
   19638       175051 :       unsigned runs = 0;
   19639      6402883 :       for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
   19640              :         {
   19641      9522451 :           while (!count-- && !sec.get_overrun ())
   19642              :             {
   19643      3294619 :               runs++;
   19644      3294619 :               tok_loc = read_location (sec);
   19645      3294619 :               count = sec.u ();
   19646              :             }
   19647      6227832 :           locs[jx] = tok_loc;
   19648              :         }
   19649       175051 :       if (count)
   19650            0 :         sec.set_overrun ();
   19651       175081 :       dump (dumper::LOCATION)
   19652            0 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
   19653              :                  ix, identifier (node), runs, n_tokens,
   19654              :                  MAP_START_LOCATION (macro),
   19655            0 :                  MAP_START_LOCATION (macro) + n_tokens);
   19656              :     }
   19657              : 
   19658          142 :   dump () && dump ("Macro location lwm:%K", macro_locs.first);
   19659          133 :   if (propagated)
   19660            3 :     spans.close ();
   19661              : 
   19662          133 :   dump.outdent ();
   19663          133 :   if (!sec.end (from ()))
   19664              :     return false;
   19665              : 
   19666              :   return true;
   19667          133 : }
   19668              : 
   19669              : /* Serialize the definition of MACRO.  */
   19670              : 
   19671              : void
   19672        78790 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
   19673              : {
   19674        78790 :   sec.u (macro->count);
   19675              : 
   19676        78790 :   bytes_out::bits_out bits = sec.stream_bits ();
   19677        78790 :   bits.b (macro->fun_like);
   19678        78790 :   bits.b (macro->variadic);
   19679        78790 :   bits.b (macro->syshdr);
   19680        78790 :   bits.bflush ();
   19681              : 
   19682        78790 :   write_location (sec, macro->line);
   19683        78790 :   if (macro->fun_like)
   19684              :     {
   19685         9800 :       sec.u (macro->paramc);
   19686         9800 :       const cpp_hashnode *const *parms = macro->parm.params;
   19687        24668 :       for (unsigned ix = 0; ix != macro->paramc; ix++)
   19688        14868 :         sec.cpp_node (parms[ix]);
   19689              :     }
   19690              : 
   19691        78790 :   unsigned len = 0;
   19692       251421 :   for (unsigned ix = 0; ix != macro->count; ix++)
   19693              :     {
   19694       172631 :       const cpp_token *token = &macro->exp.tokens[ix];
   19695       172631 :       write_location (sec, token->src_loc);
   19696       172631 :       sec.u (token->type);
   19697       172631 :       sec.u (token->flags);
   19698       172631 :       switch (cpp_token_val_index (token))
   19699              :         {
   19700            0 :         default:
   19701            0 :           gcc_unreachable ();
   19702              : 
   19703        13067 :         case CPP_TOKEN_FLD_ARG_NO:
   19704              :           /* An argument reference.  */
   19705        13067 :           sec.u (token->val.macro_arg.arg_no);
   19706        13067 :           sec.cpp_node (token->val.macro_arg.spelling);
   19707        13067 :           break;
   19708              : 
   19709        33593 :         case CPP_TOKEN_FLD_NODE:
   19710              :           /* An identifier.  */
   19711        33593 :           sec.cpp_node (token->val.node.node);
   19712        33593 :           if (token->val.node.spelling == token->val.node.node)
   19713              :             /* The spelling will usually be the same.  so optimize
   19714              :                that.  */
   19715        33593 :             sec.str (NULL, 0);
   19716              :           else
   19717            0 :             sec.cpp_node (token->val.node.spelling);
   19718              :           break;
   19719              : 
   19720              :         case CPP_TOKEN_FLD_NONE:
   19721              :           break;
   19722              : 
   19723        56367 :         case CPP_TOKEN_FLD_STR:
   19724              :           /* A string, number or comment.  Not always NUL terminated,
   19725              :              we stream out in a single concatenation with embedded
   19726              :              NULs as that's a safe default.  */
   19727        56367 :           len += token->val.str.len + 1;
   19728        56367 :           sec.u (token->val.str.len);
   19729        56367 :           break;
   19730              : 
   19731            0 :         case CPP_TOKEN_FLD_SOURCE:
   19732            0 :         case CPP_TOKEN_FLD_TOKEN_NO:
   19733            0 :         case CPP_TOKEN_FLD_PRAGMA:
   19734              :           /* These do not occur inside a macro itself.  */
   19735            0 :           gcc_unreachable ();
   19736              :         }
   19737              :     }
   19738              : 
   19739        78790 :   if (len)
   19740              :     {
   19741        52743 :       char *ptr = reinterpret_cast<char *> (sec.buf (len));
   19742        52743 :       len = 0;
   19743       209150 :       for (unsigned ix = 0; ix != macro->count; ix++)
   19744              :         {
   19745       103664 :           const cpp_token *token = &macro->exp.tokens[ix];
   19746       103664 :           if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19747              :             {
   19748        56367 :               memcpy (ptr + len, token->val.str.text,
   19749        56367 :                       token->val.str.len);
   19750        56367 :               len += token->val.str.len;
   19751        56367 :               ptr[len++] = 0;
   19752              :             }
   19753              :         }
   19754              :     }
   19755        78790 : }
   19756              : 
   19757              : /* Read a macro definition.  */
   19758              : 
   19759              : cpp_macro *
   19760          460 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
   19761              : {
   19762          460 :   unsigned count = sec.u ();
   19763              :   /* We rely on knowing cpp_reader's hash table is ident_hash, and
   19764              :      its subobject allocator is stringpool_ggc_alloc and that is just
   19765              :      a wrapper for ggc_alloc_atomic.  */
   19766          460 :   cpp_macro *macro
   19767          920 :     = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
   19768          460 :                                      + sizeof (cpp_token) * (count - !!count));
   19769          460 :   memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
   19770              : 
   19771          460 :   macro->count = count;
   19772          460 :   macro->kind = cmk_macro;
   19773          460 :   macro->imported_p = true;
   19774              : 
   19775          460 :   bytes_in::bits_in bits = sec.stream_bits ();
   19776          460 :   macro->fun_like = bits.b ();
   19777          460 :   macro->variadic = bits.b ();
   19778          460 :   macro->syshdr = bits.b ();
   19779          460 :   bits.bflush ();
   19780              : 
   19781          460 :   macro->line = read_location (sec);
   19782              : 
   19783          460 :   if (macro->fun_like)
   19784              :     {
   19785           70 :       unsigned paramc = sec.u ();
   19786           70 :       cpp_hashnode **params
   19787           70 :         = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
   19788           70 :       macro->paramc = paramc;
   19789           70 :       macro->parm.params = params;
   19790          149 :       for (unsigned ix = 0; ix != paramc; ix++)
   19791           79 :         params[ix] = sec.cpp_node ();
   19792              :     }
   19793              : 
   19794          460 :   unsigned len = 0;
   19795         1430 :   for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19796              :     {
   19797          970 :       cpp_token *token = &macro->exp.tokens[ix];
   19798          970 :       token->src_loc = read_location (sec);
   19799          970 :       token->type = cpp_ttype (sec.u ());
   19800          970 :       token->flags = sec.u ();
   19801          970 :       switch (cpp_token_val_index (token))
   19802              :         {
   19803            0 :         default:
   19804            0 :           sec.set_overrun ();
   19805            0 :           break;
   19806              : 
   19807           66 :         case CPP_TOKEN_FLD_ARG_NO:
   19808              :           /* An argument reference.  */
   19809           66 :           {
   19810           66 :             unsigned arg_no = sec.u ();
   19811           66 :             if (arg_no - 1 >= macro->paramc)
   19812            0 :               sec.set_overrun ();
   19813           66 :             token->val.macro_arg.arg_no = arg_no;
   19814           66 :             token->val.macro_arg.spelling = sec.cpp_node ();
   19815              :           }
   19816           66 :           break;
   19817              : 
   19818          219 :         case CPP_TOKEN_FLD_NODE:
   19819              :           /* An identifier.  */
   19820          219 :           token->val.node.node = sec.cpp_node ();
   19821          219 :           token->val.node.spelling = sec.cpp_node ();
   19822          219 :           if (!token->val.node.spelling)
   19823          219 :             token->val.node.spelling = token->val.node.node;
   19824              :           break;
   19825              : 
   19826              :         case CPP_TOKEN_FLD_NONE:
   19827              :           break;
   19828              : 
   19829          312 :         case CPP_TOKEN_FLD_STR:
   19830              :           /* A string, number or comment.  */
   19831          312 :           token->val.str.len = sec.u ();
   19832          312 :           len += token->val.str.len + 1;
   19833          312 :           break;
   19834              :         }
   19835              :     }
   19836              : 
   19837          460 :   if (len)
   19838          310 :     if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
   19839              :       {
   19840              :         /* There should be a final NUL.  */
   19841          310 :         if (ptr[len-1])
   19842            0 :           sec.set_overrun ();
   19843              :         /* cpp_alloc_token_string will add a final NUL.  */
   19844          310 :         const unsigned char *buf
   19845          310 :           = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
   19846          310 :         len = 0;
   19847         1243 :         for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19848              :           {
   19849          623 :             cpp_token *token = &macro->exp.tokens[ix];
   19850          623 :             if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19851              :               {
   19852          312 :                 token->val.str.text = buf + len;
   19853          312 :                 len += token->val.str.len;
   19854          312 :                 if (buf[len++])
   19855            0 :                   sec.set_overrun ();
   19856              :               }
   19857              :           }
   19858              :       }
   19859              : 
   19860          460 :   if (sec.get_overrun ())
   19861            0 :     return NULL;
   19862              :   return macro;
   19863          460 : }
   19864              : 
   19865              : /* Exported macro data.  */
   19866              : struct GTY(()) macro_export {
   19867              :   cpp_macro *def;
   19868              :   location_t undef_loc;
   19869              : 
   19870       110604 :   macro_export ()
   19871       110604 :     :def (NULL), undef_loc (UNKNOWN_LOCATION)
   19872              :   {
   19873              :   }
   19874              : };
   19875              : 
   19876              : /* Imported macro data.  */
   19877              : class macro_import {
   19878              : public:
   19879              :   struct slot {
   19880              : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
   19881              :     int offset;
   19882              : #endif
   19883              :     /* We need to ensure we don't use the LSB for representation, as
   19884              :        that's the union discriminator below.  */
   19885              :     unsigned bits;
   19886              : 
   19887              : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
   19888              :     int offset;
   19889              : #endif
   19890              : 
   19891              :   public:
   19892              :     enum Layout {
   19893              :       L_DEF = 1,
   19894              :       L_UNDEF = 2,
   19895              :       L_BOTH = 3,
   19896              :       L_MODULE_SHIFT = 2
   19897              :     };
   19898              : 
   19899              :   public:
   19900              :     /* Not a regular ctor, because we put it in a union, and that's
   19901              :        not allowed in C++ 98.  */
   19902       190088 :     static slot ctor (unsigned module, unsigned defness)
   19903              :     {
   19904       190088 :       gcc_checking_assert (defness);
   19905       190088 :       slot s;
   19906       190088 :       s.bits = defness | (module << L_MODULE_SHIFT);
   19907       190088 :       s.offset = -1;
   19908       190088 :       return s;
   19909              :     }
   19910              : 
   19911              :   public:
   19912       159555 :     unsigned get_defness () const
   19913              :     {
   19914       159555 :       return bits & L_BOTH;
   19915              :     }
   19916       113085 :     unsigned get_module () const
   19917              :     {
   19918       113085 :       return bits >> L_MODULE_SHIFT;
   19919              :     }
   19920           12 :     void become_undef ()
   19921              :     {
   19922           12 :       bits &= ~unsigned (L_DEF);
   19923           12 :       bits |= unsigned (L_UNDEF);
   19924              :     }
   19925              :   };
   19926              : 
   19927              : private:
   19928              :   typedef vec<slot, va_heap, vl_embed> ary_t;
   19929              :   union either {
   19930              :     /* Discriminated by bits 0|1 != 0.  The expected case is that
   19931              :        there will be exactly one slot per macro, hence the effort of
   19932              :        packing that.  */
   19933              :     ary_t *ary;
   19934              :     slot single;
   19935              :   } u;
   19936              : 
   19937              : public:
   19938       153676 :   macro_import ()
   19939              :   {
   19940       153676 :     u.ary = NULL;
   19941              :   }
   19942              : 
   19943              : private:
   19944      8699514 :   bool single_p () const
   19945              :   {
   19946      8699514 :     return u.single.bits & slot::L_BOTH;
   19947              :   }
   19948      8853323 :   bool occupied_p () const
   19949              :   {
   19950      8853323 :     return u.ary != NULL;
   19951              :   }
   19952              : 
   19953              : public:
   19954         1338 :   unsigned length () const
   19955              :   {
   19956         1338 :     gcc_checking_assert (occupied_p ());
   19957         1338 :     return single_p () ? 1 : u.ary->length ();
   19958              :   }
   19959      8551626 :   slot &operator[] (unsigned ix)
   19960              :   {
   19961      8551626 :     gcc_checking_assert (occupied_p ());
   19962      8551626 :     if (single_p ())
   19963              :       {
   19964      8444855 :         gcc_checking_assert (!ix);
   19965      8444855 :         return u.single;
   19966              :       }
   19967              :     else
   19968       106771 :       return (*u.ary)[ix];
   19969              :   }
   19970              : 
   19971              : public:
   19972              :   slot &exported ();
   19973              :   slot &append (unsigned module, unsigned defness);
   19974              : };
   19975              : 
   19976              : /* O is a new import to append to the list for.  If we're an empty
   19977              :    set, initialize us.  */
   19978              : 
   19979              : macro_import::slot &
   19980       190088 : macro_import::append (unsigned module, unsigned defness)
   19981              : {
   19982       190088 :   if (!occupied_p ())
   19983              :     {
   19984       153676 :       u.single = slot::ctor (module, defness);
   19985       153676 :       return u.single;
   19986              :     }
   19987              :   else
   19988              :     {
   19989        36412 :       bool single = single_p ();
   19990        36412 :       ary_t *m = single ? NULL : u.ary;
   19991        36412 :       vec_safe_reserve (m, 1 + single);
   19992        36412 :       if (single)
   19993        36409 :         m->quick_push (u.single);
   19994        36412 :       u.ary = m;
   19995        36412 :       return *u.ary->quick_push (slot::ctor (module, defness));
   19996              :     }
   19997              : }
   19998              : 
   19999              : /* We're going to export something.  Make sure the first import slot
   20000              :    is us.  */
   20001              : 
   20002              : macro_import::slot &
   20003       110271 : macro_import::exported ()
   20004              : {
   20005       110271 :   if (occupied_p () && !(*this)[0].get_module ())
   20006              :     {
   20007          133 :       slot &res = (*this)[0];
   20008          133 :       res.bits |= slot::L_DEF;
   20009          133 :       return res;
   20010              :     }
   20011              : 
   20012       110138 :   slot *a = &append (0, slot::L_DEF);
   20013       110138 :   if (!single_p ())
   20014              :     {
   20015        32056 :       slot &f = (*this)[0];
   20016        32056 :       std::swap (f, *a);
   20017        32056 :       a = &f;
   20018              :     }
   20019              :   return *a;
   20020              : }
   20021              : 
   20022              : /* The import (&exported) macros.  cpp_hasnode's deferred field
   20023              :    indexes this array (offset by 1, so zero means 'not present'.  */
   20024              : 
   20025              : static vec<macro_import, va_heap, vl_embed> *macro_imports;
   20026              : 
   20027              : /* The exported macros.  A macro_import slot's zeroth element's offset
   20028              :    indexes this array.  If the zeroth slot is not for module zero,
   20029              :    there is no export.  */
   20030              : 
   20031              : static GTY(()) vec<macro_export, va_gc> *macro_exports;
   20032              : 
   20033              : /* The reachable set of header imports from this TU.  */
   20034              : 
   20035              : static GTY(()) bitmap headers;
   20036              : 
   20037              : /* Get the (possibly empty) macro imports for NODE.  */
   20038              : 
   20039              : static macro_import &
   20040       158165 : get_macro_imports (cpp_hashnode *node)
   20041              : {
   20042       158165 :   if (node->deferred)
   20043         4489 :     return (*macro_imports)[node->deferred - 1];
   20044              : 
   20045       153676 :   vec_safe_reserve (macro_imports, 1);
   20046       153676 :   node->deferred = macro_imports->length () + 1;
   20047       153676 :   return *vec_safe_push (macro_imports, macro_import ());
   20048              : }
   20049              : 
   20050              : /* Get the macro export for export EXP of NODE.  */
   20051              : 
   20052              : static macro_export &
   20053       110271 : get_macro_export (macro_import::slot &slot)
   20054              : {
   20055       110271 :   if (slot.offset >= 0)
   20056          133 :     return (*macro_exports)[slot.offset];
   20057              : 
   20058       110138 :   vec_safe_reserve (macro_exports, 1);
   20059       110138 :   slot.offset = macro_exports->length ();
   20060       110138 :   return *macro_exports->quick_push (macro_export ());
   20061              : }
   20062              : 
   20063              : /* If NODE is an exportable macro, add it to the export set.  */
   20064              : 
   20065              : static int
   20066      4001420 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
   20067              : {
   20068      4001420 :   bool exporting = false;
   20069              : 
   20070      4001420 :   if (cpp_user_macro_p (node))
   20071       514397 :     if (cpp_macro *macro = node->value.macro)
   20072              :       /* Ignore imported, builtins, command line and forced header macros.  */
   20073       513940 :       if (!macro->imported_p
   20074       513940 :           && !macro->lazy && macro->line >= spans.main_start ())
   20075              :         {
   20076        78215 :           gcc_checking_assert (macro->kind == cmk_macro);
   20077              :           /* I don't want to deal with this corner case, that I suspect is
   20078              :              a devil's advocate reading of the standard.  */
   20079        78215 :           gcc_checking_assert (!macro->extra_tokens);
   20080              : 
   20081        78215 :           macro_import::slot &slot = get_macro_imports (node).exported ();
   20082        78215 :           macro_export &exp = get_macro_export (slot);
   20083        78215 :           exp.def = macro;
   20084        78215 :           exporting = true;
   20085              :         }
   20086              : 
   20087      3923205 :   if (!exporting && node->deferred)
   20088              :     {
   20089          613 :       macro_import &imports = (*macro_imports)[node->deferred - 1];
   20090          613 :       macro_import::slot &slot = imports[0];
   20091          613 :       if (!slot.get_module ())
   20092              :         {
   20093          582 :           gcc_checking_assert (slot.get_defness ());
   20094              :           exporting = true;
   20095              :         }
   20096              :     }
   20097              : 
   20098        78215 :   if (exporting)
   20099        78797 :     static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
   20100              : 
   20101      4001420 :   return 1; /* Don't stop.  */
   20102              : }
   20103              : 
   20104              : /* Order cpp_hashnodes A_ and B_ by their exported macro locations.  */
   20105              : 
   20106              : static int
   20107      4124228 : macro_loc_cmp (const void *a_, const void *b_)
   20108              : {
   20109      4124228 :   const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
   20110      4124228 :   macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
   20111      4124228 :   const macro_export &export_a = (*macro_exports)[import_a[0].offset];
   20112      4124228 :   location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
   20113              : 
   20114      4124228 :   const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
   20115      4124228 :   macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
   20116      4124228 :   const macro_export &export_b = (*macro_exports)[import_b[0].offset];
   20117      4124228 :   location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
   20118              : 
   20119      4124228 :   if (loc_a < loc_b)
   20120              :     return +1;
   20121      2119233 :   else if (loc_a > loc_b)
   20122              :     return -1;
   20123              :   else
   20124            0 :     return 0;
   20125              : }
   20126              : 
   20127              : /* Gather the macro definitions and undefinitions that we will need to
   20128              :    write out.   */
   20129              : 
   20130              : vec<cpp_hashnode *> *
   20131          909 : module_state::prepare_macros (cpp_reader *reader)
   20132              : {
   20133          909 :   vec<cpp_hashnode *> *macros;
   20134          909 :   vec_alloc (macros, 100);
   20135              : 
   20136          909 :   cpp_forall_identifiers (reader, maybe_add_macro, macros);
   20137              : 
   20138          933 :   dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
   20139              : 
   20140          909 :   macros->qsort (macro_loc_cmp);
   20141              : 
   20142              :   // Note the locations.
   20143        80615 :   for (unsigned ix = macros->length (); ix--;)
   20144              :     {
   20145        78797 :       cpp_hashnode *node = (*macros)[ix];
   20146        78797 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20147        78797 :       macro_export &mac = (*macro_exports)[slot.offset];
   20148              : 
   20149        78797 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20150            1 :         continue;
   20151              : 
   20152        78796 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20153           12 :         note_location (mac.undef_loc);
   20154        78796 :       if (mac.def)
   20155              :         {
   20156        78790 :           note_location (mac.def->line);
   20157       330211 :           for (unsigned ix = 0; ix != mac.def->count; ix++)
   20158       172631 :             note_location (mac.def->exp.tokens[ix].src_loc);
   20159              :         }
   20160              :     }
   20161              : 
   20162          909 :   return macros;
   20163              : }
   20164              : 
   20165              : /* Write out the exported defines.  This is two sections, one
   20166              :    containing the definitions, the other a table of node names.  */
   20167              : 
   20168              : unsigned
   20169          909 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
   20170              :                             unsigned *crc_p)
   20171              : {
   20172          976 :   dump () && dump ("Writing macros");
   20173          909 :   dump.indent ();
   20174              : 
   20175              :   /* Write the defs */
   20176          909 :   bytes_out sec (to);
   20177          909 :   sec.begin ();
   20178              : 
   20179          909 :   unsigned count = 0;
   20180        80615 :   for (unsigned ix = macros->length (); ix--;)
   20181              :     {
   20182        78797 :       cpp_hashnode *node = (*macros)[ix];
   20183        78797 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20184        78797 :       gcc_assert (!slot.get_module () && slot.get_defness ());
   20185              : 
   20186        78797 :       macro_export &mac = (*macro_exports)[slot.offset];
   20187        78797 :       gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
   20188              :                   == (mac.undef_loc != UNKNOWN_LOCATION)
   20189              :                   && !!(slot.get_defness () & macro_import::slot::L_DEF)
   20190              :                   == (mac.def != NULL));
   20191              : 
   20192        78797 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20193              :         {
   20194            1 :           warning_at (mac.def->line, 0,
   20195              :                       "not exporting %<#define %E%> as it is a keyword",
   20196              :                       identifier (node));
   20197            1 :           slot.offset = 0;
   20198            1 :           continue;
   20199              :         }
   20200              : 
   20201        78796 :       count++;
   20202        78796 :       slot.offset = sec.pos;
   20203        78796 :       dump (dumper::MACRO)
   20204           24 :         && dump ("Writing macro %s%s%s %I at %u",
   20205           24 :                  slot.get_defness () & macro_import::slot::L_UNDEF
   20206              :                  ? "#undef" : "",
   20207           24 :                  slot.get_defness () == macro_import::slot::L_BOTH
   20208              :                  ? " & " : "",
   20209           24 :                  slot.get_defness () & macro_import::slot::L_DEF
   20210              :                  ? "#define" : "",
   20211              :                  identifier (node), slot.offset);
   20212        78796 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20213           12 :         write_location (sec, mac.undef_loc);
   20214        78796 :       if (mac.def)
   20215        78790 :         write_define (sec, mac.def);
   20216              :     }
   20217          909 :   if (count)
   20218              :     // We may have ended on a tokenless macro with a very short
   20219              :     // location, that will cause problems reading its bit flags.
   20220          145 :     sec.u (0);
   20221          909 :   sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
   20222              : 
   20223          909 :   if (count)
   20224              :     {
   20225              :       /* Write the table.  */
   20226          145 :       bytes_out sec (to);
   20227          145 :       sec.begin ();
   20228          145 :       sec.u (count);
   20229              : 
   20230        79086 :       for (unsigned ix = macros->length (); ix--;)
   20231              :         {
   20232        78796 :           const cpp_hashnode *node = (*macros)[ix];
   20233        78796 :           macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20234              : 
   20235        78796 :           if (slot.offset)
   20236              :             {
   20237        78796 :               sec.cpp_node (node);
   20238        78796 :               sec.u (slot.get_defness ());
   20239        78796 :               sec.u (slot.offset);
   20240              :             }
   20241              :         }
   20242          145 :       sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
   20243          145 :     }
   20244              : 
   20245          909 :   dump.outdent ();
   20246          909 :   return count;
   20247          909 : }
   20248              : 
   20249              : bool
   20250          939 : module_state::read_macros ()
   20251              : {
   20252              :   /* Get the def section.  */
   20253          939 :   if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
   20254              :     return false;
   20255              : 
   20256              :   /* Get the tbl section, if there are defs. */
   20257          939 :   if (slurp->macro_defs.more_p ()
   20258          939 :       && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
   20259              :     return false;
   20260              : 
   20261              :   return true;
   20262              : }
   20263              : 
   20264              : /* Install the macro name table.  */
   20265              : 
   20266              : void
   20267          945 : module_state::install_macros ()
   20268              : {
   20269          945 :   bytes_in &sec = slurp->macro_tbl;
   20270          945 :   if (!sec.size)
   20271              :     return;
   20272              : 
   20273          202 :   dump () && dump ("Reading macro table %M", this);
   20274          180 :   dump.indent ();
   20275              : 
   20276          180 :   unsigned count = sec.u ();
   20277          202 :   dump () && dump ("%u macros", count);
   20278        80130 :   while (count--)
   20279              :     {
   20280        79950 :       cpp_hashnode *node = sec.cpp_node ();
   20281        79950 :       macro_import &imp = get_macro_imports (node);
   20282        79950 :       unsigned flags = sec.u () & macro_import::slot::L_BOTH;
   20283        79950 :       if (!flags)
   20284            0 :         sec.set_overrun ();
   20285              : 
   20286        79950 :       if (sec.get_overrun ())
   20287              :         break;
   20288              : 
   20289        79950 :       macro_import::slot &slot = imp.append (mod, flags);
   20290        79950 :       slot.offset = sec.u ();
   20291              : 
   20292        79950 :       dump (dumper::MACRO)
   20293           84 :         && dump ("Read %s macro %s%s%s %I at %u",
   20294           30 :                  imp.length () > 1 ? "add" : "new",
   20295           27 :                  flags & macro_import::slot::L_UNDEF ? "#undef" : "",
   20296              :                  flags == macro_import::slot::L_BOTH ? " & " : "",
   20297           30 :                  flags & macro_import::slot::L_DEF ? "#define" : "",
   20298              :                  identifier (node), slot.offset);
   20299              : 
   20300              :       /* We'll leak an imported definition's TOKEN_FLD_STR's data
   20301              :          here.  But that only happens when we've had to resolve the
   20302              :          deferred macro before this import -- why are you doing
   20303              :          that?  */
   20304        79950 :       if (cpp_macro *cur = cpp_set_deferred_macro (node))
   20305        32044 :         if (!cur->imported_p)
   20306              :           {
   20307        32044 :             macro_import::slot &slot = imp.exported ();
   20308        32044 :             macro_export &exp = get_macro_export (slot);
   20309        32044 :             exp.def = cur;
   20310        79950 :             dump (dumper::MACRO)
   20311            0 :               && dump ("Saving current #define %I", identifier (node));
   20312              :           }
   20313              :     }
   20314              : 
   20315              :   /* We're now done with the table.  */
   20316          180 :   elf_in::release (slurp->from, sec);
   20317              : 
   20318          180 :   dump.outdent ();
   20319              : }
   20320              : 
   20321              : /* Import the transitive macros.  */
   20322              : 
   20323              : void
   20324          903 : module_state::import_macros ()
   20325              : {
   20326          903 :   bitmap_ior_into (headers, slurp->headers);
   20327              : 
   20328          903 :   bitmap_iterator bititer;
   20329          903 :   unsigned bitnum;
   20330         1848 :   EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
   20331          945 :     (*modules)[bitnum]->install_macros ();
   20332          903 : }
   20333              : 
   20334              : /* NODE is being undefined at LOC.  Record it in the export table, if
   20335              :    necessary.  */
   20336              : 
   20337              : void
   20338       335308 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
   20339              : {
   20340       335308 :   if (!node->deferred)
   20341              :     /* The macro is not imported, so our undef is irrelevant.  */
   20342              :     return;
   20343              : 
   20344           12 :   unsigned n = dump.push (NULL);
   20345              : 
   20346           12 :   macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
   20347           12 :   macro_export &exp = get_macro_export (slot);
   20348              : 
   20349           12 :   exp.undef_loc = loc;
   20350           12 :   slot.become_undef ();
   20351           12 :   exp.def = NULL;
   20352              : 
   20353           18 :   dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
   20354              : 
   20355           12 :   dump.pop (n);
   20356              : }
   20357              : 
   20358              : /* NODE is a deferred macro node.  Determine the definition and return
   20359              :    it, with NULL if undefined.  May issue diagnostics.
   20360              : 
   20361              :    This can leak memory, when merging declarations -- the string
   20362              :    contents (TOKEN_FLD_STR) of each definition are allocated in
   20363              :    unreclaimable cpp objstack.  Only one will win.  However, I do not
   20364              :    expect this to be common -- mostly macros have a single point of
   20365              :    definition.  Perhaps we could restore the objstack to its position
   20366              :    after the first imported definition (if that wins)?  The macros
   20367              :    themselves are GC'd.  */
   20368              : 
   20369              : cpp_macro *
   20370          436 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
   20371              :                               cpp_hashnode *node)
   20372              : {
   20373          436 :   macro_import &imports = (*macro_imports)[node->deferred - 1];
   20374              : 
   20375          436 :   unsigned n = dump.push (NULL);
   20376          442 :   dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
   20377              : 
   20378          436 :   bitmap visible (BITMAP_GGC_ALLOC ());
   20379              : 
   20380          436 :   if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
   20381            0 :         && !imports[0].get_module ()))
   20382              :     {
   20383              :       /* Calculate the set of visible header imports.  */
   20384          436 :       bitmap_copy (visible, headers);
   20385         1179 :       for (unsigned ix = imports.length (); ix--;)
   20386              :         {
   20387          743 :           const macro_import::slot &slot = imports[ix];
   20388          743 :           unsigned mod = slot.get_module ();
   20389          743 :           if ((slot.get_defness () & macro_import::slot::L_UNDEF)
   20390          743 :               && bitmap_bit_p (visible, mod))
   20391              :             {
   20392           12 :               bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
   20393           12 :               bitmap_and_compl_into (visible, arg);
   20394           12 :               bitmap_set_bit (visible, mod);
   20395              :             }
   20396              :         }
   20397              :     }
   20398          436 :   bitmap_set_bit (visible, 0);
   20399              : 
   20400              :   /* Now find the macros that are still visible.  */
   20401          436 :   bool failed = false;
   20402          436 :   cpp_macro *def = NULL;
   20403          436 :   vec<macro_export> defs;
   20404          436 :   defs.create (imports.length ());
   20405         1179 :   for (unsigned ix = imports.length (); ix--;)
   20406              :     {
   20407          743 :       const macro_import::slot &slot = imports[ix];
   20408          743 :       unsigned mod = slot.get_module ();
   20409          743 :       if (bitmap_bit_p (visible, mod))
   20410              :         {
   20411          731 :           macro_export *pushed = NULL;
   20412          731 :           if (mod)
   20413              :             {
   20414          466 :               const module_state *imp = (*modules)[mod];
   20415          466 :               bytes_in &sec = imp->slurp->macro_defs;
   20416          466 :               if (!sec.get_overrun ())
   20417              :                 {
   20418          466 :                   dump (dumper::MACRO)
   20419            6 :                     && dump ("Reading macro %s%s%s %I module %M at %u",
   20420            6 :                              slot.get_defness () & macro_import::slot::L_UNDEF
   20421              :                              ? "#undef" : "",
   20422            6 :                              slot.get_defness () == macro_import::slot::L_BOTH
   20423              :                              ? " & " : "",
   20424            6 :                              slot.get_defness () & macro_import::slot::L_DEF
   20425              :                              ? "#define" : "",
   20426            6 :                              identifier (node), imp, slot.offset);
   20427          466 :                   sec.random_access (slot.offset);
   20428              : 
   20429          466 :                   macro_export exp;
   20430          466 :                   if (slot.get_defness () & macro_import::slot::L_UNDEF)
   20431           12 :                     exp.undef_loc = imp->read_location (sec);
   20432          466 :                   if (slot.get_defness () & macro_import::slot::L_DEF)
   20433          460 :                     exp.def = imp->read_define (sec, reader);
   20434          466 :                   if (sec.get_overrun ())
   20435            0 :                     error_at (loc, "macro definitions of %qE corrupted",
   20436            0 :                               imp->name);
   20437              :                   else
   20438          466 :                     pushed = defs.quick_push (exp);
   20439              :                 }
   20440              :             }
   20441              :           else
   20442          265 :             pushed = defs.quick_push ((*macro_exports)[slot.offset]);
   20443          731 :           if (pushed && pushed->def)
   20444              :             {
   20445          725 :               if (!def)
   20446              :                 def = pushed->def;
   20447          292 :               else if (cpp_compare_macros (def, pushed->def))
   20448          743 :                 failed = true;
   20449              :             }
   20450              :         }
   20451              :     }
   20452              : 
   20453          436 :   if (failed)
   20454              :     {
   20455              :       /* If LOC is the first loc, this is the end of file check, which
   20456              :          is a warning.  */
   20457           15 :       auto_diagnostic_group d;
   20458           15 :       if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
   20459            9 :         warning_at (loc, OPT_Winvalid_imported_macros,
   20460              :                     "inconsistent imported macro definition %qE",
   20461              :                     identifier (node));
   20462              :       else
   20463            6 :         error_at (loc, "inconsistent imported macro definition %qE",
   20464              :                   identifier (node));
   20465           60 :       for (unsigned ix = defs.length (); ix--;)
   20466              :         {
   20467           30 :           macro_export &exp = defs[ix];
   20468           30 :           if (exp.undef_loc)
   20469            0 :             inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
   20470           30 :           if (exp.def)
   20471           30 :             inform (exp.def->line, "%<#define %s%>",
   20472              :                     cpp_macro_definition (reader, node, exp.def));
   20473              :         }
   20474           15 :       def = NULL;
   20475           15 :     }
   20476              : 
   20477          436 :   defs.release ();
   20478              : 
   20479          436 :   dump.pop (n);
   20480              : 
   20481          436 :   return def;
   20482              : }
   20483              : 
   20484              : /* Stream the static aggregates.  Sadly some headers (ahem:
   20485              :    iostream) contain static vars, and rely on them to run global
   20486              :    ctors.  */
   20487              : unsigned
   20488          909 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
   20489              : {
   20490          909 :   if (!static_aggregates && !tls_aggregates)
   20491              :     return 0;
   20492              : 
   20493           45 :   dump () && dump ("Writing initializers");
   20494           45 :   dump.indent ();
   20495              : 
   20496           45 :   static_aggregates = nreverse (static_aggregates);
   20497           45 :   tls_aggregates = nreverse (tls_aggregates);
   20498              : 
   20499           45 :   unsigned count = 0;
   20500           45 :   trees_out sec (to, this, table, ~0u);
   20501           45 :   sec.begin ();
   20502              : 
   20503           45 :   tree list = static_aggregates;
   20504          135 :   for (int passes = 0; passes != 2; passes++)
   20505              :     {
   20506          258 :       for (tree init = list; init; init = TREE_CHAIN (init))
   20507          168 :         if (TREE_LANG_FLAG_0 (init))
   20508              :           {
   20509          144 :             if (STATIC_INIT_DECOMP_BASE_P (init))
   20510              :               {
   20511              :                 /* Ensure that in the returned result chain if the
   20512              :                    STATIC_INIT_DECOMP_*BASE_P flags are set, there is
   20513              :                    always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
   20514              :                    followed by one or more STATIC_INIT_DECOMP_NONBASE_P.  */
   20515           21 :                 int phase = 0;
   20516           21 :                 tree last = NULL_TREE;
   20517           21 :                 for (tree init2 = TREE_CHAIN (init);
   20518          102 :                      init2; init2 = TREE_CHAIN (init2))
   20519              :                   {
   20520          123 :                     if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
   20521              :                       ;
   20522          102 :                     else if (phase == 0
   20523          123 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20524              :                       {
   20525          102 :                         phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
   20526              :                         last = init2;
   20527              :                       }
   20528           81 :                     else if (IN_RANGE (phase, 1, 2)
   20529          162 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20530              :                       {
   20531           60 :                         if (TREE_LANG_FLAG_0 (init2))
   20532           81 :                           phase = 2;
   20533              :                         last = init2;
   20534              :                       }
   20535              :                     else
   20536              :                       break;
   20537              :                   }
   20538           21 :                 if (phase == 2)
   20539              :                   {
   20540              :                     /* In that case, add markers about it so that the
   20541              :                        STATIC_INIT_DECOMP_BASE_P and
   20542              :                        STATIC_INIT_DECOMP_NONBASE_P flags can be restored.  */
   20543           21 :                     sec.tree_node (build_int_cst (integer_type_node,
   20544           21 :                                                   2 * passes + 1));
   20545           21 :                     phase = 1;
   20546          123 :                     for (tree init2 = init; init2 != TREE_CHAIN (last);
   20547          102 :                          init2 = TREE_CHAIN (init2))
   20548          102 :                       if (TREE_LANG_FLAG_0 (init2))
   20549              :                         {
   20550          102 :                           tree decl = TREE_VALUE (init2);
   20551          102 :                           if (phase == 1
   20552          102 :                               && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20553              :                             {
   20554           21 :                               sec.tree_node (build_int_cst (integer_type_node,
   20555           21 :                                                             2 * passes + 2));
   20556           21 :                               phase = 2;
   20557              :                             }
   20558          102 :                           dump ("Initializer:%u for %N", count, decl);
   20559          102 :                           sec.tree_node (decl);
   20560          102 :                           ++count;
   20561              :                         }
   20562           21 :                     sec.tree_node (integer_zero_node);
   20563           21 :                     init = last;
   20564           21 :                     continue;
   20565           21 :                   }
   20566              :               }
   20567              : 
   20568          123 :             tree decl = TREE_VALUE (init);
   20569              : 
   20570          123 :             dump ("Initializer:%u for %N", count, decl);
   20571          123 :             sec.tree_node (decl);
   20572          123 :             ++count;
   20573              :           }
   20574              : 
   20575           90 :       list = tls_aggregates;
   20576              :     }
   20577              : 
   20578           45 :   sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
   20579           45 :   dump.outdent ();
   20580              : 
   20581           45 :   return count;
   20582           45 : }
   20583              : 
   20584              : /* We have to defer some post-load processing until we've completed
   20585              :    reading, because they can cause more reading.  */
   20586              : 
   20587              : static void
   20588        12474 : post_load_processing ()
   20589              : {
   20590              :   /* We mustn't cause a GC, our caller should have arranged for that
   20591              :      not to happen.  */
   20592        12474 :   gcc_checking_assert (function_depth);
   20593              : 
   20594        12474 :   if (!post_load_decls)
   20595              :     return;
   20596              : 
   20597         7789 :   tree old_cfd = current_function_decl;
   20598         7789 :   struct function *old_cfun = cfun;
   20599        16757 :   while (post_load_decls->length ())
   20600              :     {
   20601         8968 :       tree decl = post_load_decls->pop ();
   20602              : 
   20603         9023 :       dump () && dump ("Post-load processing of %N", decl);
   20604              : 
   20605         8968 :       if (VAR_P (decl) && DECL_NTTP_OBJECT_P (decl))
   20606              :         {
   20607            9 :           if (!DECL_SIZE (decl))
   20608              :             {
   20609            3 :               push_to_top_level ();
   20610            3 :               cp_finish_decl (decl, DECL_INITIAL (decl), false, NULL_TREE, 0);
   20611            3 :               pop_from_top_level ();
   20612              :             }
   20613            9 :           continue;
   20614              :         }
   20615              : 
   20616         8959 :       gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
   20617         8959 :       expand_or_defer_fn (decl);
   20618              :       /* As in module_state::read_cluster.  */
   20619          444 :       if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   20620         8987 :           && DECL_NOT_REALLY_EXTERN (decl))
   20621            6 :         DECL_EXTERNAL (decl) = false;
   20622              :     }
   20623              : 
   20624         7789 :   set_cfun (old_cfun);
   20625         7789 :   current_function_decl = old_cfd;
   20626              : }
   20627              : 
   20628              : bool
   20629           45 : module_state::read_inits (unsigned count)
   20630              : {
   20631           45 :   trees_in sec (this);
   20632           45 :   if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
   20633              :     return false;
   20634           57 :   dump () && dump ("Reading %u initializers", count);
   20635           45 :   dump.indent ();
   20636              : 
   20637           45 :   lazy_snum = ~0u;
   20638           45 :   int decomp_phase = 0;
   20639           45 :   tree *aggrp = NULL;
   20640          270 :   for (unsigned ix = 0; ix != count; ix++)
   20641              :     {
   20642          225 :       tree last = NULL_TREE;
   20643          225 :       if (decomp_phase)
   20644          102 :         last = *aggrp;
   20645              :       /* Merely referencing the decl causes its initializer to be read
   20646              :          and added to the correct list.  */
   20647          225 :       tree decl = sec.tree_node ();
   20648              :       /* module_state::write_inits can add special INTEGER_CST markers in
   20649              :          between the decls.  1 means STATIC_INIT_DECOMP_BASE_P entries
   20650              :          follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
   20651              :          entries follow in static_aggregates, 3 means
   20652              :          STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
   20653              :          4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
   20654              :          0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence.  */
   20655          225 :       if (tree_fits_shwi_p (decl))
   20656              :         {
   20657           63 :           if (sec.get_overrun ())
   20658              :             break;
   20659           63 :           decomp_phase = tree_to_shwi (decl);
   20660           63 :           if (decomp_phase)
   20661              :             {
   20662           42 :               aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
   20663              :               last = *aggrp;
   20664              :             }
   20665           63 :           decl = sec.tree_node ();
   20666              :         }
   20667              : 
   20668          225 :       if (sec.get_overrun ())
   20669              :         break;
   20670          225 :       if (decl)
   20671          225 :         dump ("Initializer:%u for %N", ix, decl);
   20672          225 :       if (decomp_phase)
   20673              :         {
   20674          102 :           tree init = *aggrp;
   20675          102 :           gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
   20676          102 :           if ((decomp_phase & 1) != 0)
   20677           21 :             STATIC_INIT_DECOMP_BASE_P (init) = 1;
   20678              :           else
   20679           81 :             STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
   20680              :         }
   20681              :     }
   20682           45 :   if (decomp_phase && !sec.get_overrun ())
   20683              :     {
   20684            0 :       tree decl = sec.tree_node ();
   20685            0 :       gcc_assert (integer_zerop (decl));
   20686              :     }
   20687           45 :   lazy_snum = 0;
   20688           45 :   post_load_processing ();
   20689           45 :   dump.outdent ();
   20690           45 :   if (!sec.end (from ()))
   20691              :     return false;
   20692              :   return true;
   20693           45 : }
   20694              : 
   20695              : void
   20696         2815 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
   20697              :                             unsigned *crc_ptr)
   20698              : {
   20699         2815 :   bytes_out cfg (to);
   20700              : 
   20701         2815 :   cfg.begin ();
   20702              : 
   20703        30965 :   for (unsigned ix = MSC_HWM; ix--;)
   20704        25335 :     cfg.u (counts[ix]);
   20705              : 
   20706         2815 :   if (dump ())
   20707              :     {
   20708          300 :       dump ("Cluster sections are [%u,%u)",
   20709              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20710          300 :       dump ("Bindings %u", counts[MSC_bindings]);
   20711          300 :       dump ("Pendings %u", counts[MSC_pendings]);
   20712          300 :       dump ("Entities %u", counts[MSC_entities]);
   20713          300 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20714          300 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20715          300 :       dump ("Macros %u", counts[MSC_macros]);
   20716          300 :       dump ("Initializers %u", counts[MSC_inits]);
   20717              :     }
   20718              : 
   20719         2815 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
   20720         2815 : }
   20721              : 
   20722              : bool
   20723         2997 : module_state::read_counts (unsigned counts[MSC_HWM])
   20724              : {
   20725         2997 :   bytes_in cfg;
   20726              : 
   20727         2997 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
   20728              :     return false;
   20729              : 
   20730        29970 :   for (unsigned ix = MSC_HWM; ix--;)
   20731        26973 :     counts[ix] = cfg.u ();
   20732              : 
   20733         2997 :   if (dump ())
   20734              :     {
   20735          532 :       dump ("Declaration sections are [%u,%u)",
   20736              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20737          532 :       dump ("Bindings %u", counts[MSC_bindings]);
   20738          532 :       dump ("Pendings %u", counts[MSC_pendings]);
   20739          532 :       dump ("Entities %u", counts[MSC_entities]);
   20740          532 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20741          532 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20742          532 :       dump ("Macros %u", counts[MSC_macros]);
   20743          532 :       dump ("Initializers %u", counts[MSC_inits]);
   20744              :     }
   20745              : 
   20746         2997 :   return cfg.end (from ());
   20747         2997 : }
   20748              : 
   20749              : /* Tool configuration:  MOD_SNAME_PFX .config
   20750              : 
   20751              :    This is data that confirms current state (or fails).  */
   20752              : 
   20753              : void
   20754         2815 : module_state::write_config (elf_out *to, module_state_config &config,
   20755              :                             unsigned inner_crc)
   20756              : {
   20757         2815 :   bytes_out cfg (to);
   20758              : 
   20759         2815 :   cfg.begin ();
   20760              : 
   20761              :   /* Write version and inner crc as u32 values, for easier
   20762              :      debug inspection.  */
   20763         3115 :   dump () && dump ("Writing version=%V, inner_crc=%x",
   20764              :                    MODULE_VERSION, inner_crc);
   20765         2815 :   cfg.u32 (unsigned (MODULE_VERSION));
   20766         2815 :   cfg.u32 (inner_crc);
   20767              : 
   20768         2815 :   cfg.u (to->name (is_header () ? "" : get_flatname ()));
   20769              : 
   20770              :   /* Configuration. */
   20771         3115 :   dump () && dump ("Writing target='%s', host='%s'",
   20772              :                    TARGET_MACHINE, HOST_MACHINE);
   20773         2815 :   unsigned target = to->name (TARGET_MACHINE);
   20774         2815 :   unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
   20775              :                    ? target : to->name (HOST_MACHINE));
   20776         2815 :   cfg.u (target);
   20777         2815 :   cfg.u (host);
   20778              : 
   20779         2815 :   cfg.str (config.dialect_str);
   20780         2815 :   cfg.u (extensions);
   20781              : 
   20782              :   /* Global tree information.  We write the globals crc separately,
   20783              :      rather than mix it directly into the overall crc, as it is used
   20784              :      to ensure data match between instances of the compiler, not
   20785              :      integrity of the file.  */
   20786         3115 :   dump () && dump ("Writing globals=%u, crc=%x",
   20787              :                    fixed_trees->length (), global_crc);
   20788         2815 :   cfg.u (fixed_trees->length ());
   20789         2815 :   cfg.u32 (global_crc);
   20790              : 
   20791         2815 :   if (is_partition ())
   20792          208 :     cfg.u (is_interface ());
   20793              : 
   20794         2815 :   cfg.u (config.num_imports);
   20795         2815 :   cfg.u (config.num_partitions);
   20796         2815 :   cfg.u (config.num_entities);
   20797              : 
   20798         2815 :   cfg.loc (config.ordinary_locs);
   20799         2815 :   cfg.loc (config.macro_locs);
   20800         2815 :   cfg.u (config.loc_range_bits);
   20801              : 
   20802         2815 :   cfg.u (config.active_init);
   20803              : 
   20804              :   /* Now generate CRC, we'll have incorporated the inner CRC because
   20805              :      of its serialization above.  */
   20806         2815 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
   20807         3115 :   dump () && dump ("Writing CRC=%x", crc);
   20808         2815 : }
   20809              : 
   20810              : void
   20811           40 : module_state::note_cmi_name ()
   20812              : {
   20813           40 :   if (!cmi_noted_p && filename)
   20814              :     {
   20815           40 :       cmi_noted_p = true;
   20816           40 :       inform (loc, "compiled module file is %qs",
   20817              :               maybe_add_cmi_prefix (filename));
   20818              :     }
   20819           40 : }
   20820              : 
   20821              : bool
   20822         3105 : module_state::read_config (module_state_config &config, bool complain)
   20823              : {
   20824         3105 :   bytes_in cfg;
   20825              : 
   20826         3105 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
   20827              :     return false;
   20828              : 
   20829              :   /* Check version.  */
   20830         3105 :   unsigned my_ver = MODULE_VERSION;
   20831         3105 :   unsigned their_ver = cfg.u32 ();
   20832         3640 :   dump () && dump  (my_ver == their_ver ? "Version %V"
   20833              :                     : "Expecting %V found %V", my_ver, their_ver);
   20834         3105 :   if (their_ver != my_ver)
   20835              :     {
   20836              :       /* The compiler versions differ.  Close enough? */
   20837            0 :       verstr_t my_string, their_string;
   20838              : 
   20839            0 :       version2string (my_ver, my_string);
   20840            0 :       version2string (their_ver, their_string);
   20841              : 
   20842              :       /* Reject when either is non-experimental or when experimental
   20843              :          major versions differ.  */
   20844            0 :       auto_diagnostic_group d;
   20845            0 :       bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
   20846              :                         || !IS_EXPERIMENTAL (their_ver)
   20847            0 :                         || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
   20848              :                        /* The 'I know what I'm doing' switch.  */
   20849            0 :                        && !flag_module_version_ignore);
   20850            0 :       bool inform_p = true;
   20851            0 :       if (!complain)
   20852              :         inform_p = false;
   20853            0 :       else if (reject_p)
   20854              :         {
   20855            0 :           cfg.set_overrun ();
   20856            0 :           error_at (loc, "compiled module is %sversion %s",
   20857              :                     IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20858              :                     their_string);
   20859              :         }
   20860              :       else
   20861            0 :         inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
   20862              :                              IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20863              :                              their_string);
   20864              : 
   20865            0 :       if (inform_p)
   20866              :         {
   20867            0 :           inform (loc, "compiler is %sversion %s%s%s",
   20868              :                   IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
   20869              :                   my_string,
   20870            0 :                   reject_p ? "" : flag_module_version_ignore
   20871            0 :                   ? ", be it on your own head!" : ", close enough?",
   20872              :                   reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
   20873            0 :           note_cmi_name ();
   20874              :         }
   20875              : 
   20876            0 :       if (reject_p)
   20877            0 :         goto done;
   20878            0 :     }
   20879              : 
   20880              :   /*  We wrote the inner crc merely to merge it, so simply read it
   20881              :       back and forget it.  */
   20882         3105 :   cfg.u32 ();
   20883              : 
   20884              :   /* Check module name.  */
   20885         3105 :   {
   20886         3105 :     const char *their_name = from ()->name (cfg.u ());
   20887         3105 :     const char *our_name = "";
   20888              : 
   20889         3105 :     if (!is_header ())
   20890         2066 :       our_name = get_flatname ();
   20891              : 
   20892              :     /* Header units can be aliased, so name checking is
   20893              :        inappropriate.  */
   20894         3105 :     if (0 != strcmp (their_name, our_name))
   20895              :       {
   20896            0 :         error_at (loc,
   20897            0 :                   their_name[0] && our_name[0] ? G_("module %qs found")
   20898              :                   : their_name[0]
   20899              :                   ? G_("header module expected, module %qs found")
   20900              :                   : G_("module %qs expected, header module found"),
   20901            0 :                   their_name[0] ? their_name : our_name);
   20902            0 :         cfg.set_overrun ();
   20903            0 :         goto done;
   20904              :       }
   20905              :   }
   20906              : 
   20907              :   /* Check the CRC after the above sanity checks, so that the user is
   20908              :      clued in.  */
   20909         3105 :   {
   20910         3105 :     unsigned e_crc = crc;
   20911         3105 :     crc = cfg.get_crc ();
   20912         3640 :     dump () && dump ("Reading CRC=%x", crc);
   20913              :     /* When not complaining we haven't set directness yet, so ignore the
   20914              :        mismatch. */
   20915         3105 :     if (complain && !is_direct () && crc != e_crc)
   20916              :       {
   20917            3 :         error_at (loc, "module %qs CRC mismatch", get_flatname ());
   20918            3 :         cfg.set_overrun ();
   20919            3 :         goto done;
   20920              :       }
   20921              :   }
   20922              : 
   20923              :   /* Check target & host.  */
   20924         3102 :   {
   20925         3102 :     const char *their_target = from ()->name (cfg.u ());
   20926         3102 :     const char *their_host = from ()->name (cfg.u ());
   20927         3637 :     dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
   20928         3102 :     if (strcmp (their_target, TARGET_MACHINE)
   20929         3102 :         || strcmp (their_host, HOST_MACHINE))
   20930              :       {
   20931            0 :         error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
   20932              :                   their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
   20933            0 :         cfg.set_overrun ();
   20934            0 :         goto done;
   20935              :       }
   20936              :   }
   20937              : 
   20938              :   /* Check compilation dialect.  This must match.  */
   20939         3102 :   {
   20940         3102 :     const char *their_dialect = cfg.str ();
   20941         3102 :     if (strcmp (their_dialect, config.dialect_str))
   20942              :       {
   20943            1 :         if (complain)
   20944            1 :           error_at (loc, "language dialect differs %qs, expected %qs",
   20945              :                     their_dialect, config.dialect_str);
   20946            1 :         cfg.set_overrun ();
   20947            1 :         goto done;
   20948              :       }
   20949              :   }
   20950              : 
   20951              :   /* Check for extensions.  If they set any, we must have them set
   20952              :      too.  */
   20953         3101 :   {
   20954         3101 :     unsigned ext = cfg.u ();
   20955         3101 :     unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
   20956         3101 :     if (flag_openmp_simd)
   20957            3 :       allowed |= SE_OPENMP_SIMD;
   20958         3101 :     if (flag_openacc)
   20959            3 :       allowed |= SE_OPENACC;
   20960              : 
   20961         3101 :     if (unsigned bad = ext & ~allowed)
   20962              :       {
   20963            9 :         if (bad & SE_OPENMP)
   20964            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
   20965            6 :         else if (bad & SE_OPENMP_SIMD)
   20966            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
   20967              :                          "%<-fopenmp-simd%> to enable");
   20968            9 :         if (bad & SE_OPENACC)
   20969            3 :           error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
   20970              :                          "enable");
   20971            9 :         cfg.set_overrun ();
   20972            9 :         goto done;
   20973              :       }
   20974         3092 :     extensions = ext;
   20975              :   }
   20976              : 
   20977              :   /* Check global trees.  */
   20978         3092 :   {
   20979         3092 :     unsigned their_fixed_length = cfg.u ();
   20980         3092 :     unsigned their_fixed_crc = cfg.u32 ();
   20981         3627 :     dump () && dump ("Read globals=%u, crc=%x",
   20982              :                      their_fixed_length, their_fixed_crc);
   20983         3092 :     if (!flag_preprocess_only
   20984         3092 :         && (their_fixed_length != fixed_trees->length ()
   20985         3038 :             || their_fixed_crc != global_crc))
   20986              :       {
   20987            0 :         error_at (loc, "fixed tree mismatch");
   20988            0 :         cfg.set_overrun ();
   20989            0 :         goto done;
   20990              :       }
   20991              :   }
   20992              : 
   20993              :   /* All non-partitions are interfaces.  */
   20994         3092 :   interface_p = !is_partition () || cfg.u ();
   20995              : 
   20996         3092 :   config.num_imports = cfg.u ();
   20997         3092 :   config.num_partitions = cfg.u ();
   20998         3092 :   config.num_entities = cfg.u ();
   20999              : 
   21000         3092 :   config.ordinary_locs = cfg.loc ();
   21001         3092 :   config.macro_locs = cfg.loc ();
   21002         3092 :   config.loc_range_bits = cfg.u ();
   21003              : 
   21004         3092 :   config.active_init = cfg.u ();
   21005              : 
   21006         3105 :  done:
   21007         3105 :   return cfg.end (from ());
   21008         3105 : }
   21009              : 
   21010              : /* Comparator for ordering the Ordered Ordinary Location array.  */
   21011              : 
   21012              : static int
   21013          120 : ool_cmp (const void *a_, const void *b_)
   21014              : {
   21015          120 :   auto *a = *static_cast<const module_state *const *> (a_);
   21016          120 :   auto *b = *static_cast<const module_state *const *> (b_);
   21017          120 :   if (a == b)
   21018              :     return 0;
   21019          120 :   else if (a->ordinary_locs.first < b->ordinary_locs.first)
   21020              :     return -1;
   21021              :   else
   21022           50 :     return +1;
   21023              : }
   21024              : 
   21025              : /* Use ELROND format to record the following sections:
   21026              :      qualified-names        : binding value(s)
   21027              :      MOD_SNAME_PFX.README   : human readable, strings
   21028              :      MOD_SNAME_PFX.ENV      : environment strings, strings
   21029              :      MOD_SNAME_PFX.nms      : namespace hierarchy
   21030              :      MOD_SNAME_PFX.udi      : namespace using-directives
   21031              :      MOD_SNAME_PFX.bnd      : binding table
   21032              :      MOD_SNAME_PFX.spc      : specialization table
   21033              :      MOD_SNAME_PFX.imp      : import table
   21034              :      MOD_SNAME_PFX.ent      : entity table
   21035              :      MOD_SNAME_PFX.prt      : partitions table
   21036              :      MOD_SNAME_PFX.olm      : ordinary line maps
   21037              :      MOD_SNAME_PFX.mlm      : macro line maps
   21038              :      MOD_SNAME_PFX.def      : macro definitions
   21039              :      MOD_SNAME_PFX.mac      : macro index
   21040              :      MOD_SNAME_PFX.ini      : inits
   21041              :      MOD_SNAME_PFX.cnt      : counts
   21042              :      MOD_SNAME_PFX.cfg      : config data
   21043              : */
   21044              : 
   21045              : bool
   21046         2844 : module_state::write_begin (elf_out *to, cpp_reader *reader,
   21047              :                            module_state_config &config, unsigned &crc)
   21048              : {
   21049              :   /* Figure out remapped module numbers, which might elide
   21050              :      partitions.  */
   21051         2844 :   bitmap partitions = NULL;
   21052         2844 :   if (!is_header () && !is_partition ())
   21053         1727 :     partitions = BITMAP_GGC_ALLOC ();
   21054         2844 :   write_init_maps ();
   21055              : 
   21056         2844 :   unsigned mod_hwm = 1;
   21057         6373 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   21058              :     {
   21059          685 :       module_state *imp = (*modules)[ix];
   21060              : 
   21061              :       /* Promote any non-partition direct import from a partition, unless
   21062              :          we're a partition.  */
   21063          625 :       if (!is_partition () && !imp->is_partition ()
   21064         1120 :           && imp->is_partition_direct ())
   21065           12 :         imp->directness = MD_PURVIEW_DIRECT;
   21066              : 
   21067              :       /* Write any import that is not a partition, unless we're a
   21068              :          partition.  */
   21069          685 :       if (!partitions || !imp->is_partition ())
   21070          495 :         imp->remap = mod_hwm++;
   21071              :       else
   21072              :         {
   21073          229 :           dump () && dump ("Partition %M %u", imp, ix);
   21074          190 :           bitmap_set_bit (partitions, ix);
   21075          190 :           imp->remap = 0;
   21076              :           /* All interface partitions must be exported.  */
   21077          190 :           if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
   21078              :             {
   21079            3 :               error_at (imp->loc, "interface partition is not exported");
   21080            3 :               bitmap_set_bit (exports, imp->mod);
   21081              :             }
   21082              : 
   21083              :           /* All the partition entities should have been loaded when
   21084              :              loading the partition.  */
   21085          190 :           if (CHECKING_P)
   21086         1245 :             for (unsigned jx = 0; jx != imp->entity_num; jx++)
   21087              :               {
   21088         1055 :                 binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
   21089         1055 :                 gcc_checking_assert (!slot->is_lazy ());
   21090              :               }
   21091              :         }
   21092              : 
   21093          685 :       if (imp->is_direct () && (imp->remap || imp->is_partition ()))
   21094          668 :         note_location (imp->imported_from ());
   21095              :     }
   21096              : 
   21097         2844 :   if (partitions && bitmap_empty_p (partitions))
   21098              :     /* No partitions present.  */
   21099              :     partitions = nullptr;
   21100              : 
   21101              :   /* Find the set of decls we must write out.  */
   21102         2844 :   depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
   21103              :   /* Add the specializations before the writables, so that we can
   21104              :      detect injected friend specializations.  */
   21105         2844 :   table.add_specializations (true);
   21106         2844 :   table.add_specializations (false);
   21107         2844 :   if (partial_specializations)
   21108              :     {
   21109          216 :       table.add_partial_entities (partial_specializations);
   21110          216 :       partial_specializations = NULL;
   21111              :     }
   21112         2844 :   table.add_namespace_entities (global_namespace, partitions);
   21113         2844 :   if (class_members)
   21114              :     {
   21115           12 :       table.add_class_entities (class_members);
   21116           12 :       class_members = NULL;
   21117              :     }
   21118              : 
   21119              :   /* Now join everything up.  */
   21120         2844 :   table.find_dependencies (this);
   21121              : 
   21122         2844 :   if (!table.finalize_dependencies ())
   21123              :     return false;
   21124              : 
   21125              : #if CHECKING_P
   21126              :   /* We're done verifying at-most once reading, reset to verify
   21127              :      at-most once writing.  */
   21128         2815 :   note_defs = note_defs_table_t::create_ggc (1000);
   21129              : #endif
   21130              : 
   21131              :   /* Determine Strongly Connected Components.  This will also strip any
   21132              :      unnecessary dependencies on imported or TU-local entities.  */
   21133         2815 :   vec<depset *> sccs = table.connect ();
   21134              : 
   21135         2815 :   vec_alloc (ool, modules->length ());
   21136         3493 :   for (unsigned ix = modules->length (); --ix;)
   21137              :     {
   21138          678 :       auto *import = (*modules)[ix];
   21139          678 :       if (import->loadedness > ML_NONE
   21140          678 :           && !(partitions && bitmap_bit_p (partitions, import->mod)))
   21141          488 :         ool->quick_push (import);
   21142              :     }
   21143         2815 :   ool->qsort (ool_cmp);
   21144              : 
   21145         2815 :   write_diagnostic_classification (nullptr, global_dc, nullptr);
   21146              : 
   21147         2815 :   vec<cpp_hashnode *> *macros = nullptr;
   21148         2815 :   if (is_header ())
   21149          909 :     macros = prepare_macros (reader);
   21150              : 
   21151         2815 :   config.num_imports = mod_hwm;
   21152         2815 :   config.num_partitions = modules->length () - mod_hwm;
   21153         2815 :   auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
   21154         2815 :   unsigned counts[MSC_HWM];
   21155         2815 :   memset (counts, 0, sizeof (counts));
   21156              : 
   21157              :   /* depset::cluster is the cluster number,
   21158              :      depset::section is unspecified scratch value.
   21159              : 
   21160              :      The following loops make use of the tarjan property that
   21161              :      dependencies will be earlier in the SCCS array.  */
   21162              : 
   21163              :   /* This first loop determines the number of depsets in each SCC, and
   21164              :      also the number of namespaces we're dealing with.  During the
   21165              :      loop, the meaning of a couple of depset fields now change:
   21166              : 
   21167              :      depset::cluster -> size_of cluster, if first of cluster & !namespace
   21168              :      depset::section -> section number of cluster (if !namespace). */
   21169              : 
   21170         2815 :   unsigned n_spaces = 0;
   21171         2815 :   counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
   21172       346024 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21173              :     {
   21174       343209 :       depset **base = &sccs[ix];
   21175              : 
   21176       649725 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21177              :         {
   21178         5301 :           n_spaces++;
   21179         5301 :           size = 1;
   21180              :         }
   21181              :       else
   21182              :         {
   21183              :           /* Count the members in this cluster.  */
   21184      1536888 :           for (size = 1; ix + size < sccs.length (); size++)
   21185      1534413 :             if (base[size]->cluster != base[0]->cluster)
   21186              :               break;
   21187              : 
   21188      1874796 :           for (unsigned jx = 0; jx != size; jx++)
   21189              :             {
   21190              :               /* Set the section number.  */
   21191      1536888 :               base[jx]->cluster = ~(~0u >> 1); /* A bad value.  */
   21192      1536888 :               base[jx]->section = counts[MSC_sec_hwm];
   21193              :             }
   21194              : 
   21195              :           /* Save the size in the first member's cluster slot.  */
   21196       337908 :           base[0]->cluster = size;
   21197              : 
   21198       337908 :           counts[MSC_sec_hwm]++;
   21199              :         }
   21200              :     }
   21201              : 
   21202              :   /* Write the clusters.  Namespace decls are put in the spaces array.
   21203              :      The meaning of depset::cluster changes to provide the
   21204              :      unnamed-decl count of the depset's decl (and remains zero for
   21205              :      non-decls and non-unnamed).  */
   21206         2815 :   unsigned bytes = 0;
   21207         2815 :   vec<depset *> spaces;
   21208         2815 :   spaces.create (n_spaces);
   21209              : 
   21210       348839 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21211              :     {
   21212       343209 :       depset **base = &sccs[ix];
   21213              : 
   21214       343209 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21215              :         {
   21216         5301 :           tree decl = base[0]->get_entity ();
   21217         5301 :           if (decl == global_namespace)
   21218         2555 :             base[0]->cluster = 0;
   21219         2746 :           else if (!base[0]->is_import ())
   21220              :             {
   21221         2746 :               base[0]->cluster = counts[MSC_entities]++;
   21222         2746 :               spaces.quick_push (base[0]);
   21223         2746 :               counts[MSC_namespaces]++;
   21224         2746 :               if (CHECKING_P)
   21225              :                 {
   21226              :                   /* Add it to the entity map, such that we can tell it is
   21227              :                      part of us.  */
   21228         2746 :                   bool existed;
   21229         2746 :                   unsigned *slot = &entity_map->get_or_insert
   21230         2746 :                     (DECL_UID (decl), &existed);
   21231         2746 :                   if (existed)
   21232              :                     /* It must have come from a partition.  */
   21233            0 :                     gcc_checking_assert
   21234              :                       (import_entity_module (*slot)->is_partition ());
   21235         2746 :                   *slot = ~base[0]->cluster;
   21236              :                 }
   21237       343349 :               dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
   21238              :             }
   21239              :           size = 1;
   21240              :         }
   21241              :       else
   21242              :         {
   21243       337908 :           size = base[0]->cluster;
   21244              : 
   21245              :           /* Cluster is now used to number entities.  */
   21246       337908 :           base[0]->cluster = ~(~0u >> 1); /* A bad value.  */
   21247              : 
   21248       337908 :           sort_cluster (&table, base, size);
   21249              : 
   21250              :           /* Record the section for consistency checking during stream
   21251              :              out -- we don't want to start writing decls in different
   21252              :              sections.  */
   21253       337908 :           table.section = base[0]->section;
   21254       337908 :           bytes += write_cluster (to, base, size, table, counts, &crc);
   21255       337908 :           table.section = 0;
   21256              :         }
   21257              :     }
   21258              : 
   21259              :   /* depset::cluster - entity number (on entities)
   21260              :      depset::section - cluster number  */
   21261              :   /* We'd better have written as many sections and found as many
   21262              :      namespaces as we predicted.  */
   21263         5630 :   gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
   21264              :               && spaces.length () == counts[MSC_namespaces]);
   21265              : 
   21266              :   /* Write the entities.  None happens if we contain namespaces or
   21267              :      nothing. */
   21268         2815 :   config.num_entities = counts[MSC_entities];
   21269         2815 :   if (counts[MSC_entities])
   21270         2549 :     write_entities (to, sccs, counts[MSC_entities], &crc);
   21271              : 
   21272              :   /* Write the namespaces.  */
   21273         2815 :   if (counts[MSC_namespaces])
   21274          597 :     write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
   21275              : 
   21276              :   /* Write any using-directives.  */
   21277         2815 :   if (counts[MSC_namespaces])
   21278          597 :     counts[MSC_using_directives]
   21279          597 :       = write_using_directives (to, table, spaces, &crc);
   21280              : 
   21281              :   /* Write the bindings themselves.  */
   21282         2815 :   counts[MSC_bindings] = write_bindings (to, sccs, &crc);
   21283              : 
   21284              :   /* Write the unnamed.  */
   21285         2815 :   counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
   21286              : 
   21287              :   /* Write the import table.  */
   21288         2815 :   if (config.num_imports > 1)
   21289          458 :     write_imports (to, &crc);
   21290              : 
   21291              :   /* Write elided partition table.  */
   21292         2815 :   if (config.num_partitions)
   21293          136 :     write_partitions (to, config.num_partitions, &crc);
   21294              : 
   21295              :   /* Write the line maps.  */
   21296         2815 :   if (config.ordinary_locs)
   21297              :     {
   21298         2716 :       write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
   21299         2716 :       write_diagnostic_classification (to, global_dc, &crc);
   21300              :     }
   21301         2815 :   if (config.macro_locs)
   21302          128 :     write_macro_maps (to, map_info, &crc);
   21303              : 
   21304         2815 :   if (is_header ())
   21305              :     {
   21306          909 :       counts[MSC_macros] = write_macros (to, macros, &crc);
   21307          909 :       counts[MSC_inits] = write_inits (to, table, &crc);
   21308          909 :       vec_free (macros);
   21309              :     }
   21310              : 
   21311         2815 :   unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21312         2815 :   dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
   21313          300 :                    clusters, (bytes + clusters / 2) / (clusters + !clusters));
   21314         2815 :   trees_out::instrument ();
   21315              : 
   21316         2815 :   write_counts (to, counts, &crc);
   21317              : 
   21318         2815 :   spaces.release ();
   21319         2815 :   sccs.release ();
   21320              : 
   21321         2815 :   vec_free (macro_loc_remap);
   21322         2815 :   vec_free (ord_loc_remap);
   21323         2815 :   vec_free (ool);
   21324              : 
   21325              :   // FIXME:QOI:  Have a command line switch to control more detailed
   21326              :   // information (which might leak data you do not want to leak).
   21327              :   // Perhaps (some of) the write_readme contents should also be
   21328              :   // so-controlled.
   21329         2815 :   if (false)
   21330              :     write_env (to);
   21331              : 
   21332         2815 :   return true;
   21333         2844 : }
   21334              : 
   21335              : // Finish module writing after we've emitted all dynamic initializers.
   21336              : 
   21337              : void
   21338         2815 : module_state::write_end (elf_out *to, cpp_reader *reader,
   21339              :                          module_state_config &config, unsigned &crc)
   21340              : {
   21341              :   /* And finish up.  */
   21342         2815 :   write_config (to, config, crc);
   21343              : 
   21344              :   /* Human-readable info.  */
   21345         2815 :   write_readme (to, reader, config.dialect_str);
   21346              : 
   21347         3115 :   dump () && dump ("Wrote %u sections", to->get_section_limit ());
   21348         2815 : }
   21349              : 
   21350              : /* Initial read of a CMI.  Checks config, loads up imports and line
   21351              :    maps.  */
   21352              : 
   21353              : bool
   21354         3061 : module_state::read_initial (cpp_reader *reader)
   21355              : {
   21356         3061 :   module_state_config config;
   21357         3061 :   bool ok = true;
   21358              : 
   21359         3061 :   if (ok && !read_config (config))
   21360              :     ok = false;
   21361              : 
   21362         3048 :   bool have_locs = ok && read_prepare_maps (&config);
   21363              : 
   21364              :   /* Ordinary maps before the imports.  */
   21365         3048 :   if (!(have_locs && config.ordinary_locs))
   21366           71 :     ordinary_locs.first = line_table->highest_location + 1;
   21367         2990 :   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
   21368              :     ok = false;
   21369              : 
   21370              :   /* Allocate the REMAP vector.  */
   21371         3061 :   slurp->alloc_remap (config.num_imports);
   21372              : 
   21373         3061 :   if (ok)
   21374              :     {
   21375              :       /* Read the import table.  Decrement current to stop this CMI
   21376              :          from being evicted during the import. */
   21377         3048 :       slurp->current--;
   21378         3048 :       if (config.num_imports > 1 && !read_imports (reader, line_table))
   21379              :         ok = false;
   21380         3048 :       slurp->current++;
   21381              :     }
   21382              : 
   21383              :   /* Read the elided partition table, if we're the primary partition.  */
   21384         3048 :   if (ok && config.num_partitions && is_module ()
   21385         3075 :       && !read_partitions (config.num_partitions))
   21386              :     ok = false;
   21387              : 
   21388              :   /* Determine the module's number.  */
   21389         3061 :   gcc_checking_assert (mod == MODULE_UNKNOWN);
   21390         3061 :   gcc_checking_assert (this != this_module ());
   21391              : 
   21392         3061 :   {
   21393              :     /* Allocate space in the entities array now -- that array must be
   21394              :        monotonically in step with the modules array.  */
   21395         3061 :     entity_lwm = vec_safe_length (entity_ary);
   21396         3061 :     entity_num = config.num_entities;
   21397         3061 :     gcc_checking_assert (modules->length () == 1
   21398              :                          || modules->last ()->entity_lwm <= entity_lwm);
   21399         3061 :     vec_safe_reserve (entity_ary, config.num_entities);
   21400              : 
   21401         3061 :     binding_slot slot;
   21402         3061 :     slot.u.binding = NULL_TREE;
   21403      1277872 :     for (unsigned count = config.num_entities; count--;)
   21404      1274811 :       entity_ary->quick_push (slot);
   21405              :   }
   21406              : 
   21407              :   /* We'll run out of other resources before we run out of module
   21408              :      indices.  */
   21409         3061 :   mod = modules->length ();
   21410         3061 :   vec_safe_push (modules, this);
   21411              : 
   21412              :   /* We always import and export ourselves. */
   21413         3061 :   bitmap_set_bit (imports, mod);
   21414         3061 :   bitmap_set_bit (exports, mod);
   21415              : 
   21416         3061 :   if (ok)
   21417         3048 :     (*slurp->remap)[0] = mod << 1;
   21418         3593 :   dump () && dump ("Assigning %M module number %u", this, mod);
   21419              : 
   21420              :   /* We should not have been frozen during the importing done by
   21421              :      read_config.  */
   21422         3061 :   gcc_assert (!from ()->is_frozen ());
   21423              : 
   21424              :   /* Macro maps after the imports.  */
   21425         3061 :   if (!(ok && have_locs && config.macro_locs))
   21426         2928 :     macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   21427          133 :   else if (!read_macro_maps (config.macro_locs))
   21428              :     ok = false;
   21429              : 
   21430              :   /* Diagnostic classification streaming needs to come after reading
   21431              :      macro maps to handle _Pragmas in macros.  */
   21432         3048 :   if (ok && have_locs && config.ordinary_locs
   21433         6051 :       && !read_diagnostic_classification (global_dc))
   21434              :     ok = false;
   21435              : 
   21436              :   /* Note whether there's an active initializer.  */
   21437         3061 :   active_init_p = !is_header () && bool (config.active_init);
   21438              : 
   21439         3061 :   gcc_assert (slurp->current == ~0u);
   21440         3061 :   return ok;
   21441              : }
   21442              : 
   21443              : /* Read a preprocessor state.  */
   21444              : 
   21445              : bool
   21446          945 : module_state::read_preprocessor (bool outermost)
   21447              : {
   21448          945 :   gcc_checking_assert (is_header () && slurp
   21449              :                        && slurp->remap_module (0) == mod);
   21450              : 
   21451          945 :   if (loadedness == ML_PREPROCESSOR)
   21452            6 :     return !(from () && from ()->get_error ());
   21453              : 
   21454          939 :   bool ok = true;
   21455              : 
   21456              :   /* Read direct header imports.  */
   21457          939 :   unsigned len = slurp->remap->length ();
   21458          981 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21459              :     {
   21460           42 :       unsigned map = (*slurp->remap)[ix];
   21461           42 :       if (map & 1)
   21462              :         {
   21463           42 :           module_state *import = (*modules)[map >> 1];
   21464           42 :           if (import->is_header ())
   21465              :             {
   21466           42 :               ok = import->read_preprocessor (false);
   21467           42 :               bitmap_ior_into (slurp->headers, import->slurp->headers);
   21468              :             }
   21469              :         }
   21470              :     }
   21471              : 
   21472              :   /* Record as a direct header.  */
   21473          939 :   if (ok)
   21474          939 :     bitmap_set_bit (slurp->headers, mod);
   21475              : 
   21476          939 :   if (ok && !read_macros ())
   21477              :     ok = false;
   21478              : 
   21479          939 :   loadedness = ML_PREPROCESSOR;
   21480          939 :   announce ("macros");
   21481              : 
   21482          939 :   if (flag_preprocess_only)
   21483              :     /* We're done with the string table.  */
   21484           39 :     from ()->release ();
   21485              : 
   21486          939 :   return check_read (outermost, ok);
   21487              : }
   21488              : 
   21489              : /* Read language state.  */
   21490              : 
   21491              : bool
   21492         3084 : module_state::read_language (bool outermost)
   21493              : {
   21494         3084 :   gcc_checking_assert (!lazy_snum);
   21495              : 
   21496         3084 :   if (loadedness == ML_LANGUAGE)
   21497           87 :     return !(slurp && from () && from ()->get_error ());
   21498              : 
   21499         2997 :   gcc_checking_assert (slurp && slurp->current == ~0u
   21500              :                        && slurp->remap_module (0) == mod);
   21501              : 
   21502         2997 :   bool ok = true;
   21503              : 
   21504              :   /* Read direct imports.  */
   21505         2997 :   unsigned len = slurp->remap->length ();
   21506         3400 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21507              :     {
   21508          403 :       unsigned map = (*slurp->remap)[ix];
   21509          403 :       if (map & 1)
   21510              :         {
   21511          400 :           module_state *import = (*modules)[map >> 1];
   21512          400 :           if (!import->read_language (false))
   21513          403 :             ok = false;
   21514              :         }
   21515              :     }
   21516              : 
   21517         2997 :   unsigned counts[MSC_HWM];
   21518              : 
   21519         2997 :   if (ok && !read_counts (counts))
   21520              :     ok = false;
   21521              : 
   21522         2997 :   function_depth++; /* Prevent unexpected GCs.  */
   21523              : 
   21524         2997 :   if (ok && counts[MSC_entities] != entity_num)
   21525              :     ok = false;
   21526         2997 :   if (ok && counts[MSC_entities]
   21527         2768 :       && !read_entities (counts[MSC_entities],
   21528              :                          counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21529              :     ok = false;
   21530              : 
   21531              :   /* Read the namespace hierarchy.  */
   21532         2997 :   if (ok && counts[MSC_namespaces]
   21533         3609 :       && !read_namespaces (counts[MSC_namespaces]))
   21534              :     ok = false;
   21535              : 
   21536              :   /* Read any using-directives.  */
   21537         2997 :   if (ok && counts[MSC_using_directives]
   21538         3159 :       && !read_using_directives (counts[MSC_using_directives]))
   21539              :     ok = false;
   21540              : 
   21541         2997 :   if (ok && !read_bindings (counts[MSC_bindings],
   21542              :                             counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21543              :     ok = false;
   21544              : 
   21545              :   /* And unnamed.  */
   21546         2997 :   if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
   21547              :     ok = false;
   21548              : 
   21549         2997 :   if (ok)
   21550              :     {
   21551         2997 :       slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21552         2997 :       available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21553              :     }
   21554              : 
   21555         2997 :   if (!flag_module_lazy
   21556         2997 :       || (is_partition ()
   21557          247 :           && module_interface_p ()
   21558          217 :           && !module_partition_p ()))
   21559              :     {
   21560              :       /* Read the sections in forward order, so that dependencies are read
   21561              :          first.  See note about tarjan_connect.  */
   21562          557 :       ggc_collect ();
   21563              : 
   21564          557 :       lazy_snum = ~0u;
   21565              : 
   21566          557 :       unsigned hwm = counts[MSC_sec_hwm];
   21567       176970 :       for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
   21568       176413 :         if (!load_section (ix, NULL))
   21569              :           {
   21570              :             ok = false;
   21571              :             break;
   21572              :           }
   21573          557 :       lazy_snum = 0;
   21574          557 :       post_load_processing ();
   21575              : 
   21576          557 :       ggc_collect ();
   21577              : 
   21578          557 :       if (ok && CHECKING_P)
   21579       680673 :         for (unsigned ix = 0; ix != entity_num; ix++)
   21580       680116 :           gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
   21581              :     }
   21582              : 
   21583              :   // If the import is a header-unit, we need to register initializers
   21584              :   // of any static objects it contains (looking at you _Ioinit).
   21585              :   // Notice, the ordering of these initializers will be that of a
   21586              :   // dynamic initializer at this point in the current TU.  (Other
   21587              :   // instances of these objects in other TUs will be initialized as
   21588              :   // part of that TU's global initializers.)
   21589         2997 :   if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
   21590              :     ok = false;
   21591              : 
   21592         2997 :   function_depth--;
   21593              : 
   21594         3364 :   announce (flag_module_lazy ? "lazy" : "imported");
   21595         2997 :   loadedness = ML_LANGUAGE;
   21596              : 
   21597         2997 :   gcc_assert (slurp->current == ~0u);
   21598              : 
   21599              :   /* We're done with the string table.  */
   21600         2997 :   from ()->release ();
   21601              : 
   21602         2997 :   return check_read (outermost, ok);
   21603              : }
   21604              : 
   21605              : bool
   21606       215863 : module_state::maybe_defrost ()
   21607              : {
   21608       215863 :   bool ok = true;
   21609       215863 :   if (from ()->is_frozen ())
   21610              :     {
   21611            9 :       if (lazy_open >= lazy_limit)
   21612            3 :         freeze_an_elf ();
   21613           18 :       dump () && dump ("Defrosting '%s'", filename);
   21614            9 :       ok = from ()->defrost (maybe_add_cmi_prefix (filename));
   21615            9 :       lazy_open++;
   21616              :     }
   21617              : 
   21618       215863 :   return ok;
   21619              : }
   21620              : 
   21621              : /* Load section SNUM, dealing with laziness.  It doesn't matter if we
   21622              :    have multiple concurrent loads, because we do not use TREE_VISITED
   21623              :    when reading back in.  */
   21624              : 
   21625              : bool
   21626       215863 : module_state::load_section (unsigned snum, binding_slot *mslot)
   21627              : {
   21628       215863 :   if (from ()->get_error ())
   21629              :     return false;
   21630              : 
   21631       215863 :   if (snum >= slurp->current)
   21632            0 :     from ()->set_error (elf::E_BAD_LAZY);
   21633       215863 :   else if (maybe_defrost ())
   21634              :     {
   21635       215863 :       unsigned old_current = slurp->current;
   21636       215863 :       slurp->current = snum;
   21637       215863 :       slurp->lru = 0;  /* Do not swap out.  */
   21638       215863 :       slurp->remaining--;
   21639       215863 :       read_cluster (snum);
   21640       215863 :       slurp->lru = ++lazy_lru;
   21641       215863 :       slurp->current = old_current;
   21642              :     }
   21643              : 
   21644       215863 :   if (mslot && mslot->is_lazy ())
   21645              :     {
   21646              :       /* Oops, the section didn't set this slot.  */
   21647            0 :       from ()->set_error (elf::E_BAD_DATA);
   21648            0 :       *mslot = NULL_TREE;
   21649              :     }
   21650              : 
   21651       215863 :   bool ok = !from ()->get_error ();
   21652       215863 :   if (!ok)
   21653              :     {
   21654            0 :       error_at (loc, "failed to read compiled module cluster %u: %s",
   21655            0 :                 snum, from ()->get_error (filename));
   21656            0 :       note_cmi_name ();
   21657              :     }
   21658              : 
   21659       215863 :   maybe_completed_reading ();
   21660              : 
   21661       215863 :   return ok;
   21662              : }
   21663              : 
   21664              : void
   21665       222844 : module_state::maybe_completed_reading ()
   21666              : {
   21667       222844 :   if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
   21668              :     {
   21669         2484 :       lazy_open--;
   21670              :       /* We no longer need the macros, all tokenizing has been done.  */
   21671         2484 :       slurp->release_macros ();
   21672              : 
   21673         2484 :       from ()->end ();
   21674         2484 :       slurp->close ();
   21675         2484 :       slurped ();
   21676              :     }
   21677       222844 : }
   21678              : 
   21679              : /* After a reading operation, make sure things are still ok.  If not,
   21680              :    emit an error and clean up.  */
   21681              : 
   21682              : bool
   21683         7018 : module_state::check_read (bool outermost, bool ok)
   21684              : {
   21685         7018 :   gcc_checking_assert (!outermost || slurp->current == ~0u);
   21686              : 
   21687         7018 :   if (!ok)
   21688           34 :     from ()->set_error ();
   21689              : 
   21690         7018 :   if (int e = from ()->get_error ())
   21691              :     {
   21692           40 :       auto_diagnostic_group d;
   21693           40 :       error_at (loc, "failed to read compiled module: %s",
   21694           40 :                 from ()->get_error (filename));
   21695           40 :       note_cmi_name ();
   21696              : 
   21697           40 :       if (e == EMFILE
   21698           40 :           || e == ENFILE
   21699              : #if MAPPED_READING
   21700           40 :           || e == ENOMEM
   21701              : #endif
   21702              :           || false)
   21703            0 :         inform (loc, "consider using %<-fno-module-lazy%>,"
   21704              :                 " increasing %<-param-lazy-modules=%u%> value,"
   21705              :                 " or increasing the per-process file descriptor limit",
   21706              :                 param_lazy_modules);
   21707           40 :       else if (e == ENOENT)
   21708           21 :         inform (loc, "imports must be built before being imported");
   21709              : 
   21710           40 :       if (outermost)
   21711           37 :         fatal_error (loc, "returning to the gate for a mechanical issue");
   21712              : 
   21713            3 :       ok = false;
   21714            3 :     }
   21715              : 
   21716         6981 :   maybe_completed_reading ();
   21717              : 
   21718         6981 :   return ok;
   21719              : }
   21720              : 
   21721              : /* Return the IDENTIFIER_NODE naming module IX.  This is the name
   21722              :    including dots.  */
   21723              : 
   21724              : char const *
   21725          408 : module_name (unsigned ix, bool header_ok)
   21726              : {
   21727          408 :   if (modules)
   21728              :     {
   21729          408 :       module_state *imp = (*modules)[ix];
   21730              : 
   21731          408 :       if (ix && !imp->name)
   21732            0 :         imp = imp->parent;
   21733              : 
   21734          408 :       if (header_ok || !imp->is_header ())
   21735          408 :         return imp->get_flatname ();
   21736              :     }
   21737              : 
   21738              :   return NULL;
   21739              : }
   21740              : 
   21741              : /* Return the bitmap describing what modules are imported.  Remember,
   21742              :    we always import ourselves.  */
   21743              : 
   21744              : bitmap
   21745       197467 : get_import_bitmap ()
   21746              : {
   21747       197467 :   return this_module ()->imports;
   21748              : }
   21749              : 
   21750              : /* Get the original decl for an instantiation at TINST, or NULL_TREE
   21751              :    if we're not an instantiation.  */
   21752              : 
   21753              : static tree
   21754       273401 : orig_decl_for_instantiation (tinst_level *tinst)
   21755              : {
   21756       273401 :   if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
   21757              :     return NULL_TREE;
   21758              : 
   21759       105424 :   tree decl = tinst->tldcl;
   21760       105424 :   if (TREE_CODE (decl) == TREE_LIST)
   21761            0 :     decl = TREE_PURPOSE (decl);
   21762       105424 :   if (TYPE_P (decl))
   21763        18756 :     decl = TYPE_NAME (decl);
   21764              :   return decl;
   21765              : }
   21766              : 
   21767              : /* Return the visible imports and path of instantiation for an
   21768              :    instantiation at TINST.  If TINST is nullptr, we're not in an
   21769              :    instantiation, and thus will return the visible imports of the
   21770              :    current TU (and NULL *PATH_MAP_P).   We cache the information on
   21771              :    the tinst level itself.  */
   21772              : 
   21773              : static bitmap
   21774       165457 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
   21775              : {
   21776       165457 :   gcc_checking_assert (modules_p ());
   21777              : 
   21778       165457 :   tree decl = orig_decl_for_instantiation (tinst);
   21779       165457 :   if (!decl)
   21780              :     {
   21781        74407 :       gcc_assert (!tinst || !tinst->next);
   21782              :       /* Not inside an instantiation, just the regular case.  */
   21783        74407 :       *path_map_p = nullptr;
   21784        74407 :       return get_import_bitmap ();
   21785              :     }
   21786              : 
   21787        91050 :   if (!tinst->path)
   21788              :     {
   21789              :       /* Calculate.  */
   21790        27134 :       bitmap visible = path_of_instantiation (tinst->next, path_map_p);
   21791        27134 :       bitmap path_map = *path_map_p;
   21792              : 
   21793        27134 :       if (!path_map)
   21794              :         {
   21795         3647 :           path_map = BITMAP_GGC_ALLOC ();
   21796         3647 :           bitmap_set_bit (path_map, 0);
   21797              :         }
   21798              : 
   21799        27134 :       if (unsigned mod = get_originating_module (decl))
   21800         2368 :         if (!bitmap_bit_p (path_map, mod))
   21801              :           {
   21802              :             /* This is brand new information!  */
   21803          145 :             bitmap new_path = BITMAP_GGC_ALLOC ();
   21804          145 :             bitmap_copy (new_path, path_map);
   21805          145 :             bitmap_set_bit (new_path, mod);
   21806          145 :             path_map = new_path;
   21807              : 
   21808          145 :             bitmap imports = (*modules)[mod]->imports;
   21809          145 :             if (bitmap_intersect_compl_p (imports, visible))
   21810              :               {
   21811              :                 /* IMPORTS contains additional modules to VISIBLE.  */
   21812           23 :                 bitmap new_visible = BITMAP_GGC_ALLOC ();
   21813              : 
   21814           23 :                 bitmap_ior (new_visible, visible, imports);
   21815           23 :                 visible = new_visible;
   21816              :               }
   21817              :           }
   21818              : 
   21819        27134 :       tinst->path = path_map;
   21820        27134 :       tinst->visible = visible;
   21821              :     }
   21822              : 
   21823        91050 :   *path_map_p = tinst->path;
   21824        91050 :   return tinst->visible;
   21825              : }
   21826              : 
   21827              : /* Return the bitmap describing what modules are visible along the
   21828              :    path of instantiation.  If we're not an instantiation, this will be
   21829              :    the visible imports of the TU.  *PATH_MAP_P is filled in with the
   21830              :    modules owning the instantiation path -- we see the module-linkage
   21831              :    entities of those modules.  */
   21832              : 
   21833              : bitmap
   21834     34481979 : visible_instantiation_path (bitmap *path_map_p)
   21835              : {
   21836     34481979 :   if (!modules_p ())
   21837              :     return NULL;
   21838              : 
   21839       138323 :   return path_of_instantiation (current_instantiation (), path_map_p);
   21840              : }
   21841              : 
   21842              : /* Returns the bitmap describing what modules were visible from the
   21843              :    module that the current instantiation originated from.  If we're
   21844              :    not an instantiation, returns NULL.  *MODULE_P is filled in with
   21845              :    the originating module of the definition for this instantiation.  */
   21846              : 
   21847              : bitmap
   21848       107944 : visible_from_instantiation_origination (unsigned *module_p)
   21849              : {
   21850       107944 :   if (!modules_p ())
   21851              :     return NULL;
   21852              : 
   21853       107944 :   tree decl = orig_decl_for_instantiation (current_instantiation ());
   21854       107944 :   if (!decl)
   21855              :     return NULL;
   21856              : 
   21857        14374 :   *module_p = get_originating_module (decl);
   21858        14374 :   return (*modules)[*module_p]->imports;
   21859              : }
   21860              : 
   21861              : /* We've just directly imported IMPORT.  Update our import/export
   21862              :    bitmaps.  IS_EXPORT is true if we're reexporting the OTHER.  */
   21863              : 
   21864              : void
   21865         3151 : module_state::set_import (module_state const *import, bool is_export)
   21866              : {
   21867         3151 :   gcc_checking_assert (this != import);
   21868              : 
   21869              :   /* We see IMPORT's exports (which includes IMPORT).  If IMPORT is
   21870              :      the primary interface or a partition we'll see its imports.  */
   21871         3151 :   bitmap_ior_into (imports, import->is_module () || import->is_partition ()
   21872              :                    ? import->imports : import->exports);
   21873              : 
   21874         3151 :   if (is_export)
   21875              :     /* We'll export OTHER's exports.  */
   21876          486 :     bitmap_ior_into (exports, import->exports);
   21877         3151 : }
   21878              : 
   21879              : /* Return the declaring entity of DECL.  That is the decl determining
   21880              :    how to decorate DECL with module information.  Returns NULL_TREE if
   21881              :    it's the global module.  */
   21882              : 
   21883              : tree
   21884    114895219 : get_originating_module_decl (tree decl)
   21885              : {
   21886              :   /* An enumeration constant.  */
   21887    114895219 :   if (TREE_CODE (decl) == CONST_DECL
   21888        10860 :       && DECL_CONTEXT (decl)
   21889    114906079 :       && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
   21890         9567 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   21891    114885652 :   else if (TREE_CODE (decl) == FIELD_DECL
   21892    114843318 :            || TREE_CODE (decl) == USING_DECL
   21893    229702802 :            || CONST_DECL_USING_P (decl))
   21894              :     {
   21895        69795 :       decl = DECL_CONTEXT (decl);
   21896        69795 :       if (TREE_CODE (decl) != FUNCTION_DECL)
   21897        69795 :         decl = TYPE_NAME (decl);
   21898              :     }
   21899              : 
   21900    114895219 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
   21901              :                        || TREE_CODE (decl) == FUNCTION_DECL
   21902              :                        || TREE_CODE (decl) == TYPE_DECL
   21903              :                        || TREE_CODE (decl) == VAR_DECL
   21904              :                        || TREE_CODE (decl) == CONCEPT_DECL
   21905              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
   21906              : 
   21907    117864129 :   for (;;)
   21908              :     {
   21909              :       /* Uninstantiated template friends are owned by the befriending
   21910              :          class -- not their context.  */
   21911    116379674 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   21912    116379674 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   21913        14071 :         decl = TYPE_NAME (DECL_CHAIN (decl));
   21914              : 
   21915              :       /* An imported temploid friend is attached to the same module the
   21916              :          befriending class was.  */
   21917    116379674 :       if (imported_temploid_friends)
   21918      4284465 :         if (tree *slot = imported_temploid_friends->get (decl))
   21919          519 :           decl = *slot;
   21920              : 
   21921    116379674 :       int use;
   21922    116379674 :       if (tree ti = node_template_info (decl, use))
   21923              :         {
   21924      1099320 :           decl = TI_TEMPLATE (ti);
   21925      1099320 :           if (TREE_CODE (decl) != TEMPLATE_DECL)
   21926              :             {
   21927              :               /* A friend template specialization.  */
   21928           60 :               gcc_checking_assert (OVL_P (decl) || identifier_p (decl));
   21929           60 :               return global_namespace;
   21930              :             }
   21931              :         }
   21932              :       else
   21933              :         {
   21934    115280354 :           tree ctx = CP_DECL_CONTEXT (decl);
   21935    115280354 :           if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21936              :             break;
   21937              : 
   21938       385195 :           if (TYPE_P (ctx))
   21939              :             {
   21940       345914 :               ctx = TYPE_NAME (ctx);
   21941       345914 :               if (!ctx)
   21942              :                 {
   21943              :                   /* Some kind of internal type.  */
   21944            0 :                   gcc_checking_assert (DECL_ARTIFICIAL (decl));
   21945            0 :                   return global_namespace;
   21946              :                 }
   21947              :             }
   21948       385195 :           decl = ctx;
   21949              :         }
   21950      1484455 :     }
   21951              : 
   21952    114895159 :   return decl;
   21953              : }
   21954              : 
   21955              : /* If DECL is imported, return which module imported it, or 0 for the current
   21956              :    module.  Except that if GLOBAL_M1, return -1 for decls attached to the
   21957              :    global module.  */
   21958              : 
   21959              : int
   21960      1702533 : get_originating_module (tree decl, bool global_m1)
   21961              : {
   21962      1702533 :   tree owner = get_originating_module_decl (decl);
   21963      1702533 :   tree not_tmpl = STRIP_TEMPLATE (owner);
   21964              : 
   21965      1702533 :   if (!DECL_LANG_SPECIFIC (not_tmpl))
   21966       514402 :     return global_m1 ? -1 : 0;
   21967              : 
   21968      2263748 :   if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
   21969              :     return -1;
   21970              : 
   21971       120906 :   int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
   21972       120906 :   gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
   21973              :   return mod;
   21974              : }
   21975              : 
   21976              : /* DECL is imported, return which module imported it.
   21977              :    If FLEXIBLE, return -1 if not found, otherwise checking ICE.  */
   21978              : 
   21979              : unsigned
   21980        81885 : get_importing_module (tree decl, bool flexible)
   21981              : {
   21982        81885 :   unsigned index = import_entity_index (decl, flexible);
   21983        81885 :   if (index == ~(~0u >> 1))
   21984              :     return -1;
   21985        31426 :   module_state *module = import_entity_module (index);
   21986              : 
   21987        31426 :   return module->mod;
   21988              : }
   21989              : 
   21990              : /* Is it permissible to redeclare OLDDECL with NEWDECL.
   21991              : 
   21992              :    If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
   21993              :    the current scope's module and attachment.  */
   21994              : 
   21995              : bool
   21996       227131 : module_may_redeclare (tree olddecl, tree newdecl)
   21997              : {
   21998       227131 :   tree decl = olddecl;
   21999       307605 :   for (;;)
   22000              :     {
   22001       267368 :       tree ctx = CP_DECL_CONTEXT (decl);
   22002       267368 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   22003              :         // Found the namespace-scope decl.
   22004              :         break;
   22005        41759 :       if (!CLASS_TYPE_P (ctx))
   22006              :         // We've met a non-class scope.  Such a thing is not
   22007              :         // reopenable, so we must be ok.
   22008              :         return true;
   22009        40237 :       decl = TYPE_NAME (ctx);
   22010        40237 :     }
   22011              : 
   22012       225609 :   int use_tpl = 0;
   22013       225609 :   if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
   22014              :     // Specializations of any kind can be redeclared anywhere.
   22015              :     // FIXME: Should we be checking this in more places on the scope chain?
   22016              :     return true;
   22017              : 
   22018       164190 :   module_state *old_mod = get_primary (this_module ());
   22019       164190 :   module_state *new_mod = old_mod;
   22020              : 
   22021       164190 :   tree old_origin = get_originating_module_decl (decl);
   22022       164190 :   tree old_inner = STRIP_TEMPLATE (old_origin);
   22023       164190 :   bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
   22024       253241 :                              && DECL_MODULE_ATTACH_P (old_inner));
   22025       253241 :   if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22026              :     {
   22027         8798 :       unsigned index = import_entity_index (old_origin);
   22028         8798 :       old_mod = get_primary (import_entity_module (index));
   22029              :     }
   22030              : 
   22031       164190 :   bool newdecl_attached_p = module_attach_p ();
   22032       164190 :   if (newdecl)
   22033              :     {
   22034        46870 :       tree new_origin = get_originating_module_decl (newdecl);
   22035        46870 :       tree new_inner = STRIP_TEMPLATE (new_origin);
   22036        46870 :       newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
   22037        90063 :                             && DECL_MODULE_ATTACH_P (new_inner));
   22038        90063 :       if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
   22039              :         {
   22040          139 :           unsigned index = import_entity_index (new_origin);
   22041          139 :           new_mod = get_primary (import_entity_module (index));
   22042              :         }
   22043              :     }
   22044              : 
   22045              :   /* Module attachment needs to match.  */
   22046       164190 :   if (olddecl_attached_p == newdecl_attached_p)
   22047              :     {
   22048       164046 :       if (!olddecl_attached_p)
   22049              :         /* Both are GM entities, OK.  */
   22050              :         return true;
   22051              : 
   22052         1446 :       if (new_mod == old_mod)
   22053              :         /* Both attached to same named module, OK.  */
   22054              :         return true;
   22055              :     }
   22056              : 
   22057              :   /* Attached to different modules, error.  */
   22058          171 :   decl = newdecl ? newdecl : olddecl;
   22059          171 :   location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
   22060          171 :   if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
   22061              :     {
   22062            3 :       if (newdecl_attached_p)
   22063            3 :         error_at (loc, "declaring %qD in module %qs conflicts with builtin "
   22064              :                   "in global module", decl, new_mod->get_flatname ());
   22065              :       else
   22066            0 :         error_at (loc, "declaration %qD conflicts with builtin", decl);
   22067              :     }
   22068          333 :   else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22069              :     {
   22070          153 :       auto_diagnostic_group d;
   22071          153 :       if (newdecl_attached_p)
   22072           75 :         error_at (loc, "redeclaring %qD in module %qs conflicts with import",
   22073              :                   decl, new_mod->get_flatname ());
   22074              :       else
   22075           78 :         error_at (loc, "redeclaring %qD in global module conflicts with import",
   22076              :                   decl);
   22077              : 
   22078          153 :       if (olddecl_attached_p)
   22079          105 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22080              :                 "import declared attached to module %qs",
   22081              :                 old_mod->get_flatname ());
   22082              :       else
   22083           48 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22084              :                 "import declared in global module");
   22085          153 :     }
   22086              :   else
   22087              :     {
   22088           15 :       auto_diagnostic_group d;
   22089           15 :       if (newdecl_attached_p)
   22090           15 :         error_at (loc, "conflicting declaration of %qD in module %qs",
   22091              :                   decl, new_mod->get_flatname ());
   22092              :       else
   22093            0 :         error_at (loc, "conflicting declaration of %qD in global module",
   22094              :                   decl);
   22095              : 
   22096           15 :       if (olddecl_attached_p)
   22097            0 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22098              :                 "previously declared in module %qs",
   22099              :                 old_mod->get_flatname ());
   22100              :       else
   22101           15 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22102              :                 "previously declared in global module");
   22103           15 :     }
   22104              :   return false;
   22105              : }
   22106              : 
   22107              : /* DECL is being created by this TU.  Record it came from here.  We
   22108              :    record module purview, so we can see if partial or explicit
   22109              :    specialization needs to be written out, even though its purviewness
   22110              :    comes from the most general template.  */
   22111              : 
   22112              : void
   22113    995249344 : set_instantiating_module (tree decl)
   22114              : {
   22115    995249344 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
   22116              :               || VAR_P (decl)
   22117              :               || TREE_CODE (decl) == TYPE_DECL
   22118              :               || TREE_CODE (decl) == CONCEPT_DECL
   22119              :               || TREE_CODE (decl) == TEMPLATE_DECL
   22120              :               || TREE_CODE (decl) == CONST_DECL
   22121              :               || (TREE_CODE (decl) == NAMESPACE_DECL
   22122              :                   && DECL_NAMESPACE_ALIAS (decl)));
   22123              : 
   22124    995249344 :   if (!modules_p ())
   22125              :     return;
   22126              : 
   22127      4031716 :   decl = STRIP_TEMPLATE (decl);
   22128              : 
   22129      4031716 :   if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
   22130       376936 :     retrofit_lang_decl (decl);
   22131              : 
   22132      4031716 :   if (DECL_LANG_SPECIFIC (decl))
   22133              :     {
   22134      3061005 :       DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
   22135              :       /* If this was imported, we'll still be in the entity_hash.  */
   22136      3061005 :       DECL_MODULE_IMPORT_P (decl) = false;
   22137              :     }
   22138              : }
   22139              : 
   22140              : /* If DECL is a class member, whose class is not defined in this TU
   22141              :    (it was imported), remember this decl.  */
   22142              : 
   22143              : void
   22144       159114 : set_defining_module (tree decl)
   22145              : {
   22146       159114 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   22147              :                        || !DECL_MODULE_IMPORT_P (decl));
   22148              : 
   22149       159114 :   if (module_maybe_has_cmi_p ())
   22150              :     {
   22151              :       /* We need to track all declarations within a module, not just those
   22152              :          in the module purview, because we don't necessarily know yet if
   22153              :          this module will require a CMI while in the global fragment.  */
   22154       110335 :       tree ctx = DECL_CONTEXT (decl);
   22155       110335 :       if (ctx
   22156       110335 :           && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
   22157        23894 :           && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
   22158       121900 :           && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
   22159              :         {
   22160              :           /* This entity's context is from an import.  We may need to
   22161              :              record this entity to make sure we emit it in the CMI.
   22162              :              Template specializations are in the template hash tables,
   22163              :              so we don't need to record them here as well.  */
   22164           39 :           int use_tpl = -1;
   22165           39 :           tree ti = node_template_info (decl, use_tpl);
   22166           39 :           if (use_tpl <= 0)
   22167              :             {
   22168           39 :               if (ti)
   22169              :                 {
   22170           21 :                   gcc_checking_assert (!use_tpl);
   22171              :                   /* Get to the TEMPLATE_DECL.  */
   22172           21 :                   decl = TI_TEMPLATE (ti);
   22173              :                 }
   22174              : 
   22175              :               /* Record it on the class_members list.  */
   22176           39 :               vec_safe_push (class_members, decl);
   22177              :             }
   22178              :         }
   22179              :     }
   22180       159114 : }
   22181              : 
   22182              : /* Also remember DECL if it's a newly declared class template partial
   22183              :    specialization, because these are not necessarily added to the
   22184              :    instantiation tables.  */
   22185              : 
   22186              : void
   22187      8554808 : set_defining_module_for_partial_spec (tree decl)
   22188              : {
   22189      8554808 :   if (module_maybe_has_cmi_p ()
   22190        27555 :       && DECL_IMPLICIT_TYPEDEF_P (decl)
   22191      8577980 :       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   22192        23172 :     vec_safe_push (partial_specializations, decl);
   22193      8554808 : }
   22194              : 
   22195              : /* Record that DECL is declared in this TU, and note attachment and
   22196              :    exporting for namespace-scope entities.  FRIEND_P is true if
   22197              :    this is a friend declaration.  */
   22198              : 
   22199              : void
   22200    320732460 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
   22201              : {
   22202    320732460 :   set_instantiating_module (decl);
   22203              : 
   22204              :   /* DECL_CONTEXT may not be set yet when we're called for
   22205              :      non-namespace-scope entities.  */
   22206    320732460 :   if (!DECL_CONTEXT (decl) || !DECL_NAMESPACE_SCOPE_P (decl))
   22207              :     return;
   22208              : 
   22209    119445539 :   gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
   22210              : 
   22211    119445539 :   if (module_attach_p ())
   22212              :     {
   22213         4342 :       retrofit_lang_decl (decl);
   22214         4342 :       DECL_MODULE_ATTACH_P (decl) = true;
   22215              :     }
   22216              : 
   22217              :   /* It is ill-formed to export a declaration with internal linkage.  However,
   22218              :      at the point this function is called we don't yet always know whether this
   22219              :      declaration has internal linkage; instead we defer this check for callers
   22220              :      to do once visibility has been determined.  */
   22221    119445539 :   if (module_exporting_p ())
   22222       159115 :     DECL_MODULE_EXPORT_P (decl) = true;
   22223              : }
   22224              : 
   22225              : /* True if it's OK to redefine DECL under P1811 because it is attached to the
   22226              :    global module and its definition was imported; [basic.def.odr]/12 "there can
   22227              :    be more than one definition...provided that each definition appears in a
   22228              :    different translation unit"
   22229              : 
   22230              :    This does not depend on whether DECL was already defined, so that for
   22231              :    instance it can affect redefinition of default arguments.  This should only
   22232              :    be used when we notice a redefinition.  */
   22233              : 
   22234              : bool
   22235    355513318 : redefinable_import_p (tree decl)
   22236              : {
   22237    355513318 :   if (!modules_p () || named_module_attach_p ())
   22238              :     return false;
   22239              : 
   22240      1069381 :   decl = STRIP_TEMPLATE (decl);
   22241              : 
   22242      1069381 :   if (TREE_CODE (decl) == TYPE_DECL
   22243        10611 :       && !DECL_IMPLICIT_TYPEDEF_P (decl)
   22244      1071942 :       && !TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl))
   22245              :     /* Typedefs are always redefinable, use normal handling.  */
   22246              :     return false;
   22247              : 
   22248      1066835 :   return (DECL_LANG_SPECIFIC (decl)
   22249      1065238 :           && DECL_MODULE_ENTITY_P (decl)
   22250              :           /* Use this instead of DECL_MODULE_IMPORT_P so we don't get confused
   22251              :              by partitions or set_instantiating_module.  */
   22252              :           /* FIXME this also means we allow multiple redefinitions.  */
   22253      1136893 :           && get_importing_module (decl, /*flexible*/true) > 0);
   22254              : }
   22255              : 
   22256              : /* Checks whether DECL within a module unit has valid linkage for its kind.
   22257              :    Must be called after visibility for DECL has been finalised.  */
   22258              : 
   22259              : void
   22260    402954153 : check_module_decl_linkage (tree decl)
   22261              : {
   22262    402954153 :   if (!module_has_cmi_p ())
   22263              :     return;
   22264              : 
   22265              :   /* A header unit shall not contain a definition of a non-inline function
   22266              :      or variable (not template) whose name has external linkage.  */
   22267       536414 :   if (header_module_p ()
   22268       484176 :       && !processing_template_decl
   22269       159973 :       && ((TREE_CODE (decl) == FUNCTION_DECL
   22270        94537 :            && !DECL_DECLARED_INLINE_P (decl))
   22271       123946 :           || (TREE_CODE (decl) == VAR_DECL
   22272        37538 :               && !DECL_INLINE_VAR_P (decl)))
   22273        45287 :       && decl_defined_p (decl)
   22274         4476 :       && !(DECL_LANG_SPECIFIC (decl)
   22275         4476 :            && DECL_TEMPLATE_INSTANTIATION (decl))
   22276       540890 :       && DECL_EXTERNAL_LINKAGE_P (decl))
   22277           60 :     error_at (DECL_SOURCE_LOCATION (decl),
   22278              :               "external linkage definition of %qD in header module must "
   22279              :               "be declared %<inline%>", decl);
   22280              : 
   22281              :   /* An internal-linkage declaration cannot be generally be exported.
   22282              :      But it's OK to export any declaration from a header unit, including
   22283              :      internal linkage declarations.  */
   22284       536414 :   if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
   22285              :     {
   22286              :       /* Let's additionally treat any exported declaration within an
   22287              :          internal namespace as exporting a declaration with internal
   22288              :          linkage, as this would also implicitly export the internal
   22289              :          linkage namespace.  */
   22290         2035 :       if (decl_anon_ns_mem_p (decl))
   22291              :         {
   22292           50 :           error_at (DECL_SOURCE_LOCATION (decl),
   22293              :                     "exporting declaration %qD declared in unnamed namespace",
   22294              :                     decl);
   22295           50 :           DECL_MODULE_EXPORT_P (decl) = false;
   22296              :         }
   22297         1985 :       else if (decl_linkage (decl) == lk_internal)
   22298              :         {
   22299           17 :           error_at (DECL_SOURCE_LOCATION (decl),
   22300              :                     "exporting declaration %qD with internal linkage", decl);
   22301           17 :           DECL_MODULE_EXPORT_P (decl) = false;
   22302              :         }
   22303              :     }
   22304              : }
   22305              : 
   22306              : /* Given a scope CTX, find the scope we want to attach the key to,
   22307              :    or NULL if no key scope is required.  */
   22308              : 
   22309              : static tree
   22310         3170 : adjust_key_scope (tree ctx)
   22311              : {
   22312              :   /* For members, key it to the containing type to handle deduplication
   22313              :      correctly.  For fields, this is necessary as FIELD_DECLs have no
   22314              :      dep and so would only be streamed after the lambda type, defeating
   22315              :      our ability to merge them.
   22316              : 
   22317              :      Other class-scope key decls might depend on the type of the lambda
   22318              :      but be within the same cluster; we need to ensure that we never
   22319              :      first see the key decl while streaming the lambda type as merging
   22320              :      would then fail when comparing the partially-streamed lambda type
   22321              :      of the key decl with the existing (PR c++/122310).
   22322              : 
   22323              :      Perhaps sort_cluster can be adjusted to handle this better, but
   22324              :      this is a simple workaround (and might down on the number of
   22325              :      entries in keyed_table as a bonus).  */
   22326         5331 :   while (!DECL_NAMESPACE_SCOPE_P (ctx))
   22327         2161 :     if (DECL_CLASS_SCOPE_P (ctx))
   22328         2047 :       ctx = TYPE_NAME (DECL_CONTEXT (ctx));
   22329              :     else
   22330          114 :       ctx = DECL_CONTEXT (ctx);
   22331              : 
   22332         3170 :   return ctx;
   22333              : }
   22334              : 
   22335              : /* DECL is keyed to CTX for odr purposes.  */
   22336              : 
   22337              : void
   22338      1947761 : maybe_key_decl (tree ctx, tree decl)
   22339              : {
   22340      1947761 :   if (!modules_p ())
   22341              :     return;
   22342              : 
   22343              :   /* We only need to deal here with decls attached to var, field,
   22344              :      parm, type, function, or concept decls.  */
   22345        13150 :   if (TREE_CODE (ctx) != VAR_DECL
   22346        13150 :       && TREE_CODE (ctx) != FIELD_DECL
   22347              :       && TREE_CODE (ctx) != PARM_DECL
   22348              :       && TREE_CODE (ctx) != TYPE_DECL
   22349              :       && TREE_CODE (ctx) != FUNCTION_DECL
   22350              :       && TREE_CODE (ctx) != CONCEPT_DECL)
   22351              :     return;
   22352              : 
   22353        26297 :   gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22354              :                        || TREE_CODE (ctx) == FUNCTION_DECL);
   22355              : 
   22356              :   /* We don't need to use the keyed map for functions with definitions,
   22357              :      as we can instead use the MK_local_type handling for streaming.  */
   22358        13150 :   if (TREE_CODE (ctx) == FUNCTION_DECL
   22359        13150 :       && (has_definition (ctx)
   22360              :           /* If we won't be streaming this definition there's also no
   22361              :              need to record the key, as it will not be useful for merging
   22362              :              (this function is non-inline and so a matching declaration
   22363              :              will always be an ODR violation anyway).  */
   22364          120 :           || !module_maybe_has_cmi_p ()))
   22365              :     return;
   22366              : 
   22367          714 :   ctx = adjust_key_scope (ctx);
   22368              : 
   22369          714 :   if (!keyed_table)
   22370          166 :     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22371              : 
   22372          714 :   auto &vec = keyed_table->get_or_insert (ctx);
   22373          714 :   if (!vec.length ())
   22374              :     {
   22375          657 :       retrofit_lang_decl (ctx);
   22376          657 :       DECL_MODULE_KEYED_DECLS_P (ctx) = true;
   22377              :     }
   22378          714 :   if (CHECKING_P)
   22379          891 :     for (tree t : vec)
   22380           63 :       gcc_checking_assert (t != decl);
   22381              : 
   22382          714 :   vec.safe_push (decl);
   22383              : }
   22384              : 
   22385              : /* Find the scope that the local type or lambda DECL is keyed to, if any.  */
   22386              : 
   22387              : static tree
   22388         2492 : get_keyed_decl_scope (tree decl)
   22389              : {
   22390         2492 :   gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   22391              : 
   22392         7476 :   tree scope = (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22393         4756 :                 ? LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))
   22394         2492 :                 : CP_DECL_CONTEXT (decl));
   22395         2492 :   if (!scope)
   22396              :     return NULL_TREE;
   22397              : 
   22398         2456 :   gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
   22399              :                        || TREE_CODE (scope) == FIELD_DECL
   22400              :                        || TREE_CODE (scope) == PARM_DECL
   22401              :                        || TREE_CODE (scope) == TYPE_DECL
   22402              :                        || (TREE_CODE (scope) == FUNCTION_DECL
   22403              :                            && !has_definition (scope))
   22404              :                        || TREE_CODE (scope) == CONCEPT_DECL);
   22405              : 
   22406         2456 :   scope = adjust_key_scope (scope);
   22407              : 
   22408         4912 :   gcc_checking_assert (scope
   22409              :                        && DECL_LANG_SPECIFIC (scope)
   22410              :                        && DECL_MODULE_KEYED_DECLS_P (scope));
   22411              :   return scope;
   22412              : }
   22413              : 
   22414              : /* DECL is an instantiated friend that should be attached to the same
   22415              :    module that ORIG is.  */
   22416              : 
   22417              : void
   22418      2766348 : propagate_defining_module (tree decl, tree orig)
   22419              : {
   22420      2766348 :   if (!modules_p ())
   22421              :     return;
   22422              : 
   22423         8171 :   tree not_tmpl = STRIP_TEMPLATE (orig);
   22424        16285 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
   22425              :     {
   22426          126 :       tree inner = STRIP_TEMPLATE (decl);
   22427          126 :       retrofit_lang_decl (inner);
   22428          126 :       DECL_MODULE_ATTACH_P (inner) = true;
   22429              :     }
   22430              : 
   22431        16285 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
   22432              :     {
   22433          277 :       bool exists = imported_temploid_friends->put (decl, orig);
   22434              : 
   22435              :       /* We should only be called if lookup for an existing decl
   22436              :          failed, in which case there shouldn't already be an entry
   22437              :          in the map.  */
   22438          277 :       gcc_assert (!exists);
   22439              :     }
   22440              : }
   22441              : 
   22442              : /* NEWDECL matched with OLDDECL, transfer defining module information
   22443              :    onto OLDDECL.  We've already validated attachment matches.  */
   22444              : 
   22445              : void
   22446     20668362 : transfer_defining_module (tree olddecl, tree newdecl)
   22447              : {
   22448     20668362 :   if (!modules_p ())
   22449              :     return;
   22450              : 
   22451        74852 :   tree old_inner = STRIP_TEMPLATE (olddecl);
   22452        74852 :   tree new_inner = STRIP_TEMPLATE (newdecl);
   22453              : 
   22454        74852 :   if (DECL_LANG_SPECIFIC (new_inner))
   22455              :     {
   22456        73815 :       retrofit_lang_decl (old_inner);
   22457        73815 :       if (DECL_MODULE_PURVIEW_P (new_inner))
   22458        22937 :         DECL_MODULE_PURVIEW_P (old_inner) = true;
   22459        73815 :       if (!DECL_MODULE_IMPORT_P (new_inner))
   22460        73815 :         DECL_MODULE_IMPORT_P (old_inner) = false;
   22461              :     }
   22462              : 
   22463        74852 :   if (tree *p = imported_temploid_friends->get (newdecl))
   22464              :     {
   22465           58 :       tree orig = *p;
   22466           58 :       tree &slot = imported_temploid_friends->get_or_insert (olddecl);
   22467           58 :       if (!slot)
   22468           41 :         slot = orig;
   22469           17 :       else if (slot != orig)
   22470              :         /* This can happen when multiple classes declare the same
   22471              :            friend function (e.g. g++.dg/modules/tpl-friend-4);
   22472              :            make sure we at least attach to the same module.  */
   22473            3 :         gcc_checking_assert (get_originating_module (slot)
   22474              :                              == get_originating_module (orig));
   22475              :     }
   22476              : }
   22477              : 
   22478              : /* DECL is being freed, clear data we don't need anymore.  */
   22479              : 
   22480              : void
   22481        53016 : remove_defining_module (tree decl)
   22482              : {
   22483        53016 :   if (!modules_p ())
   22484              :     return;
   22485              : 
   22486        53016 :   if (imported_temploid_friends)
   22487        53016 :     imported_temploid_friends->remove (decl);
   22488              : }
   22489              : 
   22490              : /* Create the flat name string.  It is simplest to have it handy.  */
   22491              : 
   22492              : void
   22493         6507 : module_state::set_flatname ()
   22494              : {
   22495         6507 :   gcc_checking_assert (!flatname);
   22496         6507 :   if (parent)
   22497              :     {
   22498          704 :       auto_vec<tree,5> ids;
   22499          704 :       size_t len = 0;
   22500          704 :       char const *primary = NULL;
   22501          704 :       size_t pfx_len = 0;
   22502              : 
   22503          704 :       for (module_state *probe = this;
   22504         1711 :            probe;
   22505         1007 :            probe = probe->parent)
   22506         1543 :         if (is_partition () && !probe->is_partition ())
   22507              :           {
   22508          536 :             primary = probe->get_flatname ();
   22509          536 :             pfx_len = strlen (primary);
   22510          536 :             break;
   22511              :           }
   22512              :         else
   22513              :           {
   22514         1007 :             ids.safe_push (probe->name);
   22515         1007 :             len += IDENTIFIER_LENGTH (probe->name) + 1;
   22516              :           }
   22517              : 
   22518          704 :       char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
   22519          704 :       flatname = flat;
   22520              : 
   22521          704 :       if (primary)
   22522              :         {
   22523          536 :           memcpy (flat, primary, pfx_len);
   22524          536 :           flat += pfx_len;
   22525          536 :           *flat++ = ':';
   22526              :         }
   22527              : 
   22528         1711 :       for (unsigned len = 0; ids.length ();)
   22529              :         {
   22530         1007 :           if (len)
   22531          303 :             flat[len++] = '.';
   22532         1007 :           tree elt = ids.pop ();
   22533         1007 :           unsigned l = IDENTIFIER_LENGTH (elt);
   22534         1007 :           memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
   22535         1007 :           len += l;
   22536              :         }
   22537          704 :     }
   22538         5803 :   else if (is_header ())
   22539         1931 :     flatname = TREE_STRING_POINTER (name);
   22540              :   else
   22541         3872 :     flatname = IDENTIFIER_POINTER (name);
   22542         6507 : }
   22543              : 
   22544              : /* Open the GCM file and prepare to read.  Return whether that was
   22545              :    successful.  */
   22546              : 
   22547              : bool
   22548         3129 : module_state::open_slurp (cpp_reader *reader)
   22549              : {
   22550         3129 :   if (slurp)
   22551              :     return true;
   22552              : 
   22553         3085 :   if (lazy_open >= lazy_limit)
   22554            9 :     freeze_an_elf ();
   22555              : 
   22556         3085 :   int fd = -1;
   22557         3085 :   int e = ENOENT;
   22558         3085 :   if (filename)
   22559              :     {
   22560         3085 :       const char *file = maybe_add_cmi_prefix (filename);
   22561         3617 :       dump () && dump ("CMI is %s", file);
   22562         3085 :       if (note_module_cmi_yes || inform_cmi_p)
   22563           12 :         inform (loc, "reading CMI %qs", file);
   22564              :       /* Add the CMI file to the dependency tracking. */
   22565         3085 :       if (cpp_get_deps (reader))
   22566           15 :         deps_add_dep (cpp_get_deps (reader), file);
   22567         3085 :       fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
   22568         3085 :       e = errno;
   22569              :     }
   22570              : 
   22571         3085 :   gcc_checking_assert (!slurp);
   22572         6146 :   slurp = new slurping (new elf_in (fd, e));
   22573              : 
   22574         3085 :   bool ok = from ()->begin (loc);
   22575         3085 :   if (ok)
   22576              :     {
   22577         3061 :       lazy_open++;
   22578         3061 :       slurp->lru = ++lazy_lru;
   22579              :     }
   22580              :   return ok;
   22581              : }
   22582              : 
   22583              : /* Return whether importing this GCM would work without an error in
   22584              :    read_config.  */
   22585              : 
   22586              : bool
   22587           50 : module_state::check_importable (cpp_reader *reader)
   22588              : {
   22589           50 :   if (loadedness > ML_CONFIG)
   22590              :     return true;
   22591           47 :   if (!open_slurp (reader))
   22592              :     return false;
   22593           44 :   module_state_config config;
   22594           44 :   return read_config (config, /*complain*/false);
   22595              : }
   22596              : 
   22597              : /* Read the CMI file for a module.  */
   22598              : 
   22599              : bool
   22600         3082 : module_state::do_import (cpp_reader *reader, bool outermost)
   22601              : {
   22602         3082 :   gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
   22603              : 
   22604              :   /* If this TU is a partition of the module we're importing,
   22605              :      that module is the primary module interface.  */
   22606         3082 :   if (this_module ()->is_partition ()
   22607         3139 :       && this == get_primary (this_module ()))
   22608            9 :     module_p = true;
   22609              : 
   22610         3082 :   loc = linemap_module_loc (line_table, loc, get_flatname ());
   22611              : 
   22612         3082 :   bool ok = open_slurp (reader);
   22613         3082 :   if (!from ()->get_error ())
   22614              :     {
   22615         3061 :       announce ("importing");
   22616         3061 :       loadedness = ML_CONFIG;
   22617         3061 :       ok = read_initial (reader);
   22618              :     }
   22619              : 
   22620         3082 :   gcc_assert (slurp->current == ~0u);
   22621              : 
   22622         3082 :   return check_read (outermost, ok);
   22623              : }
   22624              : 
   22625              : /* Attempt to increase the file descriptor limit.  */
   22626              : 
   22627              : static bool
   22628         5039 : try_increase_lazy (unsigned want)
   22629              : {
   22630         5039 :   gcc_checking_assert (lazy_open >= lazy_limit);
   22631              : 
   22632              :   /* If we're increasing, saturate at hard limit.  */
   22633         5039 :   if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
   22634         5039 :     want = lazy_hard_limit;
   22635              : 
   22636              : #if HAVE_SETRLIMIT
   22637         5039 :   if ((!lazy_limit || !param_lazy_modules)
   22638         5027 :       && lazy_hard_limit
   22639         5027 :       && want <= lazy_hard_limit)
   22640              :     {
   22641         5027 :       struct rlimit rlimit;
   22642         5027 :       rlimit.rlim_cur = want + LAZY_HEADROOM;
   22643         5027 :       rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
   22644         5027 :       if (!setrlimit (RLIMIT_NOFILE, &rlimit))
   22645         5027 :         lazy_limit = want;
   22646              :     }
   22647              : #endif
   22648              : 
   22649         5039 :   return lazy_open < lazy_limit;
   22650              : }
   22651              : 
   22652              : /* Pick a victim module to freeze its reader.  */
   22653              : 
   22654              : void
   22655           12 : module_state::freeze_an_elf ()
   22656              : {
   22657           12 :   if (try_increase_lazy (lazy_open * 2))
   22658              :     return;
   22659              : 
   22660           12 :   module_state *victim = NULL;
   22661           42 :   for (unsigned ix = modules->length (); ix--;)
   22662              :     {
   22663           30 :       module_state *candidate = (*modules)[ix];
   22664           30 :       if (candidate && candidate->slurp && candidate->slurp->lru
   22665           18 :           && candidate->from ()->is_freezable ()
   22666           39 :           && (!victim || victim->slurp->lru > candidate->slurp->lru))
   22667           30 :         victim = candidate;
   22668              :     }
   22669              : 
   22670           12 :   if (victim)
   22671              :     {
   22672           18 :       dump () && dump ("Freezing '%s'", victim->filename);
   22673            9 :       if (victim->slurp->macro_defs.size)
   22674              :         /* Save the macro definitions to a buffer.  */
   22675            0 :         victim->from ()->preserve (victim->slurp->macro_defs);
   22676            9 :       if (victim->slurp->macro_tbl.size)
   22677              :         /* Save the macro definitions to a buffer.  */
   22678            0 :         victim->from ()->preserve (victim->slurp->macro_tbl);
   22679            9 :       victim->from ()->freeze ();
   22680            9 :       lazy_open--;
   22681              :     }
   22682              :   else
   22683            3 :     dump () && dump ("No module available for freezing");
   22684              : }
   22685              : 
   22686              : /* Load the lazy slot *MSLOT, INDEX'th slot of the module.  */
   22687              : 
   22688              : bool
   22689        35469 : module_state::lazy_load (unsigned index, binding_slot *mslot)
   22690              : {
   22691        35469 :   unsigned n = dump.push (this);
   22692              : 
   22693        35469 :   gcc_checking_assert (function_depth);
   22694              : 
   22695        35469 :   unsigned cookie = mslot->get_lazy ();
   22696        35469 :   unsigned snum = cookie >> 2;
   22697        35877 :   dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
   22698              : 
   22699        35469 :   bool ok = load_section (snum, mslot);
   22700              : 
   22701        35469 :   dump.pop (n);
   22702              : 
   22703        35469 :   return ok;
   22704              : }
   22705              : 
   22706              : /* Load MOD's binding for NS::ID into *MSLOT.  *MSLOT contains the
   22707              :    lazy cookie.  OUTER is true if this is the outermost lazy, (used
   22708              :    for diagnostics).  */
   22709              : 
   22710              : void
   22711         3981 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
   22712              : {
   22713         3981 :   int count = errorcount + warningcount;
   22714              : 
   22715         3981 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22716              : 
   22717              :   /* Make sure lazy loading from a template context behaves as if
   22718              :      from a non-template context.  */
   22719         3981 :   processing_template_decl_sentinel ptds;
   22720              : 
   22721              :   /* Stop GC happening, even in outermost loads (because our caller
   22722              :      could well be building up a lookup set).  */
   22723         3981 :   function_depth++;
   22724              : 
   22725         3981 :   gcc_checking_assert (mod);
   22726         3981 :   module_state *module = (*modules)[mod];
   22727         3981 :   unsigned n = dump.push (module);
   22728              : 
   22729         3981 :   unsigned snum = mslot->get_lazy ();
   22730         4336 :   dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
   22731              :                    module->name, snum);
   22732              : 
   22733         3981 :   bool ok = !recursive_lazy (snum);
   22734         3981 :   if (ok)
   22735              :     {
   22736         3981 :       ok = module->load_section (snum, mslot);
   22737         3981 :       lazy_snum = 0;
   22738         3981 :       post_load_processing ();
   22739              :     }
   22740              : 
   22741         3981 :   dump.pop (n);
   22742              : 
   22743         3981 :   function_depth--;
   22744              : 
   22745         3981 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22746              : 
   22747         3981 :   if (!ok)
   22748            0 :     fatal_error (input_location,
   22749            0 :                  module->is_header ()
   22750              :                  ? G_("failed to load binding %<%E%s%E%>")
   22751              :                  : G_("failed to load binding %<%E%s%E@%s%>"),
   22752            0 :                  ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22753              :                  module->get_flatname ());
   22754              : 
   22755         3981 :   if (count != errorcount + warningcount)
   22756           27 :     inform (input_location,
   22757           27 :             module->is_header ()
   22758              :             ? G_("during load of binding %<%E%s%E%>")
   22759              :             : G_("during load of binding %<%E%s%E@%s%>"),
   22760           27 :             ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22761              :             module->get_flatname ());
   22762         3981 : }
   22763              : 
   22764              : /* Load any pending entities keyed to NS and NAME.
   22765              :    Used to find pending types if we don't yet have a decl built.  */
   22766              : 
   22767              : void
   22768     40543926 : lazy_load_pendings (tree ns, tree name)
   22769              : {
   22770              :   /* Make sure lazy loading from a template context behaves as if
   22771              :      from a non-template context.  */
   22772     40543926 :   processing_template_decl_sentinel ptds;
   22773              : 
   22774     40543926 :   pending_key key;
   22775     40543926 :   key.ns = ns;
   22776     40543926 :   key.id = name;
   22777              : 
   22778     40543926 :   auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
   22779     40538895 :   if (!pending_vec)
   22780     40536035 :     return;
   22781              : 
   22782         7891 :   int count = errorcount + warningcount;
   22783              : 
   22784         7891 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22785         7891 :   bool ok = !recursive_lazy ();
   22786         7891 :   if (ok)
   22787              :     {
   22788         7891 :       function_depth++; /* Prevent GC */
   22789         7891 :       unsigned n = dump.push (NULL);
   22790         8395 :       dump () && dump ("Reading %u pending entities keyed to %P",
   22791              :                        pending_vec->length (), key.ns, key.id);
   22792        87606 :       for (unsigned ix = pending_vec->length (); ix--;)
   22793              :         {
   22794        71824 :           unsigned index = (*pending_vec)[ix];
   22795        71824 :           binding_slot *slot = &(*entity_ary)[index];
   22796              : 
   22797        71824 :           if (slot->is_lazy ())
   22798              :             {
   22799         3292 :               module_state *import = import_entity_module (index);
   22800         3292 :               if (!import->lazy_load (index - import->entity_lwm, slot))
   22801        71824 :                 ok = false;
   22802              :             }
   22803        68532 :           else if (dump ())
   22804              :             {
   22805          363 :               module_state *import = import_entity_module (index);
   22806        71824 :               dump () && dump ("Entity %M[%u] already loaded",
   22807          363 :                                import, index - import->entity_lwm);
   22808              :             }
   22809              :         }
   22810              : 
   22811         7891 :       pending_table->remove (key);
   22812         7891 :       dump.pop (n);
   22813         7891 :       lazy_snum = 0;
   22814         7891 :       post_load_processing ();
   22815         7891 :       function_depth--;
   22816              :     }
   22817              : 
   22818         7891 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22819              : 
   22820         7891 :   if (!ok)
   22821            0 :     fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
   22822            0 :                  key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22823              : 
   22824         7891 :   if (count != errorcount + warningcount)
   22825            0 :     inform (input_location, "during load of pendings for %<%E%s%E%>",
   22826            0 :             key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22827     40543926 : }
   22828              : 
   22829              : /* Load any pending entities keyed to the top-key of DECL.  */
   22830              : 
   22831              : void
   22832     40444495 : lazy_load_pendings (tree decl)
   22833              : {
   22834     40444495 :   tree key_decl;
   22835     40444495 :   tree ns = find_pending_key (decl, &key_decl);
   22836     40444495 :   return lazy_load_pendings (ns, DECL_NAME (key_decl));
   22837              : }
   22838              : 
   22839              : static void
   22840         2800 : direct_import (module_state *import, cpp_reader *reader)
   22841              : {
   22842         2800 :   timevar_start (TV_MODULE_IMPORT);
   22843         2800 :   unsigned n = dump.push (import);
   22844              : 
   22845         2800 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22846         2800 :   if (import->loadedness == ML_NONE)
   22847         1854 :     if (!import->do_import (reader, true))
   22848            0 :       gcc_unreachable ();
   22849              : 
   22850         2763 :   this_module ()->set_import (import, import->exported_p);
   22851              : 
   22852         2763 :   if (import->loadedness < ML_LANGUAGE)
   22853              :     {
   22854         2684 :       if (!keyed_table)
   22855         2360 :         keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22856         2684 :       import->read_language (true);
   22857              :     }
   22858              : 
   22859         2763 :   dump.pop (n);
   22860         2763 :   timevar_stop (TV_MODULE_IMPORT);
   22861         2763 : }
   22862              : 
   22863              : /* Import module IMPORT.  */
   22864              : 
   22865              : void
   22866         2564 : import_module (module_state *import, location_t from_loc, bool exporting_p,
   22867              :                tree, cpp_reader *reader)
   22868              : {
   22869              :   /* A non-partition implementation unit has no name.  */
   22870         2564 :   if (!this_module ()->name && this_module ()->parent == import)
   22871              :     {
   22872            3 :       auto_diagnostic_group d;
   22873            3 :       error_at (from_loc, "import of %qs within its own implementation unit",
   22874              :                 import->get_flatname());
   22875            3 :       inform (import->loc, "module declared here");
   22876            3 :       return;
   22877            3 :     }
   22878              : 
   22879         2561 :   if (!import->check_circular_import (from_loc))
   22880              :     return;
   22881              : 
   22882         2555 :   if (!import->is_header () && current_lang_depth ())
   22883              :     /* Only header units should appear inside language
   22884              :        specifications.  The std doesn't specify this, but I think
   22885              :        that's an error in resolving US 033, because language linkage
   22886              :        is also our escape clause to getting things into the global
   22887              :        module, so we don't want to confuse things by having to think
   22888              :        about whether 'extern "C++" { import foo; }' puts foo's
   22889              :        contents into the global module all of a sudden.  */
   22890            6 :     warning (0, "import of named module %qs inside language-linkage block",
   22891              :              import->get_flatname ());
   22892              : 
   22893         2555 :   if (exporting_p || module_exporting_p ())
   22894          331 :     import->exported_p = true;
   22895              : 
   22896         2555 :   if (import->loadedness != ML_NONE)
   22897              :     {
   22898          943 :       from_loc = ordinary_loc_of (line_table, from_loc);
   22899          943 :       linemap_module_reparent (line_table, import->loc, from_loc);
   22900              :     }
   22901              : 
   22902         2555 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22903              : 
   22904         2555 :   direct_import (import, reader);
   22905              : }
   22906              : 
   22907              : /* Declare the name of the current module to be NAME.  EXPORTING_p is
   22908              :    true if this TU is the exporting module unit.  */
   22909              : 
   22910              : void
   22911         3222 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
   22912              :                 tree, cpp_reader *reader)
   22913              : {
   22914         3222 :   gcc_assert (global_namespace == current_scope ());
   22915              : 
   22916         3222 :   module_state *current = this_module ();
   22917         3222 :   if (module_purview_p () || module->loadedness > ML_CONFIG)
   22918              :     {
   22919            6 :       auto_diagnostic_group d;
   22920           12 :       error_at (from_loc, module_purview_p ()
   22921              :                 ? G_("module already declared")
   22922              :                 : G_("module already imported"));
   22923            6 :       if (module_purview_p ())
   22924            0 :         module = current;
   22925           12 :       inform (module->loc, module_purview_p ()
   22926              :               ? G_("module %qs declared here")
   22927              :               : G_("module %qs imported here"),
   22928              :               module->get_flatname ());
   22929            6 :       return;
   22930            6 :     }
   22931              : 
   22932         3216 :   gcc_checking_assert (module->is_module ());
   22933         3216 :   gcc_checking_assert (module->is_direct () && module->has_location ());
   22934              : 
   22935              :   /* Yer a module, 'arry.  */
   22936         3216 :   module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
   22937              : 
   22938              :   // Even in header units, we consider the decls to be purview
   22939         3216 :   module_kind |= MK_PURVIEW;
   22940              : 
   22941         3216 :   if (module->is_partition ())
   22942          220 :     module_kind |= MK_PARTITION;
   22943         3216 :   if (exporting_p)
   22944              :     {
   22945         2900 :       module->interface_p = true;
   22946         2900 :       module_kind |= MK_INTERFACE;
   22947              :     }
   22948              : 
   22949         3216 :   if (module_has_cmi_p ())
   22950              :     {
   22951              :       /* Copy the importing information we may have already done.  We
   22952              :          do not need to separate out the imports that only happen in
   22953              :          the GMF, in spite of what the literal wording of the std
   22954              :          might imply.  See p2191, the core list had a discussion
   22955              :          where the module implementors agreed that the GMF of a named
   22956              :          module is invisible to importers.  */
   22957         2971 :       module->imports = current->imports;
   22958              : 
   22959         2971 :       module->mod = 0;
   22960         2971 :       (*modules)[0] = module;
   22961              :     }
   22962              :   else
   22963              :     {
   22964          245 :       module->interface_p = true;
   22965          245 :       current->parent = module; /* So mangler knows module identity. */
   22966          245 :       direct_import (module, reader);
   22967              :     }
   22968              : }
   22969              : 
   22970              : /* Return true IFF we must emit a module global initializer function
   22971              :    (which will be called by importers' init code).  */
   22972              : 
   22973              : bool
   22974       107434 : module_global_init_needed ()
   22975              : {
   22976       107434 :   return module_has_cmi_p () && !header_module_p ();
   22977              : }
   22978              : 
   22979              : /* Calculate which, if any, import initializers need calling.  */
   22980              : 
   22981              : bool
   22982        99779 : module_determine_import_inits ()
   22983              : {
   22984        99779 :   if (!modules || header_module_p ())
   22985              :     return false;
   22986              : 
   22987              :   /* Prune active_init_p.  We need the same bitmap allocation
   22988              :      scheme as for the imports member.  */
   22989         3928 :   function_depth++; /* Disable GC.  */
   22990         3928 :   bitmap covered_imports (BITMAP_GGC_ALLOC ());
   22991              : 
   22992         3928 :   bool any = false;
   22993              : 
   22994              :   /* Because indirect imports are before their direct import, and
   22995              :      we're scanning the array backwards, we only need one pass!  */
   22996         6826 :   for (unsigned ix = modules->length (); --ix;)
   22997              :     {
   22998         2898 :       module_state *import = (*modules)[ix];
   22999              : 
   23000         2898 :       if (!import->active_init_p)
   23001              :         ;
   23002           64 :       else if (bitmap_bit_p (covered_imports, ix))
   23003            9 :         import->active_init_p = false;
   23004              :       else
   23005              :         {
   23006              :           /* Everything this imports is therefore handled by its
   23007              :              initializer, so doesn't need initializing by us.  */
   23008           55 :           bitmap_ior_into (covered_imports, import->imports);
   23009           55 :           any = true;
   23010              :         }
   23011              :     }
   23012         3928 :   function_depth--;
   23013              : 
   23014         3928 :   return any;
   23015              : }
   23016              : 
   23017              : /* Emit calls to each direct import's global initializer.  Including
   23018              :    direct imports of directly imported header units.  The initializers
   23019              :    of (static) entities in header units will be called by their
   23020              :    importing modules (for the instance contained within that), or by
   23021              :    the current TU (for the instances we've brought in).  Of course
   23022              :    such header unit behaviour is evil, but iostream went through that
   23023              :    door some time ago.  */
   23024              : 
   23025              : void
   23026           55 : module_add_import_initializers ()
   23027              : {
   23028           55 :   if (!modules || header_module_p ())
   23029            0 :     return;
   23030              : 
   23031           55 :   tree fntype = build_function_type (void_type_node, void_list_node);
   23032           55 :   releasing_vec args;  // There are no args
   23033              : 
   23034          125 :   for (unsigned ix = modules->length (); --ix;)
   23035              :     {
   23036           70 :       module_state *import = (*modules)[ix];
   23037           70 :       if (import->active_init_p)
   23038              :         {
   23039           55 :           tree name = mangle_module_global_init (ix);
   23040           55 :           tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
   23041              : 
   23042           55 :           DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
   23043           55 :           SET_DECL_ASSEMBLER_NAME (fndecl, name);
   23044           55 :           TREE_PUBLIC (fndecl) = true;
   23045           55 :           determine_visibility (fndecl);
   23046              : 
   23047           55 :           tree call = cp_build_function_call_vec (fndecl, &args,
   23048              :                                                   tf_warning_or_error);
   23049           55 :           finish_expr_stmt (call);
   23050              :         }
   23051              :     }
   23052           55 : }
   23053              : 
   23054              : /* NAME & LEN are a preprocessed header name, possibly including the
   23055              :    surrounding "" or <> characters.  Return the raw string name of the
   23056              :    module to which it refers.  This will be an absolute path, or begin
   23057              :    with ./, so it is immediately distinguishable from a (non-header
   23058              :    unit) module name.  If READER is non-null, ask the preprocessor to
   23059              :    locate the header to which it refers using the appropriate include
   23060              :    path.  Note that we do never do \ processing of the string, as that
   23061              :    matches the preprocessor's behaviour.  */
   23062              : 
   23063              : static const char *
   23064        25959 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
   23065              :                           const char *str, size_t &len_r)
   23066              : {
   23067        25959 :   size_t len = len_r;
   23068        25959 :   static char *buf = 0;
   23069        25959 :   static size_t alloc = 0;
   23070              : 
   23071        25959 :   if (!unquoted)
   23072              :     {
   23073            4 :       gcc_checking_assert (len >= 2
   23074              :                            && ((reader && str[0] == '<' && str[len-1] == '>')
   23075              :                                || (str[0] == '"' && str[len-1] == '"')));
   23076            4 :       str += 1;
   23077            4 :       len -= 2;
   23078              :     }
   23079              : 
   23080        25959 :   if (reader)
   23081              :     {
   23082            4 :       gcc_assert (!unquoted);
   23083              : 
   23084            4 :       if (len >= alloc)
   23085              :         {
   23086            4 :           alloc = len + 1;
   23087            4 :           buf = XRESIZEVEC (char, buf, alloc);
   23088              :         }
   23089            4 :       memcpy (buf, str, len);
   23090            4 :       buf[len] = 0;
   23091              : 
   23092            8 :       if (const char *hdr
   23093            4 :           = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
   23094              :         {
   23095            4 :           len = strlen (hdr);
   23096            4 :           str = hdr;
   23097              :         }
   23098              :       else
   23099            0 :         str = buf;
   23100              :     }
   23101              : 
   23102        25959 :   if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
   23103              :     {
   23104              :       /* Prepend './'  */
   23105            9 :       if (len + 3 > alloc)
   23106              :         {
   23107            9 :           alloc = len + 3;
   23108            9 :           buf = XRESIZEVEC (char, buf, alloc);
   23109              :         }
   23110              : 
   23111            9 :       buf[0] = '.';
   23112            9 :       buf[1] = DIR_SEPARATOR;
   23113            9 :       memmove (buf + 2, str, len);
   23114            9 :       len += 2;
   23115            9 :       buf[len] = 0;
   23116            9 :       str = buf;
   23117              :     }
   23118              : 
   23119        25959 :   len_r = len;
   23120        25959 :   return str;
   23121              : }
   23122              : 
   23123              : /* Set the CMI name from a cody packet.  Issue an error if
   23124              :    ill-formed.  */
   23125              : 
   23126         5905 : void module_state::set_filename (const Cody::Packet &packet)
   23127              : {
   23128         5905 :   if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23129              :     {
   23130              :       /* If we've seen this import before we better have the same CMI.  */
   23131         5902 :       const std::string &path = packet.GetString ();
   23132         5902 :       if (!filename)
   23133         5899 :         filename = xstrdup (packet.GetString ().c_str ());
   23134            3 :       else if (filename != path)
   23135            0 :         error_at (loc, "mismatching compiled module interface: "
   23136              :                   "had %qs, got %qs", filename, path.c_str ());
   23137              :     }
   23138              :   else
   23139              :     {
   23140            3 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23141            3 :       fatal_error (loc, "unknown compiled module interface: %s",
   23142            3 :                    packet.GetString ().c_str ());
   23143              :     }
   23144         5902 : }
   23145              : 
   23146              : /* The list of importable headers from C++ Table 24.  */
   23147              : 
   23148              : static const char *
   23149              : importable_headers[] =
   23150              :   {
   23151              :     "algorithm", "any", "array", "atomic",
   23152              :     "barrier", "bit", "bitset",
   23153              :     "charconv", "chrono", "compare", "complex", "concepts",
   23154              :     "condition_variable", "contracts", "coroutine",
   23155              :     "debugging", "deque",
   23156              :     "exception", "execution", "expected",
   23157              :     "filesystem", "flat_map", "flat_set", "format", "forward_list",
   23158              :     "fstream", "functional", "future",
   23159              :     "generator",
   23160              :     "hazard_pointer", "hive",
   23161              :     "initializer_list", "inplace_vector", "iomanip", "ios", "iosfwd",
   23162              :     "iostream", "istream", "iterator",
   23163              :     "latch", "limits", "linalg", "list", "locale",
   23164              :     "map", "mdspan", "memory", "memory_resource", "meta", "mutex",
   23165              :     "new", "numbers", "numeric",
   23166              :     "optional", "ostream",
   23167              :     "print",
   23168              :     "queue",
   23169              :     "random", "ranges", "ratio", "rcu", "regex",
   23170              :     "scoped_allocator", "semaphore", "set", "shared_mutex", "simd",
   23171              :     "source_location", "span", "spanstream", "sstream", "stack", "stacktrace",
   23172              :     "stdexcept", "stdfloat", "stop_token", "streambuf", "string",
   23173              :     "string_view", "syncstream", "system_error",
   23174              :     "text_encoding", "thread", "tuple", "type_traits", "typeindex", "typeinfo",
   23175              :     "unordered_map", "unordered_set",
   23176              :     "utility",
   23177              :     "valarray", "variant", "vector", "version"
   23178              :   };
   23179              : 
   23180              : /* True iff <name> is listed as an importable standard header.  */
   23181              : 
   23182              : static bool
   23183        23651 : is_importable_header (const char *name)
   23184              : {
   23185        23651 :   unsigned lo = 0;
   23186        23651 :   unsigned hi = ARRAY_SIZE (importable_headers);
   23187       179430 :   while (hi > lo)
   23188              :     {
   23189       158557 :       unsigned mid = (lo + hi)/2;
   23190       158557 :       int cmp = strcmp (name, importable_headers[mid]);
   23191       158557 :       if (cmp > 0)
   23192        36561 :         lo = mid + 1;
   23193       121996 :       else if (cmp < 0)
   23194              :         hi = mid;
   23195              :       else
   23196              :         return true;
   23197              :     }
   23198              :   return false;
   23199              : }
   23200              : 
   23201              : /* Figure out whether to treat HEADER as an include or an import.  */
   23202              : 
   23203              : static char *
   23204        25026 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
   23205              :                          _cpp_file *file, bool angle, const char **alternate)
   23206              : {
   23207        25026 :   if (!modules_p () || !flag_module_include_translate)
   23208              :     {
   23209              :       /* Turn off.  */
   23210            4 :       cpp_get_callbacks (reader)->translate_include = NULL;
   23211            4 :       return nullptr;
   23212              :     }
   23213              : 
   23214        25022 :   const char *path = _cpp_get_file_path (file);
   23215              : 
   23216        25022 :   dump.push (NULL);
   23217              : 
   23218        26547 :   dump () && dump ("Checking include translation '%s'", path);
   23219        25022 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23220              : 
   23221        25022 :   size_t len = strlen (path);
   23222        25022 :   path = canonicalize_header_name (NULL, loc, true, path, len);
   23223        25022 :   auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
   23224              : 
   23225        25022 :   enum class xlate_kind {
   23226              :     unknown, text, import, invalid
   23227        25022 :   } translate = xlate_kind::unknown;
   23228              : 
   23229        25022 :   if (packet.GetCode () == Cody::Client::PC_BOOL)
   23230        24975 :     translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
   23231           47 :   else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23232              :     {
   23233              :       /* Record the CMI name for when we do the import.
   23234              :          We may already know about this import, but libcpp doesn't yet.  */
   23235           47 :       module_state *import = get_module (build_string (len, path));
   23236           47 :       import->set_filename (packet);
   23237           47 :       if (import->check_importable (reader))
   23238              :         translate = xlate_kind::import;
   23239              :       else
   23240            0 :         translate = xlate_kind::invalid;
   23241              :     }
   23242              :   else
   23243              :     {
   23244            0 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23245            0 :       error_at (loc, "cannot determine %<#include%> translation of %s: %s",
   23246            0 :                 path, packet.GetString ().c_str ());
   23247              :     }
   23248              : 
   23249        25022 :   bool note = (translate == xlate_kind::invalid);
   23250        25022 :   if (note_include_translate_yes && translate == xlate_kind::import)
   23251              :     note = true;
   23252        25019 :   else if (note_include_translate_no && translate == xlate_kind::unknown)
   23253              :     note = true;
   23254        25016 :   else if (note_includes)
   23255              :     /* We do not expect the note_includes vector to be large, so O(N)
   23256              :        iteration.  */
   23257          434 :     for (unsigned ix = note_includes->length (); !note && ix--;)
   23258          217 :       if (!strcmp ((*note_includes)[ix], path))
   23259            1 :         note = true;
   23260              : 
   23261              :   /* Maybe try importing a different header instead.  */
   23262        25022 :   if (alternate && translate == xlate_kind::unknown)
   23263              :     {
   23264        24574 :       const char *fname = _cpp_get_file_name (file);
   23265              :       /* Redirect importable <name> to <bits/stdc++.h>.  */
   23266              :       /* ??? Generalize to use a .json.  */
   23267        24574 :       expanded_location eloc = expand_location (loc);
   23268        27338 :       auto indir = [](const char *f, const char *d)
   23269              :       {
   23270         2764 :         if (!filename_ncmp (f, d, strlen (d))) return true;
   23271              :         /* Also check canonical paths (c++/123879).  */
   23272          926 :         auto cf = lrealpath (f); auto cd = lrealpath (d);
   23273          926 :         bool r = cf && cd && !filename_ncmp (cf, cd, strlen (cd));
   23274          926 :         free (cf); free (cd);
   23275          926 :         return r;
   23276              :       };
   23277        23651 :       if (angle && is_importable_header (fname)
   23278              :           /* Exclude <version> which often goes with import std.  */
   23279         2778 :           && strcmp (fname, "version") != 0
   23280              :           /* Don't redirect #includes between headers under the same include
   23281              :              path directory (i.e. between library headers); if the import
   23282              :              brings in the current file we then get redefinition errors.  */
   23283         2764 :           && !indir (eloc.file, _cpp_get_file_dir (file)->name)
   23284              :           /* ??? These are needed when running a toolchain from the build
   23285              :              directory, because libsupc++ headers aren't linked into
   23286              :              libstdc++-v3/include with the other headers.  */
   23287          848 :           && !strstr (eloc.file, "libstdc++-v3/include")
   23288        25060 :           && !strstr (eloc.file, "libsupc++"))
   23289          403 :         *alternate = "bits/stdc++.h";
   23290              :     }
   23291              : 
   23292        25022 :   if (note)
   23293           10 :     inform (loc, translate == xlate_kind::import
   23294              :             ? G_("include %qs translated to import")
   23295              :             : translate == xlate_kind::invalid
   23296            3 :             ? G_("import of %qs failed, falling back to include")
   23297              :             : G_("include %qs processed textually"), path);
   23298              : 
   23299        28069 :   dump () && dump (translate == xlate_kind::import
   23300              :                    ? "Translating include to import"
   23301              :                    : "Keeping include as include");
   23302        25022 :   dump.pop (0);
   23303              : 
   23304        25022 :   if (translate != xlate_kind::import)
   23305              :     return nullptr;
   23306              : 
   23307              :   /* Create the translation text.  */
   23308           47 :   loc = ordinary_loc_of (lmaps, loc);
   23309           47 :   const line_map_ordinary *map
   23310           47 :     = linemap_check_ordinary (linemap_lookup (lmaps, loc));
   23311           47 :   unsigned col = SOURCE_COLUMN (map, loc);
   23312           47 :   col -= (col != 0); /* Columns are 1-based.  */
   23313              : 
   23314           47 :   unsigned alloc = len + col + 60;
   23315           47 :   char *res = XNEWVEC (char, alloc);
   23316              : 
   23317           47 :   strcpy (res, "__import");
   23318           47 :   unsigned actual = 8;
   23319           47 :   if (col > actual)
   23320              :     {
   23321              :       /* Pad out so the filename appears at the same position.  */
   23322           44 :       memset (res + actual, ' ', col - actual);
   23323           44 :       actual = col;
   23324              :     }
   23325              :   /* No need to encode characters, that's not how header names are
   23326              :      handled.  */
   23327           47 :   actual += snprintf (res + actual, alloc - actual,
   23328              :                       "\"%s\" [[__translated]];\n", path);
   23329           47 :   gcc_checking_assert (actual < alloc);
   23330              : 
   23331              :   /* cpplib will delete the buffer.  */
   23332              :   return res;
   23333        25022 : }
   23334              : 
   23335              : static void
   23336          933 : begin_header_unit (cpp_reader *reader)
   23337              : {
   23338              :   /* Set the module header name from the main_input_filename.  */
   23339          933 :   const char *main = main_input_filename;
   23340          933 :   size_t len = strlen (main);
   23341          933 :   main = canonicalize_header_name (NULL, 0, true, main, len);
   23342          933 :   module_state *module = get_module (build_string (len, main));
   23343              : 
   23344          933 :   preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
   23345          933 : }
   23346              : 
   23347              : /* We've just properly entered the main source file.  I.e. after the
   23348              :    command line, builtins and forced headers.  Record the line map and
   23349              :    location of this map.  Note we may be called more than once.  The
   23350              :    first call sticks.  */
   23351              : 
   23352              : void
   23353       101777 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
   23354              :                        const line_map_ordinary *map)
   23355              : {
   23356       101777 :   gcc_checking_assert (lmaps == line_table);
   23357       101777 :   if (modules_p () && !spans.init_p ())
   23358              :     {
   23359         5027 :       unsigned n = dump.push (NULL);
   23360         5027 :       spans.init (lmaps, map);
   23361         5027 :       dump.pop (n);
   23362         5027 :       if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
   23363              :         {
   23364              :           /* Tell the preprocessor this is an include file.  */
   23365          924 :           cpp_retrofit_as_include (reader);
   23366          924 :           begin_header_unit (reader);
   23367              :         }
   23368              :     }
   23369       101777 : }
   23370              : 
   23371              : /* Process the pending_import queue, making sure we know the
   23372              :    filenames.   */
   23373              : 
   23374              : static void
   23375         5942 : name_pending_imports (cpp_reader *reader)
   23376              : {
   23377         5942 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23378              : 
   23379         5942 :   if (!vec_safe_length (pending_imports))
   23380              :     /* Not doing anything.  */
   23381              :     return;
   23382              : 
   23383         5089 :   timevar_start (TV_MODULE_MAPPER);
   23384              : 
   23385         5089 :   auto n = dump.push (NULL);
   23386         5711 :   dump () && dump ("Resolving direct import names");
   23387         5089 :   bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
   23388         5089 :                     || cpp_get_deps (reader));
   23389         5089 :   bool any = false;
   23390              : 
   23391        11129 :   for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23392              :     {
   23393         6040 :       module_state *module = (*pending_imports)[ix];
   23394         6040 :       gcc_checking_assert (module->is_direct ());
   23395         6040 :       if (!module->filename && !module->visited_p)
   23396              :         {
   23397         5930 :           bool export_p = (module->is_module ()
   23398         5930 :                            && (module->is_partition ()
   23399         3074 :                                || module->is_exported ()));
   23400              : 
   23401         5930 :           Cody::Flags flags = Cody::Flags::None;
   23402         5930 :           if (flag_preprocess_only
   23403         5930 :               && !(module->is_header () && !export_p))
   23404              :             {
   23405          141 :               if (!want_deps)
   23406           72 :                 continue;
   23407              :               flags = Cody::Flags::NameOnly;
   23408              :             }
   23409              : 
   23410         5858 :           if (!any)
   23411              :             {
   23412         5016 :               any = true;
   23413         5016 :               mapper->Cork ();
   23414              :             }
   23415         5858 :           if (export_p)
   23416         3025 :             mapper->ModuleExport (module->get_flatname (), flags);
   23417              :           else
   23418         2833 :             mapper->ModuleImport (module->get_flatname (), flags);
   23419         5858 :           module->visited_p = true;
   23420              :         }
   23421              :     }
   23422              : 
   23423         5089 :   if (any)
   23424              :     {
   23425         5016 :       auto response = mapper->Uncork ();
   23426         5016 :       auto r_iter = response.begin ();
   23427        10965 :       for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23428              :         {
   23429         5952 :           module_state *module = (*pending_imports)[ix];
   23430         5952 :           if (module->visited_p)
   23431              :             {
   23432         5858 :               module->visited_p = false;
   23433         5858 :               gcc_checking_assert (!module->filename);
   23434              : 
   23435         5858 :               module->set_filename (*r_iter);
   23436         5855 :               ++r_iter;
   23437              :             }
   23438              :         }
   23439         5013 :     }
   23440              : 
   23441         5086 :   dump.pop (n);
   23442              : 
   23443         5086 :   timevar_stop (TV_MODULE_MAPPER);
   23444              : }
   23445              : 
   23446              : /* We've just lexed a module-specific control line for MODULE.  Mark
   23447              :    the module as a direct import, and possibly load up its macro
   23448              :    state.  Returns the primary module, if this is a module
   23449              :    declaration.  */
   23450              : /* Perhaps we should offer a preprocessing mode where we read the
   23451              :    directives from the header unit, rather than require the header's
   23452              :    CMI.  */
   23453              : 
   23454              : module_state *
   23455         6059 : preprocess_module (module_state *module, location_t from_loc,
   23456              :                    bool in_purview, bool is_import, bool is_export,
   23457              :                    cpp_reader *reader)
   23458              : {
   23459         6059 :   if (!is_import)
   23460              :     {
   23461         3378 :       if (in_purview || module->loc)
   23462              :         {
   23463              :           /* We've already seen a module declaration.  If only preprocessing
   23464              :              then we won't complain in declare_module, so complain here.  */
   23465           42 :           if (flag_preprocess_only)
   23466            6 :             error_at (from_loc,
   23467              :                       in_purview
   23468              :                       ? G_("module already declared")
   23469              :                       : G_("module already imported"));
   23470              :           /* Always pretend this was an import to aid error recovery.  */
   23471              :           is_import = true;
   23472              :         }
   23473              :       else
   23474              :         {
   23475              :           /* Record it is the module.  */
   23476         3336 :           module->module_p = true;
   23477         3336 :           if (is_export)
   23478              :             {
   23479         3002 :               module->exported_p = true;
   23480         3002 :               module->interface_p = true;
   23481              :             }
   23482              :         }
   23483              :     }
   23484              : 
   23485         6059 :   if (module->directness < MD_DIRECT + in_purview)
   23486              :     {
   23487              :       /* Mark as a direct import.  */
   23488         6013 :       module->directness = module_directness (MD_DIRECT + in_purview);
   23489              : 
   23490              :       /* Set the location to be most informative for users.  */
   23491         6013 :       from_loc = ordinary_loc_of (line_table, from_loc);
   23492         6013 :       if (module->loadedness != ML_NONE)
   23493            6 :         linemap_module_reparent (line_table, module->loc, from_loc);
   23494              :       else
   23495              :         {
   23496              :           /* Don't overwrite the location if we're importing ourselves
   23497              :              after already having seen a module-declaration.  */
   23498         6007 :           if (!(is_import && module->is_module ()))
   23499         5977 :             module->loc = from_loc;
   23500         6007 :           if (!module->flatname)
   23501         5968 :             module->set_flatname ();
   23502              :         }
   23503              :     }
   23504              : 
   23505         6059 :   auto desired = ML_CONFIG;
   23506         6059 :   if (is_import
   23507         2723 :       && module->is_header ()
   23508         6987 :       && (!cpp_get_options (reader)->preprocessed
   23509            3 :           || cpp_get_options (reader)->directives_only))
   23510              :     /* We need preprocessor state now.  */
   23511              :     desired = ML_PREPROCESSOR;
   23512              : 
   23513         6059 :   if (!is_import || module->loadedness < desired)
   23514              :     {
   23515         6040 :       vec_safe_push (pending_imports, module);
   23516              : 
   23517         6040 :       if (desired == ML_PREPROCESSOR)
   23518              :         {
   23519          909 :           unsigned n = dump.push (NULL);
   23520              : 
   23521         1108 :           dump () && dump ("Reading %M preprocessor state", module);
   23522          909 :           name_pending_imports (reader);
   23523              : 
   23524              :           /* Preserve the state of the line-map.  */
   23525          909 :           auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
   23526              : 
   23527              :           /* We only need to close the span, if we're going to emit a
   23528              :              CMI.  But that's a little tricky -- our token scanner
   23529              :              needs to be smarter -- and this isn't much state.
   23530              :              Remember, we've not parsed anything at this point, so
   23531              :              our module state flags are inadequate.  */
   23532          909 :           spans.maybe_init ();
   23533          909 :           spans.close ();
   23534              : 
   23535          909 :           timevar_start (TV_MODULE_IMPORT);
   23536              : 
   23537              :           /* Load the config of each pending import -- we must assign
   23538              :              module numbers monotonically.  */
   23539         2914 :           for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23540              :             {
   23541         1096 :               auto *import = (*pending_imports)[ix];
   23542         1274 :               if (!(import->is_module ()
   23543          178 :                     && (import->is_partition () || import->is_exported ()))
   23544          924 :                   && import->loadedness == ML_NONE
   23545         2020 :                   && (!flag_preprocess_only
   23546           51 :                       || (import->is_header ()
   23547              :                           /* Allow a missing/unimportable GCM with -MG.
   23548              :                              FIXME We should also try falling back to #include
   23549              :                              before giving up entirely.  */
   23550           42 :                           && (!cpp_get_options (reader)->deps.missing_files
   23551            3 :                               || import->check_importable (reader)))))
   23552              :                 {
   23553          912 :                   unsigned n = dump.push (import);
   23554          912 :                   import->do_import (reader, true);
   23555          912 :                   dump.pop (n);
   23556              :                 }
   23557              :             }
   23558          909 :           vec_free (pending_imports);
   23559              : 
   23560              :           /* Restore the line-map state.  */
   23561          909 :           spans.open (linemap_module_restore (line_table, pre_hwm));
   23562              : 
   23563              :           /* Now read the preprocessor state of this particular
   23564              :              import.  */
   23565          909 :           if (module->loadedness == ML_CONFIG
   23566          909 :               && module->read_preprocessor (true))
   23567          903 :             module->import_macros ();
   23568              : 
   23569          909 :           timevar_stop (TV_MODULE_IMPORT);
   23570              : 
   23571          909 :           dump.pop (n);
   23572              :         }
   23573              :     }
   23574              : 
   23575         6059 :   return is_import ? NULL : get_primary (module);
   23576              : }
   23577              : 
   23578              : /* We've completed phase-4 translation.  Emit any dependency
   23579              :    information for the not-yet-loaded direct imports, and fill in
   23580              :    their file names.  We'll have already loaded up the direct header
   23581              :    unit wavefront.  */
   23582              : 
   23583              : void
   23584         5033 : preprocessed_module (cpp_reader *reader)
   23585              : {
   23586         5033 :   unsigned n = dump.push (NULL);
   23587              : 
   23588         5619 :   dump () && dump ("Completed phase-4 (tokenization) processing");
   23589              : 
   23590         5033 :   name_pending_imports (reader);
   23591         5030 :   vec_free (pending_imports);
   23592              : 
   23593         5030 :   spans.maybe_init ();
   23594         5030 :   spans.close ();
   23595              : 
   23596         5030 :   using iterator = hash_table<module_state_hash>::iterator;
   23597         5030 :   if (mkdeps *deps = cpp_get_deps (reader))
   23598              :     {
   23599              :       /* Walk the module hash, informing the dependency machinery.  */
   23600           57 :       iterator end = modules_hash->end ();
   23601          228 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23602              :         {
   23603          114 :           module_state *module = *iter;
   23604              : 
   23605          114 :           if (module->is_direct ())
   23606              :             {
   23607           90 :               if (module->is_module ()
   23608           90 :                   && (module->is_interface () || module->is_partition ()))
   23609           36 :                 deps_add_module_target (deps, module->get_flatname (),
   23610           36 :                                         maybe_add_cmi_prefix (module->filename),
   23611              :                                         module->is_header (),
   23612              :                                         module->is_exported ());
   23613              :               else
   23614           54 :                 deps_add_module_dep (deps, module->get_flatname ());
   23615              :             }
   23616              :         }
   23617              :     }
   23618              : 
   23619         5030 :   if (flag_header_unit && !flag_preprocess_only)
   23620              :     {
   23621              :       /* Find the main module -- remember, it's not yet in the module
   23622              :          array.  */
   23623          921 :       iterator end = modules_hash->end ();
   23624         1896 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23625              :         {
   23626          975 :           module_state *module = *iter;
   23627          975 :           if (module->is_module ())
   23628              :             {
   23629          921 :               declare_module (module, cpp_main_loc (reader), true, NULL, reader);
   23630          921 :               module_kind |= MK_EXPORTING;
   23631          921 :               break;
   23632              :             }
   23633              :         }
   23634              :     }
   23635              : 
   23636         5030 :   dump.pop (n);
   23637         5030 : }
   23638              : 
   23639              : /* VAL is a global tree, add it to the global vec if it is
   23640              :    interesting.  Add some of its targets, if they too are
   23641              :    interesting.  We do not add identifiers, as they can be re-found
   23642              :    via the identifier hash table.  There is a cost to the number of
   23643              :    global trees.  */
   23644              : 
   23645              : static int
   23646      3118881 : maybe_add_global (tree val, unsigned &crc)
   23647              : {
   23648      3118881 :   int v = 0;
   23649              : 
   23650      3118881 :   if (val && !(identifier_p (val) || TREE_VISITED (val)))
   23651              :     {
   23652       959726 :       TREE_VISITED (val) = true;
   23653       959726 :       crc = crc32_unsigned (crc, fixed_trees->length ());
   23654       959726 :       vec_safe_push (fixed_trees, val);
   23655       959726 :       v++;
   23656              : 
   23657       959726 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
   23658       959726 :         v += maybe_add_global (TREE_TYPE (val), crc);
   23659       959726 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
   23660       629123 :         v += maybe_add_global (TYPE_NAME (val), crc);
   23661              :     }
   23662              : 
   23663      3118881 :   return v;
   23664              : }
   23665              : 
   23666              : /* Initialize module state.  Create the hash table, determine the
   23667              :    global trees.  Create the module for current TU.  */
   23668              : 
   23669              : void
   23670         5033 : init_modules (cpp_reader *reader)
   23671              : {
   23672              :   /* PCH should not be reachable because of lang-specs, but the
   23673              :      user could have overridden that.  */
   23674         5033 :   if (pch_file)
   23675            0 :     fatal_error (input_location,
   23676              :                  "C++ modules are incompatible with precompiled headers");
   23677              : 
   23678         5033 :   if (cpp_get_options (reader)->traditional)
   23679            0 :     fatal_error (input_location,
   23680              :                  "C++ modules are incompatible with traditional preprocessing");
   23681              : 
   23682              :   /* :: is always exported.  */
   23683         5033 :   DECL_MODULE_EXPORT_P (global_namespace) = true;
   23684              : 
   23685         5033 :   modules_hash = hash_table<module_state_hash>::create_ggc (31);
   23686         5033 :   vec_safe_reserve (modules, 20);
   23687              : 
   23688              :   /* Create module for current TU.  */
   23689         5033 :   module_state *current
   23690         5033 :     = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
   23691         5033 :   current->mod = 0;
   23692         5033 :   bitmap_set_bit (current->imports, 0);
   23693         5033 :   modules->quick_push (current);
   23694              : 
   23695         5033 :   gcc_checking_assert (!fixed_trees);
   23696              : 
   23697         5033 :   headers = BITMAP_GGC_ALLOC ();
   23698              : 
   23699         5033 :   if (note_includes)
   23700              :     /* Canonicalize header names.  */
   23701            2 :     for (unsigned ix = 0; ix != note_includes->length (); ix++)
   23702              :       {
   23703            1 :         const char *hdr = (*note_includes)[ix];
   23704            1 :         size_t len = strlen (hdr);
   23705              : 
   23706            1 :         bool system = hdr[0] == '<';
   23707            1 :         bool user = hdr[0] == '"';
   23708            1 :         bool delimed = system || user;
   23709              : 
   23710            1 :         if (len <= (delimed ? 2 : 0)
   23711            1 :             || (delimed && hdr[len-1] != (system ? '>' : '"')))
   23712            0 :           error ("invalid header name %qs", hdr);
   23713              : 
   23714            1 :         hdr = canonicalize_header_name (delimed ? reader : NULL,
   23715              :                                         0, !delimed, hdr, len);
   23716            1 :         char *path = XNEWVEC (char, len + 1);
   23717            1 :         memcpy (path, hdr, len);
   23718            1 :         path[len] = 0;
   23719              : 
   23720            1 :         (*note_includes)[ix] = path;
   23721              :       }
   23722              : 
   23723         5033 :   if (note_cmis)
   23724              :     /* Canonicalize & mark module names.  */
   23725           12 :     for (unsigned ix = 0; ix != note_cmis->length (); ix++)
   23726              :       {
   23727            6 :         const char *name = (*note_cmis)[ix];
   23728            6 :         size_t len = strlen (name);
   23729              : 
   23730            6 :         bool is_system = name[0] == '<';
   23731            6 :         bool is_user = name[0] == '"';
   23732            6 :         bool is_pathname = false;
   23733            6 :         if (!(is_system || is_user))
   23734           12 :           for (unsigned ix = len; !is_pathname && ix--;)
   23735            9 :             is_pathname = IS_DIR_SEPARATOR (name[ix]);
   23736            6 :         if (is_system || is_user || is_pathname)
   23737              :           {
   23738            3 :             if (len <= (is_pathname ? 0 : 2)
   23739            3 :                 || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
   23740              :               {
   23741            0 :                 error ("invalid header name %qs", name);
   23742            0 :                 continue;
   23743              :               }
   23744              :             else
   23745            3 :               name = canonicalize_header_name (is_pathname ? nullptr : reader,
   23746              :                                                0, is_pathname, name, len);
   23747              :           }
   23748            6 :         if (auto module = get_module (name))
   23749            6 :           module->inform_cmi_p = 1;
   23750              :         else
   23751            0 :           error ("invalid module name %qs", name);
   23752              :       }
   23753              : 
   23754         5033 :   dump.push (NULL);
   23755              : 
   23756              :   /* Determine lazy handle bound.  */
   23757         5033 :   {
   23758         5033 :     unsigned limit = 1000;
   23759              : #if HAVE_GETRLIMIT
   23760         5033 :     struct rlimit rlimit;
   23761         5033 :     if (!getrlimit (RLIMIT_NOFILE, &rlimit))
   23762              :       {
   23763         5033 :         lazy_hard_limit = (rlimit.rlim_max < 1000000
   23764         5033 :                            ? unsigned (rlimit.rlim_max) : 1000000);
   23765         5033 :         lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
   23766         5033 :                            ? lazy_hard_limit - LAZY_HEADROOM : 0);
   23767         5033 :         if (rlimit.rlim_cur < limit)
   23768            0 :           limit = unsigned (rlimit.rlim_cur);
   23769              :       }
   23770              : #endif
   23771         5033 :     limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
   23772              : 
   23773         5033 :     if (unsigned parm = param_lazy_modules)
   23774              :       {
   23775         5033 :         if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
   23776            6 :           lazy_limit = parm;
   23777              :       }
   23778              :     else
   23779            0 :       lazy_limit = limit;
   23780              :   }
   23781              : 
   23782         5033 :   if (dump ())
   23783              :     {
   23784          586 :       verstr_t ver;
   23785          586 :       version2string (MODULE_VERSION, ver);
   23786          586 :       dump ("Source: %s", main_input_filename);
   23787          586 :       dump ("Compiler: %s", version_string);
   23788          586 :       dump ("Modules: %s", ver);
   23789          586 :       dump ("Checking: %s",
   23790              : #if CHECKING_P
   23791              :             "checking"
   23792              : #elif ENABLE_ASSERT_CHECKING
   23793              :             "asserting"
   23794              : #else
   23795              :             "release"
   23796              : #endif
   23797              :             );
   23798          586 :       dump ("Compiled by: "
   23799              : #ifdef __GNUC__
   23800              :             "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
   23801              : #ifdef __OPTIMIZE__
   23802              :             "optimizing"
   23803              : #else
   23804              :             "not optimizing"
   23805              : #endif
   23806              : #else
   23807              :             "not GCC"
   23808              : #endif
   23809              :             );
   23810          586 :       dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
   23811          586 :       dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
   23812          586 :       dump ("Lazy limit: %u", lazy_limit);
   23813          586 :       dump ("Lazy hard limit: %u", lazy_hard_limit);
   23814          586 :       dump ("");
   23815              :     }
   23816              : 
   23817              :   /* Construct the global tree array.  This is an array of unique
   23818              :      global trees (& types).  Do this now, rather than lazily, as
   23819              :      some global trees are lazily created and we don't want that to
   23820              :      mess with our syndrome of fixed trees.  */
   23821         5033 :   unsigned crc = 0;
   23822         5033 :   vec_alloc (fixed_trees, 250);
   23823              : 
   23824         5619 :   dump () && dump ("+Creating globals");
   23825              :   /* Insert the TRANSLATION_UNIT_DECL.  */
   23826         5033 :   TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
   23827         5033 :   fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
   23828        35231 :   for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
   23829              :     {
   23830        25165 :       const tree *ptr = global_tree_arys[jx].first;
   23831        25165 :       unsigned limit = global_tree_arys[jx].second;
   23832              : 
   23833      1540098 :       for (unsigned ix = 0; ix != limit; ix++, ptr++)
   23834              :         {
   23835      1514933 :           !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
   23836      1514933 :           unsigned v = maybe_add_global (*ptr, crc);
   23837      1691319 :           dump () && dump ("+%u", v);
   23838              :         }
   23839              :     }
   23840              :   /* OS- and machine-specific types are dynamically registered at
   23841              :      runtime, so cannot be part of global_tree_arys.  */
   23842         5033 :   registered_builtin_types && dump ("") && dump ("+\tB:");
   23843        20132 :   for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
   23844              :     {
   23845        15099 :       unsigned v = maybe_add_global (TREE_VALUE (t), crc);
   23846        16857 :       dump () && dump ("+%u", v);
   23847              :     }
   23848         5033 :   global_crc = crc32_unsigned (crc, fixed_trees->length ());
   23849         5033 :   dump ("") && dump ("Created %u unique globals, crc=%x",
   23850              :                      fixed_trees->length (), global_crc);
   23851       969792 :   for (unsigned ix = fixed_trees->length (); ix--;)
   23852       964759 :     TREE_VISITED ((*fixed_trees)[ix]) = false;
   23853              : 
   23854         5033 :   dump.pop (0);
   23855              : 
   23856         5033 :   if (!flag_module_lazy)
   23857              :     /* Get the mapper now, if we're not being lazy.  */
   23858          320 :     get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23859              : 
   23860         5033 :   if (!flag_preprocess_only)
   23861              :     {
   23862         4889 :       pending_table = new pending_map_t (EXPERIMENT (1, 400));
   23863         4889 :       entity_map = new entity_map_t (EXPERIMENT (1, 400));
   23864         4889 :       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
   23865         4889 :       imported_temploid_friends
   23866         4889 :         = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
   23867              :     }
   23868              : 
   23869              : #if CHECKING_P
   23870         5033 :   note_defs = note_defs_table_t::create_ggc (1000);
   23871              : #endif
   23872              : 
   23873         5033 :   if (flag_header_unit && cpp_get_options (reader)->preprocessed)
   23874            9 :     begin_header_unit (reader);
   23875              : 
   23876              :   /* Collect here to make sure things are tagged correctly (when
   23877              :      aggressively GC'd).  */
   23878         5033 :   ggc_collect ();
   23879         5033 : }
   23880              : 
   23881              : /* If NODE is a deferred macro, load it.  */
   23882              : 
   23883              : static int
   23884        83254 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
   23885              : {
   23886        83254 :   location_t main_loc
   23887        83254 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   23888              : 
   23889        83254 :   if (cpp_user_macro_p (node)
   23890        83254 :       && !node->value.macro)
   23891              :     {
   23892           72 :       cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
   23893           72 :       dump () && dump ("Loaded macro #%s %I",
   23894              :                        macro ? "define" : "undef", identifier (node));
   23895              :     }
   23896              : 
   23897        83254 :   return 1;
   23898              : }
   23899              : 
   23900              : /* At the end of tokenizing, we no longer need the macro tables of
   23901              :    imports.  But the user might have requested some checking.  */
   23902              : 
   23903              : void
   23904        99990 : maybe_check_all_macros (cpp_reader *reader)
   23905              : {
   23906        99990 :   if (!warn_imported_macros)
   23907              :     return;
   23908              : 
   23909              :   /* Force loading of any remaining deferred macros.  This will
   23910              :      produce diagnostics if they are ill-formed.  */
   23911           21 :   unsigned n = dump.push (NULL);
   23912           21 :   cpp_forall_identifiers (reader, load_macros, NULL);
   23913           21 :   dump.pop (n);
   23914              : }
   23915              : 
   23916              : // State propagated from finish_module_processing to fini_modules
   23917              : 
   23918              : struct module_processing_cookie
   23919              : {
   23920              :   elf_out out;
   23921              :   module_state_config config;
   23922              :   char *cmi_name;
   23923              :   char *tmp_name;
   23924              :   unsigned crc;
   23925              :   bool began;
   23926              : 
   23927         2965 :   module_processing_cookie (char *cmi, char *tmp, int fd, int e)
   23928         2965 :     : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
   23929              :   {
   23930              :   }
   23931         2965 :   ~module_processing_cookie ()
   23932              :   {
   23933         2965 :     XDELETEVEC (tmp_name);
   23934         2965 :     XDELETEVEC (cmi_name);
   23935         2965 :   }
   23936              : };
   23937              : 
   23938              : /* Write the CMI, if we're a module interface.  */
   23939              : 
   23940              : void *
   23941        99779 : finish_module_processing (cpp_reader *reader)
   23942              : {
   23943        99779 :   module_processing_cookie *cookie = nullptr;
   23944              : 
   23945        99779 :   if (header_module_p ())
   23946          921 :     module_kind &= ~MK_EXPORTING;
   23947              : 
   23948        99779 :   if (!modules || !this_module ()->name)
   23949              :     {
   23950        96811 :       if (flag_module_only)
   23951            6 :         warning (0, "%<-fmodule-only%> used for non-interface");
   23952              :     }
   23953         2968 :   else if (!flag_syntax_only)
   23954              :     {
   23955         2965 :       int fd = -1;
   23956         2965 :       int e = -1;
   23957              : 
   23958         2965 :       timevar_start (TV_MODULE_EXPORT);
   23959              : 
   23960              :       /* Force a valid but empty line map at the end.  This simplifies
   23961              :          the line table preparation and writing logic.  */
   23962         2965 :       linemap_add (line_table, LC_ENTER, false, "", 0);
   23963              : 
   23964              :       /* We write to a tmpname, and then atomically rename.  */
   23965         2965 :       char *cmi_name = NULL;
   23966         2965 :       char *tmp_name = NULL;
   23967         2965 :       module_state *state = this_module ();
   23968              : 
   23969         2965 :       unsigned n = dump.push (state);
   23970         2965 :       state->announce ("creating");
   23971         2965 :       if (state->filename)
   23972              :         {
   23973         2965 :           size_t len = 0;
   23974         2965 :           cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
   23975         2965 :           tmp_name = XNEWVEC (char, len + 3);
   23976         2965 :           memcpy (tmp_name, cmi_name, len);
   23977         2965 :           strcpy (&tmp_name[len], "~");
   23978              : 
   23979         2965 :           if (!errorcount)
   23980           42 :             for (unsigned again = 2; ; again--)
   23981              :               {
   23982         2892 :                 fd = open (tmp_name,
   23983              :                            O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
   23984              :                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   23985         2892 :                 e = errno;
   23986         2892 :                 if (fd >= 0 || !again || e != ENOENT)
   23987              :                   break;
   23988           42 :                 create_dirs (tmp_name);
   23989              :               }
   23990         2965 :           if (note_module_cmi_yes || state->inform_cmi_p)
   23991            3 :             inform (state->loc, "writing CMI %qs", cmi_name);
   23992         3265 :           dump () && dump ("CMI is %s", cmi_name);
   23993              :         }
   23994              : 
   23995         2965 :       cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
   23996              : 
   23997         2965 :       if (errorcount)
   23998              :         /* Don't write the module if we have reported errors.  */;
   23999         2850 :       else if (erroneous_templates
   24000         2850 :                && !erroneous_templates->is_empty ())
   24001              :         {
   24002              :           /* Don't write the module if it contains an erroneous template.
   24003              :              Also emit notes about where errors occurred in case
   24004              :              -Wno-template-body was passed.  */
   24005            6 :           auto_diagnostic_group d;
   24006            6 :           error_at (state->loc, "not writing module %qs due to errors "
   24007              :                     "in template bodies", state->get_flatname ());
   24008            6 :           if (!warn_template_body)
   24009            3 :             inform (state->loc, "enable %<-Wtemplate-body%> for more details");
   24010           12 :           for (auto e : *erroneous_templates)
   24011            6 :             inform (e.second, "first error in %qD appeared here", e.first);
   24012            6 :         }
   24013         2844 :       else if (cookie->out.begin ())
   24014              :         {
   24015              :           /* So crashes finger-point the module decl.  */
   24016         2844 :           iloc_sentinel ils = state->loc;
   24017         2844 :           if (state->write_begin (&cookie->out, reader, cookie->config,
   24018         2844 :                                   cookie->crc))
   24019         2815 :             cookie->began = true;
   24020         2844 :         }
   24021              : 
   24022         2965 :       dump.pop (n);
   24023         2965 :       timevar_stop (TV_MODULE_EXPORT);
   24024              : 
   24025         2965 :       ggc_collect ();
   24026              :     }
   24027              : 
   24028        99779 :   if (modules)
   24029              :     {
   24030         4849 :       unsigned n = dump.push (NULL);
   24031         5435 :       dump () && dump ("Imported %u modules", modules->length () - 1);
   24032         5435 :       dump () && dump ("Containing %u clusters", available_clusters);
   24033         4849 :       dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
   24034          586 :                        (loaded_clusters * 100 + available_clusters / 2) /
   24035          586 :                        (available_clusters + !available_clusters));
   24036         4849 :       dump.pop (n);
   24037              :     }
   24038              : 
   24039        99779 :   return cookie;
   24040              : }
   24041              : 
   24042              : // Do the final emission of a module.  At this point we know whether
   24043              : // the module static initializer is a NOP or not.
   24044              : 
   24045              : static void
   24046         2965 : late_finish_module (cpp_reader *reader,  module_processing_cookie *cookie,
   24047              :                     bool init_fn_non_empty)
   24048              : {
   24049         2965 :   timevar_start (TV_MODULE_EXPORT);
   24050              : 
   24051         2965 :   module_state *state = this_module ();
   24052         2965 :   unsigned n = dump.push (state);
   24053         2965 :   state->announce ("finishing");
   24054              : 
   24055         2965 :   cookie->config.active_init = init_fn_non_empty;
   24056         2965 :   if (cookie->began)
   24057         2815 :     state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
   24058              : 
   24059         2965 :   if (cookie->out.end () && cookie->cmi_name)
   24060              :     {
   24061              :       /* Some OS's do not replace NEWNAME if it already exists.
   24062              :          This'll have a race condition in erroneous concurrent
   24063              :          builds.  */
   24064         2850 :       unlink (cookie->cmi_name);
   24065         2850 :       if (rename (cookie->tmp_name, cookie->cmi_name))
   24066              :         {
   24067            0 :           dump () && dump ("Rename ('%s','%s') errno=%u",
   24068            0 :                            cookie->tmp_name, cookie->cmi_name, errno);
   24069            0 :           cookie->out.set_error (errno);
   24070              :         }
   24071              :     }
   24072              : 
   24073         2965 :   if (cookie->out.get_error () && cookie->began)
   24074              :     {
   24075            0 :       error_at (state->loc, "failed to write compiled module: %s",
   24076            0 :                 cookie->out.get_error (state->filename));
   24077            0 :       state->note_cmi_name ();
   24078              :     }
   24079              : 
   24080         2965 :   if (!errorcount)
   24081              :     {
   24082         2809 :       auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   24083         2809 :       mapper->ModuleCompiled (state->get_flatname ());
   24084              :     }
   24085          156 :   else if (cookie->cmi_name)
   24086              :     {
   24087              :       /* We failed, attempt to erase all evidence we even tried.  */
   24088          156 :       unlink (cookie->tmp_name);
   24089          156 :       unlink (cookie->cmi_name);
   24090              :     }
   24091              : 
   24092         2965 :   delete cookie;
   24093         2965 :   dump.pop (n);
   24094         2965 :   timevar_stop (TV_MODULE_EXPORT);
   24095         2965 : }
   24096              : 
   24097              : void
   24098        99779 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
   24099              : {
   24100        99779 :   if (cookie)
   24101         2965 :     late_finish_module (reader,
   24102              :                         static_cast<module_processing_cookie *> (cookie),
   24103              :                         has_inits);
   24104              : 
   24105              :   /* We're done with the macro tables now.  */
   24106        99779 :   vec_free (macro_exports);
   24107        99779 :   vec_free (macro_imports);
   24108        99779 :   headers = NULL;
   24109              : 
   24110              :   /* We're now done with everything but the module names.  */
   24111        99779 :   set_cmi_repo (NULL);
   24112        99779 :   if (mapper)
   24113              :     {
   24114         4849 :       timevar_start (TV_MODULE_MAPPER);
   24115         4849 :       module_client::close_module_client (0, mapper);
   24116         4849 :       mapper = nullptr;
   24117         4849 :       timevar_stop (TV_MODULE_MAPPER);
   24118              :     }
   24119        99779 :   module_state_config::release ();
   24120              : 
   24121              : #if CHECKING_P
   24122        99779 :   note_defs = NULL;
   24123              : #endif
   24124              : 
   24125        99779 :   if (modules)
   24126         7852 :     for (unsigned ix = modules->length (); --ix;)
   24127         3003 :       if (module_state *state = (*modules)[ix])
   24128         3003 :         state->release ();
   24129              : 
   24130              :   /* No need to lookup modules anymore.  */
   24131        99779 :   modules_hash = NULL;
   24132              : 
   24133              :   /* Or entity array.  We still need the entity map to find import numbers.  */
   24134        99779 :   vec_free (entity_ary);
   24135        99779 :   entity_ary = NULL;
   24136              : 
   24137              :   /* Or remember any pending entities.  */
   24138       104628 :   delete pending_table;
   24139        99779 :   pending_table = NULL;
   24140              : 
   24141              :   /* Or any keys -- Let it go!  */
   24142       102305 :   delete keyed_table;
   24143        99779 :   keyed_table = NULL;
   24144              : 
   24145              :   /* Allow a GC, we've possibly made much data unreachable.  */
   24146        99779 :   ggc_collect ();
   24147        99779 : }
   24148              : 
   24149              : /* If CODE is a module option, handle it & return true.  Otherwise
   24150              :    return false.  For unknown reasons I cannot get the option
   24151              :    generation machinery to set fmodule-mapper or -fmodule-header to
   24152              :    make a string type option variable.  */
   24153              : 
   24154              : bool
   24155      1979890 : handle_module_option (unsigned code, const char *str, int)
   24156              : {
   24157      1979890 :   auto hdr = CMS_header;
   24158              : 
   24159      1979890 :   switch (opt_code (code))
   24160              :     {
   24161           48 :     case OPT_fmodule_mapper_:
   24162           48 :       module_mapper_name = str;
   24163           48 :       return true;
   24164              : 
   24165           12 :     case OPT_fmodule_header_:
   24166           12 :       {
   24167           12 :         if (!strcmp (str, "user"))
   24168              :           hdr = CMS_user;
   24169           12 :         else if (!strcmp (str, "system"))
   24170              :           hdr = CMS_system;
   24171              :         else
   24172            0 :           error ("unknown header kind %qs", str);
   24173              :       }
   24174              :       /* Fallthrough.  */
   24175              : 
   24176          936 :     case OPT_fmodule_header:
   24177          936 :       flag_header_unit = hdr;
   24178          936 :       flag_modules = 1;
   24179          936 :       return true;
   24180              : 
   24181            1 :     case OPT_flang_info_include_translate_:
   24182            1 :       vec_safe_push (note_includes, str);
   24183            1 :       return true;
   24184              : 
   24185            6 :     case OPT_flang_info_module_cmi_:
   24186            6 :       vec_safe_push (note_cmis, str);
   24187            6 :       return true;
   24188              : 
   24189              :     default:
   24190              :       return false;
   24191              :     }
   24192              : }
   24193              : 
   24194              : /* Set preprocessor callbacks and options for modules.  */
   24195              : 
   24196              : void
   24197       101580 : module_preprocess_options (cpp_reader *reader)
   24198              : {
   24199       101580 :   gcc_checking_assert (!lang_hooks.preprocess_undef);
   24200       101580 :   if (modules_p ())
   24201              :     {
   24202         5033 :       auto *cb = cpp_get_callbacks (reader);
   24203              : 
   24204         5033 :       cb->translate_include = maybe_translate_include;
   24205         5033 :       cb->user_deferred_macro = module_state::deferred_macro;
   24206         5033 :       if (flag_header_unit)
   24207              :         {
   24208              :           /* If the preprocessor hook is already in use, that
   24209              :              implementation will call the undef langhook.  */
   24210          933 :           if (cb->undef)
   24211            0 :             lang_hooks.preprocess_undef = module_state::undef_macro;
   24212              :           else
   24213          933 :             cb->undef = module_state::undef_macro;
   24214              :         }
   24215         5033 :       auto *opt = cpp_get_options (reader);
   24216         5033 :       opt->module_directives = true;
   24217         5033 :       if (flag_no_output)
   24218           18 :         opt->directives_only = true;
   24219         5033 :       if (opt->main_search == CMS_none)
   24220         5027 :         opt->main_search = cpp_main_search (flag_header_unit);
   24221              :     }
   24222       101580 : }
   24223              : 
   24224              : #include "gt-cp-module.h"
        

Generated by: LCOV version 2.5.0-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.