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-11-14 16:39:50 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       255454 : static inline cpp_hashnode *cpp_node (tree id)
     285              : {
     286       255454 :   return CPP_HASHNODE (GCC_IDENT_TO_HT_IDENT (id));
     287              : }
     288              : 
     289       157750 : 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       157750 : #pragma GCC diagnostic push
     296       157750 : #pragma GCC diagnostic ignored "-Warray-bounds"
     297       157750 :   return HT_IDENT_TO_GCC_IDENT (HT_NODE (const_cast<cpp_hashnode *> (node)));
     298       157750 : #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       104026 :     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       957452 :   data ()
     388       957452 :     :buffer (NULL), size (0), pos (0)
     389              :   {
     390              :   }
     391       972227 :   ~data ()
     392              :   {
     393              :     /* Make sure the derived and/or using class know what they're
     394              :        doing.  */
     395       972227 :     gcc_checking_assert (!buffer);
     396       972227 :   }
     397              : 
     398              : protected:
     399    709890948 :   char *use (unsigned count)
     400              :   {
     401    709890948 :     if (size < pos + count)
     402              :       return NULL;
     403    709890948 :     char *res = &buffer[pos];
     404    709890948 :     pos += count;
     405    419801890 :     return res;
     406              :   }
     407              : 
     408              :   unsigned calc_crc (unsigned) const;
     409              : 
     410              : public:
     411     55506174 :   void unuse (unsigned count)
     412              :   {
     413     55506174 :     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       895864 : data::allocator::grow (data &obj, unsigned needed, bool exact)
     428              : {
     429       895864 :   gcc_checking_assert (needed ? needed > obj.size : !obj.size);
     430       895864 :   if (!needed)
     431              :     /* Pick a default size.  */
     432       368442 :     needed = EXPERIMENT (100, 1000);
     433              : 
     434       895864 :   if (!exact)
     435       887332 :     needed *= 2;
     436       895864 :   obj.buffer = grow (obj.buffer, needed);
     437       895864 :   if (obj.buffer)
     438       895864 :     obj.size = needed;
     439              :   else
     440            0 :     obj.pos = obj.size = 0;
     441       895864 : }
     442              : 
     443              : /* Free a buffer.  */
     444              : 
     445              : void
     446       383267 : data::allocator::shrink (data &obj)
     447              : {
     448            0 :   shrink (obj.buffer);
     449       383267 :   obj.buffer = NULL;
     450       383267 :   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       597591 : data::calc_crc (unsigned l) const
     470              : {
     471       597591 :   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       239618 :   bytes_in ()
     487       239618 :     : parent (), overrun (false)
     488              :   {
     489              :   }
     490       242539 :   ~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      1788973 :   bool more_p () const
     504              :   {
     505      1788973 :     return pos != size;
     506              :   }
     507              : 
     508              : public:
     509              :   /* Start reading at OFFSET.  */
     510          467 :   void random_access (unsigned offset)
     511              :   {
     512          467 :     if (offset > size)
     513            0 :       set_overrun ();
     514          467 :     pos = offset;
     515              :   }
     516              : 
     517              : public:
     518      1617183 :   void align (unsigned boundary)
     519              :   {
     520      1617183 :     if (unsigned pad = pos & (boundary - 1))
     521      3130895 :       read (boundary - pad);
     522              :   }
     523              : 
     524              : public:
     525    290089058 :   const char *read (unsigned count)
     526              :   {
     527      1513712 :     char *ptr = use (count);
     528    290089058 :     if (!ptr)
     529            0 :       set_overrun ();
     530    236080514 :     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       240757 :   unsigned get_crc () const
     537              :   {
     538       240757 :     return *(const unsigned *)&buffer[0];
     539              :   }
     540              : 
     541              : public:
     542              :   /* Manipulate the overrun flag.  */
     543    168579112 :   bool get_overrun () const
     544              :   {
     545    168579112 :     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       237652 : bytes_in::check_crc () const
     576              : {
     577       237652 :   if (size < 4)
     578              :     return false;
     579              : 
     580       237652 :   unsigned c_crc = calc_crc (size);
     581       237652 :   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       711784 :   bytes_out (allocator *memory)
     600       711784 :     : parent (), memory (memory)
     601              :   {
     602              :   }
     603       711784 :   ~bytes_out ()
     604              :   {
     605       742498 :   }
     606              : 
     607              : public:
     608   1069766739 :   bool streaming_p () const
     609              :   {
     610   1069766739 :     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      2445649 :   void align (unsigned boundary)
     624              :   {
     625      2445649 :     if (unsigned pad = pos & (boundary - 1))
     626      2287727 :       write (boundary - pad);
     627      2445649 :   }
     628              : 
     629              : public:
     630    419801890 :   char *write (unsigned count, bool exact = false)
     631              :   {
     632    419801890 :     if (size < pos + count)
     633       512601 :       memory->grow (*this, pos + count, exact);
     634    419801890 :     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       312214 :   void cpp_node (const cpp_hashnode *node)
     653              :   {
     654       312214 :     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     55449085 : bit_flush (data& bits, uint32_t& bit_val, unsigned& bit_pos)
     683              : {
     684     55449085 :   gcc_assert (bit_pos);
     685     55449085 :   unsigned bytes = (bit_pos + 7) / 8;
     686     55449085 :   bits.unuse (4 - bytes);
     687     55449085 :   bit_pos = 0;
     688     55449085 :   bit_val = 0;
     689     55449085 :   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     16590897 :   bits_in (bytes_in& in)
     712     16590897 :     : in (in)
     713              :   { }
     714              : 
     715     16590897 :   ~bits_in ()
     716              :   {
     717     15583910 :     bflush ();
     718     16590897 :   }
     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     34354744 :   void bflush ()
     726              :   {
     727     34354744 :     if (bit_pos)
     728     18770834 :       bit_flush (in, bit_val, bit_pos);
     729     34354744 :   }
     730              : 
     731              :   /* Read one bit.  */
     732    568091839 :   bool b ()
     733              :   {
     734    568091839 :     if (!bit_pos)
     735     24837878 :       bit_val = in.u32 ();
     736    568091839 :     bool x = (bit_val >> bit_pos) & 1;
     737    568091839 :     bit_pos = (bit_pos + 1) % 32;
     738    568091839 :     return x;
     739              :   }
     740              : };
     741              : 
     742              : /* Factory function for bits_in.  */
     743              : 
     744              : bytes_in::bits_in
     745     16590897 : bytes_in::stream_bits ()
     746              : {
     747     16590897 :   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     24452656 :   bits_out (bytes_out& out)
     759     24452656 :     : out (out)
     760              :   { }
     761              : 
     762     24452656 :   ~bits_out ()
     763              :   {
     764        78832 :     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     50576189 :   void bflush ()
     773              :   {
     774     50576189 :     if (bit_pos)
     775              :       {
     776     27636243 :         out.u32 (bit_val);
     777     27636243 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     778              :       }
     779     50576189 :     out.spans[2]++;
     780     50576189 :     is_set = -1;
     781     50576189 :   }
     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    836080517 :   void b (bool x)
     788              :   {
     789    836080517 :     if (is_set != x)
     790              :       {
     791     87018501 :         is_set = x;
     792     87018501 :         out.spans[x]++;
     793              :       }
     794    836080517 :     out.lengths[x]++;
     795    836080517 :     bit_val |= unsigned (x) << bit_pos++;
     796    836080517 :     if (bit_pos == 32)
     797              :       {
     798      9042008 :         out.u32 (bit_val);
     799      9042008 :         out.lengths[2] += bit_flush (out, bit_val, bit_pos);
     800              :       }
     801    836080517 :   }
     802              : };
     803              : 
     804              : /* Factory function for bits_out.  */
     805              : 
     806              : bytes_out::bits_out
     807     24452656 : bytes_out::stream_bits ()
     808              : {
     809     24452656 :   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       362754 : bytes_out::set_crc (unsigned *crc_ptr)
     821              : {
     822       362754 :   if (crc_ptr)
     823              :     {
     824       359939 :       gcc_checking_assert (pos >= 4);
     825              : 
     826       359939 :       unsigned crc = calc_crc (pos);
     827       359939 :       unsigned accum = *crc_ptr;
     828              :       /* Only mix the existing *CRC_PTR if it is non-zero.  */
     829       359939 :       accum = accum ? crc32_combine (accum, crc, pos - 4) : crc;
     830       359939 :       *crc_ptr = accum;
     831              : 
     832              :       /* Buffer will be sufficiently aligned.  */
     833       359939 :       *(unsigned *)buffer = crc;
     834              :     }
     835       362754 : }
     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     36687374 : bytes_out::u32 (unsigned val)
     844              : {
     845     36687374 :   if (char *ptr = write (4))
     846              :     {
     847     36687374 :       ptr[0] = val;
     848     36687374 :       ptr[1] = val >> 8;
     849     36687374 :       ptr[2] = val >> 16;
     850     36687374 :       ptr[3] = val >> 24;
     851              :     }
     852     36687374 : }
     853              : 
     854              : unsigned
     855     24847628 : bytes_in::u32 ()
     856              : {
     857     24847628 :   unsigned val = 0;
     858     24847628 :   if (const char *ptr = read (4))
     859              :     {
     860     24847628 :       val |= (unsigned char)ptr[0];
     861     24847628 :       val |= (unsigned char)ptr[1] << 8;
     862     24847628 :       val |= (unsigned char)ptr[2] << 16;
     863     24847628 :       val |= (unsigned char)ptr[3] << 24;
     864              :     }
     865              : 
     866     24847628 :   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    147801738 : bytes_out::i (int v)
     892              : {
     893    147801738 :   if (char *ptr = write (1))
     894              :     {
     895    147801738 :       if (v <= 0x3f && v >= -0x40)
     896    114855903 :         *ptr = v & 0x7f;
     897              :       else
     898              :         {
     899     32945835 :           unsigned bytes = 0;
     900     32945835 :           int probe;
     901     32945835 :           if (v >= 0)
     902            0 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     903            0 :               bytes++;
     904              :           else
     905     49956495 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
     906     17010660 :               bytes++;
     907     32945835 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
     908     32945835 :           if ((ptr = write (++bytes)))
     909     82902330 :             for (; bytes--; v >>= 8)
     910     49956495 :               ptr[bytes] = v & 0xff;
     911              :         }
     912              :     }
     913    147801738 : }
     914              : 
     915              : int
     916    101750829 : bytes_in::i ()
     917              : {
     918    101750829 :   int v = 0;
     919    101750829 :   if (const char *ptr = read (1))
     920              :     {
     921    101750829 :       v = *ptr & 0xff;
     922    101750829 :       if (v & 0x80)
     923              :         {
     924     22913128 :           unsigned bytes = (v >> 4) & 0x7;
     925     22913128 :           v &= 0xf;
     926     22913128 :           if (v & 0x8)
     927     22913128 :             v |= -1 ^ 0x7;
     928              :           /* unsigned necessary due to left shifts of -ve values.  */
     929     22913128 :           unsigned uv = unsigned (v);
     930     22913128 :           if ((ptr = read (++bytes)))
     931     58690751 :             while (bytes--)
     932     35777623 :               uv = (uv << 8) | (*ptr++ & 0xff);
     933     22913128 :           v = int (uv);
     934              :         }
     935     78837701 :       else if (v & 0x40)
     936     10176170 :         v |= -1 ^ 0x3f;
     937              :     }
     938              : 
     939    101750829 :   return v;
     940              : }
     941              : 
     942              : void
     943    126393944 : bytes_out::u (unsigned v)
     944              : {
     945    126393944 :   if (char *ptr = write (1))
     946              :     {
     947    126393944 :       if (v <= 0x7f)
     948    109849501 :         *ptr = v;
     949              :       else
     950              :         {
     951     16544443 :           unsigned bytes = 0;
     952     16544443 :           unsigned probe;
     953     19724913 :           for (probe = v >> 8; probe > 0xf; probe >>= 8)
     954      3180470 :             bytes++;
     955     16544443 :           *ptr = 0x80 | bytes << 4 | probe;
     956     16544443 :           if ((ptr = write (++bytes)))
     957     36269356 :             for (; bytes--; v >>= 8)
     958     19724913 :               ptr[bytes] = v & 0xff;
     959              :         }
     960              :     }
     961    126393944 : }
     962              : 
     963              : unsigned
     964     87985562 : bytes_in::u ()
     965              : {
     966     87985562 :   unsigned v = 0;
     967              : 
     968     87985562 :   if (const char *ptr = read (1))
     969              :     {
     970     87985562 :       v = *ptr & 0xff;
     971     87985562 :       if (v & 0x80)
     972              :         {
     973     11653427 :           unsigned bytes = (v >> 4) & 0x7;
     974     11653427 :           v &= 0xf;
     975     11653427 :           if ((ptr = read (++bytes)))
     976     25564449 :             while (bytes--)
     977     13911022 :               v = (v << 8) | (*ptr++ & 0xff);
     978              :         }
     979              :     }
     980              : 
     981     87985562 :   return v;
     982              : }
     983              : 
     984              : void
     985     30197022 : bytes_out::wi (HOST_WIDE_INT v)
     986              : {
     987     30197022 :   if (char *ptr = write (1))
     988              :     {
     989     30197022 :       if (v <= 0x3f && v >= -0x40)
     990      5755953 :         *ptr = v & 0x7f;
     991              :       else
     992              :         {
     993     24441069 :           unsigned bytes = 0;
     994     24441069 :           HOST_WIDE_INT probe;
     995     24441069 :           if (v >= 0)
     996     89760257 :             for (probe = v >> 8; probe > 0x7; probe >>= 8)
     997     65322687 :               bytes++;
     998              :           else
     999        11068 :             for (probe = v >> 8; probe < -0x8; probe >>= 8)
    1000         7569 :               bytes++;
    1001     24441069 :           *ptr = 0x80 | bytes << 4 | (probe & 0xf);
    1002     24441069 :           if ((ptr = write (++bytes)))
    1003    114212394 :             for (; bytes--; v >>= 8)
    1004     89771325 :               ptr[bytes] = v & 0xff;
    1005              :         }
    1006              :     }
    1007     30197022 : }
    1008              : 
    1009              : HOST_WIDE_INT
    1010     19879312 : bytes_in::wi ()
    1011              : {
    1012     19879312 :   HOST_WIDE_INT v = 0;
    1013     19879312 :   if (const char *ptr = read (1))
    1014              :     {
    1015     19879312 :       v = *ptr & 0xff;
    1016     19879312 :       if (v & 0x80)
    1017              :         {
    1018     17928277 :           unsigned bytes = (v >> 4) & 0x7;
    1019     17928277 :           v &= 0xf;
    1020     17928277 :           if (v & 0x8)
    1021         2154 :             v |= -1 ^ 0x7;
    1022              :           /* unsigned necessary due to left shifts of -ve values.  */
    1023     17928277 :           unsigned HOST_WIDE_INT uv = (unsigned HOST_WIDE_INT) v;
    1024     17928277 :           if ((ptr = read (++bytes)))
    1025     83078957 :             while (bytes--)
    1026     65150680 :               uv = (uv << 8) | (*ptr++ & 0xff);
    1027     17928277 :           v = (HOST_WIDE_INT) uv;
    1028              :         }
    1029      1951035 :       else if (v & 0x40)
    1030         9478 :         v |= -1 ^ 0x3f;
    1031              :     }
    1032              : 
    1033     19879312 :   return v;
    1034              : }
    1035              : 
    1036              : /* unsigned wide ints are just written as signed wide ints.  */
    1037              : 
    1038              : inline void
    1039     30196050 : bytes_out::wu (unsigned HOST_WIDE_INT v)
    1040              : {
    1041     30196050 :   wi ((HOST_WIDE_INT) v);
    1042              : }
    1043              : 
    1044              : inline unsigned HOST_WIDE_INT
    1045     19878714 : bytes_in::wu ()
    1046              : {
    1047     39146392 :   return (unsigned HOST_WIDE_INT) wi ();
    1048              : }
    1049              : 
    1050              : /* size_t written as unsigned or unsigned wide int.  */
    1051              : 
    1052              : inline void
    1053      2398956 : bytes_out::z (size_t s)
    1054              : {
    1055      2398956 :   if (sizeof (s) == sizeof (unsigned))
    1056              :     u (s);
    1057              :   else
    1058      4764937 :     wu (s);
    1059              : }
    1060              : 
    1061              : inline size_t
    1062      1598849 : bytes_in::z ()
    1063              : {
    1064      1598849 :   if (sizeof (size_t) == sizeof (unsigned))
    1065              :     return u ();
    1066              :   else
    1067      3197698 :     return wu ();
    1068              : }
    1069              : 
    1070              : /* location_t written as 32- or 64-bit as needed.  */
    1071              : 
    1072     26889581 : inline void bytes_out::loc (location_t l)
    1073              : {
    1074     26889581 :   if (sizeof (location_t) > sizeof (unsigned))
    1075     50954137 :     wu (l);
    1076              :   else
    1077              :     u (l);
    1078              : }
    1079              : 
    1080     17671891 : inline location_t bytes_in::loc ()
    1081              : {
    1082     17671891 :   if (sizeof (location_t) > sizeof (unsigned))
    1083     35340690 :     return wu ();
    1084              :   else
    1085              :     return u ();
    1086              : }
    1087              : 
    1088              : /* Buffer simply memcpied.  */
    1089              : void *
    1090      2445649 : bytes_out::buf (size_t len)
    1091              : {
    1092      2445649 :   align (sizeof (void *) * 2);
    1093      2445649 :   return write (len);
    1094              : }
    1095              : 
    1096              : void
    1097      2392823 : bytes_out::buf (const void *src, size_t len)
    1098              : {
    1099      2392823 :   if (void *ptr = buf (len))
    1100      2392823 :     memcpy (ptr, src, len);
    1101      2392823 : }
    1102              : 
    1103              : const void *
    1104      1617183 : bytes_in::buf (size_t len)
    1105              : {
    1106      1617183 :   align (sizeof (void *) * 2);
    1107      1617183 :   const char *ptr = read (len);
    1108              : 
    1109      1617183 :   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      2398938 : bytes_out::str (const char *string, size_t len)
    1117              : {
    1118      2365975 :   z (len);
    1119      2365975 :   if (len)
    1120              :     {
    1121      2365975 :       gcc_checking_assert (!string[len]);
    1122      2365975 :       buf (string, len + 1);
    1123              :     }
    1124      2365975 : }
    1125              : 
    1126              : const char *
    1127      1598843 : bytes_in::str (size_t *len_p)
    1128              : {
    1129      1598843 :   size_t len = z ();
    1130              : 
    1131              :   /* We're about to trust some user data.  */
    1132      1598843 :   if (overrun)
    1133            0 :     len = 0;
    1134      1598843 :   if (len_p)
    1135      1594840 :     *len_p = len;
    1136      1598843 :   const char *str = NULL;
    1137      1598843 :   if (len)
    1138              :     {
    1139      1598624 :       str = reinterpret_cast<const char *> (buf (len + 1));
    1140      1598624 :       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       255673 : bytes_in::cpp_node ()
    1151              : {
    1152       255673 :   size_t len;
    1153       255673 :   const char *s = str (&len);
    1154       255673 :   if (!len)
    1155              :     return NULL;
    1156       255454 :   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       464651 :   int get_error () const
    1309              :   {
    1310       464651 :     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       218802 :   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       243774 :   static void grow (data &data, unsigned needed)
    1428              :   {
    1429       243774 :     gcc_checking_assert (!data.buffer);
    1430              : #if !MAPPED_READING
    1431              :     data.buffer = XNEWVEC (char, needed);
    1432              : #endif
    1433       243774 :     data.size = needed;
    1434       243774 :   }
    1435       250543 :   static void shrink (data &data)
    1436              :   {
    1437              : #if !MAPPED_READING
    1438              :     XDELETEVEC (data.buffer);
    1439              : #endif
    1440       250543 :     data.buffer = NULL;
    1441       250543 :     data.size = 0;
    1442              :   }
    1443              : 
    1444              : public:
    1445       240713 :   const section *get_section (unsigned s) const
    1446              :   {
    1447       240713 :     if (s * sizeof (section) < sectab.size)
    1448       240713 :       return reinterpret_cast<const section *>
    1449       240713 :         (&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       240713 :   bool read (data *d, const section *s)
    1464              :   {
    1465       240713 :     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       325013 :   const char *name (unsigned offset)
    1499              :   {
    1500       650026 :     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       237652 : bytes_in::begin (location_t loc, elf_in *source, unsigned snum, const char *name)
    1605              : {
    1606       237652 :   if (!source->read (this, source->find (snum))
    1607       237652 :       || !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       237652 :   pos = 4;
    1618       237652 :   return true;
    1619              : }
    1620              : 
    1621              : /* Finish reading a section.  */
    1622              : 
    1623              : bool
    1624       236533 : bytes_in::end (elf_in *src)
    1625              : {
    1626       236533 :   if (more_p ())
    1627           13 :     set_overrun ();
    1628       236533 :   if (overrun)
    1629           13 :     src->set_error ();
    1630              : 
    1631       236533 :   src->shrink (*this);
    1632              : 
    1633       236533 :   return !overrun;
    1634              : }
    1635              : 
    1636              : /* Begin writing buffer.  */
    1637              : 
    1638              : void
    1639       362754 : 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       362754 : bytes_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    1652              : {
    1653       362754 :   lengths[3] += pos;
    1654       362754 :   spans[3]++;
    1655              : 
    1656       362754 :   set_crc (crc_ptr);
    1657       362754 :   unsigned sec_num = sink->add (*this, !crc_ptr, name);
    1658       362754 :   memory->shrink (*this);
    1659              : 
    1660       362754 :   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       243774 : elf_in::read (data *data, unsigned pos, unsigned length)
    1726              : {
    1727              : #if MAPPED_READING
    1728       243774 :   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       243774 :   grow (*data, length);
    1741              : #if MAPPED_READING
    1742       243774 :   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       243774 :   return data->buffer;
    1753              : }
    1754              : 
    1755              : /* Read section SNUM of TYPE.  Return section pointer or NULL on error.  */
    1756              : 
    1757              : const elf::section *
    1758       240713 : elf_in::find (unsigned snum, unsigned type)
    1759              : {
    1760       240713 :   const section *sec = get_section (snum);
    1761       240713 :   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       883887 : elf_out::grow (char *data, unsigned needed)
    1979              : {
    1980       883887 :   if (!data)
    1981              :     {
    1982              :       /* First allocation, check we're aligned.  */
    1983       368442 :       gcc_checking_assert (!(pos & (SECTION_ALIGN - 1)));
    1984              : #if MAPPED_WRITING
    1985       368442 :       data = hdr.buffer + (pos - offset);
    1986              : #endif
    1987              :     }
    1988              : 
    1989              : #if MAPPED_WRITING
    1990       883887 :   unsigned off = data - hdr.buffer;
    1991       883887 :   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       883887 :   return data;
    2011              : }
    2012              : 
    2013              : #if MAPPED_WRITING
    2014              : /* Shrinking is a NOP.  */
    2015              : void
    2016       368442 : elf_out::shrink (char *)
    2017              : {
    2018       368442 : }
    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      1661027 : elf_out::strtab_write (const char *s, unsigned l)
    2026              : {
    2027      1661027 :   if (strtab.pos + l > strtab.size)
    2028         1333 :     data::simple_memory.grow (strtab, strtab.pos + l, false);
    2029      1661027 :   memcpy (strtab.buffer + strtab.pos, s, l);
    2030      1661027 :   unsigned res = strtab.pos;
    2031      1661027 :   strtab.pos += l;
    2032      1661027 :   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       668777 : elf_out::strtab_write (tree decl, int inner)
    2040              : {
    2041       668777 :   tree ctx = CP_DECL_CONTEXT (decl);
    2042       668777 :   if (TYPE_P (ctx))
    2043         8126 :     ctx = TYPE_NAME (ctx);
    2044       668777 :   if (ctx != global_namespace)
    2045       345181 :     strtab_write (ctx, -1);
    2046              : 
    2047       668777 :   tree name = DECL_NAME (decl);
    2048       668777 :   if (!name)
    2049          339 :     name = DECL_ASSEMBLER_NAME_RAW (decl);
    2050       668777 :   strtab_write (IDENTIFIER_POINTER (name), IDENTIFIER_LENGTH (name));
    2051              : 
    2052       668777 :   if (inner)
    2053       484764 :     strtab_write (&"::{}"[inner+1], 2);
    2054       668777 : }
    2055              : 
    2056              : /* Map IDENTIFIER IDENT to strtab offset.  Inserts into strtab if not
    2057              :    already there.  */
    2058              : 
    2059              : unsigned
    2060       166560 : elf_out::name (tree ident)
    2061              : {
    2062       166560 :   unsigned res = 0;
    2063       166560 :   if (ident)
    2064              :     {
    2065       166505 :       bool existed;
    2066       166505 :       int *slot = &identtab.get_or_insert (ident, &existed);
    2067       166505 :       if (!existed)
    2068       295002 :         *slot = strtab_write (IDENTIFIER_POINTER (ident),
    2069       147501 :                               IDENTIFIER_LENGTH (ident) + 1);
    2070       166505 :       res = *slot;
    2071              :     }
    2072       166560 :   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       323596 : elf_out::qualified_name (tree decl, bool is_defn)
    2089              : {
    2090       323596 :   gcc_checking_assert (DECL_P (decl) && decl != global_namespace);
    2091       323596 :   unsigned result = strtab.pos;
    2092              : 
    2093       323596 :   strtab_write (decl, is_defn);
    2094       323596 :   strtab_write ("", 1);
    2095              : 
    2096       323596 :   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       368442 : elf_out::add (unsigned type, unsigned name, unsigned off, unsigned size,
    2105              :               unsigned flags)
    2106              : {
    2107       368442 :   gcc_checking_assert (!(off & (SECTION_ALIGN - 1)));
    2108       368442 :   if (sectab.pos + sizeof (section) > sectab.size)
    2109         4956 :     data::simple_memory.grow (sectab, sectab.pos + sizeof (section), false);
    2110       368442 :   section *sec = reinterpret_cast<section *> (sectab.buffer + sectab.pos);
    2111       368442 :   memset (sec, 0, sizeof (section));
    2112       368442 :   sec->type = type;
    2113       368442 :   sec->flags = flags;
    2114       368442 :   sec->name = name;
    2115       368442 :   sec->offset = off;
    2116       368442 :   sec->size = size;
    2117       368442 :   if (flags & SHF_STRINGS)
    2118         5659 :     sec->entsize = 1;
    2119              : 
    2120       368442 :   unsigned res = sectab.pos;
    2121       368442 :   sectab.pos += sizeof (section);
    2122       368442 :   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         9691 :       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       362754 : elf_out::write (const bytes_out &buf)
    2173              : {
    2174       362754 :   gcc_checking_assert (buf.memory == this);
    2175              :   /* A directly mapped buffer.  */
    2176       362754 :   gcc_checking_assert (buf.buffer - hdr.buffer >= 0
    2177              :                        && buf.buffer - hdr.buffer + buf.size <= extent);
    2178       362754 :   unsigned res = pos;
    2179       362754 :   pos += buf.pos;
    2180              : 
    2181              :   /* Align up.  We're not going to advance into the next page. */
    2182       362754 :   pos += -pos & (SECTION_ALIGN - 1);
    2183              : 
    2184       362754 :   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       362754 : elf_out::add (const bytes_out &data, bool string_p, unsigned name)
    2195              : {
    2196       362754 :   unsigned off = write (data);
    2197              : 
    2198       725508 :   return add (string_p ? SHT_STRTAB : SHT_PROGBITS, name,
    2199       362754 :               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      6642852 :   template<unsigned I> void set_flag_bit ()
    2426              :   {
    2427            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2428      6642852 :     discriminator |= 1u << I;
    2429      4214631 :   }
    2430      7112094 :   template<unsigned I> void clear_flag_bit ()
    2431              :   {
    2432            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2433      7112094 :     discriminator &= ~(1u << I);
    2434      7112094 :   }
    2435    668572638 :   template<unsigned I> bool get_flag_bit () const
    2436              :   {
    2437            0 :     gcc_checking_assert (I < 2 || !is_binding ());
    2438    814183245 :     return bool ((discriminator >> I) & 1);
    2439              :   }
    2440              : 
    2441              : public:
    2442    655671831 :   bool is_binding () const
    2443              :   {
    2444    156937332 :     return !get_flag_bit<DB_ZERO_BIT> ();
    2445              :   }
    2446    343881752 :   entity_kind get_entity_kind () const
    2447              :   {
    2448        41631 :     if (is_binding ())
    2449              :       return EK_BINDING;
    2450    260691466 :     return entity_kind ((discriminator >> DB_KIND_BIT) & ((1u << EK_BITS) - 1));
    2451              :   }
    2452              :   const char *entity_kind_name () const;
    2453              : 
    2454              : public:
    2455     10900281 :   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     10900281 :     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      2708585 :   bool is_pending_entity () const
    2466              :   {
    2467      4291392 :     return (get_entity_kind () == EK_SPECIALIZATION
    2468      1582807 :             || get_entity_kind () == EK_PARTIAL
    2469      4248914 :             || (get_entity_kind () == EK_DECL
    2470      1493445 :                 && 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     58569901 :   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     26027499 :     tree inner = STRIP_TEMPLATE (get_entity ());
    2487     58569901 :     return (get_flag_bit<DB_TU_LOCAL_BIT> ()
    2488     58569901 :             && (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      1572636 :   bool refs_tu_local (bool strict = false) const
    2495              :   {
    2496       546007 :     return (get_flag_bit<DB_REF_PURVIEW_BIT> ()
    2497       546007 :             || (strict && get_flag_bit <DB_REF_GLOBAL_BIT> ()));
    2498              :   }
    2499      3818134 :   bool is_exposure (bool strict = false) const
    2500              :   {
    2501      1909008 :     return (get_flag_bit<DB_EXPOSE_PURVIEW_BIT> ()
    2502      3818134 :             || (strict && get_flag_bit <DB_EXPOSE_GLOBAL_BIT> ()));
    2503              :   }
    2504      2661701 :   bool is_ignored_exposure_context () const
    2505              :   {
    2506      2661701 :     return get_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
    2507              :   }
    2508              : 
    2509              : public:
    2510     35631685 :   bool is_import () const
    2511              :   {
    2512     10951380 :     return get_flag_bit<DB_IMPORTED_BIT> ();
    2513              :   }
    2514     21635900 :   bool is_unreached () const
    2515              :   {
    2516      1372125 :     return get_flag_bit<DB_UNREACHED_BIT> ();
    2517              :   }
    2518      2362972 :   bool is_hidden () const
    2519              :   {
    2520      2362972 :     return get_flag_bit<DB_HIDDEN_BIT> ();
    2521              :   }
    2522      1403737 :   bool is_maybe_recursive () const
    2523              :   {
    2524      1403737 :     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      1688589 :   bool is_type_spec () const
    2531              :   {
    2532      1688589 :     return get_flag_bit<DB_TYPE_SPEC_BIT> ();
    2533              :   }
    2534      1688589 :   bool is_friend_spec () const
    2535              :   {
    2536      1688589 :     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     12900807 :   bool is_special () const
    2552              :   {
    2553     12900807 :     return get_flag_bit<DB_SPECIAL_BIT> ();
    2554              :   }
    2555      2428221 :   void set_special ()
    2556              :   {
    2557      2428221 :     set_flag_bit<DB_SPECIAL_BIT> ();
    2558              :   }
    2559              : 
    2560              : public:
    2561    217153228 :   tree get_entity () const
    2562              :   {
    2563     58569901 :     return entity;
    2564              :   }
    2565     20947086 :   tree get_name () const
    2566              :   {
    2567     20947086 :     gcc_checking_assert (is_binding ());
    2568     20947086 :     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     22116594 :     inline static hashval_t hash (const compare_type &p)
    2583              :     {
    2584     22116594 :       hashval_t h = pointer_hash<tree_node>::hash (p.first);
    2585     22116594 :       if (p.second)
    2586              :         {
    2587       244233 :           hashval_t nh = IDENTIFIER_HASH_VALUE (p.second);
    2588       244233 :           h = iterative_hash_hashval_t (h, nh);
    2589              :         }
    2590     22116594 :       return h;
    2591              :     }
    2592    124860512 :     inline static bool equal (const value_type b, const compare_type &p)
    2593              :     {
    2594    124860512 :       if (b->entity != p.first)
    2595              :         return false;
    2596              : 
    2597     15989338 :       if (p.second)
    2598        54435 :         return b->discriminator == reinterpret_cast<uintptr_t> (p.second);
    2599              :       else
    2600     15934903 :         return !b->is_binding ();
    2601              :     }
    2602              : 
    2603              :     /* (re)hasher for a binding itself.  */
    2604     95772536 :     inline static hashval_t hash (const value_type b)
    2605              :     {
    2606     95772536 :       hashval_t h = pointer_hash<tree_node>::hash (b->entity);
    2607     95772536 :       if (b->is_binding ())
    2608              :         {
    2609      4484900 :           hashval_t nh = IDENTIFIER_HASH_VALUE (b->get_name ());
    2610      4484900 :           h = iterative_hash_hashval_t (h, nh);
    2611              :         }
    2612     95772536 :       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      3445589 :     static void remove (value_type p)
    2625              :     {
    2626      3445589 :       delete (p);
    2627      3445589 :     }
    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       340527 :     hash (size_t size, hash *c = NULL)
    2663       340527 :       : parent (size), chain (c), current (NULL), section (0),
    2664       340527 :         reached_unreached (false), writing_merge_key (false),
    2665       340527 :         ignore_exposure (false)
    2666              :     {
    2667       340527 :       worklist.create (size);
    2668       340527 :       dep_adl_entity_list.create (16);
    2669       340527 :     }
    2670       340527 :     ~hash ()
    2671              :     {
    2672       340527 :       worklist.release ();
    2673       340527 :       dep_adl_entity_list.release ();
    2674       340527 :     }
    2675              : 
    2676              :   public:
    2677    162054192 :     bool is_key_order () const
    2678              :     {
    2679    162054192 :       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      1022053 :     temp_override<bool> ignore_exposure_if (bool cond)
    2687              :     {
    2688       807005 :       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       340498 :     tarjan (unsigned size)
    2737       340498 :       : index (0)
    2738              :     {
    2739       340498 :       result.create (size);
    2740       340498 :       stack.create (50);
    2741       340498 :     }
    2742       340498 :     ~tarjan ()
    2743              :     {
    2744       340498 :       gcc_assert (!stack.length ());
    2745       340498 :       stack.release ();
    2746       340498 :     }
    2747              : 
    2748              :   public:
    2749              :     void connect (depset *);
    2750              :   };
    2751              : };
    2752              : 
    2753              : inline
    2754      3445589 : depset::depset (tree entity)
    2755      3445589 :   :entity (entity), discriminator (0), cluster (0), section (0)
    2756              : {
    2757      3445589 :   deps.create (0);
    2758              : }
    2759              : 
    2760              : inline
    2761      3445589 : depset::~depset ()
    2762              : {
    2763      3445589 :   deps.release ();
    2764      3445589 : }
    2765              : 
    2766              : const char *
    2767        45278 : depset::entity_kind_name () const
    2768              : {
    2769              :   /* Same order as entity_kind.  */
    2770        45278 :   static const char *const names[] =
    2771              :     {"decl", "specialization", "partial", "using",
    2772              :      "namespace", "tu-local", "redirect", "binding"};
    2773        45278 :   static_assert (ARRAY_SIZE (names) == EK_EXPLICIT_HWM + 1,
    2774              :                  "names must have an entry for every explicit entity_kind");
    2775        45278 :   entity_kind kind = get_entity_kind ();
    2776        45278 :   gcc_checking_assert (kind < ARRAY_SIZE (names));
    2777        45278 :   return names[kind];
    2778              : }
    2779              : 
    2780              : /* Create a depset for a namespace binding NS::NAME.  */
    2781              : 
    2782       164071 : depset *depset::make_binding (tree ns, tree name)
    2783              : {
    2784       164071 :   depset *binding = new depset (ns);
    2785              : 
    2786       164071 :   binding->discriminator = reinterpret_cast <uintptr_t> (name);
    2787              : 
    2788       164071 :   return binding;
    2789              : }
    2790              : 
    2791      3281518 : depset *depset::make_entity (tree entity, entity_kind ek, bool is_defn)
    2792              : {
    2793      3281518 :   depset *r = new depset (entity);
    2794              : 
    2795      3281518 :   r->discriminator = ((1 << DB_ZERO_BIT)
    2796      3281518 :                       | (ek << DB_KIND_BIT)
    2797      3281518 :                       | is_defn << DB_DEFN_BIT);
    2798              : 
    2799      3281518 :   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     49602585 :   static hashval_t hash (const value_type &k)
    2816              :   {
    2817     49602585 :     hashval_t h = IDENTIFIER_HASH_VALUE (k.id);
    2818     49602585 :     h = iterative_hash_hashval_t (DECL_UID (k.ns), h);
    2819              : 
    2820     49602585 :     return h;
    2821              :   }
    2822     11157237 :   static bool equal (const value_type &k, const value_type &l)
    2823              :   {
    2824     11157237 :     return k.ns == l.ns && k.id == l.id;
    2825              :   }
    2826       222239 :   static void mark_empty (value_type &k)
    2827              :   {
    2828       222239 :     k.ns = k.id = NULL_TREE;
    2829              :   }
    2830         7907 :   static void mark_deleted (value_type &k)
    2831              :   {
    2832         7907 :     k.ns = NULL_TREE;
    2833         7907 :     gcc_checking_assert (k.id);
    2834         7907 :   }
    2835    395216797 :   static bool is_empty (const value_type &k)
    2836              :   {
    2837    395166311 :     return k.ns == NULL_TREE && k.id == NULL_TREE;
    2838              :   }
    2839     17257080 :   static bool is_deleted (const value_type &k)
    2840              :   {
    2841     17257080 :     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      3299386 :   merge_key ()
    2992      2667689 :     :ref_q (REF_QUAL_NONE), coro_disc (0), iobj_p (0), xobj_p (0), index (0),
    2993      3299386 :      ret (NULL_TREE), args (NULL_TREE),
    2994      3299386 :      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       132815 :   bool is_duplicate (tree decl)
    3121              :   {
    3122       265630 :     return find_duplicate (decl) != NULL;
    3123              :   }
    3124       170062 :   tree maybe_duplicate (tree decl)
    3125              :   {
    3126       170062 :     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       141827 :   void post_process (post_process_data data)
    3146              :   {
    3147       141827 :     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       220140 : trees_in::trees_in (module_state *state)
    3162       220140 :   :parent (), state (state), unused (0)
    3163              : {
    3164       220140 :   duplicates = NULL;
    3165       220140 :   back_refs.create (500);
    3166       220140 :   post_decls.create (0);
    3167       220140 :   post_types.create (0);
    3168       220140 : }
    3169              : 
    3170       220140 : trees_in::~trees_in ()
    3171              : {
    3172       322226 :   delete (duplicates);
    3173       220140 :   back_refs.release ();
    3174       220140 :   post_decls.release ();
    3175       220140 :   post_types.release ();
    3176       220140 : }
    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    133241317 :   bool is_initial_scan () const
    3232              :   {
    3233    221422109 :     return !streaming_p () && !is_key_order ();
    3234              :   }
    3235    108527260 :   bool is_key_order () const
    3236              :   {
    3237     88180792 :     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       675366 :   void set_importing (int i ATTRIBUTE_UNUSED)
    3251              :   {
    3252              : #if CHECKING_P
    3253       675366 :     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       681070 : trees_out::trees_out (allocator *mem, module_state *state, depset::hash &deps,
    3357              :                       unsigned section)
    3358      1362140 :   :parent (mem), state (state), tree_map (500),
    3359       681070 :    dep_hash (&deps), ref_num (0), section (section),
    3360       681070 :    writing_local_entities (false), walking_bit_field_unit (false)
    3361              : {
    3362              : #if CHECKING_P
    3363       681070 :   importedness = 0;
    3364              : #endif
    3365       681070 : }
    3366              : 
    3367       681070 : trees_out::~trees_out ()
    3368              : {
    3369       681070 : }
    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         6123 :   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       101061 :   ~loc_spans ()
    3414              :   {
    3415       101061 :     delete spans;
    3416       101061 :   }
    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        16035 :   bool init_p () const
    3430              :   {
    3431        16035 :     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         5942 :   void maybe_init ()
    3439              :   {
    3440         5942 :     if (!init_p ())
    3441            6 :       init (line_table, nullptr);
    3442         5942 :   }
    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       469680 :   location_t main_start () const
    3453              :   {
    3454       469680 :     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    346728611 :   static int compare (const void *a_, const void *b_)
    3497              :   {
    3498    346728611 :     auto *a = static_cast<const ord_loc_info *> (a_);
    3499    346728611 :     auto *b = static_cast<const ord_loc_info *> (b_);
    3500              : 
    3501    346728611 :     if (a->src != b->src)
    3502     50730632 :       return a->src < b->src ? -1 : +1;
    3503              : 
    3504              :     // Ensure no overlap
    3505    295997979 :     gcc_checking_assert (a->offset + a->span <= b->offset
    3506              :                          || b->offset + b->span <= a->offset);
    3507              : 
    3508    295997979 :     gcc_checking_assert (a->offset != b->offset);
    3509    295997979 :     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    151117648 :   static hashval_t hash (const value_type &v)
    3520              :   {
    3521    128399521 :     auto h = pointer_hash<const line_map_ordinary>::hash (v.src);
    3522    151117648 :     return iterative_hash_hashval_t (v.offset, h);
    3523              :   }
    3524    166451872 :   static bool equal (const value_type &v, const compare_type p)
    3525              :   {
    3526    166451872 :     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    481619920 :   static bool is_empty (value_type &v)
    3534              :   {
    3535    481619920 :     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      2557925 :   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     10051489 :   static int compare (const void *a_, const void *b_)
    3555              :   {
    3556     10051489 :     auto *a = static_cast<const macro_loc_info *> (a_);
    3557     10051489 :     auto *b = static_cast<const macro_loc_info *> (b_);
    3558              : 
    3559     10051489 :     gcc_checking_assert (MAP_START_LOCATION (a->src)
    3560              :                          != MAP_START_LOCATION (b->src));
    3561     10051489 :     if (MAP_START_LOCATION (a->src) < MAP_START_LOCATION (b->src))
    3562              :       return -1;
    3563              :     else
    3564      4850866 :       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     10737332 :   static hashval_t hash (compare_type p)
    3575              :   {
    3576     10737332 :     return pointer_hash<const line_map_macro>::hash (p);
    3577              :   }
    3578      9063500 :   static hashval_t hash (const value_type &v)
    3579              :   {
    3580      9063500 :     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     34739068 :   static bool is_empty (value_type &v)
    3592              :   {
    3593     34739068 :     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       172618 :   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      1253197 :   unsigned remap_module (unsigned owner)
    3667              :   {
    3668      1253197 :     if (owner < remap->length ())
    3669      1253197 :       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      1324935 :   elf_in *from () const
    3830              :   {
    3831      1324935 :     return slurp->from;
    3832              :   }
    3833              : 
    3834              :  public:
    3835              :   /* Kind of this module.  */
    3836       151847 :   bool is_module () const
    3837              :   {
    3838       151847 :     return module_p;
    3839              :   }
    3840      2603802 :   bool is_header () const
    3841              :   {
    3842      2603802 :     return header_p;
    3843              :   }
    3844          645 :   bool is_interface () const
    3845              :   {
    3846          645 :     return interface_p;
    3847              :   }
    3848       376033 :   bool is_partition () const
    3849              :   {
    3850       376033 :     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        11853 : module_state::module_state (tree name, module_state *parent, bool partition)
    4040        11853 :   : imports (BITMAP_GGC_ALLOC ()), exports (BITMAP_GGC_ALLOC ()),
    4041        11853 :     parent (parent), name (name), slurp (NULL),
    4042        11853 :     flatname (NULL), filename (NULL),
    4043        11853 :     entity_lwm (~0u >> 1), entity_num (0),
    4044        11853 :     ordinary_locs (0, 0), macro_locs (0, 0),
    4045        11853 :     loc (UNKNOWN_LOCATION),
    4046        11853 :     crc (0), mod (MODULE_UNKNOWN), remap (0), subst (0)
    4047              : {
    4048        11853 :   loadedness = ML_NONE;
    4049              : 
    4050        11853 :   module_p = header_p = interface_p = partition_p = false;
    4051              : 
    4052        11853 :   directness = MD_NONE;
    4053        11853 :   exported_p = false;
    4054              : 
    4055        11853 :   cmi_noted_p = false;
    4056        11853 :   active_init_p = false;
    4057              : 
    4058        11853 :   partition_p = partition;
    4059              : 
    4060        11853 :   inform_cmi_p = false;
    4061        11853 :   visited_p = false;
    4062              : 
    4063        11853 :   extensions = 0;
    4064        11853 :   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        11853 :   gcc_checking_assert (!(parent && header_p));
    4075        11853 : }
    4076              : 
    4077           62 : module_state::~module_state ()
    4078              : {
    4079           62 :   release ();
    4080           62 : }
    4081              : 
    4082              : /* Hash module state.  */
    4083              : static hashval_t
    4084        17457 : module_name_hash (const_tree name)
    4085              : {
    4086        17457 :   if (TREE_CODE (name) == STRING_CST)
    4087         3543 :     return htab_hash_string (TREE_STRING_POINTER (name));
    4088              :   else
    4089        13914 :     return IDENTIFIER_HASH_VALUE (name);
    4090              : }
    4091              : 
    4092              : hashval_t
    4093         4860 : module_state_hash::hash (const value_type m)
    4094              : {
    4095         4860 :   hashval_t ph = pointer_hash<void>::hash
    4096         4860 :     (reinterpret_cast<void *> (reinterpret_cast<uintptr_t> (m->parent)
    4097         4860 :                                | m->is_partition ()));
    4098         4860 :   hashval_t nh = module_name_hash (m->name);
    4099         4860 :   return iterative_hash_hashval_t (ph, nh);
    4100              : }
    4101              : 
    4102              : /* Hash a name.  */
    4103              : hashval_t
    4104        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         8558 : module_state_hash::equal (const value_type existing,
    4114              :                           const compare_type &candidate)
    4115              : {
    4116         8558 :   uintptr_t ep = (reinterpret_cast<uintptr_t> (existing->parent)
    4117         8558 :                   | existing->is_partition ());
    4118         8558 :   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         7117 :   if (existing->name == candidate.first)
    4124              :     return true;
    4125              : 
    4126              :   /* If neither are string csts, they can't be equal.  */
    4127         1468 :   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       391148 : this_module() {
    4200       391148 :   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        34099 : inline module_client *get_mapper (location_t loc, class mkdeps *deps)
    4243              : {
    4244        34099 :   auto *res = mapper;
    4245          320 :   if (!res)
    4246         5036 :     res = make_mapper (loc, deps);
    4247        34099 :   return res;
    4248              : }
    4249              : 
    4250              : /********************************************************************/
    4251              : static tree
    4252       507641 : get_clone_target (tree decl)
    4253              : {
    4254       507641 :   tree target;
    4255              : 
    4256       507641 :   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       440297 :     target = DECL_CLONED_FUNCTION (decl);
    4264              : 
    4265       507641 :   gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (target));
    4266              : 
    4267       507641 :   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    144600260 : node_template_info (tree decl, int &use)
    4286              : {
    4287    144600260 :   tree ti = NULL_TREE;
    4288    144600260 :   int use_tpl = -1;
    4289    144600260 :   if (DECL_IMPLICIT_TYPEDEF_P (decl))
    4290              :     {
    4291     26163479 :       tree type = TREE_TYPE (decl);
    4292              : 
    4293     26163479 :       ti = TYPE_TEMPLATE_INFO (type);
    4294     26163479 :       if (ti)
    4295              :         {
    4296      6363982 :           if (TYPE_LANG_SPECIFIC (type))
    4297      6351860 :             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    118436781 :   else if (DECL_LANG_SPECIFIC (decl)
    4314    118436781 :            && (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    101534276 :       use_tpl = DECL_USE_TEMPLATE (decl);
    4322    101534276 :       ti = DECL_TEMPLATE_INFO (decl);
    4323              :     }
    4324              : 
    4325    144600260 :   use = use_tpl;
    4326    144600260 :   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      2022894 : import_entity_index (tree decl, bool null_ok = false)
    4337              : {
    4338      2022894 :   if (unsigned *slot = entity_map->get (DECL_UID (decl)))
    4339      1972249 :     return *slot;
    4340              : 
    4341        50645 :   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       135683 : import_entity_module (unsigned index)
    4350              : {
    4351       135683 :   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       135623 :   unsigned pos = 1;
    4357       135623 :   unsigned len = modules->length () - pos;
    4358       302801 :   while (len)
    4359              :     {
    4360       167178 :       unsigned half = len / 2;
    4361       167178 :       module_state *probe = (*modules)[pos + half];
    4362       167178 :       if (index < probe->entity_lwm)
    4363              :         len = half;
    4364       136050 :       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    476540636 :   void indent ()
    4416              :   {
    4417          309 :     if (dumps)
    4418       733489 :       dumps->indent++;
    4419              :   }
    4420    476540636 :   void outdent ()
    4421              :   {
    4422    476540636 :     if (dumps)
    4423              :       {
    4424       733489 :         gcc_checking_assert (dumps->indent);
    4425       733489 :         dumps->indent--;
    4426              :       }
    4427    476540636 :   }
    4428              : 
    4429              : public:
    4430              :   /* Is dump enabled?.  */
    4431    283887543 :   bool operator () (int mask = 0)
    4432              :   {
    4433      5684674 :     if (!dumps || !dumps->stream)
    4434              :       return false;
    4435       513592 :     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       108773 : dumper::push (module_state *m)
    4450              : {
    4451       108773 :   FILE *stream = NULL;
    4452       108773 :   if (!dumps || !dumps->stack.length ())
    4453              :     {
    4454       107469 :       stream = dump_begin (module_dump_id, &flags);
    4455       107469 :       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       108730 : void dumper::pop (unsigned n)
    4494              : {
    4495       108730 :   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       537745 : dumper::impl::nested_name (tree t)
    4520              : {
    4521       537745 :   tree ti = NULL_TREE;
    4522       537745 :   int origin = -1;
    4523       537745 :   tree name = NULL_TREE;
    4524              : 
    4525       537745 :   if (t && TREE_CODE (t) == TU_LOCAL_ENTITY)
    4526            0 :     t = TU_LOCAL_ENTITY_NAME (t);
    4527              : 
    4528       537715 :   if (t && TREE_CODE (t) == TREE_BINFO)
    4529          384 :     t = BINFO_TYPE (t);
    4530              : 
    4531       537745 :   if (t && TYPE_P (t))
    4532       260860 :     t = TYPE_NAME (t);
    4533              : 
    4534       537703 :   if (t && DECL_P (t))
    4535              :     {
    4536       451453 :       if (t == global_namespace || DECL_TEMPLATE_PARM_P (t))
    4537              :         ;
    4538       419318 :       else if (tree ctx = DECL_CONTEXT (t))
    4539       328951 :         if (TREE_CODE (ctx) == TRANSLATION_UNIT_DECL
    4540       328951 :             || nested_name (ctx))
    4541       328951 :           fputs ("::", stream);
    4542              : 
    4543       451453 :       int use_tpl;
    4544       451453 :       ti = node_template_info (t, use_tpl);
    4545       142066 :       if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    4546       593474 :           && (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == t))
    4547              :         t = TI_TEMPLATE (ti);
    4548       451453 :       tree not_tmpl = t;
    4549       451453 :       if (TREE_CODE (t) == TEMPLATE_DECL)
    4550              :         {
    4551        23881 :           fputs ("template ", stream);
    4552        23881 :           not_tmpl = DECL_TEMPLATE_RESULT (t);
    4553              :         }
    4554              : 
    4555        23881 :       if (not_tmpl
    4556       451449 :           && DECL_P (not_tmpl)
    4557       451449 :           && DECL_LANG_SPECIFIC (not_tmpl)
    4558       271560 :           && 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       454680 :       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       451453 :   if (name)
    4581       501324 :     switch (TREE_CODE (name))
    4582              :       {
    4583        13683 :       default:
    4584        13683 :         fputs ("#unnamed#", stream);
    4585        13683 :         break;
    4586              : 
    4587       461927 :       case IDENTIFIER_NODE:
    4588       461927 :         fwrite (IDENTIFIER_POINTER (name), 1, IDENTIFIER_LENGTH (name), stream);
    4589       461927 :         break;
    4590              : 
    4591        25622 :       case INTEGER_CST:
    4592        25622 :         print_hex (wi::to_wide (name), stream);
    4593        25622 :         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        36421 :     fputs ("#null#", stream);
    4604              : 
    4605       537745 :   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       537745 :   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       513368 :   else if (origin == -2)
    4623           45 :     fprintf (stream, "@???");
    4624              : 
    4625       537745 :   if (ti)
    4626              :     {
    4627       142066 :       tree args = INNERMOST_TEMPLATE_ARGS (TI_ARGS (ti));
    4628       142066 :       fputs ("<", stream);
    4629       142066 :       if (args)
    4630       358727 :         for (int ix = 0; ix != TREE_VEC_LENGTH (args); ix++)
    4631              :           {
    4632       216661 :             if (ix)
    4633        74595 :               fputs (",", stream);
    4634       216661 :             nested_name (TREE_VEC_ELT (args, ix));
    4635              :           }
    4636       142066 :       fputs (">", stream);
    4637              :     }
    4638              : 
    4639       537745 :   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       450752 : dumper::operator () (const char *format, ...)
    4666              : {
    4667       450752 :   if (!(*this) ())
    4668              :     return false;
    4669              : 
    4670       385499 :   bool no_nl = format[0] == '+';
    4671       385499 :   format += no_nl;
    4672              : 
    4673       385499 :   if (dumps->bol)
    4674              :     {
    4675              :       /* Module import indent.  */
    4676       199151 :       if (unsigned depth = dumps->stack.length () - 1)
    4677              :         {
    4678        22800 :           const char *prefix = ">>>>";
    4679        45582 :           fprintf (dumps->stream, (depth <= strlen (prefix)
    4680        22782 :                                    ? &prefix[strlen (prefix) - depth]
    4681              :                                    : ">.%d.>"), depth);
    4682              :         }
    4683              : 
    4684              :       /* Local indent.  */
    4685       199151 :       if (unsigned indent = dumps->indent)
    4686              :         {
    4687       106624 :           const char *prefix = "      ";
    4688       208232 :           fprintf (dumps->stream, (indent <= strlen (prefix)
    4689       101608 :                                    ? &prefix[strlen (prefix) - indent]
    4690              :                                    : "  .%d.  "), indent);
    4691              :         }
    4692       199151 :       dumps->bol = false;
    4693              :     }
    4694              : 
    4695       385499 :   va_list args;
    4696       385499 :   va_start (args, format);
    4697      1128250 :   while (const char *esc = strchr (format, '%'))
    4698              :     {
    4699       742751 :       fwrite (format, 1, (size_t)(esc - format), dumps->stream);
    4700       742751 :       format = ++esc;
    4701       742751 :       switch (*format++)
    4702              :         {
    4703            0 :         default:
    4704            0 :           gcc_unreachable ();
    4705              : 
    4706          586 :         case '%':
    4707          586 :           fputc ('%', dumps->stream);
    4708          586 :           break;
    4709              : 
    4710       113248 :         case 'C': /* Code */
    4711       113248 :           {
    4712       113248 :             tree_code code = (tree_code)va_arg (args, unsigned);
    4713       113248 :             fputs (get_tree_code_name (code), dumps->stream);
    4714              :           }
    4715       113248 :           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       126732 :         case 'N': /* Name.  */
    4746       126732 :           {
    4747       126732 :             tree t = va_arg (args, tree);
    4748       253953 :             while (t && TREE_CODE (t) == OVERLOAD)
    4749          489 :               t = OVL_FUNCTION (t);
    4750       126732 :             fputc ('\'', dumps->stream);
    4751       126732 :             dumps->nested_name (t);
    4752       126732 :             fputc ('\'', dumps->stream);
    4753              :           }
    4754       126732 :           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        63216 :         case 'd': /* Decimal Int.  */
    4818        63216 :           {
    4819        63216 :             int d = va_arg (args, int);
    4820        63216 :             fprintf (dumps->stream, "%d", d);
    4821              :           }
    4822        63216 :           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       128817 :         case 's': /* String. */
    4832       128817 :           {
    4833       128817 :             const char *s = va_arg (args, char *);
    4834       128817 :             gcc_checking_assert (s);
    4835       128817 :             fputs (s, dumps->stream);
    4836              :           }
    4837       128817 :           break;
    4838              : 
    4839       250779 :         case 'u': /* Unsigned.  */
    4840       250779 :           {
    4841       250779 :             unsigned u = va_arg (args, unsigned);
    4842       250779 :             fprintf (dumps->stream, "%u", u);
    4843              :           }
    4844       250779 :           break;
    4845              : 
    4846         2685 :         case 'x': /* Hex. */
    4847         2685 :           {
    4848         2685 :             unsigned x = va_arg (args, unsigned);
    4849         2685 :             fprintf (dumps->stream, "%x", x);
    4850              :           }
    4851         2685 :           break;
    4852              :         }
    4853              :     }
    4854       385499 :   fputs (format, dumps->stream);
    4855       385499 :   va_end (args);
    4856       385499 :   if (!no_nl)
    4857              :     {
    4858       199151 :       dumps->bol = true;
    4859       199151 :       fputc ('\n', dumps->stream);
    4860              :     }
    4861              :   return true;
    4862              : }
    4863              : 
    4864              : struct note_def_cache_hasher : ggc_cache_ptr_hash<tree_node>
    4865              : {
    4866       259563 :   static int keep_cache_entry (tree t)
    4867              :   {
    4868       259563 :     if (!CHECKING_P)
    4869              :       /* GTY is unfortunately not clever enough to conditionalize
    4870              :          this.  */
    4871              :       gcc_unreachable ();
    4872              : 
    4873       259563 :     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       346065 : trees_in::assert_definition (tree decl ATTRIBUTE_UNUSED,
    4895              :                              bool installing ATTRIBUTE_UNUSED)
    4896              : {
    4897              : #if CHECKING_P
    4898       346065 :   tree *slot = note_defs->find_slot (decl, installing ? INSERT : NO_INSERT);
    4899       346065 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    4900       346065 :   if (installing)
    4901              :     {
    4902              :       /* We must be inserting for the first time.  */
    4903       213250 :       gcc_assert (!*slot);
    4904       213250 :       *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       132815 :     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       346065 :   if (not_tmpl != decl)
    4921       202818 :     gcc_assert (!note_defs->find_slot (not_tmpl, NO_INSERT));
    4922              : #endif
    4923       346065 : }
    4924              : 
    4925              : void
    4926       517904 : trees_out::assert_definition (tree decl ATTRIBUTE_UNUSED)
    4927              : {
    4928              : #if CHECKING_P
    4929       517904 :   tree *slot = note_defs->find_slot (decl, INSERT);
    4930       517904 :   gcc_assert (!*slot);
    4931       517904 :   *slot = decl;
    4932       517904 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    4933       294165 :     gcc_assert (!note_defs->find_slot (DECL_TEMPLATE_RESULT (decl), NO_INSERT));
    4934              : #endif
    4935       517904 : }
    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       104341 : set_cmi_repo (const char *r)
    4955              : {
    4956       104341 :   XDELETEVEC (cmi_repo);
    4957       104341 :   XDELETEVEC (cmi_path);
    4958       104341 :   cmi_path_alloc = 0;
    4959              : 
    4960       104341 :   cmi_repo = NULL;
    4961       104341 :   cmi_repo_length = 0;
    4962              : 
    4963       104341 :   if (!r || !r[0])
    4964              :     return;
    4965              : 
    4966         5033 :   size_t len = strlen (r);
    4967         5033 :   cmi_repo = XNEWVEC (char, len + 1);
    4968         5033 :   memcpy (cmi_repo, r, len + 1);
    4969              : 
    4970         5033 :   if (len > 1 && IS_DIR_SEPARATOR (cmi_repo[len-1]))
    4971         5033 :     len--;
    4972         5033 :   if (len == 1 && cmi_repo[0] == '.')
    4973           27 :     len--;
    4974         5033 :   cmi_repo[len] = 0;
    4975         5033 :   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           41 : create_dirs (char *path)
    5013              : {
    5014           41 :   char *base = path;
    5015              :   /* Skip past initial slashes of absolute path.  */
    5016           41 :   while (IS_DIR_SEPARATOR (*base))
    5017            0 :     base++;
    5018              : 
    5019              :   /* Try and create the missing directories.  */
    5020         2514 :   for (; *base; base++)
    5021         2473 :     if (IS_DIR_SEPARATOR (*base))
    5022              :       {
    5023          240 :         char sep = *base;
    5024          240 :         *base = 0;
    5025          240 :         int failed = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
    5026          256 :         dump () && dump ("Mkdir ('%s') errno:=%u", path, failed ? errno : 0);
    5027          240 :         *base = sep;
    5028          240 :         if (failed
    5029              :             /* Maybe racing with another creator (of a *different*
    5030              :                module).  */
    5031           47 :             && errno != EEXIST)
    5032              :           break;
    5033              :       }
    5034           41 : }
    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       116053 : friend_from_decl_list (tree frnd)
    5041              : {
    5042       116053 :   tree res = frnd;
    5043              : 
    5044       116053 :   if (TREE_CODE (frnd) != TEMPLATE_DECL)
    5045              :     {
    5046        73272 :       tree tmpl = NULL_TREE;
    5047        73272 :       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        61323 :       else if (DECL_TEMPLATE_INFO (frnd))
    5055              :         {
    5056        61323 :           tmpl = DECL_TI_TEMPLATE (frnd);
    5057        61323 :           if (TREE_CODE (tmpl) != TEMPLATE_DECL)
    5058              :             tmpl = NULL_TREE;
    5059              :         }
    5060              : 
    5061        83831 :       if (tmpl && DECL_TEMPLATE_RESULT (tmpl) == res)
    5062              :         res = tmpl;
    5063              :     }
    5064              : 
    5065       116053 :   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      3049271 : trees_out::begin ()
    5114              : {
    5115      3049271 :   gcc_assert (!streaming_p () || !tree_map.elements ());
    5116              : 
    5117      3049271 :   mark_trees ();
    5118      3049271 :   if (streaming_p ())
    5119       340543 :     parent::begin ();
    5120      3049271 : }
    5121              : 
    5122              : unsigned
    5123       340543 : trees_out::end (elf_out *sink, unsigned name, unsigned *crc_ptr)
    5124              : {
    5125       340543 :   gcc_checking_assert (streaming_p ());
    5126              : 
    5127       340543 :   unmark_trees ();
    5128       340543 :   return parent::end (sink, name, crc_ptr);
    5129              : }
    5130              : 
    5131              : void
    5132      2708728 : trees_out::end ()
    5133              : {
    5134      2708728 :   gcc_assert (!streaming_p ());
    5135              : 
    5136      2708728 :   unmark_trees ();
    5137              :   /* Do not parent::end -- we weren't streaming.  */
    5138      2708728 : }
    5139              : 
    5140              : void
    5141      3049271 : trees_out::mark_trees ()
    5142              : {
    5143      3049271 :   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      2382548 :       tree_map.~ptr_int_hash_map ();
    5149      2382548 :       new (&tree_map) ptr_int_hash_map (size);
    5150              :     }
    5151              : 
    5152              :   /* Install the fixed trees, with +ve references.  */
    5153      3049271 :   unsigned limit = fixed_trees->length ();
    5154    587602052 :   for (unsigned ix = 0; ix != limit; ix++)
    5155              :     {
    5156    584552781 :       tree val = (*fixed_trees)[ix];
    5157    584552781 :       bool existed = tree_map.put (val, ix + tag_fixed);
    5158    584552781 :       gcc_checking_assert (!TREE_VISITED (val) && !existed);
    5159    584552781 :       TREE_VISITED (val) = true;
    5160              :     }
    5161              : 
    5162      3049271 :   ref_num = 0;
    5163      3049271 : }
    5164              : 
    5165              : /* Unmark the trees we encountered  */
    5166              : 
    5167              : void
    5168      3049271 : trees_out::unmark_trees ()
    5169              : {
    5170      3049271 :   ptr_int_hash_map::iterator end (tree_map.end ());
    5171    700758323 :   for (ptr_int_hash_map::iterator iter (tree_map.begin ()); iter != end; ++iter)
    5172              :     {
    5173    697709052 :       tree node = reinterpret_cast<tree> ((*iter).first);
    5174    697709052 :       int ref = (*iter).second;
    5175              :       /* We should have visited the node, and converted its mergeable
    5176              :          reference to a regular reference.  */
    5177    697709052 :       gcc_checking_assert (TREE_VISITED (node)
    5178              :                            && (ref <= tag_backref || ref >= tag_fixed));
    5179    697709052 :       TREE_VISITED (node) = false;
    5180              :     }
    5181      3049271 : }
    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      4736891 : trees_out::mark_by_value (tree decl)
    5189              : {
    5190      4736891 :   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      4736891 :   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      4732689 :       bool existed = tree_map.put (decl, tag_value);
    5201      4732689 :       gcc_checking_assert (!existed);
    5202      4732689 :       TREE_VISITED (decl) = true;
    5203              :     }
    5204      4736891 : }
    5205              : 
    5206              : int
    5207    134925950 : trees_out::get_tag (tree t)
    5208              : {
    5209    134925950 :   gcc_checking_assert (TREE_VISITED (t));
    5210    134925950 :   return *tree_map.get (t);
    5211              : }
    5212              : 
    5213              : /* Insert T into the map, return its tag number.    */
    5214              : 
    5215              : int
    5216    113156271 : trees_out::insert (tree t, walk_kind walk)
    5217              : {
    5218    113156271 :   gcc_checking_assert (walk != WK_normal || !TREE_VISITED (t));
    5219    113156271 :   int tag = --ref_num;
    5220    113156271 :   bool existed;
    5221    113156271 :   int &slot = tree_map.get_or_insert (t, &existed);
    5222    113156271 :   gcc_checking_assert (TREE_VISITED (t) == existed
    5223              :                        && (!existed
    5224              :                            || (walk == WK_value && slot == tag_value)));
    5225    113156271 :   TREE_VISITED (t) = true;
    5226    113156271 :   slot = tag;
    5227              : 
    5228    113156271 :   return tag;
    5229              : }
    5230              : 
    5231              : /* Insert T into the backreference array.  Return its back reference
    5232              :    number.  */
    5233              : 
    5234              : int
    5235     21720042 : trees_in::insert (tree t)
    5236              : {
    5237     21720042 :   gcc_checking_assert (t || get_overrun ());
    5238     21720042 :   back_refs.safe_push (t);
    5239     21720042 :   return -(int)back_refs.length ();
    5240              : }
    5241              : 
    5242              : /* A chained set of decls.  */
    5243              : 
    5244              : void
    5245       176892 : trees_out::chained_decls (tree decls)
    5246              : {
    5247       409718 :   for (; decls; decls = DECL_CHAIN (decls))
    5248       232826 :     tree_node (decls);
    5249       176892 :   tree_node (NULL_TREE);
    5250       176892 : }
    5251              : 
    5252              : tree
    5253        60787 : trees_in::chained_decls ()
    5254              : {
    5255        60787 :   tree decls = NULL_TREE;
    5256        60787 :   for (tree *chain = &decls;;)
    5257       145691 :     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        60787 :   return decls;
    5271              : }
    5272              : 
    5273              : /* A vector of decls following DECL_CHAIN.  */
    5274              : 
    5275              : void
    5276       437214 : trees_out::vec_chained_decls (tree decls)
    5277              : {
    5278       437214 :   if (streaming_p ())
    5279              :     {
    5280              :       unsigned len = 0;
    5281              : 
    5282      1404520 :       for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5283      1185959 :         len++;
    5284       218561 :       u (len);
    5285              :     }
    5286              : 
    5287      2809542 :   for (tree decl = decls; decl; decl = DECL_CHAIN (decl))
    5288              :     {
    5289       471189 :       if (DECL_IMPLICIT_TYPEDEF_P (decl)
    5290      2390253 :           && 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      2372320 :         tree_node (decl);
    5296              :     }
    5297       437214 : }
    5298              : 
    5299              : vec<tree, va_heap> *
    5300       137994 : trees_in::vec_chained_decls ()
    5301              : {
    5302       137994 :   vec<tree, va_heap> *v = NULL;
    5303              : 
    5304       137994 :   if (unsigned len = u ())
    5305              :     {
    5306        72342 :       vec_alloc (v, len);
    5307              : 
    5308       932132 :       for (unsigned ix = 0; ix < len; ix++)
    5309              :         {
    5310       787448 :           tree decl = tree_node ();
    5311       787448 :           if (decl && !DECL_P (decl))
    5312              :             {
    5313            0 :               set_overrun ();
    5314            0 :               break;
    5315              :             }
    5316       787448 :           v->quick_push (decl);
    5317              :         }
    5318              : 
    5319        72342 :       if (get_overrun ())
    5320              :         {
    5321            0 :           vec_free (v);
    5322            0 :           v = NULL;
    5323              :         }
    5324              :     }
    5325              : 
    5326       137994 :   return v;
    5327              : }
    5328              : 
    5329              : /* A vector of trees.  */
    5330              : 
    5331              : void
    5332       306564 : trees_out::tree_vec (vec<tree, va_gc> *v)
    5333              : {
    5334       306564 :   unsigned len = vec_safe_length (v);
    5335       306564 :   if (streaming_p ())
    5336       153259 :     u (len);
    5337       392228 :   for (unsigned ix = 0; ix != len; ix++)
    5338        85664 :     tree_node ((*v)[ix]);
    5339       306564 : }
    5340              : 
    5341              : vec<tree, va_gc> *
    5342        96478 : trees_in::tree_vec ()
    5343              : {
    5344        96478 :   vec<tree, va_gc> *v = NULL;
    5345        96478 :   if (unsigned len = u ())
    5346              :     {
    5347        24281 :       vec_alloc (v, len);
    5348        75427 :       for (unsigned ix = 0; ix != len; ix++)
    5349        26865 :         v->quick_push (tree_node ());
    5350              :     }
    5351        96478 :   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       466242 : trees_out::tree_list (tree list, bool has_purpose)
    5391              : {
    5392      2119527 :   for (; list; list = TREE_CHAIN (list))
    5393              :     {
    5394      1653285 :       gcc_checking_assert (TREE_VALUE (list));
    5395      1653285 :       tree_node (TREE_VALUE (list));
    5396      1653285 :       if (has_purpose)
    5397      1596555 :         tree_node (TREE_PURPOSE (list));
    5398              :     }
    5399       466242 :   tree_node (NULL_TREE);
    5400       466242 : }
    5401              : 
    5402              : tree
    5403       148621 : trees_in::tree_list (bool has_purpose)
    5404              : {
    5405       148621 :   tree res = NULL_TREE;
    5406              : 
    5407       700680 :   for (tree *chain = &res; tree value = tree_node ();
    5408      1104118 :        chain = &TREE_CHAIN (*chain))
    5409              :     {
    5410       552059 :       tree purpose = has_purpose ? tree_node () : NULL_TREE;
    5411       552059 :       *chain = build_tree_list (purpose, value);
    5412       552059 :     }
    5413              : 
    5414       148621 :   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     22821919 : trees_out::start (tree t, bool code_streamed)
    5468              : {
    5469     22821919 :   if (TYPE_P (t))
    5470              :     {
    5471       861361 :       enum tree_code code = TREE_CODE (t);
    5472       861361 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    5473              :       /* All these types are TYPE_NON_COMMON.  */
    5474       861361 :       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     22821919 :   if (!code_streamed)
    5483     22066003 :     u (TREE_CODE (t));
    5484              : 
    5485     22821919 :   switch (TREE_CODE (t))
    5486              :     {
    5487     20448207 :     default:
    5488     20448207 :       if (VL_EXP_CLASS_P (t))
    5489       881723 :         u (VL_EXP_OPERAND_LENGTH (t));
    5490              :       break;
    5491              : 
    5492       903982 :     case INTEGER_CST:
    5493       903982 :       u (TREE_INT_CST_NUNITS (t));
    5494       903982 :       u (TREE_INT_CST_EXT_NUNITS (t));
    5495       903982 :       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        67819 :     case STRING_CST:
    5514        67819 :       str (TREE_STRING_POINTER (t), TREE_STRING_LENGTH (t));
    5515        67819 :       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       149341 :     case TREE_BINFO:
    5547       149341 :       u (BINFO_N_BASE_BINFOS (t));
    5548       149341 :       break;
    5549              : 
    5550      1252495 :     case TREE_VEC:
    5551      1252495 :       u (TREE_VEC_LENGTH (t));
    5552      1252495 :       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     22821919 :       break;
    5565              :     }
    5566     22821919 : }
    5567              : 
    5568              : /* Start tree read.  Allocate the receiving node.  */
    5569              : 
    5570              : tree
    5571     15559574 : trees_in::start (unsigned code)
    5572              : {
    5573     15559574 :   tree t = NULL_TREE;
    5574              : 
    5575     15559574 :   if (!code)
    5576     14112574 :     code = u ();
    5577              : 
    5578     15559574 :   switch (code)
    5579              :     {
    5580     13961775 :     default:
    5581     13961775 :       if (code >= MAX_TREE_CODES)
    5582              :         {
    5583            0 :         fail:
    5584            0 :           set_overrun ();
    5585            0 :           return NULL_TREE;
    5586              :         }
    5587     13961775 :       else if (TREE_CODE_CLASS (code) == tcc_vl_exp)
    5588              :         {
    5589       605805 :           unsigned ops = u ();
    5590       605805 :           t = build_vl_exp (tree_code (code), ops);
    5591              :         }
    5592              :       else
    5593     13355970 :         t = make_node (tree_code (code));
    5594              :       break;
    5595              : 
    5596       605648 :     case INTEGER_CST:
    5597       605648 :       {
    5598       605648 :         unsigned n = u ();
    5599       605648 :         unsigned e = u ();
    5600       605648 :         t = make_int_cst (n, e);
    5601              :       }
    5602       605648 :       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        50239 :     case STRING_CST:
    5627        50239 :       {
    5628        50239 :         size_t l;
    5629        50239 :         const char *chars = str (&l);
    5630        50239 :         t = build_string (l, chars);
    5631              :       }
    5632        50239 :       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        93779 :     case TREE_BINFO:
    5661        93779 :       t = make_tree_binfo (u ());
    5662        93779 :       break;
    5663              : 
    5664       848058 :     case TREE_VEC:
    5665       848058 :       t = make_tree_vec (u ());
    5666       848058 :       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       863681 : get_importer_interface (tree decl)
    5693              : {
    5694              :   /* Internal linkage entities must be emitted in each importer if
    5695              :      there is a definition available.  */
    5696       863681 :   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       400415 :   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       393611 :   if (DECL_LANG_SPECIFIC (decl)
    5710       393611 :       && DECL_EXPLICIT_INSTANTIATION (decl))
    5711        29108 :     return (header_module_p () && !DECL_EXTERNAL (decl)
    5712        29108 :             ? importer_interface::always_emit
    5713              :             : importer_interface::external);
    5714              : 
    5715              :   /* A gnu_inline function is never emitted in any TU.  */
    5716       364503 :   if (TREE_CODE (decl) == FUNCTION_DECL
    5717       270471 :       && DECL_DECLARED_INLINE_P (decl)
    5718       626174 :       && 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     22861114 : 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     22861114 :   tree_code code = TREE_CODE (t);
    5741              : 
    5742     22861114 :   WB (t->base.side_effects_flag);
    5743     22861114 :   WB (t->base.constant_flag);
    5744     22861114 :   WB (t->base.addressable_flag);
    5745     22861114 :   WB (t->base.volatile_flag);
    5746     22861114 :   WB (t->base.readonly_flag);
    5747              :   /* base.asm_written_flag is a property of the current TU's use of
    5748              :      this decl.  */
    5749     22861114 :   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     22861114 :   WB (t->base.nothrow_flag);
    5755     22861114 :   WB (t->base.static_flag);
    5756              :   /* This is TYPE_CACHED_VALUES_P for types.  */
    5757     22861114 :   WB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5758     22861114 :   WB (t->base.private_flag);
    5759     22861114 :   WB (t->base.protected_flag);
    5760     22861114 :   WB (t->base.deprecated_flag);
    5761     22861114 :   WB (t->base.default_def_flag);
    5762              : 
    5763     22861114 :   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     19843664 :     default:
    5774     19843664 :       WB (t->base.u.bits.lang_flag_0);
    5775     19843664 :       bool flag_1 = t->base.u.bits.lang_flag_1;
    5776     19843664 :       if (!flag_1)
    5777              :         ;
    5778       650407 :       else if (code == TEMPLATE_INFO)
    5779              :         /* This is TI_PENDING_TEMPLATE_FLAG, not relevant to reader.  */
    5780              :         flag_1 = false;
    5781       643275 :       else if (code == VAR_DECL)
    5782              :         {
    5783              :           /* This is DECL_INITIALIZED_P.  */
    5784       116487 :           if (TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    5785              :             /* We'll set this when reading the definition.  */
    5786     19843664 :             flag_1 = false;
    5787              :         }
    5788     19843664 :       WB (flag_1);
    5789     19843664 :       WB (t->base.u.bits.lang_flag_2);
    5790     19843664 :       WB (t->base.u.bits.lang_flag_3);
    5791     19843664 :       WB (t->base.u.bits.lang_flag_4);
    5792     19843664 :       WB (t->base.u.bits.lang_flag_5);
    5793     19843664 :       WB (t->base.u.bits.lang_flag_6);
    5794     19843664 :       WB (t->base.u.bits.saturating_flag);
    5795     19843664 :       WB (t->base.u.bits.unsigned_flag);
    5796     19843664 :       WB (t->base.u.bits.packed_flag);
    5797     19843664 :       WB (t->base.u.bits.user_align);
    5798     19843664 :       WB (t->base.u.bits.nameless_flag);
    5799     19843664 :       WB (t->base.u.bits.atomic_flag);
    5800     19843664 :       WB (t->base.u.bits.unavailable_flag);
    5801     19843664 :       break;
    5802              :     }
    5803              : 
    5804     19843664 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5805              :     {
    5806       900556 :       WB (t->type_common.no_force_blk_flag);
    5807       900556 :       WB (t->type_common.needs_constructing_flag);
    5808       900556 :       WB (t->type_common.transparent_aggr_flag);
    5809       900556 :       WB (t->type_common.restrict_flag);
    5810       900556 :       WB (t->type_common.string_flag);
    5811       900556 :       WB (t->type_common.lang_flag_0);
    5812       900556 :       WB (t->type_common.lang_flag_1);
    5813       900556 :       WB (t->type_common.lang_flag_2);
    5814       900556 :       WB (t->type_common.lang_flag_3);
    5815       900556 :       WB (t->type_common.lang_flag_4);
    5816       900556 :       WB (t->type_common.lang_flag_5);
    5817       900556 :       WB (t->type_common.lang_flag_6);
    5818       900556 :       WB (t->type_common.typeless_storage);
    5819              :     }
    5820              : 
    5821     19843664 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    5822              :     return;
    5823              : 
    5824      4914222 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    5825              :     {
    5826      4914222 :       WB (t->decl_common.nonlocal_flag);
    5827      4914222 :       WB (t->decl_common.virtual_flag);
    5828      4914222 :       WB (t->decl_common.ignored_flag);
    5829      4914222 :       WB (t->decl_common.abstract_flag);
    5830      4914222 :       WB (t->decl_common.artificial_flag);
    5831      4914222 :       WB (t->decl_common.preserve_flag);
    5832      4914222 :       WB (t->decl_common.debug_expr_is_from);
    5833      4914222 :       WB (t->decl_common.lang_flag_0);
    5834      4914222 :       WB (t->decl_common.lang_flag_1);
    5835      4914222 :       WB (t->decl_common.lang_flag_2);
    5836      4914222 :       WB (t->decl_common.lang_flag_3);
    5837      4914222 :       WB (t->decl_common.lang_flag_4);
    5838              : 
    5839      4914222 :       {
    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      4914222 :         bool interface_known = t->decl_common.lang_flag_5;
    5844      4914222 :         if (interface_known
    5845      4914222 :             && get_importer_interface (t) == importer_interface::unknown)
    5846              :           interface_known = false;
    5847      4914222 :         WB (interface_known);
    5848              :       }
    5849              : 
    5850      4914222 :       WB (t->decl_common.lang_flag_6);
    5851      4914222 :       WB (t->decl_common.lang_flag_7);
    5852      4914222 :       WB (t->decl_common.lang_flag_8);
    5853      4914222 :       WB (t->decl_common.decl_flag_0);
    5854              : 
    5855      4914222 :       {
    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      4914222 :         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      4914222 :         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      4913742 :         if (!is_external
    5869      4131129 :             && VAR_OR_FUNCTION_DECL_P (t)
    5870      5313592 :             && get_importer_interface (t) == importer_interface::external)
    5871              :           is_external = true;
    5872      4914222 :         WB (is_external);
    5873              :       }
    5874              : 
    5875      4914222 :       WB (t->decl_common.decl_flag_2);
    5876      4914222 :       WB (t->decl_common.decl_flag_3);
    5877      4914222 :       WB (t->decl_common.not_gimple_reg_flag);
    5878      4914222 :       WB (t->decl_common.decl_by_reference_flag);
    5879      4914222 :       WB (t->decl_common.decl_read_flag);
    5880      4914222 :       WB (t->decl_common.decl_nonshareable_flag);
    5881      4914222 :       WB (t->decl_common.decl_not_flexarray);
    5882              :     }
    5883              :   else
    5884              :     return;
    5885              : 
    5886      4914222 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    5887              :     {
    5888      2313202 :       WB (t->decl_with_vis.defer_output);
    5889      2313202 :       WB (t->decl_with_vis.hard_register);
    5890      2313202 :       WB (t->decl_with_vis.common_flag);
    5891      2313202 :       WB (t->decl_with_vis.in_text_section);
    5892      2313202 :       WB (t->decl_with_vis.in_constant_pool);
    5893      2313202 :       WB (t->decl_with_vis.dllimport_flag);
    5894      2313202 :       WB (t->decl_with_vis.weak_flag);
    5895      2313202 :       WB (t->decl_with_vis.seen_in_bind_expr);
    5896      2313202 :       WB (t->decl_with_vis.comdat_flag);
    5897      2313202 :       WB (t->decl_with_vis.visibility_specified);
    5898      2313202 :       WB (t->decl_with_vis.init_priority_p);
    5899      2313202 :       WB (t->decl_with_vis.shadowed_for_var_p);
    5900      2313202 :       WB (t->decl_with_vis.cxx_constructor);
    5901      2313202 :       WB (t->decl_with_vis.cxx_destructor);
    5902      2313202 :       WB (t->decl_with_vis.final);
    5903      2313202 :       WB (t->decl_with_vis.regdecl_flag);
    5904              :     }
    5905              :   else
    5906              :     return;
    5907              : 
    5908      2313202 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    5909              :     {
    5910       680319 :       WB (t->function_decl.static_ctor_flag);
    5911       680319 :       WB (t->function_decl.static_dtor_flag);
    5912       680319 :       WB (t->function_decl.uninlinable);
    5913       680319 :       WB (t->function_decl.possibly_inlined);
    5914       680319 :       WB (t->function_decl.novops_flag);
    5915       680319 :       WB (t->function_decl.returns_twice_flag);
    5916       680319 :       WB (t->function_decl.malloc_flag);
    5917       680319 :       WB (t->function_decl.declared_inline_flag);
    5918       680319 :       WB (t->function_decl.no_inline_warning_flag);
    5919       680319 :       WB (t->function_decl.no_instrument_function_entry_exit);
    5920       680319 :       WB (t->function_decl.no_limit_stack);
    5921       680319 :       WB (t->function_decl.disregard_inline_limits);
    5922       680319 :       WB (t->function_decl.pure_flag);
    5923       680319 :       WB (t->function_decl.looping_const_or_pure_flag);
    5924              : 
    5925       680319 :       WB (t->function_decl.has_debug_args_flag);
    5926       680319 :       WB (t->function_decl.versioned_function);
    5927       680319 :       WB (t->function_decl.replaceable_operator);
    5928              : 
    5929              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    5930       680319 :       unsigned kind = (unsigned)t->function_decl.decl_type;
    5931       680319 :       WB ((kind >> 0) & 1);
    5932       680319 :       WB ((kind >> 1) & 1);
    5933              :     }
    5934              : #undef WB_IF
    5935              : #undef WB
    5936              : }
    5937              : 
    5938              : bool
    5939     15583449 : 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     15583449 :   tree_code code = TREE_CODE (t);
    5946              : 
    5947     15583449 :   RB (t->base.side_effects_flag);
    5948     15583449 :   RB (t->base.constant_flag);
    5949     15583449 :   RB (t->base.addressable_flag);
    5950     15583449 :   RB (t->base.volatile_flag);
    5951     15583449 :   RB (t->base.readonly_flag);
    5952              :   /* base.asm_written_flag is not streamed.  */
    5953     15583449 :   RB (t->base.nowarning_flag);
    5954              :   /* base.visited is not streamed.  */
    5955              :   /* base.used_flag is not streamed.  */
    5956     15583449 :   RB (t->base.nothrow_flag);
    5957     15583449 :   RB (t->base.static_flag);
    5958     15583449 :   RB_IF (TREE_CODE_CLASS (code) != tcc_type, t->base.public_flag);
    5959     15583449 :   RB (t->base.private_flag);
    5960     15583449 :   RB (t->base.protected_flag);
    5961     15583449 :   RB (t->base.deprecated_flag);
    5962     15583449 :   RB (t->base.default_def_flag);
    5963              : 
    5964     15583449 :   switch (code)
    5965              :     {
    5966      2045688 :     case CALL_EXPR:
    5967      2045688 :     case INTEGER_CST:
    5968      2045688 :     case SSA_NAME:
    5969      2045688 :     case TARGET_MEM_REF:
    5970      2045688 :     case TREE_VEC:
    5971              :       /* These use different base.u fields.  */
    5972      2045688 :       goto done;
    5973              : 
    5974     13537761 :     default:
    5975     13537761 :       RB (t->base.u.bits.lang_flag_0);
    5976     13537761 :       RB (t->base.u.bits.lang_flag_1);
    5977     13537761 :       RB (t->base.u.bits.lang_flag_2);
    5978     13537761 :       RB (t->base.u.bits.lang_flag_3);
    5979     13537761 :       RB (t->base.u.bits.lang_flag_4);
    5980     13537761 :       RB (t->base.u.bits.lang_flag_5);
    5981     13537761 :       RB (t->base.u.bits.lang_flag_6);
    5982     13537761 :       RB (t->base.u.bits.saturating_flag);
    5983     13537761 :       RB (t->base.u.bits.unsigned_flag);
    5984     13537761 :       RB (t->base.u.bits.packed_flag);
    5985     13537761 :       RB (t->base.u.bits.user_align);
    5986     13537761 :       RB (t->base.u.bits.nameless_flag);
    5987     13537761 :       RB (t->base.u.bits.atomic_flag);
    5988     13537761 :       RB (t->base.u.bits.unavailable_flag);
    5989     13537761 :       break;
    5990              :     }
    5991              : 
    5992     13537761 :   if (TREE_CODE_CLASS (code) == tcc_type)
    5993              :     {
    5994       591179 :       RB (t->type_common.no_force_blk_flag);
    5995       591179 :       RB (t->type_common.needs_constructing_flag);
    5996       591179 :       RB (t->type_common.transparent_aggr_flag);
    5997       591179 :       RB (t->type_common.restrict_flag);
    5998       591179 :       RB (t->type_common.string_flag);
    5999       591179 :       RB (t->type_common.lang_flag_0);
    6000       591179 :       RB (t->type_common.lang_flag_1);
    6001       591179 :       RB (t->type_common.lang_flag_2);
    6002       591179 :       RB (t->type_common.lang_flag_3);
    6003       591179 :       RB (t->type_common.lang_flag_4);
    6004       591179 :       RB (t->type_common.lang_flag_5);
    6005       591179 :       RB (t->type_common.lang_flag_6);
    6006       591179 :       RB (t->type_common.typeless_storage);
    6007              :     }
    6008              : 
    6009     13537761 :   if (TREE_CODE_CLASS (code) != tcc_declaration)
    6010     10216981 :     goto done;
    6011              : 
    6012      3320780 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6013              :     {
    6014      3320780 :       RB (t->decl_common.nonlocal_flag);
    6015      3320780 :       RB (t->decl_common.virtual_flag);
    6016      3320780 :       RB (t->decl_common.ignored_flag);
    6017      3320780 :       RB (t->decl_common.abstract_flag);
    6018      3320780 :       RB (t->decl_common.artificial_flag);
    6019      3320780 :       RB (t->decl_common.preserve_flag);
    6020      3320780 :       RB (t->decl_common.debug_expr_is_from);
    6021      3320780 :       RB (t->decl_common.lang_flag_0);
    6022      3320780 :       RB (t->decl_common.lang_flag_1);
    6023      3320780 :       RB (t->decl_common.lang_flag_2);
    6024      3320780 :       RB (t->decl_common.lang_flag_3);
    6025      3320780 :       RB (t->decl_common.lang_flag_4);
    6026      3320780 :       RB (t->decl_common.lang_flag_5);
    6027      3320780 :       RB (t->decl_common.lang_flag_6);
    6028      3320780 :       RB (t->decl_common.lang_flag_7);
    6029      3320780 :       RB (t->decl_common.lang_flag_8);
    6030      3320780 :       RB (t->decl_common.decl_flag_0);
    6031      3320780 :       RB (t->decl_common.decl_flag_1);
    6032      3320780 :       RB (t->decl_common.decl_flag_2);
    6033      3320780 :       RB (t->decl_common.decl_flag_3);
    6034      3320780 :       RB (t->decl_common.not_gimple_reg_flag);
    6035      3320780 :       RB (t->decl_common.decl_by_reference_flag);
    6036      3320780 :       RB (t->decl_common.decl_read_flag);
    6037      3320780 :       RB (t->decl_common.decl_nonshareable_flag);
    6038      3320780 :       RB (t->decl_common.decl_not_flexarray);
    6039              :     }
    6040              :   else
    6041            0 :     goto done;
    6042              : 
    6043      3320780 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6044              :     {
    6045      1536121 :       RB (t->decl_with_vis.defer_output);
    6046      1536121 :       RB (t->decl_with_vis.hard_register);
    6047      1536121 :       RB (t->decl_with_vis.common_flag);
    6048      1536121 :       RB (t->decl_with_vis.in_text_section);
    6049      1536121 :       RB (t->decl_with_vis.in_constant_pool);
    6050      1536121 :       RB (t->decl_with_vis.dllimport_flag);
    6051      1536121 :       RB (t->decl_with_vis.weak_flag);
    6052      1536121 :       RB (t->decl_with_vis.seen_in_bind_expr);
    6053      1536121 :       RB (t->decl_with_vis.comdat_flag);
    6054      1536121 :       RB (t->decl_with_vis.visibility_specified);
    6055      1536121 :       RB (t->decl_with_vis.init_priority_p);
    6056      1536121 :       RB (t->decl_with_vis.shadowed_for_var_p);
    6057      1536121 :       RB (t->decl_with_vis.cxx_constructor);
    6058      1536121 :       RB (t->decl_with_vis.cxx_destructor);
    6059      1536121 :       RB (t->decl_with_vis.final);
    6060      1536121 :       RB (t->decl_with_vis.regdecl_flag);
    6061              :     }
    6062              :   else
    6063      1784659 :     goto done;
    6064              : 
    6065      1536121 :   if (CODE_CONTAINS_STRUCT (code, TS_FUNCTION_DECL))
    6066              :     {
    6067       465431 :       RB (t->function_decl.static_ctor_flag);
    6068       465431 :       RB (t->function_decl.static_dtor_flag);
    6069       465431 :       RB (t->function_decl.uninlinable);
    6070       465431 :       RB (t->function_decl.possibly_inlined);
    6071       465431 :       RB (t->function_decl.novops_flag);
    6072       465431 :       RB (t->function_decl.returns_twice_flag);
    6073       465431 :       RB (t->function_decl.malloc_flag);
    6074       465431 :       RB (t->function_decl.declared_inline_flag);
    6075       465431 :       RB (t->function_decl.no_inline_warning_flag);
    6076       465431 :       RB (t->function_decl.no_instrument_function_entry_exit);
    6077       465431 :       RB (t->function_decl.no_limit_stack);
    6078       465431 :       RB (t->function_decl.disregard_inline_limits);
    6079       465431 :       RB (t->function_decl.pure_flag);
    6080       465431 :       RB (t->function_decl.looping_const_or_pure_flag);
    6081              : 
    6082       465431 :       RB (t->function_decl.has_debug_args_flag);
    6083       465431 :       RB (t->function_decl.versioned_function);
    6084       465431 :       RB (t->function_decl.replaceable_operator);
    6085              : 
    6086              :       /* decl_type is a (misnamed) 2 bit discriminator.  */
    6087       465431 :       unsigned kind = 0;
    6088       465431 :       kind |= unsigned (bits.b ()) << 0;
    6089       465431 :       kind |= unsigned (bits.b ()) << 1;
    6090       465431 :       t->function_decl.decl_type = function_decl_type (kind);
    6091              :     }
    6092              : #undef RB_IF
    6093              : #undef RB
    6094      1070690 : done:
    6095     15583449 :   return !get_overrun ();
    6096              : }
    6097              : 
    6098              : void
    6099      2949878 : trees_out::lang_decl_bools (tree t, bits_out& bits)
    6100              : {
    6101              : #define WB(X) (bits.b (X))
    6102      2949878 :   const struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6103              : 
    6104      2949878 :   bits.bflush ();
    6105      2949878 :   WB (lang->u.base.language == lang_cplusplus);
    6106      2949878 :   WB ((lang->u.base.use_template >> 0) & 1);
    6107      2949878 :   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      2949878 :   WB (lang->u.base.initialized_in_class);
    6111              : 
    6112      2949878 :   WB (lang->u.base.threadprivate_or_deleted_p);
    6113      2949878 :   WB (lang->u.base.anticipated_p);
    6114      2949878 :   WB (lang->u.base.friend_or_tls);
    6115      2949878 :   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      2949878 :   WB (lang->u.base.concept_p);
    6119      2949878 :   WB (lang->u.base.var_declared_inline_p);
    6120      2949878 :   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      5809480 :   WB (lang->u.base.module_purview_p && !header_module_p ());
    6126      2949878 :   WB (lang->u.base.module_attach_p);
    6127              :   /* Importer will set module_import_p and module_entity_p themselves
    6128              :      as appropriate.  */
    6129      2949878 :   WB (lang->u.base.module_keyed_decls_p);
    6130              : 
    6131      2949878 :   WB (lang->u.base.omp_declare_mapper_p);
    6132              : 
    6133      2949878 :   switch (lang->u.base.selector)
    6134              :     {
    6135            0 :     default:
    6136            0 :       gcc_unreachable ();
    6137              : 
    6138       680319 :     case lds_fn:  /* lang_decl_fn.  */
    6139       680319 :       WB (lang->u.fn.global_ctor_p);
    6140       680319 :       WB (lang->u.fn.global_dtor_p);
    6141              : 
    6142       680319 :       WB (lang->u.fn.static_function);
    6143       680319 :       WB (lang->u.fn.pure_virtual);
    6144       680319 :       WB (lang->u.fn.defaulted_p);
    6145       680319 :       WB (lang->u.fn.has_in_charge_parm_p);
    6146       680319 :       WB (lang->u.fn.has_vtt_parm_p);
    6147              :       /* There shouldn't be a pending inline at this point.  */
    6148       680319 :       gcc_assert (!lang->u.fn.pending_inline_p);
    6149       680319 :       WB (lang->u.fn.nonconverting);
    6150       680319 :       WB (lang->u.fn.thunk_p);
    6151              : 
    6152       680319 :       WB (lang->u.fn.this_thunk_p);
    6153       680319 :       WB (lang->u.fn.omp_declare_reduction_p);
    6154       680319 :       WB (lang->u.fn.has_dependent_explicit_spec_p);
    6155       680319 :       WB (lang->u.fn.immediate_fn_p);
    6156       680319 :       WB (lang->u.fn.maybe_deleted);
    6157       680319 :       WB (lang->u.fn.coroutine_p);
    6158       680319 :       WB (lang->u.fn.implicit_constexpr);
    6159       680319 :       WB (lang->u.fn.escalated_p);
    6160       680319 :       WB (lang->u.fn.xobj_func);
    6161       680319 :       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      2949878 :     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      2949878 : }
    6182              : 
    6183              : bool
    6184      2026404 : trees_in::lang_decl_bools (tree t, bits_in& bits)
    6185              : {
    6186              : #define RB(X) ((X) = bits.b ())
    6187      2026404 :   struct lang_decl *lang = DECL_LANG_SPECIFIC (t);
    6188              : 
    6189      2026404 :   bits.bflush ();
    6190      2026404 :   lang->u.base.language = bits.b () ? lang_cplusplus : lang_c;
    6191      2026404 :   unsigned v;
    6192      2026404 :   v = bits.b () << 0;
    6193      2026404 :   v |= bits.b () << 1;
    6194      2026404 :   lang->u.base.use_template = v;
    6195              :   /* lang->u.base.not_really_extern is not streamed.  */
    6196      2026404 :   RB (lang->u.base.initialized_in_class);
    6197              : 
    6198      2026404 :   RB (lang->u.base.threadprivate_or_deleted_p);
    6199      2026404 :   RB (lang->u.base.anticipated_p);
    6200      2026404 :   RB (lang->u.base.friend_or_tls);
    6201      2026404 :   RB (lang->u.base.unknown_bound_p);
    6202              :   /* lang->u.base.odr_used is not streamed.  */
    6203      2026404 :   RB (lang->u.base.concept_p);
    6204      2026404 :   RB (lang->u.base.var_declared_inline_p);
    6205      2026404 :   RB (lang->u.base.dependent_init_p);
    6206              : 
    6207      2026404 :   RB (lang->u.base.module_purview_p);
    6208      2026404 :   RB (lang->u.base.module_attach_p);
    6209              :   /* module_import_p and module_entity_p are not streamed.  */
    6210      2026404 :   RB (lang->u.base.module_keyed_decls_p);
    6211              : 
    6212      2026404 :   RB (lang->u.base.omp_declare_mapper_p);
    6213              : 
    6214      2026404 :   switch (lang->u.base.selector)
    6215              :     {
    6216            0 :     default:
    6217            0 :       gcc_unreachable ();
    6218              : 
    6219       465431 :     case lds_fn:  /* lang_decl_fn.  */
    6220       465431 :       RB (lang->u.fn.global_ctor_p);
    6221       465431 :       RB (lang->u.fn.global_dtor_p);
    6222              : 
    6223       465431 :       RB (lang->u.fn.static_function);
    6224       465431 :       RB (lang->u.fn.pure_virtual);
    6225       465431 :       RB (lang->u.fn.defaulted_p);
    6226       465431 :       RB (lang->u.fn.has_in_charge_parm_p);
    6227       465431 :       RB (lang->u.fn.has_vtt_parm_p);
    6228              :       /* lang->u.f.n.pending_inline_p is not streamed.  */
    6229       465431 :       RB (lang->u.fn.nonconverting);
    6230       465431 :       RB (lang->u.fn.thunk_p);
    6231              : 
    6232       465431 :       RB (lang->u.fn.this_thunk_p);
    6233       465431 :       RB (lang->u.fn.omp_declare_reduction_p);
    6234       465431 :       RB (lang->u.fn.has_dependent_explicit_spec_p);
    6235       465431 :       RB (lang->u.fn.immediate_fn_p);
    6236       465431 :       RB (lang->u.fn.maybe_deleted);
    6237       465431 :       RB (lang->u.fn.coroutine_p);
    6238       465431 :       RB (lang->u.fn.implicit_constexpr);
    6239       465431 :       RB (lang->u.fn.escalated_p);
    6240       465431 :       RB (lang->u.fn.xobj_func);
    6241       465431 :       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      2026404 :     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      2026404 :   return !get_overrun ();
    6262              : }
    6263              : 
    6264              : void
    6265       233709 : trees_out::lang_type_bools (tree t, bits_out& bits)
    6266              : {
    6267              : #define WB(X) (bits.b (X))
    6268       233709 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6269              : 
    6270       233709 :   bits.bflush ();
    6271       233709 :   WB (lang->has_type_conversion);
    6272       233709 :   WB (lang->has_copy_ctor);
    6273       233709 :   WB (lang->has_default_ctor);
    6274       233709 :   WB (lang->const_needs_init);
    6275       233709 :   WB (lang->ref_needs_init);
    6276       233709 :   WB (lang->has_const_copy_assign);
    6277       233709 :   WB ((lang->use_template >> 0) & 1);
    6278       233709 :   WB ((lang->use_template >> 1) & 1);
    6279              : 
    6280       233709 :   WB (lang->has_mutable);
    6281       233709 :   WB (lang->com_interface);
    6282       233709 :   WB (lang->non_pod_class);
    6283       233709 :   WB (lang->nearly_empty_p);
    6284       233709 :   WB (lang->user_align);
    6285       233709 :   WB (lang->has_copy_assign);
    6286       233709 :   WB (lang->has_new);
    6287       233709 :   WB (lang->has_array_new);
    6288              : 
    6289       233709 :   WB ((lang->gets_delete >> 0) & 1);
    6290       233709 :   WB ((lang->gets_delete >> 1) & 1);
    6291       233709 :   WB (lang->interface_only);
    6292       233709 :   WB (lang->interface_unknown);
    6293       233709 :   WB (lang->contains_empty_class_p);
    6294       233709 :   WB (lang->anon_aggr);
    6295       233709 :   WB (lang->non_zero_init);
    6296       233709 :   WB (lang->empty_p);
    6297              : 
    6298       233709 :   WB (lang->vec_new_uses_cookie);
    6299       233709 :   WB (lang->declared_class);
    6300       233709 :   WB (lang->diamond_shaped);
    6301       233709 :   WB (lang->repeated_base);
    6302       233709 :   gcc_checking_assert (!lang->being_defined);
    6303              :   // lang->debug_requested
    6304       233709 :   WB (lang->fields_readonly);
    6305       233709 :   WB (lang->ptrmemfunc_flag);
    6306              : 
    6307       233709 :   WB (lang->lazy_default_ctor);
    6308       233709 :   WB (lang->lazy_copy_ctor);
    6309       233709 :   WB (lang->lazy_copy_assign);
    6310       233709 :   WB (lang->lazy_destructor);
    6311       233709 :   WB (lang->has_const_copy_ctor);
    6312       233709 :   WB (lang->has_complex_copy_ctor);
    6313       233709 :   WB (lang->has_complex_copy_assign);
    6314       233709 :   WB (lang->non_aggregate);
    6315              : 
    6316       233709 :   WB (lang->has_complex_dflt);
    6317       233709 :   WB (lang->has_list_ctor);
    6318       233709 :   WB (lang->non_std_layout);
    6319       233709 :   WB (lang->is_literal);
    6320       233709 :   WB (lang->lazy_move_ctor);
    6321       233709 :   WB (lang->lazy_move_assign);
    6322       233709 :   WB (lang->has_complex_move_ctor);
    6323       233709 :   WB (lang->has_complex_move_assign);
    6324              : 
    6325       233709 :   WB (lang->has_constexpr_ctor);
    6326       233709 :   WB (lang->unique_obj_representations);
    6327       233709 :   WB (lang->unique_obj_representations_set);
    6328       233709 :   gcc_checking_assert (!lang->erroneous);
    6329       233709 :   WB (lang->non_pod_aggregate);
    6330       233709 :   WB (lang->non_aggregate_pod);
    6331              : #undef WB
    6332       233709 : }
    6333              : 
    6334              : bool
    6335       153533 : trees_in::lang_type_bools (tree t, bits_in& bits)
    6336              : {
    6337              : #define RB(X) ((X) = bits.b ())
    6338       153533 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    6339              : 
    6340       153533 :   bits.bflush ();
    6341       153533 :   RB (lang->has_type_conversion);
    6342       153533 :   RB (lang->has_copy_ctor);
    6343       153533 :   RB (lang->has_default_ctor);
    6344       153533 :   RB (lang->const_needs_init);
    6345       153533 :   RB (lang->ref_needs_init);
    6346       153533 :   RB (lang->has_const_copy_assign);
    6347       153533 :   unsigned v;
    6348       153533 :   v = bits.b () << 0;
    6349       153533 :   v |= bits.b () << 1;
    6350       153533 :   lang->use_template = v;
    6351              : 
    6352       153533 :   RB (lang->has_mutable);
    6353       153533 :   RB (lang->com_interface);
    6354       153533 :   RB (lang->non_pod_class);
    6355       153533 :   RB (lang->nearly_empty_p);
    6356       153533 :   RB (lang->user_align);
    6357       153533 :   RB (lang->has_copy_assign);
    6358       153533 :   RB (lang->has_new);
    6359       153533 :   RB (lang->has_array_new);
    6360              : 
    6361       153533 :   v = bits.b () << 0;
    6362       153533 :   v |= bits.b () << 1;
    6363       153533 :   lang->gets_delete = v;
    6364       153533 :   RB (lang->interface_only);
    6365       153533 :   RB (lang->interface_unknown);
    6366       153533 :   RB (lang->contains_empty_class_p);
    6367       153533 :   RB (lang->anon_aggr);
    6368       153533 :   RB (lang->non_zero_init);
    6369       153533 :   RB (lang->empty_p);
    6370              : 
    6371       153533 :   RB (lang->vec_new_uses_cookie);
    6372       153533 :   RB (lang->declared_class);
    6373       153533 :   RB (lang->diamond_shaped);
    6374       153533 :   RB (lang->repeated_base);
    6375       153533 :   gcc_checking_assert (!lang->being_defined);
    6376       153533 :   gcc_checking_assert (!lang->debug_requested);
    6377       153533 :   RB (lang->fields_readonly);
    6378       153533 :   RB (lang->ptrmemfunc_flag);
    6379              : 
    6380       153533 :   RB (lang->lazy_default_ctor);
    6381       153533 :   RB (lang->lazy_copy_ctor);
    6382       153533 :   RB (lang->lazy_copy_assign);
    6383       153533 :   RB (lang->lazy_destructor);
    6384       153533 :   RB (lang->has_const_copy_ctor);
    6385       153533 :   RB (lang->has_complex_copy_ctor);
    6386       153533 :   RB (lang->has_complex_copy_assign);
    6387       153533 :   RB (lang->non_aggregate);
    6388              : 
    6389       153533 :   RB (lang->has_complex_dflt);
    6390       153533 :   RB (lang->has_list_ctor);
    6391       153533 :   RB (lang->non_std_layout);
    6392       153533 :   RB (lang->is_literal);
    6393       153533 :   RB (lang->lazy_move_ctor);
    6394       153533 :   RB (lang->lazy_move_assign);
    6395       153533 :   RB (lang->has_complex_move_ctor);
    6396       153533 :   RB (lang->has_complex_move_assign);
    6397              : 
    6398       153533 :   RB (lang->has_constexpr_ctor);
    6399       153533 :   RB (lang->unique_obj_representations);
    6400       153533 :   RB (lang->unique_obj_representations_set);
    6401       153533 :   gcc_checking_assert (!lang->erroneous);
    6402       153533 :   RB (lang->non_pod_aggregate);
    6403       153533 :   RB (lang->non_aggregate_pod);
    6404              : #undef RB
    6405       153533 :   return !get_overrun ();
    6406              : }
    6407              : 
    6408              : /* Read & write the core values and pointers.  */
    6409              : 
    6410              : void
    6411     59290697 : trees_out::core_vals (tree t)
    6412              : {
    6413              : #define WU(X) (u (X))
    6414              : #define WT(X) (tree_node (X))
    6415     59290697 :   tree_code code = TREE_CODE (t);
    6416              : 
    6417              :   /* First by shape of the tree.  */
    6418              : 
    6419     59290697 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    6420              :     {
    6421              :       /* Write this early, for better log information.  */
    6422     12900480 :       WT (t->decl_minimal.name);
    6423     12900480 :       if (!DECL_TEMPLATE_PARM_P (t))
    6424      9961548 :         WT (t->decl_minimal.context);
    6425              : 
    6426     12900480 :       if (state)
    6427     10614984 :         state->write_location (*this, t->decl_minimal.locus);
    6428              : 
    6429     12900480 :       if (streaming_p ())
    6430      4914222 :         if (has_warning_spec (t))
    6431         1023 :           u (get_warning_spec (t));
    6432              :     }
    6433              : 
    6434     59290697 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    6435              :     {
    6436              :       /* The only types we write also have TYPE_NON_COMMON.  */
    6437      3177170 :       gcc_checking_assert (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON));
    6438              : 
    6439              :       /* We only stream the main variant.  */
    6440      3177170 :       gcc_checking_assert (TYPE_MAIN_VARIANT (t) == t);
    6441              : 
    6442              :       /* Stream the name & context first, for better log information  */
    6443      3177170 :       WT (t->type_common.name);
    6444      3177170 :       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      3177170 :       WT (t->type_common.main_variant);
    6450              : 
    6451      3177170 :       tree canonical = t->type_common.canonical;
    6452      3177170 :       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      3177170 :       WT (canonical);
    6457              : 
    6458              :       /* type_common.next_variant is internally manipulated.  */
    6459              :       /* type_common.pointer_to, type_common.reference_to.  */
    6460              : 
    6461      3177170 :       if (streaming_p ())
    6462              :         {
    6463       861361 :           WU (t->type_common.precision);
    6464       861361 :           WU (t->type_common.contains_placeholder_bits);
    6465       861361 :           WU (t->type_common.mode);
    6466       861361 :           WU (t->type_common.align);
    6467              :         }
    6468              : 
    6469      3177170 :       if (!RECORD_OR_UNION_CODE_P (code))
    6470              :         {
    6471      2643504 :           WT (t->type_common.size);
    6472      2643504 :           WT (t->type_common.size_unit);
    6473              :         }
    6474      3177170 :       WT (t->type_common.attributes);
    6475              : 
    6476      3177170 :       WT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    6477              :     }
    6478              : 
    6479     59290697 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    6480              :     {
    6481     12900480 :       if (streaming_p ())
    6482              :         {
    6483      4914222 :           WU (t->decl_common.mode);
    6484      4914222 :           WU (t->decl_common.off_align);
    6485      4914222 :           WU (t->decl_common.align);
    6486              :         }
    6487              : 
    6488              :       /* For templates these hold instantiation (partial and/or
    6489              :          specialization) information.  */
    6490     12900480 :       if (code != TEMPLATE_DECL)
    6491              :         {
    6492     11926737 :           WT (t->decl_common.size);
    6493     11926737 :           WT (t->decl_common.size_unit);
    6494              :         }
    6495              : 
    6496     12900480 :       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     12900480 :       WT (t->decl_common.abstract_origin);
    6503              :     }
    6504              : 
    6505     59290697 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    6506              :     {
    6507      6103410 :       WT (t->decl_with_vis.assembler_name);
    6508      6103410 :       if (streaming_p ())
    6509      2313202 :         WU (t->decl_with_vis.visibility);
    6510              :     }
    6511              : 
    6512     59290697 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    6513              :     {
    6514      3177170 :       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      3166934 :       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      2633268 :           gcc_checking_assert (code == TEMPLATE_TYPE_PARM
    6529              :                                || code == TEMPLATE_TEMPLATE_PARM
    6530              :                                || code == BOUND_TEMPLATE_TEMPLATE_PARM);
    6531      2633268 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    6532      2633268 :           WT (t->type_non_common.values);
    6533      2633268 :           WT (t->type_non_common.maxval);
    6534      2633268 :           WT (t->type_non_common.minval);
    6535              :         }
    6536              : 
    6537      3177170 :       WT (t->type_non_common.lang_1);
    6538              :     }
    6539              : 
    6540     59290697 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    6541              :     {
    6542     18234780 :       if (state)
    6543     17880480 :         state->write_location (*this, t->exp.locus);
    6544              : 
    6545     18234780 :       if (streaming_p ())
    6546      8859179 :         if (has_warning_spec (t))
    6547       698062 :           u (get_warning_spec (t));
    6548              : 
    6549     18234780 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    6550     18234780 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    6551     18234780 :                         : TREE_OPERAND_LENGTH (t));
    6552     18234780 :       unsigned ix = unsigned (vl);
    6553     50166172 :       for (; ix != limit; ix++)
    6554     31931392 :         WT (TREE_OPERAND (t, ix));
    6555              :     }
    6556     41055917 :   else if (code == REQUIRES_EXPR)
    6557              :     {
    6558        25737 :       if (state)
    6559        21711 :         state->write_location (*this, REQUIRES_EXPR_LOCATION (t));
    6560              : 
    6561        25737 :       if (streaming_p ())
    6562        10583 :         if (has_warning_spec (t))
    6563            0 :           u (get_warning_spec (t));
    6564              : 
    6565        25737 :       chained_decls (REQUIRES_EXPR_PARMS (t));
    6566        25737 :       WT (REQUIRES_EXPR_REQS (t));
    6567        25737 :       WT (REQUIRES_EXPR_EXTRA_ARGS (t));
    6568              :     }
    6569              :   else
    6570              :     /* The CODE_CONTAINS tables were inaccurate when I started.  */
    6571     41030180 :     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     59290697 :   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      5379895 :     case INTEGER_CST:
    6608      5379895 :       if (streaming_p ())
    6609              :         {
    6610       903982 :           unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    6611      1811474 :           for (unsigned ix = 0; ix != num; ix++)
    6612       907492 :             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       730875 :     case VAR_DECL:
    6654       730875 :       if (DECL_CONTEXT (t)
    6655       730875 :           && 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      5901326 :     case RESULT_DECL:
    6664      5901326 :     case PARM_DECL:
    6665      5901326 :       if (DECL_HAS_VALUE_EXPR_P (t))
    6666        58257 :         WT (DECL_VALUE_EXPR (t));
    6667              :       /* FALLTHROUGH  */
    6668              : 
    6669      6150947 :     case CONST_DECL:
    6670      6150947 :     case IMPORTED_DECL:
    6671      6150947 :       WT (t->decl_common.initial);
    6672      6150947 :       break;
    6673              : 
    6674       178913 :     case FIELD_DECL:
    6675       178913 :       WT (t->field_decl.offset);
    6676       178913 :       WT (t->field_decl.bit_field_type);
    6677       178913 :       {
    6678       178913 :         auto ovr = make_temp_override (walking_bit_field_unit, true);
    6679       178913 :         WT (t->field_decl.qualifier); /* bitfield unit.  */
    6680       178913 :       }
    6681       178913 :       WT (t->field_decl.bit_offset);
    6682       178913 :       WT (t->field_decl.fcontext);
    6683       178913 :       WT (t->decl_common.initial);
    6684       178913 :       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      1360852 :     case FUNCTION_DECL:
    6695      1360852 :       if (streaming_p ())
    6696              :         {
    6697              :           /* Builtins can be streamed by value when a header declares
    6698              :              them.  */
    6699       680319 :           WU (DECL_BUILT_IN_CLASS (t));
    6700       680319 :           if (DECL_BUILT_IN_CLASS (t) != NOT_BUILT_IN)
    6701        13203 :             WU (DECL_UNCHECKED_FUNCTION_CODE (t));
    6702              :         }
    6703              : 
    6704      1360852 :       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      1360852 :       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      1360852 :       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      1360852 :       WT (t->function_decl.vindex);
    6714              : 
    6715      1360852 :       if (DECL_HAS_DEPENDENT_EXPLICIT_SPEC_P (t))
    6716         9540 :         WT (lookup_explicit_specifier (t));
    6717              :       break;
    6718              : 
    6719       163897 :     case USING_DECL:
    6720              :       /* USING_DECL_DECLS  */
    6721       163897 :       WT (t->decl_common.initial);
    6722              :       /* FALLTHROUGH  */
    6723              : 
    6724      4011218 :     case TYPE_DECL:
    6725              :       /* USING_DECL: USING_DECL_SCOPE  */
    6726              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    6727      4011218 :       WT (t->decl_non_common.result);
    6728      4011218 :       break;
    6729              : 
    6730              :       /* Miscellaneous common nodes.  */
    6731       905994 :     case BLOCK:
    6732       905994 :       if (state)
    6733              :         {
    6734       905994 :           state->write_location (*this, t->block.locus);
    6735       905994 :           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      1389100 :       for (tree decls = t->block.vars; decls; decls = DECL_CHAIN (decls))
    6741              :         {
    6742       483106 :           if (VAR_OR_FUNCTION_DECL_P (decls)
    6743       483106 :               && 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       483106 :           tree_node (decls);
    6752              :         }
    6753       905994 :       tree_node (NULL_TREE);
    6754              : 
    6755              :       /* nonlocalized_vars is a middle-end thing.  */
    6756       905994 :       WT (t->block.subblocks);
    6757       905994 :       WT (t->block.supercontext);
    6758              :       // FIXME: As for decl's abstract_origin, does this introduce crosslinks?
    6759       905994 :       WT (t->block.abstract_origin);
    6760              :       /* fragment_origin, fragment_chain are middle-end things.  */
    6761       905994 :       WT (t->block.chain);
    6762              :       /* nonlocalized_vars, block_num & die are middle endy/debug
    6763              :          things.  */
    6764       905994 :       break;
    6765              : 
    6766      1798100 :     case CALL_EXPR:
    6767      1798100 :       if (streaming_p ())
    6768       860973 :         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       771326 :     case STATEMENT_LIST:
    6792      3239505 :       for (tree stmt : tsi_range (t))
    6793      2468179 :         if (stmt)
    6794      2468179 :           WT (stmt);
    6795       771326 :       WT (NULL_TREE);
    6796       771326 :       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       298728 :       break;
    6810              : 
    6811       298728 :     case TREE_BINFO:
    6812       298728 :       {
    6813       298728 :         WT (t->binfo.common.chain);
    6814       298728 :         WT (t->binfo.offset);
    6815       298728 :         WT (t->binfo.inheritance);
    6816       298728 :         WT (t->binfo.vptr_field);
    6817              : 
    6818       298728 :         WT (t->binfo.vtable);
    6819       298728 :         WT (t->binfo.virtuals);
    6820       298728 :         WT (t->binfo.vtt_subvtt);
    6821       298728 :         WT (t->binfo.vtt_vptr);
    6822              : 
    6823       298728 :         tree_vec (BINFO_BASE_ACCESSES (t));
    6824       298728 :         unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    6825       381048 :         for (unsigned ix = 0; ix != num; ix++)
    6826        82320 :           WT (BINFO_BASE_BINFO (t, ix));
    6827              :       }
    6828              :       break;
    6829              : 
    6830      4466608 :     case TREE_LIST:
    6831      4466608 :       WT (t->list.purpose);
    6832      4466608 :       WT (t->list.value);
    6833      4466608 :       WT (t->list.common.chain);
    6834      4466608 :       break;
    6835              : 
    6836      3824490 :     case TREE_VEC:
    6837     10625042 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    6838      6800552 :         WT (TREE_VEC_ELT (t, ix));
    6839              :       /* We stash NON_DEFAULT_TEMPLATE_ARGS_COUNT on TREE_CHAIN!  */
    6840      3824490 :       gcc_checking_assert (!t->type_common.common.chain
    6841              :                            || (TREE_CODE (t->type_common.common.chain)
    6842              :                                == INTEGER_CST));
    6843      3824490 :       WT (t->type_common.common.chain);
    6844      3824490 :       break;
    6845              : 
    6846              :       /* C++-specific nodes ...  */
    6847       313806 :     case BASELINK:
    6848       313806 :       WT (((lang_tree_node *)t)->baselink.binfo);
    6849       313806 :       WT (((lang_tree_node *)t)->baselink.functions);
    6850       313806 :       WT (((lang_tree_node *)t)->baselink.access_binfo);
    6851       313806 :       WT (((lang_tree_node *)t)->baselink.common.chain);
    6852       313806 :       break;
    6853              : 
    6854       143506 :     case CONSTRAINT_INFO:
    6855       143506 :       WT (((lang_tree_node *)t)->constraint_info.template_reqs);
    6856       143506 :       WT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    6857       143506 :       WT (((lang_tree_node *)t)->constraint_info.associated_constr);
    6858       143506 :       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      3350878 :     case OVERLOAD:
    6885      3350878 :       WT (((lang_tree_node *)t)->overload.function);
    6886      3350878 :       WT (t->common.chain);
    6887      3350878 :       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        25159 :     case STATIC_ASSERT:
    6896        25159 :       WT (((lang_tree_node *)t)->static_assertion.condition);
    6897        25159 :       WT (((lang_tree_node *)t)->static_assertion.message);
    6898        25159 :       if (state)
    6899        25159 :         state->write_location
    6900        25159 :           (*this, ((lang_tree_node *)t)->static_assertion.location);
    6901              :       break;
    6902              : 
    6903       973743 :     case TEMPLATE_DECL:
    6904              :       /* Streamed with the template_decl node itself.  */
    6905       973743 :       gcc_checking_assert
    6906              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.arguments));
    6907       973743 :       gcc_checking_assert
    6908              :         (TREE_VISITED (((lang_tree_node *)t)->template_decl.result));
    6909       973743 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    6910        19438 :         WT (DECL_CHAIN (t));
    6911              :       break;
    6912              : 
    6913      2210058 :     case TEMPLATE_INFO:
    6914      2210058 :       {
    6915      2210058 :         WT (((lang_tree_node *)t)->template_info.tmpl);
    6916      2210058 :         WT (((lang_tree_node *)t)->template_info.args);
    6917      2210058 :         WT (((lang_tree_node *)t)->template_info.partial);
    6918              : 
    6919      2210058 :         const auto *ac = (((lang_tree_node *)t)
    6920              :                           ->template_info.deferred_access_checks);
    6921      2210058 :         unsigned len = vec_safe_length (ac);
    6922      2210058 :         if (streaming_p ())
    6923      1102762 :           u (len);
    6924      2210058 :         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      2831003 :     case TEMPLATE_PARM_INDEX:
    6940      2831003 :       if (streaming_p ())
    6941              :         {
    6942       626036 :           WU (((lang_tree_node *)t)->tpi.index);
    6943       626036 :           WU (((lang_tree_node *)t)->tpi.level);
    6944       626036 :           WU (((lang_tree_node *)t)->tpi.orig_level);
    6945              :         }
    6946      2831003 :       WT (((lang_tree_node *)t)->tpi.decl);
    6947              :       /* TEMPLATE_PARM_DESCENDANTS (AKA TREE_CHAIN) is an internal
    6948              :          cache, do not stream.  */
    6949      2831003 :       break;
    6950              : 
    6951        32614 :     case TRAIT_EXPR:
    6952        32614 :       WT (((lang_tree_node *)t)->trait_expression.type1);
    6953        32614 :       WT (((lang_tree_node *)t)->trait_expression.type2);
    6954        32614 :       if (state)
    6955        25738 :         state->write_location
    6956        25738 :           (*this, ((lang_tree_node *)t)->trait_expression.locus);
    6957        32614 :       if (streaming_p ())
    6958        12687 :         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     59290697 :   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     55983326 :       tree type = t->typed.type;
    6980     55983326 :       unsigned prec = 0;
    6981              : 
    6982     55983326 :       switch (code)
    6983              :         {
    6984              :         default:
    6985              :           break;
    6986              : 
    6987              :         case TEMPLATE_DECL:
    6988              :           /* We fill in the template's type separately.  */
    6989     55983326 :           type = NULL_TREE;
    6990              :           break;
    6991              : 
    6992      3847321 :         case TYPE_DECL:
    6993      3847321 :           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     55983326 :       WT (type);
    7019     55983326 :       if (prec && streaming_p ())
    7020         2985 :         WU (prec);
    7021              :     }
    7022              : 
    7023     59290697 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7024              :     {
    7025       153978 :       unsigned len = vec_safe_length (t->constructor.elts);
    7026       153978 :       if (streaming_p ())
    7027        75913 :         WU (len);
    7028       153978 :       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     59290697 : }
    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     15559574 : 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     15559574 :   tree_code code = TREE_CODE (t);
    7055              : 
    7056              :   /* First by tree shape.  */
    7057     15559574 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_MINIMAL))
    7058              :     {
    7059      3320780 :       RT (t->decl_minimal.name);
    7060      3320780 :       if (!DECL_TEMPLATE_PARM_P (t))
    7061      2887502 :         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      3320780 :       t->decl_minimal.locus = state->read_location (*this);
    7066      3320780 :       if (has_warning_spec (t))
    7067          629 :         put_warning_spec (t, u ());
    7068              :     }
    7069              : 
    7070     15559574 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_COMMON))
    7071              :     {
    7072       567304 :       RT (t->type_common.name);
    7073       567304 :       RT (t->type_common.context);
    7074              : 
    7075       567304 :       RT (t->type_common.main_variant);
    7076       567304 :       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       567304 :       RU (t->type_common.precision);
    7082       567304 :       RU (t->type_common.contains_placeholder_bits);
    7083       567304 :       RUC (machine_mode, t->type_common.mode);
    7084       567304 :       RU (t->type_common.align);
    7085              : 
    7086       567304 :       if (!RECORD_OR_UNION_CODE_P (code))
    7087              :         {
    7088       393144 :           RT (t->type_common.size);
    7089       393144 :           RT (t->type_common.size_unit);
    7090              :         }
    7091       567304 :       RT (t->type_common.attributes);
    7092              : 
    7093       567304 :       RT (t->type_common.common.chain); /* TYPE_STUB_DECL.  */
    7094              :     }
    7095              : 
    7096     15559574 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_COMMON))
    7097              :     {
    7098      3320780 :       RUC (machine_mode, t->decl_common.mode);
    7099      3320780 :       RU (t->decl_common.off_align);
    7100      3320780 :       RU (t->decl_common.align);
    7101              : 
    7102      3320780 :       if (code != TEMPLATE_DECL)
    7103              :         {
    7104      2991057 :           RT (t->decl_common.size);
    7105      2991057 :           RT (t->decl_common.size_unit);
    7106              :         }
    7107              : 
    7108      3320780 :       RT (t->decl_common.attributes);
    7109      3320780 :       RT (t->decl_common.abstract_origin);
    7110              :     }
    7111              : 
    7112     15559574 :   if (CODE_CONTAINS_STRUCT (code, TS_DECL_WITH_VIS))
    7113              :     {
    7114      1536121 :       RT (t->decl_with_vis.assembler_name);
    7115      1536121 :       RUC (symbol_visibility, t->decl_with_vis.visibility);
    7116              :     }
    7117              : 
    7118     15559574 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPE_NON_COMMON))
    7119              :     {
    7120       567304 :       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       564020 :       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       389860 :           gcc_checking_assert (!TYPE_CACHED_VALUES_P (t));
    7135       389860 :           RT (t->type_non_common.values);
    7136       389860 :           RT (t->type_non_common.maxval);
    7137       389860 :           RT (t->type_non_common.minval);
    7138              :         }
    7139              : 
    7140       567304 :       RT (t->type_non_common.lang_1);
    7141              :     }
    7142              : 
    7143     15559574 :   if (CODE_CONTAINS_STRUCT (code, TS_EXP))
    7144              :     {
    7145      6148530 :       t->exp.locus = state->read_location (*this);
    7146      6148530 :       if (has_warning_spec (t))
    7147       483831 :         put_warning_spec (t, u ());
    7148              : 
    7149      6148530 :       bool vl = TREE_CODE_CLASS (code) == tcc_vl_exp;
    7150      6148530 :       unsigned limit = (vl ? VL_EXP_OPERAND_LENGTH (t)
    7151      6148530 :                         : TREE_OPERAND_LENGTH (t));
    7152      6148530 :       unsigned ix = unsigned (vl);
    7153     16847829 :       for (; ix != limit; ix++)
    7154     10699299 :         RTU (TREE_OPERAND (t, ix));
    7155              :     }
    7156      9411044 :   else if (code == REQUIRES_EXPR)
    7157              :     {
    7158         7673 :       REQUIRES_EXPR_LOCATION (t) = state->read_location (*this);
    7159         7673 :       if (has_warning_spec (t))
    7160            0 :         put_warning_spec (t, u ());
    7161              : 
    7162         7673 :       REQUIRES_EXPR_PARMS (t) = chained_decls ();
    7163         7673 :       RTU (REQUIRES_EXPR_REQS (t));
    7164         7673 :       RTU (REQUIRES_EXPR_EXTRA_ARGS (t));
    7165              :     }
    7166              : 
    7167              :   /* Then by CODE.  Special cases and/or 1:1 tree shape
    7168              :      correspondence. */
    7169     15559574 :   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       605648 :     case INTEGER_CST:
    7194       605648 :       {
    7195       605648 :         unsigned num = TREE_INT_CST_EXT_NUNITS (t);
    7196      1213592 :         for (unsigned ix = 0; ix != num; ix++)
    7197       607944 :           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       234882 :     case VAR_DECL:
    7229       234882 :       if (DECL_CONTEXT (t)
    7230       234882 :           && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
    7231              :         {
    7232        43975 :           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      1517537 :     case RESULT_DECL:
    7242      1517537 :     case PARM_DECL:
    7243      1517537 :       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      1562660 :     case CONST_DECL:
    7254      1562660 :     case IMPORTED_DECL:
    7255      1562660 :       RT (t->decl_common.initial);
    7256      1562660 :       break;
    7257              : 
    7258        57145 :     case FIELD_DECL:
    7259        57145 :       RT (t->field_decl.offset);
    7260        57145 :       RT (t->field_decl.bit_field_type);
    7261        57145 :       RT (t->field_decl.qualifier);
    7262        57145 :       RT (t->field_decl.bit_offset);
    7263        57145 :       RT (t->field_decl.fcontext);
    7264        57145 :       RT (t->decl_common.initial);
    7265        57145 :       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       465431 :     case FUNCTION_DECL:
    7273       465431 :       {
    7274       465431 :         unsigned bltin = u ();
    7275       465431 :         t->function_decl.built_in_class = built_in_class (bltin);
    7276       465431 :         if (bltin != NOT_BUILT_IN)
    7277              :           {
    7278         9489 :             bltin = u ();
    7279         9489 :             DECL_UNCHECKED_FUNCTION_CODE (t) = built_in_function (bltin);
    7280              :           }
    7281              : 
    7282       465431 :         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       465431 :         RT (t->function_decl.vindex);
    7288              : 
    7289       465431 :         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        51942 :     case USING_DECL:
    7299              :       /* USING_DECL_DECLS  */
    7300        51942 :       RT (t->decl_common.initial);
    7301              :       /* FALLTHROUGH  */
    7302              : 
    7303       835640 :     case TYPE_DECL:
    7304              :       /* USING_DECL: USING_DECL_SCOPE  */
    7305              :       /* TYPE_DECL: DECL_ORIGINAL_TYPE */
    7306       835640 :       RT (t->decl_non_common.result);
    7307       835640 :       break;
    7308              : 
    7309              :       /* Miscellaneous common nodes.  */
    7310       314477 :     case BLOCK:
    7311       314477 :       t->block.locus = state->read_location (*this);
    7312       314477 :       t->block.end_locus = state->read_location (*this);
    7313              : 
    7314       314477 :       for (tree *chain = &t->block.vars;;)
    7315       477589 :         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       163112 :             decl = maybe_duplicate (decl);
    7324              : 
    7325       163112 :             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       163112 :             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       163112 :             *chain = decl;
    7343       163112 :             chain = &DECL_CHAIN (decl);
    7344              :           }
    7345              :         else
    7346       163112 :           break;
    7347              : 
    7348              :       /* nonlocalized_vars is middle-end.  */
    7349       314477 :       RT (t->block.subblocks);
    7350       314477 :       RT (t->block.supercontext);
    7351       314477 :       RT (t->block.abstract_origin);
    7352              :       /* fragment_origin, fragment_chain are middle-end.  */
    7353       314477 :       RT (t->block.chain);
    7354              :       /* nonlocalized_vars, block_num, die are middle endy/debug
    7355              :          things.  */
    7356       314477 :       break;
    7357              : 
    7358       591982 :     case CALL_EXPR:
    7359       591982 :       RUC (internal_fn, t->base.u.ifn);
    7360       591982 :       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       274173 :     case STATEMENT_LIST:
    7378       274173 :       {
    7379       274173 :         tree_stmt_iterator iter = tsi_start (t);
    7380      1416167 :         for (tree stmt; RT (stmt);)
    7381              :           {
    7382       867821 :             if (TREE_CODE (stmt) == DEBUG_BEGIN_STMT
    7383       148800 :                 && !MAY_HAVE_DEBUG_MARKER_STMTS)
    7384            0 :               continue;
    7385       867821 :             tsi_link_after (&iter, stmt, TSI_CONTINUE_LINKING);
    7386              :           }
    7387              :       }
    7388       274173 :       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        93779 :       break;
    7395              : 
    7396        93779 :     case TREE_BINFO:
    7397        93779 :       RT (t->binfo.common.chain);
    7398        93779 :       RT (t->binfo.offset);
    7399        93779 :       RT (t->binfo.inheritance);
    7400        93779 :       RT (t->binfo.vptr_field);
    7401              : 
    7402              :       /* Do not mark the vtables as USED in the address expressions
    7403              :          here.  */
    7404        93779 :       unused++;
    7405        93779 :       RT (t->binfo.vtable);
    7406        93779 :       RT (t->binfo.virtuals);
    7407        93779 :       RT (t->binfo.vtt_subvtt);
    7408        93779 :       RT (t->binfo.vtt_vptr);
    7409        93779 :       unused--;
    7410              : 
    7411        93779 :       BINFO_BASE_ACCESSES (t) = tree_vec ();
    7412        93779 :       if (!get_overrun ())
    7413              :         {
    7414        93779 :           unsigned num = vec_safe_length (BINFO_BASE_ACCESSES (t));
    7415       119318 :           for (unsigned ix = 0; ix != num; ix++)
    7416        25539 :             BINFO_BASE_APPEND (t, tree_node ());
    7417              :         }
    7418              :       break;
    7419              : 
    7420      1345843 :     case TREE_LIST:
    7421      1345843 :       RT (t->list.purpose);
    7422      1345843 :       RT (t->list.value);
    7423      1345843 :       RT (t->list.common.chain);
    7424      1345843 :       break;
    7425              : 
    7426       848058 :     case TREE_VEC:
    7427      2282237 :       for (unsigned ix = TREE_VEC_LENGTH (t); ix--;)
    7428      1434179 :         RT (TREE_VEC_ELT (t, ix));
    7429       848058 :       RT (t->type_common.common.chain);
    7430       848058 :       break;
    7431              : 
    7432              :       /* C++-specific nodes ...  */
    7433       105587 :     case BASELINK:
    7434       105587 :       RT (((lang_tree_node *)t)->baselink.binfo);
    7435       105587 :       RTU (((lang_tree_node *)t)->baselink.functions);
    7436       105587 :       RT (((lang_tree_node *)t)->baselink.access_binfo);
    7437       105587 :       RT (((lang_tree_node *)t)->baselink.common.chain);
    7438       105587 :       break;
    7439              : 
    7440        45166 :     case CONSTRAINT_INFO:
    7441        45166 :       RT (((lang_tree_node *)t)->constraint_info.template_reqs);
    7442        45166 :       RT (((lang_tree_node *)t)->constraint_info.declarator_reqs);
    7443        45166 :       RT (((lang_tree_node *)t)->constraint_info.associated_constr);
    7444        45166 :       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       578289 :     case OVERLOAD:
    7467       578289 :       RT (((lang_tree_node *)t)->overload.function);
    7468       578289 :       RT (t->common.chain);
    7469       578289 :       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         8374 :     case STATIC_ASSERT:
    7477         8374 :       RT (((lang_tree_node *)t)->static_assertion.condition);
    7478         8374 :       RT (((lang_tree_node *)t)->static_assertion.message);
    7479         8374 :       ((lang_tree_node *)t)->static_assertion.location
    7480         8374 :         = state->read_location (*this);
    7481         8374 :       break;
    7482              : 
    7483       329723 :     case TEMPLATE_DECL:
    7484              :       /* Streamed when reading the raw template decl itself.  */
    7485       329723 :       gcc_assert (((lang_tree_node *)t)->template_decl.arguments);
    7486       329723 :       gcc_assert (((lang_tree_node *)t)->template_decl.result);
    7487       329723 :       if (DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (t))
    7488         6956 :         RT (DECL_CHAIN (t));
    7489              :       break;
    7490              : 
    7491       737133 :     case TEMPLATE_INFO:
    7492       737133 :       RT (((lang_tree_node *)t)->template_info.tmpl);
    7493       737133 :       RT (((lang_tree_node *)t)->template_info.args);
    7494       737133 :       RT (((lang_tree_node *)t)->template_info.partial);
    7495       737133 :       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       415196 :     case TEMPLATE_PARM_INDEX:
    7514       415196 :       RU (((lang_tree_node *)t)->tpi.index);
    7515       415196 :       RU (((lang_tree_node *)t)->tpi.level);
    7516       415196 :       RU (((lang_tree_node *)t)->tpi.orig_level);
    7517       415196 :       RT (((lang_tree_node *)t)->tpi.decl);
    7518       415196 :       break;
    7519              : 
    7520         8538 :     case TRAIT_EXPR:
    7521         8538 :       RT (((lang_tree_node *)t)->trait_expression.type1);
    7522         8538 :       RT (((lang_tree_node *)t)->trait_expression.type2);
    7523         8538 :       ((lang_tree_node *)t)->trait_expression.locus
    7524         8538 :         = state->read_location (*this);
    7525         8538 :       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
    7526         8538 :       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     15559574 :   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
    7535              :     {
    7536     14446973 :       tree type = tree_node ();
    7537              : 
    7538     14446973 :       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     14446973 :       if (code != TEMPLATE_DECL)
    7549     14117250 :         t->typed.type = type;
    7550              :     }
    7551              : 
    7552     15559574 :   if (TREE_CODE (t) == CONSTRUCTOR)
    7553        54307 :     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     15559574 :   return !get_overrun ();
    7570              : }
    7571              : 
    7572              : void
    7573      6858340 : trees_out::lang_decl_vals (tree t)
    7574              : {
    7575      6858340 :   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      6858340 :   switch (lang->u.base.selector)
    7580              :     {
    7581            0 :     default:
    7582            0 :       gcc_unreachable ();
    7583              : 
    7584      1360852 :     case lds_fn:  /* lang_decl_fn.  */
    7585      1360852 :       if (streaming_p ())
    7586              :         {
    7587       680319 :           if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7588       118575 :             WU (lang->u.fn.ovl_op_code);
    7589              :         }
    7590              : 
    7591      1687708 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7592      1070120 :         WT (lang->u.fn.context);
    7593              : 
    7594      1360852 :       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      1358908 :       else if (decl_tls_wrapper_p (t))
    7602              :         /* The wrapped variable.  */
    7603           18 :         WT (lang->u.fn.befriending_classes);
    7604              :       else
    7605      1358890 :         WT (lang->u.fn.u5.cloned_function);
    7606              : 
    7607      1360852 :       if (FNDECL_USED_AUTO (t))
    7608         5671 :         WT (lang->u.fn.u.saved_auto_return_type);
    7609              : 
    7610      1360852 :       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      3924693 :     case lds_min:  /* lang_decl_min.  */
    7617      3924693 :     lds_min:
    7618      3924693 :       WT (lang->u.min.template_info);
    7619      3924693 :       {
    7620      3924693 :         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      1360852 :         if (!DECL_THUNK_P (t)
    7626      5283601 :             && (DECL_CONTEXT (t) && TYPE_P (DECL_CONTEXT (t))))
    7627              :           access = NULL_TREE;
    7628              : 
    7629      3924693 :         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      3924693 :       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      2933274 :     case lds_parm:  /* lang_decl_parm.  */
    7643      2933274 :       if (streaming_p ())
    7644              :         {
    7645       997975 :           WU (lang->u.parm.level);
    7646       997975 :           WU (lang->u.parm.index);
    7647              :         }
    7648              :       break;
    7649              :     }
    7650              : #undef WU
    7651              : #undef WT
    7652      6858340 : }
    7653              : 
    7654              : bool
    7655      2026404 : trees_in::lang_decl_vals (tree t)
    7656              : {
    7657      2026404 :   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      2026404 :   switch (lang->u.base.selector)
    7663              :     {
    7664            0 :     default:
    7665            0 :       gcc_unreachable ();
    7666              : 
    7667       465431 :     case lds_fn:  /* lang_decl_fn.  */
    7668       465431 :       if (DECL_NAME (t) && IDENTIFIER_OVL_OP_P (DECL_NAME (t)))
    7669              :         {
    7670        80137 :           unsigned code = u ();
    7671              : 
    7672              :           /* Check consistency.  */
    7673        80137 :           if (code >= OVL_OP_MAX
    7674        80137 :               || (ovl_op_info[IDENTIFIER_ASSIGN_OP_P (DECL_NAME (t))][code]
    7675        80137 :                   .ovl_op_code) == OVL_OP_ERROR_MARK)
    7676            0 :             set_overrun ();
    7677              :           else
    7678        80137 :             lang->u.fn.ovl_op_code = code;
    7679              :         }
    7680              : 
    7681       577729 :       if (DECL_CLASS_SCOPE_P (t) || DECL_UNIQUE_FRIEND_P (t))
    7682       366223 :         RT (lang->u.fn.context);
    7683              : 
    7684       465431 :       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       464833 :       else if (decl_tls_wrapper_p (t))
    7690           15 :         RT (lang->u.fn.befriending_classes);
    7691              :       else
    7692       464818 :         RT (lang->u.fn.u5.cloned_function);
    7693              : 
    7694       465431 :       if (FNDECL_USED_AUTO (t))
    7695         1721 :         RT (lang->u.fn.u.saved_auto_return_type);
    7696       465431 :       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      1339153 :     case lds_min:  /* lang_decl_min.  */
    7703      1339153 :     lds_min:
    7704      1339153 :       RT (lang->u.min.template_info);
    7705      1339153 :       RT (lang->u.min.access);
    7706      1339153 :       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       687107 :     case lds_parm:  /* lang_decl_parm.  */
    7714       687107 :       RU (lang->u.parm.level);
    7715       687107 :       RU (lang->u.parm.index);
    7716       687107 :       break;
    7717              :     }
    7718              : #undef RU
    7719              : #undef RT
    7720      2026404 :   return !get_overrun ();
    7721              : }
    7722              : 
    7723              : /* Most of the value contents of lang_type is streamed in
    7724              :    define_class.  */
    7725              : 
    7726              : void
    7727       467464 : trees_out::lang_type_vals (tree t)
    7728              : {
    7729       467464 :   const struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7730              : #define WU(X) (u (X))
    7731              : #define WT(X) (tree_node (X))
    7732       467464 :   if (streaming_p ())
    7733       233709 :     WU (lang->align);
    7734              : #undef WU
    7735              : #undef WT
    7736       467464 : }
    7737              : 
    7738              : bool
    7739       153533 : trees_in::lang_type_vals (tree t)
    7740              : {
    7741       153533 :   struct lang_type *lang = TYPE_LANG_SPECIFIC (t);
    7742              : #define RU(X) ((X) = u ())
    7743              : #define RT(X) ((X) = tree_node ())
    7744       153533 :   RU (lang->align);
    7745              : #undef RU
    7746              : #undef RT
    7747       153533 :   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     22861114 : trees_out::tree_node_bools (tree t)
    7756              : {
    7757     22861114 :   gcc_checking_assert (streaming_p ());
    7758              : 
    7759              :   /* We should never stream a namespace.  */
    7760     22861114 :   gcc_checking_assert (TREE_CODE (t) != NAMESPACE_DECL
    7761              :                        || DECL_NAMESPACE_ALIAS (t));
    7762              : 
    7763     22861114 :   bits_out bits = stream_bits ();
    7764     22861114 :   core_bools (t, bits);
    7765              : 
    7766     22861114 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7767              :     {
    7768      4914222 :     case tcc_declaration:
    7769      4914222 :       {
    7770      4914222 :         bool specific = DECL_LANG_SPECIFIC (t) != NULL;
    7771      4914222 :         bits.b (specific);
    7772      4914222 :         if (specific && VAR_P (t))
    7773       387220 :           bits.b (DECL_DECOMPOSITION_P (t));
    7774      2949878 :         if (specific)
    7775      2949878 :           lang_decl_bools (t, bits);
    7776              :       }
    7777              :       break;
    7778              : 
    7779       900556 :     case tcc_type:
    7780       900556 :       {
    7781       900556 :         bool specific = (TYPE_MAIN_VARIANT (t) == t
    7782       900556 :                          && TYPE_LANG_SPECIFIC (t) != NULL);
    7783       900556 :         gcc_assert (TYPE_LANG_SPECIFIC (t)
    7784              :                     == TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t)));
    7785              : 
    7786       900556 :         bits.b (specific);
    7787       900556 :         if (specific)
    7788       233709 :           lang_type_bools (t, bits);
    7789              :       }
    7790              :       break;
    7791              : 
    7792              :     default:
    7793              :       break;
    7794              :     }
    7795              : 
    7796     22861114 :   bits.bflush ();
    7797     22861114 : }
    7798              : 
    7799              : bool
    7800     15583449 : trees_in::tree_node_bools (tree t)
    7801              : {
    7802     15583449 :   bits_in bits = stream_bits ();
    7803     15583449 :   bool ok = core_bools (t, bits);
    7804              : 
    7805     15583449 :   if (ok)
    7806     15583449 :     switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7807              :       {
    7808      3320780 :       case tcc_declaration:
    7809      3320780 :         if (bits.b ())
    7810              :           {
    7811      2026404 :             bool decomp = VAR_P (t) && bits.b ();
    7812              : 
    7813      2026404 :             ok = maybe_add_lang_decl_raw (t, decomp);
    7814      2026404 :             if (ok)
    7815      2026404 :               ok = lang_decl_bools (t, bits);
    7816              :           }
    7817              :         break;
    7818              : 
    7819       591179 :       case tcc_type:
    7820       591179 :         if (bits.b ())
    7821              :           {
    7822       153533 :             ok = maybe_add_lang_type_raw (t);
    7823       153533 :             if (ok)
    7824       153533 :               ok = lang_type_bools (t, bits);
    7825              :           }
    7826              :         break;
    7827              : 
    7828              :       default:
    7829              :         break;
    7830              :       }
    7831              : 
    7832     15583449 :   bits.bflush ();
    7833     15583449 :   if (!ok || get_overrun ())
    7834            0 :     return false;
    7835              : 
    7836              :   return true;
    7837     15583449 : }
    7838              : 
    7839              : 
    7840              : /* Write out the lang-specific vals of node T.  */
    7841              : 
    7842              : void
    7843     59290697 : trees_out::lang_vals (tree t)
    7844              : {
    7845     59290697 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7846              :     {
    7847     12900480 :     case tcc_declaration:
    7848     12900480 :       if (DECL_LANG_SPECIFIC (t))
    7849      6858340 :         lang_decl_vals (t);
    7850              :       break;
    7851              : 
    7852      3177170 :     case tcc_type:
    7853      3177170 :       if (TYPE_MAIN_VARIANT (t) == t && TYPE_LANG_SPECIFIC (t))
    7854       467464 :         lang_type_vals (t);
    7855              :       break;
    7856              : 
    7857              :     default:
    7858              :       break;
    7859              :     }
    7860     59290697 : }
    7861              : 
    7862              : bool
    7863     15559574 : trees_in::lang_vals (tree t)
    7864              : {
    7865     15559574 :   bool ok = true;
    7866              : 
    7867     15559574 :   switch (TREE_CODE_CLASS (TREE_CODE (t)))
    7868              :     {
    7869      3320780 :     case tcc_declaration:
    7870      3320780 :       if (DECL_LANG_SPECIFIC (t))
    7871      2026404 :         ok = lang_decl_vals (t);
    7872              :       break;
    7873              : 
    7874       567304 :     case tcc_type:
    7875       567304 :       if (TYPE_LANG_SPECIFIC (t))
    7876       153533 :         ok = lang_type_vals (t);
    7877              :       else
    7878       413771 :         TYPE_LANG_SPECIFIC (t) = TYPE_LANG_SPECIFIC (TYPE_MAIN_VARIANT (t));
    7879              :       break;
    7880              : 
    7881              :     default:
    7882              :       break;
    7883              :     }
    7884              : 
    7885     15559574 :   return ok;
    7886              : }
    7887              : 
    7888              : /* Write out the value fields of node T.  */
    7889              : 
    7890              : void
    7891     59290697 : trees_out::tree_node_vals (tree t)
    7892              : {
    7893     59290697 :   core_vals (t);
    7894     59290697 :   lang_vals (t);
    7895     59290697 : }
    7896              : 
    7897              : bool
    7898     15559574 : trees_in::tree_node_vals (tree t)
    7899              : {
    7900     15559574 :   bool ok = core_vals (t);
    7901     15559574 :   if (ok)
    7902     15559574 :     ok = lang_vals (t);
    7903              : 
    7904     15559574 :   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    386245152 : trees_out::ref_node (tree t)
    7914              : {
    7915    386245152 :   if (!t)
    7916              :     {
    7917    154564801 :       if (streaming_p ())
    7918              :         {
    7919              :           /* NULL_TREE -> tt_null.  */
    7920     58740330 :           null_count++;
    7921     58740330 :           i (tt_null);
    7922              :         }
    7923              :       return WK_none;
    7924              :     }
    7925              : 
    7926    231680351 :   if (!TREE_VISITED (t))
    7927              :     return WK_normal;
    7928              : 
    7929              :   /* An already-visited tree.  It must be in the map.  */
    7930    134925950 :   int val = get_tag (t);
    7931              : 
    7932    134925950 :   if (val == tag_value)
    7933              :     /* An entry we should walk into.  */
    7934              :     return WK_value;
    7935              : 
    7936    132889560 :   const char *kind;
    7937              : 
    7938    132889560 :   if (val <= tag_backref)
    7939              :     {
    7940              :       /* Back reference -> -ve number  */
    7941    101268745 :       if (streaming_p ())
    7942     47928240 :         i (val);
    7943              :       kind = "backref";
    7944              :     }
    7945     31620815 :   else if (val >= tag_fixed)
    7946              :     {
    7947              :       /* Fixed reference -> tt_fixed */
    7948     31620815 :       val -= tag_fixed;
    7949     31620815 :       if (streaming_p ())
    7950     11117897 :         i (tt_fixed), u (val);
    7951              :       kind = "fixed";
    7952              :     }
    7953              : 
    7954    132889560 :   if (streaming_p ())
    7955              :     {
    7956     59046137 :       back_ref_count++;
    7957     59046137 :       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     32544592 : trees_in::back_ref (int tag)
    7965              : {
    7966     32544592 :   tree res = NULL_TREE;
    7967              : 
    7968     32544592 :   if (tag < 0 && unsigned (~tag) < back_refs.length ())
    7969     32544592 :     res = back_refs[~tag];
    7970              : 
    7971     32544592 :   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     32544592 :       || TREE_CODE (res) >= MAX_TREE_CODES)
    7976            0 :     set_overrun ();
    7977              :   else
    7978     32559132 :     dump (dumper::TREE) && dump ("Read backref:%d found %C:%N%S", tag,
    7979              :                                  TREE_CODE (res), res, res);
    7980     32544592 :   return res;
    7981              : }
    7982              : 
    7983              : unsigned
    7984      5557802 : trees_out::add_indirect_tpl_parms (tree parms)
    7985              : {
    7986      5557802 :   unsigned len = 0;
    7987     10472753 :   for (; parms; parms = TREE_CHAIN (parms), len++)
    7988              :     {
    7989      5885216 :       if (TREE_VISITED (parms))
    7990              :         break;
    7991              : 
    7992      4914951 :       int tag = insert (parms);
    7993      4914951 :       if (streaming_p ())
    7994      4915278 :         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      5557802 :   if (streaming_p ())
    8000       915768 :     u (len);
    8001              : 
    8002      5557802 :   return len;
    8003              : }
    8004              : 
    8005              : unsigned
    8006       560790 : trees_in::add_indirect_tpl_parms (tree parms)
    8007              : {
    8008       560790 :   unsigned len = u ();
    8009      1561901 :   for (unsigned ix = 0; ix != len; parms = TREE_CHAIN (parms), ix++)
    8010              :     {
    8011       440321 :       int tag = insert (parms);
    8012       440801 :       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       560790 :   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     12172837 : trees_out::add_indirects (tree decl)
    8025              : {
    8026     12172837 :   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     12172837 :   tree inner = decl;
    8034     12172837 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8035              :     {
    8036      5557802 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8037              : 
    8038      5557802 :       inner = DECL_TEMPLATE_RESULT (decl);
    8039      5557802 :       int tag = insert (inner);
    8040      5557802 :       if (streaming_p ())
    8041       915768 :         dump (dumper::TREE)
    8042          219 :           && dump ("Indirect:%d template's result %C:%N",
    8043          219 :                    tag, TREE_CODE (inner), inner);
    8044      5557802 :       count++;
    8045              :     }
    8046              : 
    8047     12172837 :   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      6367159 :       tree type = TREE_TYPE (inner);
    8053      6367159 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8054              :                            || TYPE_NAME (type) == inner);
    8055      6367159 :       int tag = insert (type);
    8056      6367159 :       if (streaming_p ())
    8057       706516 :         dump (dumper::TREE) && dump ("Indirect:%d decl's type %C:%N", tag,
    8058          362 :                                      TREE_CODE (type), type);
    8059      6367159 :       count++;
    8060              :     }
    8061              : 
    8062     12172837 :   if (streaming_p ())
    8063              :     {
    8064      1844561 :       u (count);
    8065      1845090 :       dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8066              :     }
    8067     12172837 : }
    8068              : 
    8069              : bool
    8070      1133493 : trees_in::add_indirects (tree decl)
    8071              : {
    8072      1133493 :   unsigned count = 0;
    8073              : 
    8074      1133493 :   tree inner = decl;
    8075      1133493 :   if (TREE_CODE (inner) == TEMPLATE_DECL)
    8076              :     {
    8077       560790 :       count += add_indirect_tpl_parms (DECL_TEMPLATE_PARMS (decl));
    8078              : 
    8079       560790 :       inner = DECL_TEMPLATE_RESULT (decl);
    8080       560790 :       int tag = insert (inner);
    8081       560790 :       dump (dumper::TREE)
    8082          228 :         && dump ("Indirect:%d templates's result %C:%N", tag,
    8083          228 :                  TREE_CODE (inner), inner);
    8084       560790 :       count++;
    8085              :     }
    8086              : 
    8087      1133493 :   if (TREE_CODE (inner) == TYPE_DECL)
    8088              :     {
    8089       433555 :       tree type = TREE_TYPE (inner);
    8090       433555 :       gcc_checking_assert (DECL_ORIGINAL_TYPE (inner)
    8091              :                            || TYPE_NAME (type) == inner);
    8092       433555 :       int tag = insert (type);
    8093       433555 :       dump (dumper::TREE)
    8094          362 :         && dump ("Indirect:%d decl's type %C:%N", tag, TREE_CODE (type), type);
    8095       433555 :       count++;
    8096              :     }
    8097              : 
    8098      1134098 :   dump (dumper::TREE) && dump ("Inserted %u indirects", count);
    8099      1133493 :   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      2938932 : trees_out::tpl_parm_value (tree parm)
    8117              : {
    8118      2938932 :   gcc_checking_assert (DECL_P (parm) && DECL_TEMPLATE_PARM_P (parm));
    8119              : 
    8120      2938932 :   int parm_tag = insert (parm);
    8121      2938932 :   if (streaming_p ())
    8122              :     {
    8123       652508 :       i (tt_tpl_parm);
    8124       652508 :       dump (dumper::TREE) && dump ("Writing template parm:%d %C:%N",
    8125          114 :                                    parm_tag, TREE_CODE (parm), parm);
    8126       652508 :       start (parm);
    8127       652508 :       tree_node_bools (parm);
    8128              :     }
    8129              : 
    8130      2938932 :   tree inner = parm;
    8131      2938932 :   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      2938932 :   tree type = NULL_TREE;
    8145      2938932 :   if (TREE_CODE (inner) == TYPE_DECL)
    8146              :     {
    8147      2633268 :       type = TREE_TYPE (inner);
    8148      2633268 :       int type_tag = insert (type);
    8149      2633268 :       if (streaming_p ())
    8150              :         {
    8151       589447 :           dump (dumper::TREE) && dump ("Writing template parm type:%d %C:%N",
    8152          108 :                                        type_tag, TREE_CODE (type), type);
    8153       589447 :           start (type);
    8154       589447 :           tree_node_bools (type);
    8155              :         }
    8156              :     }
    8157              : 
    8158      2938932 :   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      2938932 :   tree_node_vals (parm);
    8167      2938932 :   if (inner != parm)
    8168         7261 :     tree_node_vals (inner);
    8169      2938932 :   if (type)
    8170              :     {
    8171      2633268 :       tree_node_vals (type);
    8172      2633268 :       if (DECL_NAME (inner) == auto_identifier
    8173      2633268 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8174              :         {
    8175              :           /* Placeholder auto.  */
    8176       125110 :           tree_node (DECL_INITIAL (inner));
    8177       125110 :           tree_node (DECL_SIZE_UNIT (inner));
    8178              :         }
    8179              :     }
    8180              : 
    8181      2938932 :   if (streaming_p ())
    8182       652508 :     dump (dumper::TREE) && dump ("Wrote template parm:%d %C:%N",
    8183          114 :                                  parm_tag, TREE_CODE (parm), parm);
    8184      2938932 : }
    8185              : 
    8186              : tree
    8187       433278 : trees_in::tpl_parm_value ()
    8188              : {
    8189       433278 :   tree parm = start ();
    8190       433278 :   if (!parm || !tree_node_bools (parm))
    8191              :     return NULL_TREE;
    8192              : 
    8193       433278 :   int parm_tag = insert (parm);
    8194       433278 :   dump (dumper::TREE) && dump ("Reading template parm:%d %C:%N",
    8195          198 :                                parm_tag, TREE_CODE (parm), parm);
    8196              : 
    8197       433278 :   tree inner = parm;
    8198       433278 :   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       433278 :   tree type = NULL_TREE;
    8210       433278 :   if (TREE_CODE (inner) == TYPE_DECL)
    8211              :     {
    8212       389860 :       type = start ();
    8213       389860 :       if (!type || !tree_node_bools (type))
    8214              :         return NULL_TREE;
    8215       389860 :       int type_tag = insert (type);
    8216       389860 :       dump (dumper::TREE) && dump ("Reading template parm type:%d %C:%N",
    8217          120 :                                    type_tag, TREE_CODE (type), type);
    8218              : 
    8219       389860 :       TREE_TYPE (inner) = TREE_TYPE (parm) = type;
    8220       389860 :       TYPE_NAME (type) = parm;
    8221              :     }
    8222              : 
    8223       433278 :   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       433278 :   tree_node_vals (parm);
    8232       433278 :   if (inner != parm)
    8233         1267 :     tree_node_vals (inner);
    8234       433278 :   if (type)
    8235              :     {
    8236       389860 :       tree_node_vals (type);
    8237       389860 :       if (DECL_NAME (inner) == auto_identifier
    8238       389860 :           || DECL_NAME (inner) == decltype_auto_identifier)
    8239              :         {
    8240              :           /* Placeholder auto.  */
    8241        33934 :           DECL_INITIAL (inner) = tree_node ();
    8242        33934 :           DECL_SIZE_UNIT (inner) = tree_node ();
    8243              :         }
    8244       389860 :       if (TYPE_CANONICAL (type))
    8245              :         {
    8246       389860 :           gcc_checking_assert (TYPE_CANONICAL (type) == type);
    8247       389860 :           TYPE_CANONICAL (type) = canonical_type_parameter (type);
    8248              :         }
    8249              :     }
    8250              : 
    8251       433278 :   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      1846352 : trees_out::install_entity (tree decl, depset *dep)
    8259              : {
    8260      1846352 :   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      1846352 :   u (dep ? dep->cluster + 1 : 0);
    8265      1846352 :   if (CHECKING_P && dep)
    8266              :     {
    8267              :       /* Add it to the entity map, such that we can tell it is
    8268              :          part of us.  */
    8269      1330542 :       bool existed;
    8270      1330542 :       unsigned *slot = &entity_map->get_or_insert
    8271      1330542 :         (DECL_UID (decl), &existed);
    8272      1330542 :       if (existed)
    8273              :         /* If it existed, it should match.  */
    8274         1416 :         gcc_checking_assert (decl == (*entity_ary)[*slot]);
    8275      1330542 :       *slot = ~dep->cluster;
    8276              :     }
    8277      1846352 : }
    8278              : 
    8279              : bool
    8280      1228620 : trees_in::install_entity (tree decl)
    8281              : {
    8282      1228620 :   unsigned entity_index = u ();
    8283      1228620 :   if (!entity_index)
    8284              :     return false;
    8285              : 
    8286       885772 :   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       885772 :   unsigned ident = state->entity_lwm + entity_index - 1;
    8294       885772 :   (*entity_ary)[ident] = decl;
    8295              : 
    8296              :   /* And into the entity map, if it's not already there.  */
    8297       885772 :   tree not_tmpl = STRIP_TEMPLATE (decl);
    8298       885772 :   if (!DECL_LANG_SPECIFIC (not_tmpl)
    8299      1648458 :       || !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       884627 :       if (!DECL_LANG_SPECIFIC (not_tmpl))
    8304              :         {
    8305       123086 :           maybe_add_lang_decl_raw (not_tmpl, false);
    8306       123086 :           SET_DECL_LANGUAGE (not_tmpl, lang_cplusplus);
    8307              :         }
    8308       884627 :       DECL_MODULE_ENTITY_P (not_tmpl) = true;
    8309              : 
    8310              :       /* Insert into the entity hash (it cannot already be there).  */
    8311       884627 :       bool existed;
    8312       884627 :       unsigned &slot = entity_map->get_or_insert (DECL_UID (decl), &existed);
    8313       884627 :       gcc_checking_assert (!existed);
    8314       884627 :       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      5048826 : trees_out::decl_value (tree decl, depset *dep)
    8359              : {
    8360              :   /* We should not be writing clones or template parms.  */
    8361      5048826 :   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      5048826 :   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      5048826 :   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      5048410 :   merge_kind mk = get_merge_kind (decl, dep);
    8382              : 
    8383      5048410 :   if (CHECKING_P)
    8384              :     {
    8385              :       /* Never start in the middle of a template.  */
    8386      5048410 :       int use_tpl = -1;
    8387      5048410 :       if (tree ti = node_template_info (decl, use_tpl))
    8388      1765299 :         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      5048410 :   if (streaming_p ())
    8396              :     {
    8397              :       /* A new node -> tt_decl.  */
    8398      1846352 :       decl_val_count++;
    8399      1846352 :       i (tt_decl);
    8400      1846352 :       u (mk);
    8401      1846352 :       start (decl);
    8402              : 
    8403      1846352 :       if (mk != MK_unique)
    8404              :         {
    8405      1512710 :           bits_out bits = stream_bits ();
    8406      1512710 :           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       402198 :               tree o = get_originating_module_decl (decl);
    8411       402198 :               bool is_attached = false;
    8412              : 
    8413       402198 :               tree not_tmpl = STRIP_TEMPLATE (o);
    8414       402198 :               if (DECL_LANG_SPECIFIC (not_tmpl)
    8415       641961 :                   && DECL_MODULE_ATTACH_P (not_tmpl))
    8416              :                 is_attached = true;
    8417              : 
    8418       402198 :               bits.b (is_attached);
    8419              :             }
    8420      1512710 :           bits.b (dep && dep->has_defn ());
    8421      1512710 :         }
    8422      1846352 :       tree_node_bools (decl);
    8423              :     }
    8424              : 
    8425      5048410 :   int tag = insert (decl, WK_value);
    8426      5048410 :   if (streaming_p ())
    8427      1846352 :     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      5048410 :   tree inner = decl;
    8432      5048410 :   int inner_tag = 0;
    8433      5048410 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
    8434              :     {
    8435      1449700 :       inner = DECL_TEMPLATE_RESULT (decl);
    8436      1449700 :       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      1449700 :       gcc_checking_assert (TREE_TYPE (decl) == TREE_TYPE (inner));
    8441              : 
    8442      1449700 :       if (streaming_p ())
    8443              :         {
    8444       483218 :           int code = TREE_CODE (inner);
    8445       483218 :           u (code);
    8446       483218 :           start (inner, true);
    8447       483218 :           tree_node_bools (inner);
    8448       483218 :           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      5048410 :   tree type = NULL_TREE;
    8455      5048410 :   int type_tag = 0;
    8456      5048410 :   tree stub_decl = NULL_TREE;
    8457      5048410 :   int stub_tag = 0;
    8458      5048410 :   if (TREE_CODE (inner) == TYPE_DECL)
    8459              :     {
    8460      1788499 :       type = TREE_TYPE (inner);
    8461      1788499 :       bool has_type = (type == TYPE_MAIN_VARIANT (type)
    8462      1788499 :                        && TYPE_NAME (type) == inner);
    8463              : 
    8464      1788499 :       if (streaming_p ())
    8465       604379 :         u (has_type ? (unsigned) TREE_CODE (type) : 0);
    8466              : 
    8467      1788499 :       if (has_type)
    8468              :         {
    8469       815774 :           type_tag = insert (type, WK_value);
    8470       815774 :           if (streaming_p ())
    8471              :             {
    8472       271900 :               start (type, true);
    8473       271900 :               tree_node_bools (type);
    8474       271900 :               dump (dumper::TREE)
    8475          155 :                 && dump ("Writing type:%d %C:%N", type_tag,
    8476          155 :                          TREE_CODE (type), type);
    8477              :             }
    8478              : 
    8479       815774 :           stub_decl = TYPE_STUB_DECL (type);
    8480       815774 :           bool has_stub = inner != stub_decl;
    8481       815774 :           if (streaming_p ())
    8482       271900 :             u (has_stub ? (unsigned) TREE_CODE (stub_decl) : 0);
    8483       815774 :           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      5048410 :   tree container = decl_container (decl);
    8506      5048410 :   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      5048410 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8512              :     {
    8513      3828541 :       tree* temploid_friend_slot = imported_temploid_friends->get (decl);
    8514      3828541 :       gcc_checking_assert (!temploid_friend_slot || *temploid_friend_slot);
    8515      3828541 :       tree_node (temploid_friend_slot ? *temploid_friend_slot : NULL_TREE);
    8516              :     }
    8517              : 
    8518      5048410 :   {
    8519      5048410 :     auto wmk = make_temp_override (dep_hash->writing_merge_key, true);
    8520      5048410 :     if (decl != inner)
    8521      1449700 :       tpl_header (decl, &tpl_levels);
    8522      5048410 :     if (TREE_CODE (inner) == FUNCTION_DECL)
    8523      2040042 :       fn_parms_init (inner);
    8524              : 
    8525              :     /* Now write out the merging information, and then really
    8526              :        install the tag values.  */
    8527      5048410 :     key_mergeable (tag, mk, decl, inner, container, dep);
    8528              : 
    8529      5048410 :     if (streaming_p ())
    8530      5052309 :       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      5048410 :   }
    8534              : 
    8535      5048410 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8536      5048410 :     fn_parms_fini (inner);
    8537              : 
    8538      5048410 :   if (!is_key_order ())
    8539      3715354 :     tree_node_vals (decl);
    8540              : 
    8541      5048410 :   if (inner_tag)
    8542              :     {
    8543      1449700 :       if (!is_key_order ())
    8544       966482 :         tree_node_vals (inner);
    8545      1449700 :       tpl_parms_fini (decl, tpl_levels);
    8546              :     }
    8547              : 
    8548      5048410 :   if (type && !is_key_order ())
    8549              :     {
    8550       543874 :       tree_node_vals (type);
    8551       543874 :       if (stub_decl)
    8552         1599 :         tree_node_vals (stub_decl);
    8553              :     }
    8554              : 
    8555      5048410 :   if (!is_key_order ())
    8556              :     {
    8557      3715354 :       if (mk & MK_template_mask
    8558      2589628 :           || mk == MK_partial
    8559      2589628 :           || 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      1125726 :               auto *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
    8567              : 
    8568      1125726 :               if (streaming_p ())
    8569       562863 :                 u (get_mergeable_specialization_flags (mk & MK_tmpl_decl_mask,
    8570              :                                                        entry->tmpl, decl));
    8571      1125726 :               tree_node (entry->tmpl);
    8572      1125726 :               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      3715354 :       tree_node (get_constraints (decl));
    8582              :     }
    8583              : 
    8584      5048410 :   if (streaming_p ())
    8585              :     {
    8586              :       /* Do not stray outside this section.  */
    8587      1846352 :       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      1846352 :       install_entity (decl, dep);
    8592              :     }
    8593              : 
    8594      5048410 :   if (DECL_LANG_SPECIFIC (inner)
    8595      4179496 :       && DECL_MODULE_KEYED_DECLS_P (inner)
    8596      5049751 :       && 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      5048410 :   bool is_typedef = false;
    8624      5048410 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8625              :     {
    8626       972725 :       tree t = TREE_TYPE (inner);
    8627       972725 :       unsigned tdef_flags = 0;
    8628       972725 :       if (DECL_ORIGINAL_TYPE (inner)
    8629       972725 :           && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8630              :         {
    8631       972725 :           tdef_flags |= 1;
    8632       972725 :           if (TYPE_STRUCTURAL_EQUALITY_P (t)
    8633       215695 :               && TYPE_DEPENDENT_P_VALID (t)
    8634      1178694 :               && TYPE_DEPENDENT_P (t))
    8635              :             tdef_flags |= 2;
    8636              :         }
    8637       972725 :       if (streaming_p ())
    8638       332479 :         u (tdef_flags);
    8639              : 
    8640       972725 :       if (tdef_flags & 1)
    8641              :         {
    8642              :           /* A typedef type.  */
    8643       972725 :           int type_tag = insert (t);
    8644       972725 :           if (streaming_p ())
    8645       332479 :             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      5048410 :   if (streaming_p () && DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    8655              :     {
    8656       155303 :       bool cloned_p
    8657       155303 :         = (DECL_CHAIN (decl) && DECL_CLONED_FUNCTION_P (DECL_CHAIN (decl)));
    8658       110536 :       bool needs_vtt_parm_p
    8659       110536 :         = (cloned_p && CLASSTYPE_VBASECLASSES (DECL_CONTEXT (decl)));
    8660       110536 :       bool omit_inherited_parms_p
    8661       110536 :         = (cloned_p && DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (decl)
    8662        88576 :            && base_ctor_omit_inherited_parms (decl));
    8663       155303 :       unsigned flags = (int (cloned_p) << 0
    8664       155303 :                         | int (needs_vtt_parm_p) << 1
    8665       155303 :                         | int (omit_inherited_parms_p) << 2);
    8666       155303 :       u (flags);
    8667       155382 :       dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    8668              :                                    decl, cloned_p ? "" : "not ");
    8669              :     }
    8670              : 
    8671      5048410 :   if (streaming_p () && VAR_P (decl) && CP_DECL_THREAD_LOCAL_P (decl))
    8672          176 :     u (decl_tls_model (decl));
    8673              : 
    8674      5048410 :   if (streaming_p ())
    8675      1846352 :     dump (dumper::TREE) && dump ("Written decl:%d %C:%N", tag,
    8676          683 :                                  TREE_CODE (decl), decl);
    8677              : 
    8678      5048410 :   if (NAMESPACE_SCOPE_P (inner))
    8679      2799338 :     gcc_checking_assert (!dep == (VAR_OR_FUNCTION_DECL_P (inner)
    8680              :                                   && DECL_LOCAL_DECL_P (inner)));
    8681      3648578 :   else if ((TREE_CODE (inner) == TYPE_DECL
    8682       967081 :             && !is_typedef
    8683       184508 :             && TYPE_NAME (TREE_TYPE (inner)) == inner)
    8684      4431151 :            || TREE_CODE (inner) == FUNCTION_DECL)
    8685              :     {
    8686      1734465 :       bool write_defn = !dep && has_definition (decl);
    8687      1734465 :       if (streaming_p ())
    8688       578420 :         u (write_defn);
    8689      1734465 :       if (write_defn)
    8690            6 :         write_definition (decl);
    8691              :     }
    8692              : }
    8693              : 
    8694              : tree
    8695      1228620 : trees_in::decl_value ()
    8696              : {
    8697      1228620 :   int tag = 0;
    8698      1228620 :   bool is_attached = false;
    8699      1228620 :   bool has_defn = false;
    8700      1228620 :   unsigned mk_u = u ();
    8701      1228620 :   if (mk_u >= MK_hwm || !merge_kind_name[mk_u])
    8702              :     {
    8703            0 :       set_overrun ();
    8704            0 :       return NULL_TREE;
    8705              :     }
    8706              : 
    8707      1228620 :   unsigned saved_unused = unused;
    8708      1228620 :   unused = 0;
    8709              : 
    8710      1228620 :   merge_kind mk = merge_kind (mk_u);
    8711              : 
    8712      1228620 :   tree decl = start ();
    8713      1228620 :   if (decl)
    8714              :     {
    8715      1228620 :       if (mk != MK_unique)
    8716              :         {
    8717      1006987 :           bits_in bits = stream_bits ();
    8718      1006987 :           if (!(mk & MK_template_mask) && !state->is_header ())
    8719       184729 :             is_attached = bits.b ();
    8720              : 
    8721      1006987 :           has_defn = bits.b ();
    8722      1006987 :         }
    8723              : 
    8724      1228620 :       if (!tree_node_bools (decl))
    8725            0 :         decl = NULL_TREE;
    8726              :     }
    8727              : 
    8728              :   /* Insert into map.  */
    8729      1228620 :   tag = insert (decl);
    8730      1228620 :   if (decl)
    8731      1228620 :     dump (dumper::TREE)
    8732          964 :       && dump ("Reading:%d %C", tag, TREE_CODE (decl));
    8733              : 
    8734      1228620 :   tree inner = decl;
    8735      1228620 :   int inner_tag = 0;
    8736      1228620 :   if (decl && TREE_CODE (decl) == TEMPLATE_DECL)
    8737              :     {
    8738       328456 :       int code = u ();
    8739       328456 :       inner = start (code);
    8740       328456 :       if (inner && tree_node_bools (inner))
    8741       328456 :         DECL_TEMPLATE_RESULT (decl) = inner;
    8742              :       else
    8743            0 :         decl = NULL_TREE;
    8744              : 
    8745       328456 :       inner_tag = insert (inner);
    8746       328456 :       if (decl)
    8747       328456 :         dump (dumper::TREE)
    8748          204 :           && dump ("Reading:%d %C", inner_tag, TREE_CODE (inner));
    8749              :     }
    8750              : 
    8751      1228620 :   tree type = NULL_TREE;
    8752      1228620 :   int type_tag = 0;
    8753      1228620 :   tree stub_decl = NULL_TREE;
    8754      1228620 :   int stub_tag = 0;
    8755      1228620 :   if (decl && TREE_CODE (inner) == TYPE_DECL)
    8756              :     {
    8757       393384 :       if (unsigned type_code = u ())
    8758              :         {
    8759       177430 :           type = start (type_code);
    8760       177430 :           if (type && tree_node_bools (type))
    8761              :             {
    8762       177430 :               TREE_TYPE (inner) = type;
    8763       177430 :               TYPE_NAME (type) = inner;
    8764              :             }
    8765              :           else
    8766            0 :             decl = NULL_TREE;
    8767              : 
    8768       177430 :           type_tag = insert (type);
    8769       177430 :           if (decl)
    8770       177430 :             dump (dumper::TREE)
    8771          212 :               && dump ("Reading type:%d %C", type_tag, TREE_CODE (type));
    8772              : 
    8773       177430 :           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      1228620 :   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      1228620 :   tree container = decl_container ();
    8812      1228620 :   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      1228620 :   tree temploid_friend = NULL_TREE;
    8817      1228620 :   if (TREE_CODE (inner) == FUNCTION_DECL || TREE_CODE (inner) == TYPE_DECL)
    8818       858815 :     temploid_friend = tree_node ();
    8819              : 
    8820              :   /* Figure out if this decl is already known about.  */
    8821      1228620 :   int parm_tag = 0;
    8822              : 
    8823      1228620 :   if (decl != inner)
    8824       328456 :     if (!tpl_header (decl, &tpl_levels))
    8825            0 :       goto bail;
    8826      1228620 :   if (TREE_CODE (inner) == FUNCTION_DECL)
    8827       465431 :     parm_tag = fn_parms_init (inner);
    8828              : 
    8829      1228620 :   tree existing = key_mergeable (tag, mk, decl, inner, type, container,
    8830              :                                  is_attached, temploid_friend);
    8831      1228620 :   tree existing_inner = existing;
    8832      1228620 :   if (existing)
    8833              :     {
    8834       433008 :       if (existing == error_mark_node)
    8835            0 :         goto bail;
    8836              : 
    8837       433008 :       if (TREE_CODE (STRIP_TEMPLATE (existing)) == TYPE_DECL)
    8838              :         {
    8839       160543 :           tree etype = TREE_TYPE (existing);
    8840       160543 :           if (TYPE_LANG_SPECIFIC (etype)
    8841       115741 :               && COMPLETE_TYPE_P (etype)
    8842       228350 :               && !CLASSTYPE_MEMBER_VEC (etype))
    8843              :             /* Give it a member vec, we're likely gonna be looking
    8844              :                inside it.  */
    8845        14040 :             set_class_bindings (etype, -1);
    8846              :         }
    8847              : 
    8848              :       /* Install the existing decl into the back ref array.  */
    8849       433008 :       register_duplicate (decl, existing);
    8850       433008 :       back_refs[~tag] = existing;
    8851       433008 :       if (inner_tag != 0)
    8852              :         {
    8853       143185 :           existing_inner = DECL_TEMPLATE_RESULT (existing);
    8854       143185 :           back_refs[~inner_tag] = existing_inner;
    8855              :         }
    8856              : 
    8857       433008 :       if (type_tag != 0)
    8858              :         {
    8859        76856 :           tree existing_type = TREE_TYPE (existing);
    8860        76856 :           back_refs[~type_tag] = existing_type;
    8861        76856 :           if (stub_tag != 0)
    8862          242 :             back_refs[~stub_tag] = TYPE_STUB_DECL (existing_type);
    8863              :         }
    8864              :     }
    8865              : 
    8866      1228620 :   if (parm_tag)
    8867       465431 :     fn_parms_fini (parm_tag, inner, existing_inner, has_defn);
    8868              : 
    8869      1228620 :   if (!tree_node_vals (decl))
    8870            0 :     goto bail;
    8871              : 
    8872      1228620 :   if (inner_tag)
    8873              :     {
    8874       328456 :       gcc_checking_assert (DECL_TEMPLATE_RESULT (decl) == inner);
    8875              : 
    8876       328456 :       if (!tree_node_vals (inner))
    8877            0 :         goto bail;
    8878              : 
    8879       328456 :       if (!tpl_parms_fini (decl, tpl_levels))
    8880            0 :         goto bail;
    8881              :     }
    8882              : 
    8883      1228620 :   if (type && (!tree_node_vals (type)
    8884       177430 :                || (stub_decl && !tree_node_vals (stub_decl))))
    8885            0 :     goto bail;
    8886              : 
    8887      1228620 :   spec_entry spec;
    8888      1228620 :   unsigned spec_flags = 0;
    8889      1228620 :   if (mk & MK_template_mask
    8890       853330 :       || mk == MK_partial
    8891       853330 :       || mk == MK_friend_spec)
    8892              :     {
    8893        12272 :       if (mk == MK_partial)
    8894              :         spec_flags = 2;
    8895              :       else
    8896       375290 :         spec_flags = u ();
    8897              : 
    8898       387562 :       spec.tmpl = tree_node ();
    8899       387562 :       spec.args = tree_node ();
    8900              :     }
    8901              :   /* Hold constraints on the spec field, for a short while.  */
    8902      1228620 :   spec.spec = tree_node ();
    8903              : 
    8904      1229584 :   dump (dumper::TREE) && dump ("Read:%d %C:%N", tag, TREE_CODE (decl), decl);
    8905              : 
    8906      1228620 :   existing = back_refs[~tag];
    8907      1228620 :   bool installed = install_entity (existing);
    8908      1228620 :   bool is_new = existing == decl;
    8909              : 
    8910      1228620 :   if (DECL_LANG_SPECIFIC (inner)
    8911      2279454 :       && 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      1228620 :   unsigned tdef_flags = 0;
    8947      1228620 :   bool is_typedef = false;
    8948      1228620 :   if (!type && TREE_CODE (inner) == TYPE_DECL)
    8949              :     {
    8950       215954 :       tdef_flags = u ();
    8951       215954 :       if (tdef_flags & 1)
    8952       215954 :         is_typedef = true;
    8953              :     }
    8954              : 
    8955      1228620 :   if (is_new)
    8956              :     {
    8957              :       /* A newly discovered node.  */
    8958       795612 :       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       795612 :       if (installed)
    8964              :         {
    8965              :           /* Mark the entity as imported.  */
    8966       505575 :           retrofit_lang_decl (inner);
    8967       505575 :           DECL_MODULE_IMPORT_P (inner) = true;
    8968              :         }
    8969              : 
    8970       795612 :       if (temploid_friend)
    8971           34 :         imported_temploid_friends->put (decl, temploid_friend);
    8972              : 
    8973       795612 :       if (spec.spec)
    8974        33405 :         set_constraints (decl, spec.spec);
    8975              : 
    8976       795612 :       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       795612 :       if (is_typedef)
    8983              :         {
    8984              :           /* Frob it to be ready for cloning.  */
    8985       132267 :           TREE_TYPE (inner) = DECL_ORIGINAL_TYPE (inner);
    8986       132267 :           DECL_ORIGINAL_TYPE (inner) = NULL_TREE;
    8987       132267 :           if (TREE_CODE (TREE_TYPE (inner)) != TU_LOCAL_ENTITY)
    8988              :             {
    8989       132264 :               set_underlying_type (inner);
    8990       132264 :               if (tdef_flags & 2)
    8991              :                 {
    8992              :                   /* Match instantiate_alias_template's handling.  */
    8993        32606 :                   tree type = TREE_TYPE (inner);
    8994        32606 :                   TYPE_DEPENDENT_P (type) = true;
    8995        32606 :                   TYPE_DEPENDENT_P_VALID (type) = true;
    8996        32606 :                   SET_TYPE_STRUCTURAL_EQUALITY (type);
    8997              :                 }
    8998              :             }
    8999              :         }
    9000              : 
    9001       795612 :       if (inner_tag)
    9002              :         /* Set the TEMPLATE_DECL's type.  */
    9003       185271 :         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       795612 :       if (type
    9012       100574 :           && CLASS_TYPE_P (type)
    9013        86339 :           && TYPE_LANG_SPECIFIC (type)
    9014       881951 :           && !(CLASSTYPE_EXPLICIT_INSTANTIATION (type)
    9015          799 :                && CLASSTYPE_INTERFACE_KNOWN (type)
    9016          799 :                && CLASSTYPE_INTERFACE_ONLY (type)))
    9017              :         {
    9018        85592 :           CLASSTYPE_INTERFACE_ONLY (type) = false;
    9019        85592 :           CLASSTYPE_INTERFACE_UNKNOWN (type) = true;
    9020              :         }
    9021              : 
    9022              :       /* Add to specialization tables now that constraints etc are
    9023              :          added.  */
    9024       795612 :       if (mk == MK_partial)
    9025              :         {
    9026         6116 :           bool is_type = TREE_CODE (inner) == TYPE_DECL;
    9027         6116 :           spec.spec = is_type ? type : inner;
    9028         6116 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9029              :         }
    9030       789496 :       else if (mk & MK_template_mask)
    9031              :         {
    9032       224411 :           bool is_type = !(mk & MK_tmpl_decl_mask);
    9033       224411 :           spec.spec = is_type ? type : mk & MK_tmpl_tmpl_mask ? inner : decl;
    9034       224411 :           add_mergeable_specialization (!is_type, &spec, decl, spec_flags);
    9035              :         }
    9036              : 
    9037       795612 :       if (NAMESPACE_SCOPE_P (decl)
    9038       167625 :           && (mk == MK_named || mk == MK_unique
    9039       167625 :               || mk == MK_enum || mk == MK_friend_spec)
    9040       871604 :           && !(VAR_OR_FUNCTION_DECL_P (decl) && DECL_LOCAL_DECL_P (decl)))
    9041        75894 :         add_module_namespace_decl (CP_DECL_CONTEXT (decl), decl);
    9042              : 
    9043       795612 :       if (DECL_ARTIFICIAL (decl)
    9044       221766 :           && TREE_CODE (decl) == FUNCTION_DECL
    9045        24057 :           && !DECL_TEMPLATE_INFO (decl)
    9046        23675 :           && DECL_CONTEXT (decl) && TYPE_P (DECL_CONTEXT (decl))
    9047        23492 :           && TYPE_SIZE (DECL_CONTEXT (decl))
    9048       797174 :           && !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          964 :         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       795612 :       if (state->is_header ()
    9061       795612 :           && 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       795612 :       if (VAR_OR_FUNCTION_DECL_P (inner))
    9069       481723 :         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       795612 :       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       434770 :       dump (dumper::MERGE) && dump ("Deduping %N", existing);
    9090              : 
    9091       433008 :       if (inner_tag)
    9092       143185 :         DECL_TEMPLATE_RESULT (decl) = inner;
    9093              : 
    9094       433008 :       if (type)
    9095              :         {
    9096              :           /* Point at the to-be-discarded type & decl.  */
    9097        76856 :           TYPE_NAME (type) = inner;
    9098        76856 :           TREE_TYPE (inner) = type;
    9099              : 
    9100       153470 :           TYPE_STUB_DECL (type) = stub_decl ? stub_decl : inner;
    9101        76856 :           if (stub_decl)
    9102          242 :             TREE_TYPE (stub_decl) = type;
    9103              : 
    9104        76856 :           tree etype = TREE_TYPE (existing);
    9105              : 
    9106              :           /* Handle separate declarations with different attributes.  */
    9107        76856 :           tree &dattr = TYPE_ATTRIBUTES (type);
    9108        76856 :           tree &eattr = TYPE_ATTRIBUTES (etype);
    9109        76856 :           check_abi_tags (existing, decl, eattr, dattr);
    9110              :           // TODO: handle other conflicting type attributes
    9111        76856 :           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        76856 :           if ((spec_flags & 2)
    9117        76856 :               && TYPE_CANONICAL (type) != TYPE_CANONICAL (etype))
    9118            3 :             SET_TYPE_STRUCTURAL_EQUALITY (type);
    9119              :         }
    9120              : 
    9121       433008 :       if (inner_tag)
    9122              :         /* Set the TEMPLATE_DECL's type.  */
    9123       143185 :         TREE_TYPE (decl) = TREE_TYPE (inner);
    9124              : 
    9125       433008 :       if (!is_matching_decl (existing, decl, is_typedef))
    9126           42 :         unmatched_duplicate (existing);
    9127              : 
    9128       433008 :       if (TREE_CODE (inner) == FUNCTION_DECL)
    9129              :         {
    9130       197896 :           tree e_inner = STRIP_TEMPLATE (existing);
    9131       197896 :           for (auto parm = DECL_ARGUMENTS (inner);
    9132       591668 :                parm; parm = DECL_CHAIN (parm))
    9133       393772 :             DECL_CONTEXT (parm) = e_inner;
    9134              :         }
    9135              : 
    9136              :       /* And our result is the existing node.  */
    9137       433008 :       decl = existing;
    9138              :     }
    9139              : 
    9140      1228620 :   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      1228620 :   if (is_typedef)
    9153              :     {
    9154              :       /* Insert the type into the array now.  */
    9155       215954 :       tag = insert (TREE_TYPE (decl));
    9156       215954 :       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      1228620 :   unused = saved_unused;
    9162              : 
    9163      1228620 :   if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
    9164              :     {
    9165       104227 :       unsigned flags = u ();
    9166              : 
    9167       104227 :       if (is_new)
    9168              :         {
    9169        64198 :           bool cloned_p = flags & 1;
    9170        64298 :           dump (dumper::TREE) && dump ("CDTOR %N is %scloned",
    9171              :                                        decl, cloned_p ? "" : "not ");
    9172        64198 :           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        45658 :               bool up = (CLASSTYPE_MEMBER_VEC (DECL_CONTEXT (decl))
    9178        45658 :                          && !primary_template_specialization_p (decl));
    9179        45658 :               build_cdtor_clones (decl, flags & 2, flags & 4, up);
    9180              :             }
    9181              :         }
    9182              :     }
    9183              : 
    9184      1228620 :   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      1228620 :   if (!NAMESPACE_SCOPE_P (inner)
    9192       917003 :       && ((TREE_CODE (inner) == TYPE_DECL
    9193       213140 :            && !is_typedef
    9194        38542 :            && TYPE_NAME (TREE_TYPE (inner)) == inner)
    9195       878461 :           || TREE_CODE (inner) == FUNCTION_DECL)
    9196      1620298 :       && 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     17852947 : trees_out::decl_node (tree decl, walk_kind ref)
    9251              : {
    9252     17852947 :   gcc_checking_assert (DECL_P (decl) && !DECL_TEMPLATE_PARM_P (decl)
    9253              :                        && DECL_CONTEXT (decl));
    9254              : 
    9255     17852947 :   if (ref == WK_value)
    9256              :     {
    9257      1698156 :       depset *dep = dep_hash->find_dependency (decl);
    9258      1698156 :       decl_value (decl, dep);
    9259      1698156 :       return false;
    9260              :     }
    9261              : 
    9262     16154791 :   switch (TREE_CODE (decl))
    9263              :     {
    9264              :     default:
    9265              :       break;
    9266              : 
    9267      1706343 :     case FUNCTION_DECL:
    9268      1706343 :       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       100277 :     case CONST_DECL:
    9316       100277 :       {
    9317              :         /* If I end up cloning enum decls, implementing C++20 using
    9318              :            E::v, this will need tweaking.   */
    9319       100277 :         if (streaming_p ())
    9320        25001 :           i (tt_enum_decl);
    9321       100277 :         tree ctx = DECL_CONTEXT (decl);
    9322       100277 :         gcc_checking_assert (TREE_CODE (ctx) == ENUMERAL_TYPE);
    9323       100277 :         tree_node (ctx);
    9324       100277 :         tree_node (DECL_NAME (decl));
    9325              : 
    9326       100277 :         int tag = insert (decl);
    9327       100277 :         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       250020 :     case FIELD_DECL:
    9340       250020 :       {
    9341       250020 :         if (streaming_p ())
    9342        18897 :           i (tt_data_member);
    9343              : 
    9344       250020 :         tree ctx = DECL_CONTEXT (decl);
    9345       250020 :         tree_node (ctx);
    9346              : 
    9347       250020 :         tree name = NULL_TREE;
    9348              : 
    9349       250020 :         if (TREE_CODE (decl) == USING_DECL)
    9350              :           ;
    9351              :         else
    9352              :           {
    9353       248183 :             name = DECL_NAME (decl);
    9354       482082 :             if (name && IDENTIFIER_ANON_P (name))
    9355              :               name = NULL_TREE;
    9356              :           }
    9357              : 
    9358       250020 :         tree_node (name);
    9359       250020 :         if (!name && streaming_p ())
    9360              :           {
    9361         1737 :             unsigned ix = get_field_ident (ctx, decl);
    9362         1737 :             u (ix);
    9363              :           }
    9364              : 
    9365       250020 :         int tag = insert (decl);
    9366       250020 :         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       746633 :       break;
    9372              : 
    9373       746633 :     case VAR_DECL:
    9374       746633 :       gcc_checking_assert (!DECL_LOCAL_DECL_P (decl));
    9375       746633 :       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       741914 :       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       732532 :       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      5497180 :     case TYPE_DECL:
    9455      5497180 :       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      5017528 :       if (!streaming_p () && DECL_ORIGINAL_TYPE (decl)
    9460      6666946 :           && (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_IMPORT_P (decl)))
    9461          415 :         tree_node (DECL_ORIGINAL_TYPE (decl));
    9462              :       break;
    9463              :     }
    9464              : 
    9465     14668082 :   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     14668082 :   if (DECL_CLONED_FUNCTION_P (decl))
    9488              :     {
    9489       507641 :       tree target = get_clone_target (decl);
    9490       507641 :       if (streaming_p ())
    9491       241310 :         i (tt_clone_ref);
    9492              : 
    9493       507641 :       tree_node (target);
    9494       507641 :       tree_node (DECL_NAME (decl));
    9495       507641 :       if (DECL_VIRTUAL_P (decl))
    9496        33865 :         tree_node (DECL_VINDEX (decl));
    9497       507641 :       int tag = insert (decl);
    9498       507641 :       if (streaming_p ())
    9499       241310 :         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     14160441 :   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     14160441 :   int use_tpl = -1;
    9516     14160441 :   tree ti = node_template_info (decl, use_tpl);
    9517     14160441 :   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      5145249 :   if (ti && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL
    9527     19305576 :       && DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == decl)
    9528              :     {
    9529              :       tpl = TI_TEMPLATE (ti);
    9530      1298635 :     partial_template:
    9531      1298635 :       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      1298635 :       tree_node (tpl);
    9539              : 
    9540              :       /* Streaming TPL caused us to visit DECL and maybe its type,
    9541              :          if it wasn't TU-local.  */
    9542      1298635 :       if (CHECKING_P && !has_tu_local_dep (tpl))
    9543              :         {
    9544      1298608 :           gcc_checking_assert (TREE_VISITED (decl));
    9545      1298608 :           if (DECL_IMPLICIT_TYPEDEF_P (decl))
    9546       669700 :             gcc_checking_assert (TREE_VISITED (TREE_TYPE (decl)));
    9547              :         }
    9548              :       return false;
    9549              :     }
    9550              : 
    9551     12993511 :   tree ctx = CP_DECL_CONTEXT (decl);
    9552     12993511 :   depset *dep = NULL;
    9553     12993511 :   if (streaming_p ())
    9554      2176577 :     dep = dep_hash->find_dependency (decl);
    9555     10816934 :   else if (TREE_CODE (ctx) != FUNCTION_DECL
    9556       440094 :            || TREE_CODE (decl) == TEMPLATE_DECL
    9557       398675 :            || DECL_IMPLICIT_TYPEDEF_P (decl)
    9558     11173627 :            || (DECL_LANG_SPECIFIC (decl)
    9559       173527 :                && DECL_MODULE_IMPORT_P (decl)))
    9560              :     {
    9561     10460241 :       auto kind = (TREE_CODE (decl) == NAMESPACE_DECL
    9562       796201 :                    && !DECL_NAMESPACE_ALIAS (decl)
    9563     10460241 :                    ? depset::EK_NAMESPACE : depset::EK_DECL);
    9564     10460241 :       dep = dep_hash->add_dependency (decl, kind);
    9565              :     }
    9566              : 
    9567     12636818 :   if (!dep || dep->is_tu_local ())
    9568              :     {
    9569              :       /* Some internal entity of context.  Do by value.  */
    9570       688969 :       decl_value (decl, dep);
    9571       688969 :       return false;
    9572              :     }
    9573              : 
    9574     12304542 :   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       131705 :       depset *redirect = dep->deps[0];
    9579       131705 :       gcc_checking_assert (redirect->get_entity_kind () == depset::EK_PARTIAL);
    9580       131705 :       tpl = redirect->get_entity ();
    9581       131705 :       goto partial_template;
    9582              :     }
    9583              : 
    9584     12172837 :   if (streaming_p ())
    9585              :     {
    9586              :       /* Locate the entity.  */
    9587      1844561 :       unsigned index = dep->cluster;
    9588      1844561 :       unsigned import = 0;
    9589              : 
    9590      1844561 :       if (dep->is_import ())
    9591         4467 :         import = dep->section;
    9592      1840094 :       else if (CHECKING_P)
    9593              :         /* It should be what we put there.  */
    9594      1840094 :         gcc_checking_assert (index == ~import_entity_index (decl));
    9595              : 
    9596              : #if CHECKING_P
    9597         4467 :       gcc_assert (!import || importedness >= 0);
    9598              : #endif
    9599      1844561 :       i (tt_entity);
    9600      1844561 :       u (import);
    9601      1844561 :       u (index);
    9602              :     }
    9603              : 
    9604     12172837 :   int tag = insert (decl);
    9605     12172837 :   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     12172837 :   add_indirects (decl);
    9624              : 
    9625     12172837 :   return false;
    9626              : }
    9627              : 
    9628              : void
    9629     14477096 : trees_out::type_node (tree type)
    9630              : {
    9631     14477096 :   gcc_assert (TYPE_P (type));
    9632              : 
    9633     14477096 :   tree root = (TYPE_NAME (type)
    9634     14477096 :                ? TREE_TYPE (TYPE_NAME (type)) : TYPE_MAIN_VARIANT (type));
    9635     14477096 :   gcc_checking_assert (root);
    9636              : 
    9637     14477096 :   if (type != root)
    9638              :     {
    9639      3236304 :       if (streaming_p ())
    9640       670770 :         i (tt_variant_type);
    9641      3236304 :       tree_node (root);
    9642              : 
    9643      3236304 :       int flags = -1;
    9644              : 
    9645      3236304 :       if (TREE_CODE (type) == FUNCTION_TYPE
    9646      3236304 :           || TREE_CODE (type) == METHOD_TYPE)
    9647              :         {
    9648       754838 :           int quals = type_memfn_quals (type);
    9649       754838 :           int rquals = type_memfn_rqual (type);
    9650       754838 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9651       754838 :           bool late = TYPE_HAS_LATE_RETURN_TYPE (type);
    9652              : 
    9653       754838 :           if (raises != TYPE_RAISES_EXCEPTIONS (root)
    9654        25093 :               || rquals != type_memfn_rqual (root)
    9655        18013 :               || quals != type_memfn_quals (root)
    9656       772833 :               || late != TYPE_HAS_LATE_RETURN_TYPE (root))
    9657       754838 :             flags = rquals | (int (late) << 2) | (quals << 3);
    9658              :         }
    9659              :       else
    9660              :         {
    9661      2481466 :           if (TYPE_USER_ALIGN (type))
    9662        28268 :             flags = TYPE_ALIGN_RAW (type);
    9663              :         }
    9664              : 
    9665      3236304 :       if (streaming_p ())
    9666       670770 :         i (flags);
    9667              : 
    9668      3236304 :       if (flags < 0)
    9669              :         ;
    9670       783106 :       else if (TREE_CODE (type) == FUNCTION_TYPE
    9671       783106 :                || TREE_CODE (type) == METHOD_TYPE)
    9672              :         {
    9673       754838 :           tree raises = TYPE_RAISES_EXCEPTIONS (type);
    9674       754838 :           if (raises == TYPE_RAISES_EXCEPTIONS (root))
    9675        25093 :             raises = error_mark_node;
    9676       754838 :           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      3236304 :       gcc_checking_assert (TYPE_ATTRIBUTES (type)
    9682              :                            == TYPE_ATTRIBUTES (TYPE_MAIN_VARIANT (type)));
    9683              : 
    9684      3236304 :       if (streaming_p ())
    9685              :         {
    9686              :           /* Qualifiers.  */
    9687       670770 :           int rquals = cp_type_quals (root);
    9688       670770 :           int quals = cp_type_quals (type);
    9689       670770 :           if (quals == rquals)
    9690       308930 :             quals = -1;
    9691       670770 :           i (quals);
    9692              :         }
    9693              : 
    9694      3236304 :       if (ref_node (type) != WK_none)
    9695              :         {
    9696      3236304 :           int tag = insert (type);
    9697      3236304 :           if (streaming_p ())
    9698              :             {
    9699       670770 :               i (0);
    9700       670770 :               dump (dumper::TREE)
    9701          203 :                 && dump ("Wrote:%d variant type %C", tag, TREE_CODE (type));
    9702              :             }
    9703              :         }
    9704              :       return;
    9705              :     }
    9706              : 
    9707     11240792 :   if (tree name = TYPE_NAME (type))
    9708      4447427 :     if ((TREE_CODE (name) == TYPE_DECL && DECL_ORIGINAL_TYPE (name))
    9709      3496051 :         || DECL_TEMPLATE_PARM_P (name)
    9710      2384072 :         || TREE_CODE (type) == RECORD_TYPE
    9711       417219 :         || TREE_CODE (type) == UNION_TYPE
    9712      4855741 :         || TREE_CODE (type) == ENUMERAL_TYPE)
    9713              :       {
    9714      4160360 :         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      4160360 :         if (streaming_p ())
    9721              :           {
    9722       286696 :             i (tt_typedef_type);
    9723       286696 :             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      4160360 :         tree_node (name);
    9729      4160360 :         if (streaming_p ())
    9730       286696 :           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      4160360 :         gcc_checking_assert (TREE_VISITED (type) || has_tu_local_dep (name));
    9736              :         return;
    9737              :       }
    9738              : 
    9739      7080432 :   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      7074489 :   if (streaming_p ())
    9753              :     {
    9754      2256447 :       u (tt_derived_type);
    9755      2256447 :       u (TREE_CODE (type));
    9756              :     }
    9757              : 
    9758      7074489 :   tree_node (TREE_TYPE (type));
    9759      7074489 :   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       106313 :     case ARRAY_TYPE:
    9767       106313 :       tree_node (TYPE_DOMAIN (type));
    9768       106313 :       if (streaming_p ())
    9769              :         /* Dependent arrays are constructed with TYPE_DEPENENT_P
    9770              :            already set.  */
    9771        34447 :         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        99529 :     case INTEGER_TYPE:
    9785        99529 :       if (TREE_TYPE (type))
    9786              :         {
    9787              :           /* A range type (representing an array domain).  */
    9788        92194 :           tree_node (TYPE_MIN_VALUE (type));
    9789        92194 :           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      1537880 :     case METHOD_TYPE:
    9805      1537880 :     case FUNCTION_TYPE:
    9806      1537880 :       {
    9807      1537880 :         gcc_checking_assert (type_memfn_rqual (type) == REF_QUAL_NONE);
    9808              : 
    9809      1537880 :         tree arg_types = TYPE_ARG_TYPES (type);
    9810      1537880 :         if (TREE_CODE (type) == METHOD_TYPE)
    9811              :           {
    9812      1002075 :             tree_node (TREE_TYPE (TREE_VALUE (arg_types)));
    9813      1002075 :             arg_types = TREE_CHAIN (arg_types);
    9814              :           }
    9815      1537880 :         tree_node (arg_types);
    9816              :       }
    9817      1537880 :       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      1217131 :     case REFERENCE_TYPE:
    9828      1217131 :       if (streaming_p ())
    9829       270695 :         u (TYPE_REF_IS_RVALUE (type));
    9830              :       break;
    9831              : 
    9832      1442204 :     case DECLTYPE_TYPE:
    9833      1442204 :     case TYPEOF_TYPE:
    9834      1442204 :     case DEPENDENT_OPERATOR_TYPE:
    9835      1442204 :       tree_node (TYPE_VALUES_RAW (type));
    9836      1442204 :       if (TREE_CODE (type) == DECLTYPE_TYPE)
    9837              :         /* We stash a whole bunch of things into decltype's
    9838              :            flags.  */
    9839       115204 :         if (streaming_p ())
    9840        39195 :           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       287792 :     case TYPENAME_TYPE:
    9866       287792 :       {
    9867       287792 :         tree_node (TYPE_CONTEXT (type));
    9868       287792 :         tree_node (DECL_NAME (TYPE_NAME (type)));
    9869       287792 :         tree_node (TYPENAME_TYPE_FULLNAME (type));
    9870       287792 :         if (streaming_p ())
    9871        99559 :           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      7074489 :   tree_node (TYPE_ATTRIBUTES (type));
    9908              : 
    9909              :   /* We may have met the type during emitting the above.  */
    9910      7074489 :   if (ref_node (type) != WK_none)
    9911              :     {
    9912      6410989 :       int tag = insert (type);
    9913      6410989 :       if (streaming_p ())
    9914              :         {
    9915      1946283 :           i (0);
    9916      1946283 :           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     44362011 : 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     44362011 :   gcc_checking_assert (!TYPE_P (t));
    9933              : 
    9934     44362011 :   if (DECL_P (t))
    9935              :     /* No template, type, var or function, except anonymous
    9936              :        non-context vars and types.  */
    9937      1148964 :     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     67023769 :   if (is_initial_scan () && EXPR_P (t))
    9947      9021301 :     dep_hash->add_dependent_adl_entities (t);
    9948              : 
    9949     44362011 :   if (streaming_p ())
    9950              :     {
    9951              :       /* A new node -> tt_node.  */
    9952     17601839 :       tree_val_count++;
    9953     17601839 :       i (tt_node);
    9954     17601839 :       start (t);
    9955     17601839 :       tree_node_bools (t);
    9956              :     }
    9957              : 
    9958     44362011 :   if (TREE_CODE (t) == TREE_BINFO)
    9959              :     /* Binfos are decl-like and need merging information.  */
    9960       298728 :     binfo_mergeable (t);
    9961              : 
    9962     44362011 :   int tag = insert (t, WK_value);
    9963     44362011 :   if (streaming_p ())
    9964     17601839 :     dump (dumper::TREE)
    9965         2823 :       && dump ("Writing tree:%d %C:%N", tag, TREE_CODE (t), t);
    9966              : 
    9967     44362011 :   int type_tag = 0;
    9968     44362011 :   tree type = NULL_TREE;
    9969     44362011 :   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     44362011 :   tree_node_vals (t);
    9998              : 
    9999     44362011 :   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     44362011 :   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     44362011 :   if (streaming_p ())
   10019     17604662 :     dump (dumper::TREE) && dump ("Written tree:%d %C:%N", tag, TREE_CODE (t), t);
   10020     44362011 : }
   10021              : 
   10022              : tree
   10023     12059535 : trees_in::tree_value ()
   10024              : {
   10025     12059535 :   tree t = start ();
   10026     12059535 :   if (!t || !tree_node_bools (t))
   10027              :     return NULL_TREE;
   10028              : 
   10029     12059535 :   tree existing = t;
   10030     12059535 :   if (TREE_CODE (t) == TREE_BINFO)
   10031              :     {
   10032        93779 :       tree type;
   10033        93779 :       unsigned ix = binfo_mergeable (&type);
   10034        93779 :       if (TYPE_BINFO (type))
   10035              :         {
   10036              :           /* We already have a definition, this must be a duplicate.  */
   10037        41007 :           dump (dumper::MERGE)
   10038          271 :             && dump ("Deduping binfo %N[%u]", type, ix);
   10039        41007 :           existing = TYPE_BINFO (type);
   10040        56036 :           while (existing && ix--)
   10041        15029 :             existing = TREE_CHAIN (existing);
   10042        41007 :           if (existing)
   10043        41007 :             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     12059535 :   int tag = insert (existing);
   10053     12059535 :   dump (dumper::TREE)
   10054         3677 :     && dump ("Reading tree:%d %C", tag, TREE_CODE (t));
   10055              : 
   10056     12059535 :   int type_tag = 0;
   10057     12059535 :   tree type = NULL_TREE;
   10058     12059535 :   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     12059535 :   if (!tree_node_vals (t))
   10081            0 :     goto bail;
   10082              : 
   10083     12059535 :   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     12059535 :   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     12059535 :   if (TREE_CODE (t) == LAMBDA_EXPR
   10106     12059535 :       && 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     12063212 :   dump (dumper::TREE) && dump ("Read tree:%d %C:%N", tag, TREE_CODE (t), t);
   10113              : 
   10114     12059535 :   if (TREE_CODE (existing) == INTEGER_CST && !TREE_OVERFLOW (existing))
   10115              :     {
   10116       605648 :       existing = cache_integer_cst (t, true);
   10117       605648 :       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      1298996 : 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      1298996 :   if (DECL_CONTEXT (decl)
   10131      1298996 :       && (TREE_CODE (decl) == FIELD_DECL
   10132      1298993 :           || TREE_CODE (decl) == CONST_DECL))
   10133            3 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   10134              : 
   10135      1298996 :   depset *dep = dep_hash->find_dependency (decl);
   10136      1298996 :   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        15996 :       int use_tpl = -1;
   10141        15996 :       if (tree ti = node_template_info (decl, use_tpl))
   10142         2622 :         dep = dep_hash->find_dependency (TI_TEMPLATE (ti));
   10143              :     }
   10144              : 
   10145      1298996 :   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    374994187 : trees_out::tree_node (tree t)
   10206              : {
   10207    374994187 :   dump.indent ();
   10208    374994187 :   walk_kind ref = ref_node (t);
   10209    374994187 :   if (ref == WK_none)
   10210    286115692 :     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    103911492 :   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     88878337 :   if (ref != WK_normal)
   10252      2036390 :     goto skip_normal;
   10253              : 
   10254     86841947 :   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     10220222 :       int code = tt_id;
   10259     10220222 :       if (IDENTIFIER_ANON_P (t))
   10260        40030 :         code = IDENTIFIER_LAMBDA_P (t) ? tt_lambda_id : tt_anon_id;
   10261     10180192 :       else if (IDENTIFIER_INTERNAL_P (t))
   10262              :         code = tt_internal_id;
   10263     10180178 :       else if (IDENTIFIER_CONV_OP_P (t))
   10264        15436 :         code = tt_conv_id;
   10265              : 
   10266     10220222 :       if (streaming_p ())
   10267      1981972 :         i (code);
   10268              : 
   10269     10220222 :       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     10204786 :       else if (code == tt_id && streaming_p ())
   10276      1965277 :         str (IDENTIFIER_POINTER (t), IDENTIFIER_LENGTH (t));
   10277      8239509 :       else if (code == tt_internal_id && streaming_p ())
   10278            7 :         str (prefix_for_internal_label (t));
   10279              : 
   10280     10220222 :       int tag = insert (t);
   10281     10220222 :       if (streaming_p ())
   10282              :         {
   10283              :           /* We know the ordering of the 5 id tags.  */
   10284      1981972 :           static const char *const kinds[] =
   10285              :             {"", "conv_op ", "anon ", "lambda ", "internal "};
   10286      1981972 :           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     10220222 :       goto done;
   10292              :     }
   10293              : 
   10294     76621725 :   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        89604 :       if (streaming_p ())
   10301         2889 :         i (tt_binfo);
   10302        89604 :       binfo_mergeable (t);
   10303        89604 :       gcc_checking_assert (!TREE_VISITED (t));
   10304        89604 :       int tag = insert (t);
   10305        89604 :       if (streaming_p ())
   10306         2889 :         dump (dumper::TREE) && dump ("Inserting binfo:%d %N", tag, t);
   10307        89604 :       goto done;
   10308              :     }
   10309              : 
   10310     76532121 :   if (TREE_CODE (t) == INTEGER_CST
   10311      5392280 :       && !TREE_OVERFLOW (t)
   10312     81924401 :       && 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     76480230 :   if (TYPE_P (t))
   10334              :     {
   10335     14477096 :       type_node (t);
   10336     14477096 :       goto done;
   10337              :     }
   10338              : 
   10339     62003134 :   if (DECL_P (t))
   10340              :     {
   10341     19128321 :       if (DECL_TEMPLATE_PARM_P (t))
   10342              :         {
   10343      2938932 :           tpl_parm_value (t);
   10344      2938932 :           goto done;
   10345              :         }
   10346              : 
   10347     16189389 :       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     42874813 :  skip_normal:
   10393     61065994 :   if (DECL_P (t) && !decl_node (t, ref))
   10394     16738581 :     goto done;
   10395              : 
   10396              :   /* Otherwise by value */
   10397     44362011 :  by_value:
   10398     44362011 :   tree_value (t);
   10399              : 
   10400    374994187 :  done:
   10401              :   /* And, breath out.  */
   10402    374994187 :   dump.outdent ();
   10403    374994187 : }
   10404              : 
   10405              : /* Stream in a tree node.  */
   10406              : 
   10407              : tree
   10408     97900515 : trees_in::tree_node (bool is_use)
   10409              : {
   10410     97900515 :   if (get_overrun ())
   10411              :     return NULL_TREE;
   10412              : 
   10413     97900515 :   dump.indent ();
   10414     97900515 :   int tag = i ();
   10415     97900515 :   tree res = NULL_TREE;
   10416     97900515 :   switch (tag)
   10417              :     {
   10418     32129300 :     default:
   10419              :       /* backref, pull it out of the map.  */
   10420     32129300 :       res = back_ref (tag);
   10421     32129300 :       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      7569308 :     case tt_fixed:
   10440              :       /* A fixed ref, find it in the fixed_ref array.   */
   10441      7569308 :       {
   10442      7569308 :         unsigned fix = u ();
   10443      7569308 :         if (fix < (*fixed_trees).length ())
   10444              :           {
   10445      7569308 :             res = (*fixed_trees)[fix];
   10446      7569308 :             dump (dumper::TREE) && dump ("Read fixed:%u %C:%N%S", fix,
   10447         5046 :                                          TREE_CODE (res), res, res);
   10448              :           }
   10449              : 
   10450      7569308 :         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     12059535 :     case tt_node:
   10469              :       /* A new node.  Stream it in.  */
   10470     12059535 :       res = tree_value ();
   10471     12059535 :       break;
   10472              : 
   10473      1228620 :     case tt_decl:
   10474              :       /* A new decl.  Stream it in.  */
   10475      1228620 :       res = decl_value ();
   10476      1228620 :       break;
   10477              : 
   10478       433278 :     case tt_tpl_parm:
   10479              :       /* A template parameter.  Stream it in.  */
   10480       433278 :       res = tpl_parm_value ();
   10481       433278 :       break;
   10482              : 
   10483      1272314 :     case tt_id:
   10484              :       /* An identifier node.  */
   10485      1272314 :       {
   10486      1272314 :         size_t l;
   10487      1272314 :         const char *chars = str (&l);
   10488      1272314 :         res = get_identifier_with_length (chars, l);
   10489      1272314 :         int tag = insert (res);
   10490      1272314 :         dump (dumper::TREE)
   10491         1488 :           && dump ("Read identifier:%d %N", tag, res);
   10492              :       }
   10493      1272314 :       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       194068 :     case tt_typedef_type:
   10536       194068 :       res = tree_node ();
   10537       194068 :       if (res)
   10538              :         {
   10539       194068 :           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       194068 :           if (TREE_CODE (res) != TU_LOCAL_ENTITY)
   10544       194067 :             res = TREE_TYPE (res);
   10545              :         }
   10546              :       break;
   10547              : 
   10548      1512925 :     case tt_derived_type:
   10549              :       /* A type derived from some other type.  */
   10550      1512925 :       {
   10551      1512925 :         enum tree_code code = tree_code (u ());
   10552      1512925 :         res = tree_node ();
   10553              : 
   10554      1512925 :         switch (code)
   10555              :           {
   10556            0 :           default:
   10557            0 :             set_overrun ();
   10558            0 :             break;
   10559              : 
   10560        21973 :           case ARRAY_TYPE:
   10561        21973 :             {
   10562        21973 :               tree elt_type = res;
   10563        21973 :               tree domain = tree_node ();
   10564        21973 :               int dep = u ();
   10565        21973 :               if (!get_overrun ())
   10566              :                 {
   10567        21973 :                   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        21973 :                   if (!dep
   10573        19111 :                       && !COMPLETE_TYPE_P (elt_type)
   10574           32 :                       && CLASS_TYPE_P (elt_type)
   10575           32 :                       && DECL_LANG_SPECIFIC (TYPE_NAME (elt_type))
   10576        22005 :                       && 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        20440 :           case INTEGER_TYPE:
   10596        20440 :             if (res)
   10597              :               {
   10598              :                 /* A range type (representing an array domain).  */
   10599        19151 :                 tree min = tree_node ();
   10600        19151 :                 tree max = tree_node ();
   10601              : 
   10602        19151 :                 if (!get_overrun ())
   10603        19151 :                   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       426736 :           case FUNCTION_TYPE:
   10615       426736 :           case METHOD_TYPE:
   10616       426736 :             {
   10617       426736 :               tree klass =  code == METHOD_TYPE ? tree_node () : NULL_TREE;
   10618       426736 :               tree args = tree_node ();
   10619       426736 :               if (!get_overrun ())
   10620              :                 {
   10621       426736 :                   if (klass)
   10622       270139 :                     res = build_method_type_directly (klass, res, args);
   10623              :                   else
   10624       156597 :                     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       211132 :           case POINTER_TYPE:
   10638       211132 :             if (!get_overrun ())
   10639       211132 :               res = build_pointer_type (res);
   10640              :             break;
   10641              : 
   10642       177573 :           case REFERENCE_TYPE:
   10643       177573 :             {
   10644       177573 :               bool rval = bool (u ());
   10645       177573 :               if (!get_overrun ())
   10646       177573 :                 res = cp_build_reference_type (res, rval);
   10647              :             }
   10648              :             break;
   10649              : 
   10650       458934 :           case DECLTYPE_TYPE:
   10651       458934 :           case TYPEOF_TYPE:
   10652       458934 :           case DEPENDENT_OPERATOR_TYPE:
   10653       458934 :             {
   10654       458934 :               tree expr = tree_node ();
   10655       458934 :               if (!get_overrun ())
   10656              :                 {
   10657       458934 :                   res = cxx_make_type (code);
   10658       458934 :                   TYPE_VALUES_RAW (res) = expr;
   10659       458934 :                   if (code == DECLTYPE_TYPE)
   10660        23875 :                     tree_node_bools (res);
   10661       458934 :                   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        61714 :           case TYPE_ARGUMENT_PACK:
   10683        61714 :             if (!get_overrun ())
   10684              :               {
   10685        61714 :                 tree pack = cxx_make_type (TYPE_ARGUMENT_PACK);
   10686        61714 :                 ARGUMENT_PACK_ARGS (pack) = res;
   10687        61714 :                 res = pack;
   10688              :               }
   10689              :             break;
   10690              : 
   10691        65196 :           case TYPE_PACK_EXPANSION:
   10692        65196 :             {
   10693        65196 :               bool local = u ();
   10694        65196 :               tree param_packs = tree_node ();
   10695        65196 :               tree extra_args = tree_node ();
   10696        65196 :               if (!get_overrun ())
   10697              :                 {
   10698        65196 :                   tree expn = cxx_make_type (TYPE_PACK_EXPANSION);
   10699        65196 :                   SET_TYPE_STRUCTURAL_EQUALITY (expn);
   10700        65196 :                   PACK_EXPANSION_PATTERN (expn) = res;
   10701       130392 :                   PACK_EXPANSION_PARAMETER_PACKS (expn) = param_packs;
   10702        65196 :                   PACK_EXPANSION_EXTRA_ARGS (expn) = extra_args;
   10703        65196 :                   PACK_EXPANSION_LOCAL_P (expn) = local;
   10704        65196 :                   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        66226 :           case TYPENAME_TYPE:
   10719        66226 :             {
   10720        66226 :               tree ctx = tree_node ();
   10721        66226 :               tree name = tree_node ();
   10722        66226 :               tree fullname = tree_node ();
   10723        66226 :               enum tag_types tag_type = tag_types (u ());
   10724              : 
   10725        66226 :               if (!get_overrun ())
   10726        66226 :                 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      1512925 :         if (tree attribs = tree_node ())
   10775        17292 :           res = cp_build_type_attribute_variant (res, attribs);
   10776              : 
   10777      1512925 :         int tag = i ();
   10778      1512925 :         if (!tag)
   10779              :           {
   10780      1295397 :             tag = insert (res);
   10781      1295397 :             if (res)
   10782      1295397 :               dump (dumper::TREE)
   10783          678 :                 && dump ("Created:%d derived type %C", tag, code);
   10784              :           }
   10785              :         else
   10786       217528 :           res = back_ref (tag);
   10787              :       }
   10788              :       break;
   10789              : 
   10790       438873 :     case tt_variant_type:
   10791              :       /* Variant of some type.  */
   10792       438873 :       {
   10793       438873 :         res = tree_node ();
   10794       438873 :         int flags = i ();
   10795       438873 :         if (get_overrun ())
   10796              :           ;
   10797       438873 :         else if (flags < 0)
   10798              :           /* No change.  */;
   10799       209981 :         else if (TREE_CODE (res) == FUNCTION_TYPE
   10800       209981 :                  || TREE_CODE (res) == METHOD_TYPE)
   10801              :           {
   10802       208269 :             cp_ref_qualifier rqual = cp_ref_qualifier (flags & 3);
   10803       208269 :             bool late = (flags >> 2) & 1;
   10804       208269 :             cp_cv_quals quals = cp_cv_quals (flags >> 3);
   10805              : 
   10806       208269 :             tree raises = tree_node ();
   10807       208269 :             if (raises == error_mark_node)
   10808         7809 :               raises = TYPE_RAISES_EXCEPTIONS (res);
   10809              : 
   10810       208269 :             res = build_cp_fntype_variant (res, rqual, raises, late);
   10811       208269 :             if (TREE_CODE (res) == FUNCTION_TYPE)
   10812        73683 :               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       438873 :         int quals = i ();
   10821       438873 :         if (quals >= 0 && !get_overrun ())
   10822       230176 :           res = cp_build_qualified_type (res,
   10823              :                                          static_cast<cv_qualifier> (quals));
   10824              : 
   10825       438873 :         int tag = i ();
   10826       438873 :         if (!tag)
   10827              :           {
   10828       438873 :             tag = insert (res);
   10829       438873 :             if (res)
   10830       438873 :               dump (dumper::TREE)
   10831          292 :                 && dump ("Created:%d variant type %C", tag, TREE_CODE (res));
   10832              :           }
   10833              :         else
   10834            0 :           res = back_ref (tag);
   10835              :       }
   10836              :       break;
   10837              : 
   10838         5201 :     case tt_tinfo_var:
   10839         5201 :     case tt_tinfo_typedef:
   10840              :       /* A tinfo var or typedef.  */
   10841         5201 :       {
   10842         5201 :         bool is_var = tag == tt_tinfo_var;
   10843         5201 :         unsigned ix = u ();
   10844         5201 :         tree type = NULL_TREE;
   10845              : 
   10846         5201 :         if (is_var)
   10847              :           {
   10848         3119 :             tree name = tree_node ();
   10849         3119 :             type = tree_node ();
   10850              : 
   10851         3119 :             if (!get_overrun ())
   10852         3119 :               res = get_tinfo_decl_direct (type, name, int (ix));
   10853              :           }
   10854              :         else
   10855              :           {
   10856         2082 :             if (!get_overrun ())
   10857              :               {
   10858         2082 :                 type = get_pseudo_tinfo_type (ix);
   10859         2082 :                 res = TYPE_NAME (type);
   10860              :               }
   10861              :           }
   10862         5201 :         if (res)
   10863              :           {
   10864         5201 :             int tag = insert (res);
   10865         5201 :             dump (dumper::TREE)
   10866           36 :               && dump ("Created tinfo_%s:%d %S:%u for %N",
   10867              :                        is_var ? "var" : "decl", tag, res, ix, type);
   10868         5201 :             if (!is_var)
   10869              :               {
   10870         2082 :                 tag = insert (type);
   10871         2082 :                 dump (dumper::TREE)
   10872           12 :                   && dump ("Created tinfo_type:%d %u %N", tag, ix, type);
   10873              :               }
   10874              :           }
   10875              :       }
   10876              :       break;
   10877              : 
   10878         1164 :     case tt_ptrmem_type:
   10879              :       /* A pointer to member function.  */
   10880         1164 :       {
   10881         1164 :         tree type = tree_node ();
   10882         1164 :         if (type && TREE_CODE (type) == POINTER_TYPE
   10883         2328 :             && TREE_CODE (TREE_TYPE (type)) == METHOD_TYPE)
   10884              :           {
   10885         1164 :             res = build_ptrmemfunc_type (type);
   10886         1164 :             int tag = insert (res);
   10887         1167 :             dump (dumper::TREE) && dump ("Created:%d ptrmem type", tag);
   10888              :           }
   10889              :         else
   10890            0 :           set_overrun ();
   10891              :       }
   10892              :       break;
   10893              : 
   10894            9 :     case tt_nttp_var:
   10895              :       /* An NTTP object. */
   10896            9 :       {
   10897            9 :         tree init = tree_node ();
   10898            9 :         tree name = tree_node ();
   10899            9 :         if (!get_overrun ())
   10900              :           {
   10901              :             /* We don't want to check the initializer as that may require
   10902              :                name lookup, which could recursively start lazy loading.
   10903              :                Instead we know that INIT is already valid so we can just
   10904              :                apply that directly.  */
   10905            9 :             res = get_template_parm_object (init, name, /*check_init=*/false);
   10906            9 :             int tag = insert (res);
   10907            9 :             dump (dumper::TREE)
   10908            0 :               && dump ("Created nttp object:%d %N", tag, name);
   10909            9 :             vec_safe_push (post_load_decls, res);
   10910              :           }
   10911              :       }
   10912              :       break;
   10913              : 
   10914         8686 :     case tt_enum_value:
   10915              :       /* An enum const value.  */
   10916         8686 :       {
   10917         8686 :         if (tree decl = tree_node ())
   10918              :           {
   10919         8704 :             dump (dumper::TREE) && dump ("Read enum value %N", decl);
   10920         8686 :             res = DECL_INITIAL (decl);
   10921              :           }
   10922              : 
   10923         8686 :         if (!res)
   10924            0 :           set_overrun ();
   10925              :       }
   10926              :       break;
   10927              : 
   10928        14079 :     case tt_enum_decl:
   10929              :       /* An enum decl.  */
   10930        14079 :       {
   10931        14079 :         tree ctx = tree_node ();
   10932        14079 :         tree name = tree_node ();
   10933              : 
   10934        14079 :         if (!get_overrun ()
   10935        14079 :             && TREE_CODE (ctx) == ENUMERAL_TYPE)
   10936        14079 :           res = find_enum_member (ctx, name);
   10937              : 
   10938        14079 :         if (!res)
   10939            0 :           set_overrun ();
   10940              :         else
   10941              :           {
   10942        14079 :             int tag = insert (res);
   10943        14079 :             dump (dumper::TREE)
   10944           18 :               && dump ("Read enum decl:%d %C:%N", tag, TREE_CODE (res), res);
   10945              :           }
   10946              :       }
   10947              :       break;
   10948              : 
   10949        10589 :     case tt_data_member:
   10950              :       /* A data member.  */
   10951        10589 :       {
   10952        10589 :         tree ctx = tree_node ();
   10953        10589 :         tree name = tree_node ();
   10954              : 
   10955        10589 :         if (!get_overrun ()
   10956        10589 :             && RECORD_OR_UNION_TYPE_P (ctx))
   10957              :           {
   10958        10589 :             if (name)
   10959         9523 :               res = lookup_class_binding (ctx, name);
   10960              :             else
   10961         1066 :               res = lookup_field_ident (ctx, u ());
   10962              : 
   10963        10589 :             if (!res
   10964        10589 :                 || (TREE_CODE (res) != FIELD_DECL
   10965        10589 :                     && TREE_CODE (res) != USING_DECL)
   10966        21178 :                 || DECL_CONTEXT (res) != ctx)
   10967            0 :               res = NULL_TREE;
   10968              :           }
   10969              : 
   10970        10589 :         if (!res)
   10971            0 :           set_overrun ();
   10972              :         else
   10973              :           {
   10974        10589 :             int tag = insert (res);
   10975        10589 :             dump (dumper::TREE)
   10976           26 :               && dump ("Read member:%d %C:%N", tag, TREE_CODE (res), res);
   10977              :           }
   10978              :       }
   10979              :       break;
   10980              : 
   10981         1702 :     case tt_binfo:
   10982              :       /* A BINFO.  Walk the tree of the dominating type.  */
   10983         1702 :       {
   10984         1702 :         tree type;
   10985         1702 :         unsigned ix = binfo_mergeable (&type);
   10986         1702 :         if (type)
   10987              :           {
   10988         1702 :             res = TYPE_BINFO (type);
   10989         1803 :             for (; ix && res; res = TREE_CHAIN (res))
   10990          101 :               ix--;
   10991         1702 :             if (!res)
   10992            0 :               set_overrun ();
   10993              :           }
   10994              : 
   10995         1702 :         if (get_overrun ())
   10996              :           break;
   10997              : 
   10998              :         /* Insert binfo into backreferences.  */
   10999         1702 :         tag = insert (res);
   11000         1702 :         dump (dumper::TREE) && dump ("Read binfo:%d %N", tag, res);
   11001              :       }
   11002         1702 :       break;
   11003              : 
   11004           27 :     case tt_vtable:
   11005           27 :       {
   11006           27 :         unsigned ix = u ();
   11007           27 :         tree ctx = tree_node ();
   11008           27 :         dump (dumper::TREE) && dump ("Reading vtable %N[%u]", ctx, ix);
   11009           27 :         if (TREE_CODE (ctx) == RECORD_TYPE && TYPE_LANG_SPECIFIC (ctx))
   11010           27 :           for (res = CLASSTYPE_VTABLES (ctx); res; res = DECL_CHAIN (res))
   11011           27 :             if (!ix--)
   11012              :               break;
   11013           27 :         if (!res)
   11014            0 :           set_overrun ();
   11015              :       }
   11016              :       break;
   11017              : 
   11018            0 :     case tt_thunk:
   11019            0 :       {
   11020            0 :         int fixed = i ();
   11021            0 :         tree target = tree_node ();
   11022            0 :         tree virt = tree_node ();
   11023              : 
   11024            0 :         for (tree thunk = DECL_THUNKS (target);
   11025            0 :              thunk; thunk = DECL_CHAIN (thunk))
   11026            0 :           if (THUNK_FIXED_OFFSET (thunk) == fixed
   11027            0 :               && !THUNK_VIRTUAL_OFFSET (thunk) == !virt
   11028            0 :               && (!virt
   11029            0 :                   || tree_int_cst_equal (virt, THUNK_VIRTUAL_OFFSET (thunk))))
   11030              :             {
   11031            0 :               res = thunk;
   11032            0 :               break;
   11033              :             }
   11034              : 
   11035            0 :         int tag = insert (res);
   11036            0 :         if (res)
   11037            0 :           dump (dumper::TREE)
   11038            0 :             && dump ("Read:%d thunk %N to %N", tag, DECL_NAME (res), target);
   11039              :         else
   11040            0 :           set_overrun ();
   11041              :       }
   11042              :       break;
   11043              : 
   11044       156265 :     case tt_clone_ref:
   11045       156265 :       {
   11046       156265 :         tree target = tree_node ();
   11047       156265 :         tree name = tree_node ();
   11048              : 
   11049       156265 :         if (DECL_P (target) && DECL_MAYBE_IN_CHARGE_CDTOR_P (target))
   11050              :           {
   11051       156265 :             tree clone;
   11052       243211 :             FOR_EVERY_CLONE (clone, target)
   11053       243211 :               if (DECL_NAME (clone) == name)
   11054              :                 {
   11055       156265 :                   res = clone;
   11056       156265 :                   break;
   11057              :                 }
   11058              :           }
   11059              : 
   11060              :         /* A clone might have a different vtable entry.  */
   11061       156265 :         if (res && DECL_VIRTUAL_P (res))
   11062         8987 :           DECL_VINDEX (res) = tree_node ();
   11063              : 
   11064       156265 :         if (!res)
   11065            0 :           set_overrun ();
   11066       156265 :         int tag = insert (res);
   11067       156265 :         if (res)
   11068       156265 :           dump (dumper::TREE)
   11069          230 :             && dump ("Read:%d clone %N of %N", tag, DECL_NAME (res), target);
   11070              :         else
   11071            0 :           set_overrun ();
   11072              :        }
   11073              :       break;
   11074              : 
   11075      1133493 :     case tt_entity:
   11076              :       /* Index into the entity table.  Perhaps not loaded yet!  */
   11077      1133493 :       {
   11078      1133493 :         unsigned origin = state->slurp->remap_module (u ());
   11079      1133493 :         unsigned ident = u ();
   11080      1133493 :         module_state *from = (*modules)[origin];
   11081              : 
   11082      1133493 :         if (!origin || ident >= from->entity_num)
   11083            0 :           set_overrun ();
   11084      1133493 :         if (!get_overrun ())
   11085              :           {
   11086      1133493 :             binding_slot *slot = &(*entity_ary)[from->entity_lwm + ident];
   11087      1133493 :             if (slot->is_lazy ())
   11088        32192 :               if (!from->lazy_load (ident, slot))
   11089            0 :                 set_overrun ();
   11090      1133493 :             res = *slot;
   11091              :           }
   11092              : 
   11093      1133493 :         if (res)
   11094              :           {
   11095      1133493 :             const char *kind = (origin != state->mod ? "Imported" : "Named");
   11096      1133493 :             int tag = insert (res);
   11097      1133493 :             dump (dumper::TREE)
   11098          605 :               && dump ("%s:%d %C:%N@%M", kind, tag, TREE_CODE (res),
   11099          605 :                        res, (*modules)[origin]);
   11100              : 
   11101      1133493 :             if (!add_indirects (res))
   11102              :               {
   11103            0 :                 set_overrun ();
   11104            0 :                 res = NULL_TREE;
   11105              :               }
   11106              :           }
   11107              :       }
   11108              :       break;
   11109              : 
   11110         2964 :     case tt_template:
   11111              :       /* A template.  */
   11112         2964 :       if (tree tpl = tree_node ())
   11113              :         {
   11114         2964 :           res = (TREE_CODE (tpl) == TU_LOCAL_ENTITY ?
   11115         2964 :                  tpl : DECL_TEMPLATE_RESULT (tpl));
   11116         2964 :           dump (dumper::TREE)
   11117            9 :             && dump ("Read template %C:%N", TREE_CODE (res), res);
   11118              :         }
   11119              :       break;
   11120              :     }
   11121              : 
   11122     97900515 :   if (is_use && !unused && res && DECL_P (res) && !TREE_USED (res))
   11123              :     {
   11124              :       /* Mark decl used as mark_used does -- we cannot call
   11125              :          mark_used in the middle of streaming, we only need a subset
   11126              :          of its functionality.   */
   11127       771645 :       TREE_USED (res) = true;
   11128              : 
   11129              :       /* And for structured bindings also the underlying decl.  */
   11130       771645 :       if (DECL_DECOMPOSITION_P (res) && !DECL_DECOMP_IS_BASE (res))
   11131         1923 :         TREE_USED (DECL_DECOMP_BASE (res)) = true;
   11132              : 
   11133       771645 :       if (DECL_CLONED_FUNCTION_P (res))
   11134         7211 :         TREE_USED (DECL_CLONED_FUNCTION (res)) = true;
   11135              :     }
   11136              : 
   11137     97900515 :   dump.outdent ();
   11138     97900515 :   return res;
   11139              : }
   11140              : 
   11141              : void
   11142      2401285 : trees_out::tpl_parms (tree parms, unsigned &tpl_levels)
   11143              : {
   11144      2401285 :   if (!parms)
   11145              :     return;
   11146              : 
   11147      1619493 :   if (TREE_VISITED (parms))
   11148              :     {
   11149       675169 :       ref_node (parms);
   11150       675169 :       return;
   11151              :     }
   11152              : 
   11153       944324 :   tpl_parms (TREE_CHAIN (parms), tpl_levels);
   11154              : 
   11155       944324 :   tree vec = TREE_VALUE (parms);
   11156       944324 :   unsigned len = TREE_VEC_LENGTH (vec);
   11157              :   /* Depth.  */
   11158       944324 :   int tag = insert (parms);
   11159       944324 :   if (streaming_p ())
   11160              :     {
   11161       250405 :       i (len + 1);
   11162       250471 :       dump (dumper::TREE)
   11163           66 :         && dump ("Writing template parms:%d level:%N length:%d",
   11164           66 :                  tag, TREE_PURPOSE (parms), len);
   11165              :     }
   11166       944324 :   tree_node (TREE_PURPOSE (parms));
   11167              : 
   11168      3559111 :   for (unsigned ix = 0; ix != len; ix++)
   11169              :     {
   11170      1670463 :       tree parm = TREE_VEC_ELT (vec, ix);
   11171      1670463 :       tree decl = TREE_VALUE (parm);
   11172              : 
   11173      1670463 :       gcc_checking_assert (DECL_TEMPLATE_PARM_P (decl));
   11174      1670463 :       if (CHECKING_P)
   11175      1670463 :         switch (TREE_CODE (decl))
   11176              :           {
   11177            0 :           default: gcc_unreachable ();
   11178              : 
   11179         4155 :           case TEMPLATE_DECL:
   11180         4155 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TEMPLATE_PARM)
   11181              :                         && (TREE_CODE (DECL_TEMPLATE_RESULT (decl)) == TYPE_DECL)
   11182              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11183              :             break;
   11184              : 
   11185      1558207 :           case TYPE_DECL:
   11186      1558207 :             gcc_assert ((TREE_CODE (TREE_TYPE (decl)) == TEMPLATE_TYPE_PARM)
   11187              :                         && (TYPE_NAME (TREE_TYPE (decl)) == decl));
   11188              :             break;
   11189              : 
   11190       108101 :           case PARM_DECL:
   11191       108101 :             gcc_assert ((TREE_CODE (DECL_INITIAL (decl)) == TEMPLATE_PARM_INDEX)
   11192              :                         && (TREE_CODE (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))
   11193              :                             == CONST_DECL)
   11194              :                         && (DECL_TEMPLATE_PARM_P
   11195              :                             (TEMPLATE_PARM_DECL (DECL_INITIAL (decl)))));
   11196              :             break;
   11197              :           }
   11198              : 
   11199      1670463 :       tree_node (decl);
   11200      1670463 :       tree_node (TEMPLATE_PARM_CONSTRAINTS (parm));
   11201              :     }
   11202              : 
   11203       944324 :   tpl_levels++;
   11204              : }
   11205              : 
   11206              : tree
   11207       329723 : trees_in::tpl_parms (unsigned &tpl_levels)
   11208              : {
   11209       329723 :   tree parms = NULL_TREE;
   11210              : 
   11211       695355 :   while (int len = i ())
   11212              :     {
   11213       365632 :       if (len < 0)
   11214              :         {
   11215       197764 :           parms = back_ref (len);
   11216       197764 :           continue;
   11217              :         }
   11218              : 
   11219       167868 :       len -= 1;
   11220       167868 :       parms = tree_cons (NULL_TREE, NULL_TREE, parms);
   11221       167868 :       int tag = insert (parms);
   11222       167868 :       TREE_PURPOSE (parms) = tree_node ();
   11223              : 
   11224       167868 :       dump (dumper::TREE)
   11225          105 :         && dump ("Reading template parms:%d level:%N length:%d",
   11226          105 :                  tag, TREE_PURPOSE (parms), len);
   11227              : 
   11228       167868 :       tree vec = make_tree_vec (len);
   11229       613008 :       for (int ix = 0; ix != len; ix++)
   11230              :         {
   11231       277272 :           tree decl = tree_node ();
   11232       277272 :           if (!decl)
   11233              :             return NULL_TREE;
   11234              : 
   11235       277272 :           tree parm = build_tree_list (NULL, decl);
   11236       277272 :           TEMPLATE_PARM_CONSTRAINTS (parm) = tree_node ();
   11237              : 
   11238       277272 :           TREE_VEC_ELT (vec, ix) = parm;
   11239              :         }
   11240              : 
   11241       167868 :       TREE_VALUE (parms) = vec;
   11242       167868 :       tpl_levels++;
   11243              :     }
   11244              : 
   11245              :   return parms;
   11246              : }
   11247              : 
   11248              : void
   11249      1456961 : trees_out::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11250              : {
   11251      1456961 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11252      2401285 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11253              :     {
   11254       944324 :       tree vec = TREE_VALUE (parms);
   11255              : 
   11256       944324 :       tree_node (TREE_TYPE (vec));
   11257      2614787 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11258              :         {
   11259      1670463 :           tree parm = TREE_VEC_ELT (vec, ix);
   11260      1670463 :           tree dflt = TREE_PURPOSE (parm);
   11261      1670463 :           tree_node (dflt);
   11262              : 
   11263              :           /* Template template parameters need a context of their owning
   11264              :              template. This is quite tricky to infer correctly on stream-in
   11265              :              (see PR c++/98881) so we'll just provide it directly.  */
   11266      1670463 :           tree decl = TREE_VALUE (parm);
   11267      1670463 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11268         4155 :             tree_node (DECL_CONTEXT (decl));
   11269              :         }
   11270              :     }
   11271      1456961 : }
   11272              : 
   11273              : bool
   11274       329723 : trees_in::tpl_parms_fini (tree tmpl, unsigned tpl_levels)
   11275              : {
   11276       329723 :   for (tree parms = DECL_TEMPLATE_PARMS (tmpl);
   11277       497591 :        tpl_levels--; parms = TREE_CHAIN (parms))
   11278              :     {
   11279       167868 :       tree vec = TREE_VALUE (parms);
   11280              : 
   11281       167868 :       TREE_TYPE (vec) = tree_node ();
   11282       445140 :       for (unsigned ix = TREE_VEC_LENGTH (vec); ix--;)
   11283              :         {
   11284       277272 :           tree parm = TREE_VEC_ELT (vec, ix);
   11285       277272 :           tree dflt = tree_node ();
   11286       277272 :           TREE_PURPOSE (parm) = dflt;
   11287              : 
   11288       277272 :           tree decl = TREE_VALUE (parm);
   11289       277272 :           if (TREE_CODE (decl) == TEMPLATE_DECL)
   11290          890 :             DECL_CONTEXT (decl) = tree_node ();
   11291              : 
   11292       277272 :           if (get_overrun ())
   11293              :             return false;
   11294              :         }
   11295              :     }
   11296              :   return true;
   11297              : }
   11298              : 
   11299              : /* PARMS is a LIST, one node per level.
   11300              :    TREE_VALUE is a TREE_VEC of parm info for that level.
   11301              :    each ELT is a TREE_LIST
   11302              :    TREE_VALUE is PARM_DECL, TYPE_DECL or TEMPLATE_DECL
   11303              :    TREE_PURPOSE is the default value.  */
   11304              : 
   11305              : void
   11306      1456961 : trees_out::tpl_header (tree tpl, unsigned *tpl_levels)
   11307              : {
   11308      1456961 :   tree parms = DECL_TEMPLATE_PARMS (tpl);
   11309      1456961 :   tpl_parms (parms, *tpl_levels);
   11310              : 
   11311              :   /* Mark end.  */
   11312      1456961 :   if (streaming_p ())
   11313       485132 :     u (0);
   11314              : 
   11315      1456961 :   if (*tpl_levels)
   11316       886617 :     tree_node (TEMPLATE_PARMS_CONSTRAINTS (parms));
   11317      1456961 : }
   11318              : 
   11319              : bool
   11320       329723 : trees_in::tpl_header (tree tpl, unsigned *tpl_levels)
   11321              : {
   11322       329723 :   tree parms = tpl_parms (*tpl_levels);
   11323       329723 :   if (!parms)
   11324              :     return false;
   11325              : 
   11326       329723 :   DECL_TEMPLATE_PARMS (tpl) = parms;
   11327              : 
   11328       329723 :   if (*tpl_levels)
   11329       165764 :     TEMPLATE_PARMS_CONSTRAINTS (parms) = tree_node ();
   11330              : 
   11331              :   return true;
   11332              : }
   11333              : 
   11334              : /* Stream skeleton parm nodes, with their flags, type & parm indices.
   11335              :    All the parms will have consecutive tags.  */
   11336              : 
   11337              : void
   11338      2040042 : trees_out::fn_parms_init (tree fn)
   11339              : {
   11340              :   /* First init them.  */
   11341      2040042 :   int base_tag = ref_num - 1;
   11342      2040042 :   int ix = 0;
   11343      2040042 :   for (tree parm = DECL_ARGUMENTS (fn);
   11344      6161930 :        parm; parm = DECL_CHAIN (parm), ix++)
   11345              :     {
   11346      4121888 :       if (streaming_p ())
   11347              :         {
   11348      1373929 :           start (parm);
   11349      1373929 :           tree_node_bools (parm);
   11350              :         }
   11351      4121888 :       int tag = insert (parm);
   11352      4121888 :       gcc_checking_assert (base_tag - ix == tag);
   11353              :     }
   11354              :   /* Mark the end.  */
   11355      2040042 :   if (streaming_p ())
   11356       680319 :     u (0);
   11357              : 
   11358              :   /* Now stream their contents.  */
   11359      2040042 :   ix = 0;
   11360      2040042 :   for (tree parm = DECL_ARGUMENTS (fn);
   11361      6161930 :        parm; parm = DECL_CHAIN (parm), ix++)
   11362              :     {
   11363      4121888 :       if (streaming_p ())
   11364      1373929 :         dump (dumper::TREE)
   11365          222 :           && dump ("Writing parm:%d %u (%N) of %N",
   11366              :                    base_tag - ix, ix, parm, fn);
   11367      4121888 :       tree_node_vals (parm);
   11368              :     }
   11369              : 
   11370      2040042 :   if (!streaming_p ())
   11371              :     {
   11372              :       /* We must walk contract specifiers so the dependency graph is
   11373              :          complete.  */
   11374      1359723 :       if (tree contracts = get_fn_contract_specifiers (fn))
   11375            0 :         for (tree contract : tree_vec_range (contracts))
   11376            0 :           tree_node (contract);
   11377              :     }
   11378              : 
   11379              :   /* Write a reference to contracts pre/post functions, if any, to avoid
   11380              :      regenerating them in importers.  */
   11381      2040042 :   tree_node (DECL_PRE_FN (fn));
   11382      2040042 :   tree_node (DECL_POST_FN (fn));
   11383      2040042 : }
   11384              : 
   11385              : /* Build skeleton parm nodes, read their flags, type & parm indices.  */
   11386              : 
   11387              : int
   11388       465431 : trees_in::fn_parms_init (tree fn)
   11389              : {
   11390       465431 :   int base_tag = ~(int)back_refs.length ();
   11391              : 
   11392       465431 :   tree *parm_ptr = &DECL_ARGUMENTS (fn);
   11393       465431 :   int ix = 0;
   11394      1406105 :   for (; int code = u (); ix++)
   11395              :     {
   11396       940674 :       tree parm = start (code);
   11397       940674 :       if (!tree_node_bools (parm))
   11398              :         return 0;
   11399              : 
   11400       940674 :       int tag = insert (parm);
   11401       940674 :       gcc_checking_assert (base_tag - ix == tag);
   11402       940674 :       *parm_ptr = parm;
   11403       940674 :       parm_ptr = &DECL_CHAIN (parm);
   11404       940674 :     }
   11405              : 
   11406       465431 :   ix = 0;
   11407       465431 :   for (tree parm = DECL_ARGUMENTS (fn);
   11408      1406105 :        parm; parm = DECL_CHAIN (parm), ix++)
   11409              :     {
   11410       940674 :       dump (dumper::TREE)
   11411          362 :         && dump ("Reading parm:%d %u (%N) of %N",
   11412              :                  base_tag - ix, ix, parm, fn);
   11413       940674 :       if (!tree_node_vals (parm))
   11414              :         return 0;
   11415              : 
   11416              :       /* Apply relevant attributes.
   11417              :          FIXME should probably use cplus_decl_attributes for this,
   11418              :          but it's not yet ready for modules.  */
   11419              : 
   11420              :       /* TREE_USED is deliberately not streamed for most declarations,
   11421              :          but needs to be set if we have the [[maybe_unused]] attribute.  */
   11422       940674 :       if (lookup_attribute ("unused", DECL_ATTRIBUTES (parm))
   11423       940674 :           || lookup_attribute ("maybe_unused", DECL_ATTRIBUTES (parm)))
   11424              :         {
   11425         2042 :           TREE_USED (parm) = true;
   11426         2042 :           DECL_READ_P (parm) = true;
   11427              :         }
   11428              :     }
   11429              : 
   11430              :   /* Reload references to contract functions, if any.  */
   11431       465431 :   tree pre_fn = tree_node ();
   11432       465431 :   tree post_fn = tree_node ();
   11433       465431 :   set_contract_functions (fn, pre_fn, post_fn);
   11434              : 
   11435       465431 :   return base_tag;
   11436              : }
   11437              : 
   11438              : /* Read the remaining parm node data.  Replace with existing (if
   11439              :    non-null) in the map.  */
   11440              : 
   11441              : void
   11442       465431 : trees_in::fn_parms_fini (int tag, tree fn, tree existing, bool is_defn)
   11443              : {
   11444       663327 :   tree existing_parm = existing ? DECL_ARGUMENTS (existing) : NULL_TREE;
   11445       465431 :   tree parms = DECL_ARGUMENTS (fn);
   11446      1406105 :   for (tree parm = parms; parm; parm = DECL_CHAIN (parm))
   11447              :     {
   11448       940674 :       if (existing_parm)
   11449              :         {
   11450       574994 :           if (is_defn && !DECL_SAVED_TREE (existing))
   11451              :             {
   11452              :               /* If we're about to become the definition, set the
   11453              :                  names of the parms from us.  */
   11454        14545 :               DECL_NAME (existing_parm) = DECL_NAME (parm);
   11455        14545 :               DECL_SOURCE_LOCATION (existing_parm) = DECL_SOURCE_LOCATION (parm);
   11456              : 
   11457              :               /* And some other flags important for codegen are only set
   11458              :                  by the definition.  */
   11459        14545 :               TREE_ADDRESSABLE (existing_parm) = TREE_ADDRESSABLE (parm);
   11460        14545 :               DECL_BY_REFERENCE (existing_parm) = DECL_BY_REFERENCE (parm);
   11461        14545 :               DECL_NONLOCAL (existing_parm) = DECL_NONLOCAL (parm);
   11462        14545 :               DECL_ARG_TYPE (existing_parm) = DECL_ARG_TYPE (parm);
   11463              : 
   11464              :               /* Invisiref parms had their types adjusted by cp_genericize. */
   11465        14545 :               if (DECL_BY_REFERENCE (parm))
   11466              :                 {
   11467            6 :                   TREE_TYPE (existing_parm) = TREE_TYPE (parm);
   11468            6 :                   relayout_decl (existing_parm);
   11469              :                 }
   11470              :             }
   11471              : 
   11472       390931 :           back_refs[~tag] = existing_parm;
   11473       390931 :           existing_parm = DECL_CHAIN (existing_parm);
   11474              :         }
   11475       940674 :       tag--;
   11476              :     }
   11477       465431 : }
   11478              : 
   11479              : /* Encode into KEY the position of the local type (class or enum)
   11480              :    declaration DECL within FN.  The position is encoded as the
   11481              :    index of the innermost BLOCK (numbered in BFS order) along with
   11482              :    the index within its BLOCK_VARS list.  */
   11483              : 
   11484              : void
   11485        22974 : trees_out::key_local_type (merge_key& key, tree decl, tree fn)
   11486              : {
   11487        22974 :   auto_vec<tree, 4> blocks;
   11488        22974 :   blocks.quick_push (DECL_INITIAL (fn));
   11489        22974 :   unsigned block_ix = 0;
   11490       113310 :   while (block_ix != blocks.length ())
   11491              :     {
   11492        45168 :       tree block = blocks[block_ix];
   11493        45168 :       unsigned decl_ix = 0;
   11494       136800 :       for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11495              :         {
   11496       114606 :           if (TREE_CODE (var) != TYPE_DECL)
   11497        72741 :             continue;
   11498        41865 :           if (var == decl)
   11499              :             {
   11500        22974 :               key.index = (block_ix << 10) | decl_ix;
   11501        22974 :               return;
   11502              :             }
   11503        18891 :           ++decl_ix;
   11504              :         }
   11505        46380 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11506        24186 :         blocks.safe_push (sub);
   11507        22194 :       ++block_ix;
   11508              :     }
   11509              : 
   11510              :   /* Not-found value.  */
   11511            0 :   key.index = 1023;
   11512        22974 : }
   11513              : 
   11514              : /* Look up the local type corresponding at the position encoded by
   11515              :    KEY within FN and named NAME.  */
   11516              : 
   11517              : tree
   11518         4782 : trees_in::key_local_type (const merge_key& key, tree fn, tree name)
   11519              : {
   11520         4782 :   if (!DECL_INITIAL (fn))
   11521              :     return NULL_TREE;
   11522              : 
   11523         1931 :   const unsigned block_pos = key.index >> 10;
   11524         1931 :   const unsigned decl_pos = key.index & 1023;
   11525              : 
   11526         1931 :   if (decl_pos == 1023)
   11527              :     return NULL_TREE;
   11528              : 
   11529         1931 :   auto_vec<tree, 4> blocks;
   11530         1931 :   blocks.quick_push (DECL_INITIAL (fn));
   11531         1931 :   unsigned block_ix = 0;
   11532         8941 :   while (block_ix != blocks.length ())
   11533              :     {
   11534         3505 :       tree block = blocks[block_ix];
   11535         3505 :       if (block_ix == block_pos)
   11536              :         {
   11537         1931 :           unsigned decl_ix = 0;
   11538         5243 :           for (tree var = BLOCK_VARS (block); var; var = DECL_CHAIN (var))
   11539              :             {
   11540         5243 :               if (TREE_CODE (var) != TYPE_DECL)
   11541         2340 :                 continue;
   11542              :               /* Prefer using the identifier as the key for more robustness
   11543              :                  to ODR violations, except for anonymous types since their
   11544              :                  compiler-generated identifiers aren't stable.  */
   11545         5806 :               if (IDENTIFIER_ANON_P (name)
   11546         2903 :                   ? decl_ix == decl_pos
   11547          320 :                   : DECL_NAME (var) == name)
   11548              :                 return var;
   11549          972 :               ++decl_ix;
   11550              :             }
   11551              :           return NULL_TREE;
   11552              :         }
   11553         3257 :       for (tree sub = BLOCK_SUBBLOCKS (block); sub; sub = BLOCK_CHAIN (sub))
   11554         1683 :         blocks.safe_push (sub);
   11555         1574 :       ++block_ix;
   11556              :     }
   11557              : 
   11558              :   return NULL_TREE;
   11559         1931 : }
   11560              : 
   11561              : /* DEP is the depset of some decl we're streaming by value.  Determine
   11562              :    the merging behaviour.  */
   11563              : 
   11564              : merge_kind
   11565      5048410 : trees_out::get_merge_kind (tree decl, depset *dep)
   11566              : {
   11567      5048410 :   if (!dep)
   11568              :     {
   11569      1056323 :       if (VAR_OR_FUNCTION_DECL_P (decl))
   11570              :         {
   11571              :           /* Any var or function with template info should have DEP.  */
   11572       597599 :           gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   11573              :                                || !DECL_TEMPLATE_INFO (decl));
   11574       597599 :           if (DECL_LOCAL_DECL_P (decl))
   11575              :             return MK_unique;
   11576              :         }
   11577              : 
   11578              :       /* Either unique, or some member of a class that cannot have an
   11579              :          out-of-class definition.  For instance a FIELD_DECL.  */
   11580      1055991 :       tree ctx = CP_DECL_CONTEXT (decl);
   11581      1055991 :       if (TREE_CODE (ctx) == FUNCTION_DECL)
   11582              :         {
   11583              :           /* USING_DECLs and NAMESPACE_DECLs cannot have DECL_TEMPLATE_INFO --
   11584              :              this isn't permitting them to have one.   */
   11585       688709 :           gcc_checking_assert (TREE_CODE (decl) == USING_DECL
   11586              :                                || TREE_CODE (decl) == NAMESPACE_DECL
   11587              :                                || !DECL_LANG_SPECIFIC (decl)
   11588              :                                || !DECL_TEMPLATE_INFO (decl));
   11589              : 
   11590              :           return MK_unique;
   11591              :         }
   11592              : 
   11593       367282 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   11594       367282 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   11595              :         return MK_local_friend;
   11596              : 
   11597       367282 :       gcc_checking_assert (TYPE_P (ctx));
   11598              : 
   11599              :       /* Internal-only types will not need to dedup their members.  */
   11600       367282 :       if (!DECL_CONTEXT (TYPE_NAME (ctx)))
   11601              :         return MK_unique;
   11602              : 
   11603       367226 :       if (TREE_CODE (decl) == USING_DECL)
   11604              :         return MK_field;
   11605              : 
   11606       241949 :       if (TREE_CODE (decl) == FIELD_DECL)
   11607              :         {
   11608       178857 :           if (DECL_NAME (decl))
   11609              :             {
   11610              :               /* Anonymous FIELD_DECLs have a NULL name.  */
   11611       145682 :               gcc_checking_assert (!IDENTIFIER_ANON_P (DECL_NAME (decl)));
   11612              :               return MK_named;
   11613              :             }
   11614              : 
   11615        33175 :           if (walking_bit_field_unit)
   11616              :             {
   11617              :               /* The underlying storage unit for a bitfield.  We do not
   11618              :                  need to dedup it, because it's only reachable through
   11619              :                  the bitfields it represents.  And those are deduped.  */
   11620              :               // FIXME: Is that assertion correct -- do we ever fish it
   11621              :               // out and put it in an expr?
   11622          578 :               gcc_checking_assert (!DECL_NAME (decl)
   11623              :                                    && !RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   11624              :                                    && !DECL_BIT_FIELD_REPRESENTATIVE (decl));
   11625          578 :               gcc_checking_assert ((TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE
   11626              :                                     ? TREE_CODE (TREE_TYPE (TREE_TYPE (decl)))
   11627              :                                     : TREE_CODE (TREE_TYPE (decl)))
   11628              :                                    == INTEGER_TYPE);
   11629              :               return MK_unique;
   11630              :             }
   11631              : 
   11632              :           return MK_field;
   11633              :         }
   11634              : 
   11635        63092 :       if (TREE_CODE (decl) == CONST_DECL)
   11636              :         return MK_named;
   11637              : 
   11638        11298 :       if (TREE_CODE (decl) == VAR_DECL
   11639        11298 :           && DECL_VTABLE_OR_VTT_P (decl))
   11640              :         return MK_vtable;
   11641              : 
   11642         1944 :       if (DECL_THUNK_P (decl))
   11643              :         /* Thunks are unique-enough, because they're only referenced
   11644              :            from the vtable.  And that's either new (so we want the
   11645              :            thunks), or it's a duplicate (so it will be dropped).  */
   11646              :         return MK_unique;
   11647              : 
   11648              :       /* There should be no other cases.  */
   11649            0 :       gcc_unreachable ();
   11650              :     }
   11651              : 
   11652      3992087 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   11653              :                        && TREE_CODE (decl) != USING_DECL
   11654              :                        && TREE_CODE (decl) != CONST_DECL);
   11655              : 
   11656      3992087 :   if (is_key_order ())
   11657              :     {
   11658              :       /* When doing the mergeablilty graph, there's an indirection to
   11659              :          the actual depset.  */
   11660      1330542 :       gcc_assert (dep->is_special ());
   11661      1330542 :       dep = dep->deps[0];
   11662              :     }
   11663              : 
   11664      3992087 :   gcc_checking_assert (decl == dep->get_entity ());
   11665              : 
   11666      3992087 :   merge_kind mk = MK_named;
   11667      3992087 :   switch (dep->get_entity_kind ())
   11668              :     {
   11669            0 :     default:
   11670            0 :       gcc_unreachable ();
   11671              : 
   11672              :     case depset::EK_PARTIAL:
   11673              :       mk = MK_partial;
   11674              :       break;
   11675              : 
   11676      2239781 :     case depset::EK_DECL:
   11677      2239781 :       {
   11678      2239781 :         tree ctx = CP_DECL_CONTEXT (decl);
   11679              : 
   11680      2239781 :         switch (TREE_CODE (ctx))
   11681              :           {
   11682            0 :           default:
   11683            0 :             gcc_unreachable ();
   11684              : 
   11685        23238 :           case FUNCTION_DECL:
   11686        23238 :             gcc_checking_assert
   11687              :               (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   11688              : 
   11689        23238 :             if (has_definition (ctx))
   11690              :               mk = MK_local_type;
   11691              :             else
   11692              :               /* We're not providing a definition of the context to key
   11693              :                  the local type into; use the keyed map instead.  */
   11694         1360 :               mk = MK_keyed;
   11695              :             break;
   11696              : 
   11697      2216543 :           case RECORD_TYPE:
   11698      2216543 :           case UNION_TYPE:
   11699      2216543 :           case NAMESPACE_DECL:
   11700      2216543 :             if (DECL_NAME (decl) == as_base_identifier)
   11701              :               {
   11702              :                 mk = MK_as_base;
   11703              :                 break;
   11704              :               }
   11705              : 
   11706              :             /* A lambda may have a class as its context, even though it
   11707              :                isn't a member in the traditional sense; see the test
   11708              :                g++.dg/modules/lambda-6_a.C.  */
   11709      2756411 :             if (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl))
   11710      2405525 :                 && LAMBDA_TYPE_P (TREE_TYPE (decl)))
   11711              :               {
   11712         1132 :                 if (get_keyed_decl_scope (decl))
   11713              :                   mk = MK_keyed;
   11714              :                 else
   11715              :                   /* Lambdas not attached to any mangling scope are TU-local
   11716              :                      and so cannot be deduplicated.  */
   11717       692132 :                   mk = MK_unique;
   11718              :                 break;
   11719              :               }
   11720              : 
   11721      2116156 :             if (TREE_CODE (decl) == TEMPLATE_DECL
   11722      2116156 :                 ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11723       894864 :                 : decl_specialization_friend_p (decl))
   11724              :               {
   11725              :                 mk = MK_local_friend;
   11726              :                 break;
   11727              :               }
   11728              : 
   11729      2086675 :             if (DECL_DECOMPOSITION_P (decl))
   11730              :               {
   11731              :                 mk = MK_unique;
   11732              :                 break;
   11733              :               }
   11734              : 
   11735      2086198 :             if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
   11736              :               {
   11737        36752 :                 if (RECORD_OR_UNION_TYPE_P (ctx))
   11738              :                   mk = MK_field;
   11739         1195 :                 else if (DECL_IMPLICIT_TYPEDEF_P (decl)
   11740         1195 :                          && UNSCOPED_ENUM_P (TREE_TYPE (decl))
   11741         2390 :                          && TYPE_VALUES (TREE_TYPE (decl)))
   11742              :                   /* Keyed by first enum value, and underlying type.  */
   11743              :                   mk = MK_enum;
   11744              :                 else
   11745              :                   /* No way to merge it, it is an ODR land-mine.  */
   11746              :                   mk = MK_unique;
   11747              :               }
   11748              :           }
   11749              :       }
   11750              :       break;
   11751              : 
   11752      1688589 :     case depset::EK_SPECIALIZATION:
   11753      1688589 :       {
   11754      1688589 :         gcc_checking_assert (dep->is_special ());
   11755              : 
   11756      1688589 :         if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
   11757              :           /* An block-scope classes of templates are themselves
   11758              :              templates.  */
   11759         6033 :           gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (decl));
   11760              : 
   11761      1688589 :         if (dep->is_friend_spec ())
   11762              :           mk = MK_friend_spec;
   11763      1688589 :         else if (dep->is_type_spec ())
   11764              :           mk = MK_type_spec;
   11765              :         else
   11766      1203591 :           mk = MK_decl_spec;
   11767              : 
   11768      1688589 :         if (TREE_CODE (decl) == TEMPLATE_DECL)
   11769              :           {
   11770       144213 :             spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11771       144213 :             if (TREE_CODE (entry->spec) != TEMPLATE_DECL)
   11772        15183 :               mk = merge_kind (mk | MK_tmpl_tmpl_mask);
   11773              :           }
   11774              :       }
   11775              :       break;
   11776              :     }
   11777              : 
   11778              :   return mk;
   11779              : }
   11780              : 
   11781              : 
   11782              : /* The container of DECL -- not necessarily its context!  */
   11783              : 
   11784              : tree
   11785      5048410 : trees_out::decl_container (tree decl)
   11786              : {
   11787      5048410 :   int use_tpl;
   11788      5048410 :   tree tpl = NULL_TREE;
   11789      5048410 :   if (tree template_info = node_template_info (decl, use_tpl))
   11790      1765299 :     tpl = TI_TEMPLATE (template_info);
   11791      5048410 :   if (tpl == decl)
   11792            0 :     tpl = nullptr;
   11793              : 
   11794              :   /* Stream the template we're instantiated from.  */
   11795      5048410 :   tree_node (tpl);
   11796              : 
   11797      5048410 :   tree container = NULL_TREE;
   11798      5048410 :   if (TREE_CODE (decl) == TEMPLATE_DECL
   11799      5048410 :       ? DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl)
   11800      3598710 :       : decl_specialization_friend_p (decl))
   11801        29481 :     container = DECL_CHAIN (decl);
   11802              :   else
   11803      5018929 :     container = CP_DECL_CONTEXT (decl);
   11804              : 
   11805      5048410 :   if (TYPE_P (container))
   11806      2959926 :     container = TYPE_NAME (container);
   11807              : 
   11808      5048410 :   tree_node (container);
   11809              : 
   11810      5048410 :   return container;
   11811              : }
   11812              : 
   11813              : tree
   11814      1228620 : trees_in::decl_container ()
   11815              : {
   11816              :   /* The maybe-template.  */
   11817      1228620 :   (void)tree_node ();
   11818              : 
   11819      1228620 :   tree container = tree_node ();
   11820              : 
   11821      1228620 :   return container;
   11822              : }
   11823              : 
   11824              : /* Gets a 2-bit discriminator to distinguish coroutine actor or destroy
   11825              :    functions from a normal function.  */
   11826              : 
   11827              : static int
   11828      1390575 : get_coroutine_discriminator (tree inner)
   11829              : {
   11830      1390575 :   if (DECL_COROUTINE_P (inner))
   11831           78 :     if (tree ramp = DECL_RAMP_FN (inner))
   11832              :       {
   11833           18 :         if (DECL_ACTOR_FN (ramp) == inner)
   11834              :           return 1;
   11835            9 :         else if (DECL_DESTROY_FN (ramp) == inner)
   11836              :           return 2;
   11837              :         else
   11838            0 :           gcc_unreachable ();
   11839              :       }
   11840              :   return 0;
   11841              : }
   11842              : 
   11843              : /* Write out key information about a mergeable DEP.  Does not write
   11844              :    the contents of DEP itself.  The context has already been
   11845              :    written.  The container has already been streamed.  */
   11846              : 
   11847              : void
   11848      5048410 : trees_out::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   11849              :                           tree container, depset *dep)
   11850              : {
   11851      5048410 :   if (dep && is_key_order ())
   11852              :     {
   11853      1330542 :       gcc_checking_assert (dep->is_special ());
   11854      1330542 :       dep = dep->deps[0];
   11855              :     }
   11856              : 
   11857      5048410 :   if (streaming_p ())
   11858      1846352 :     dump (dumper::MERGE)
   11859         1101 :       && dump ("Writing:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   11860          993 :                dep ? dep->entity_kind_name () : "contained",
   11861         1101 :                TREE_CODE (decl), decl);
   11862              : 
   11863              :   /* Now write the locating information. */
   11864      5048410 :   if (mk & MK_template_mask)
   11865              :     {
   11866              :       /* Specializations are located via their originating template,
   11867              :          and the set of template args they specialize.  */
   11868      1688589 :       gcc_checking_assert (dep && dep->is_special ());
   11869      1688589 :       spec_entry *entry = reinterpret_cast <spec_entry *> (dep->deps[0]);
   11870              : 
   11871      1688589 :       tree_node (entry->tmpl);
   11872      1688589 :       tree_node (entry->args);
   11873      1688589 :       if (mk & MK_tmpl_decl_mask)
   11874      1203591 :         if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   11875              :           {
   11876              :             /* Variable template partial specializations might need
   11877              :                constraints (see spec_hasher::equal).  It's simpler to
   11878              :                write NULL when we don't need them.  */
   11879        26958 :             tree constraints = NULL_TREE;
   11880              : 
   11881        26958 :             if (uses_template_parms (entry->args))
   11882          954 :               constraints = get_constraints (inner);
   11883        26958 :             tree_node (constraints);
   11884              :           }
   11885              : 
   11886      1688589 :       if (CHECKING_P)
   11887              :         {
   11888              :           /* Make sure we can locate the decl.  */
   11889      1688589 :           tree existing = match_mergeable_specialization
   11890      1688589 :             (bool (mk & MK_tmpl_decl_mask), entry);
   11891              : 
   11892      1688589 :           gcc_assert (existing);
   11893      1688589 :           if (mk & MK_tmpl_decl_mask)
   11894              :             {
   11895      1203591 :               if (mk & MK_tmpl_tmpl_mask)
   11896        12027 :                 existing = DECL_TI_TEMPLATE (existing);
   11897              :             }
   11898              :           else
   11899              :             {
   11900       484998 :               if (mk & MK_tmpl_tmpl_mask)
   11901         3156 :                 existing = CLASSTYPE_TI_TEMPLATE (existing);
   11902              :               else
   11903       481842 :                 existing = TYPE_NAME (existing);
   11904              :             }
   11905              : 
   11906              :           /* The walkabout should have found ourselves.  */
   11907      1688589 :           gcc_checking_assert (TREE_CODE (decl) == TYPE_DECL
   11908              :                                ? same_type_p (TREE_TYPE (decl),
   11909              :                                               TREE_TYPE (existing))
   11910              :                                : existing == decl);
   11911              :         }
   11912              :     }
   11913      3359821 :   else if (mk != MK_unique)
   11914              :     {
   11915      2667689 :       merge_key key;
   11916      2667689 :       tree name = DECL_NAME (decl);
   11917              : 
   11918      2667689 :       switch (mk)
   11919              :         {
   11920            0 :         default:
   11921            0 :           gcc_unreachable ();
   11922              : 
   11923      2246922 :         case MK_named:
   11924      2246922 :         case MK_friend_spec:
   11925      2246922 :           if (IDENTIFIER_CONV_OP_P (name))
   11926         7987 :             name = conv_op_identifier;
   11927              : 
   11928      2246922 :           if (TREE_CODE (inner) == FUNCTION_DECL)
   11929              :             {
   11930              :               /* Functions are distinguished by parameter types.  */
   11931      1261051 :               tree fn_type = TREE_TYPE (inner);
   11932              : 
   11933      1261051 :               key.ref_q = type_memfn_rqual (fn_type);
   11934      1261051 :               key.coro_disc = get_coroutine_discriminator (inner);
   11935      1261051 :               key.iobj_p = DECL_IOBJ_MEMBER_FUNCTION_P (inner);
   11936      1261051 :               key.xobj_p = DECL_XOBJ_MEMBER_FUNCTION_P (inner);
   11937      1261051 :               key.args = TYPE_ARG_TYPES (fn_type);
   11938              : 
   11939      1261051 :               if (tree reqs = get_constraints (inner))
   11940              :                 {
   11941        93123 :                   if (cxx_dialect < cxx20)
   11942           48 :                     reqs = CI_ASSOCIATED_CONSTRAINTS (reqs);
   11943              :                   else
   11944       186198 :                     reqs = CI_DECLARATOR_REQS (reqs);
   11945        93123 :                   key.constraints = reqs;
   11946              :                 }
   11947              : 
   11948      1261051 :               if (IDENTIFIER_CONV_OP_P (name)
   11949      1261051 :                   || (decl != inner
   11950       699012 :                       && !(name == fun_identifier
   11951              :                            /* In case the user names something _FUN  */
   11952          168 :                            && LAMBDA_TYPE_P (DECL_CONTEXT (inner)))))
   11953              :                 /* And a function template, or conversion operator needs
   11954              :                    the return type.  Except for the _FUN thunk of a
   11955              :                    generic lambda, which has a recursive decl_type'd
   11956              :                    return type.  */
   11957              :                 // FIXME: What if the return type is a voldemort?
   11958       706915 :                 key.ret = fndecl_declared_return_type (inner);
   11959              :             }
   11960              :           break;
   11961              : 
   11962       193431 :         case MK_field:
   11963       193431 :           {
   11964       193431 :             unsigned ix = 0;
   11965       193431 :             if (TREE_CODE (inner) != FIELD_DECL)
   11966              :               name = NULL_TREE;
   11967              :             else
   11968        32597 :               gcc_checking_assert (!name || !IDENTIFIER_ANON_P (name));
   11969              : 
   11970       193431 :             for (tree field = TYPE_FIELDS (TREE_TYPE (container));
   11971      3983799 :                  ; field = DECL_CHAIN (field))
   11972              :               {
   11973      4177230 :                 tree finner = STRIP_TEMPLATE (field);
   11974      4177230 :                 if (TREE_CODE (finner) == TREE_CODE (inner))
   11975              :                   {
   11976      1439783 :                     if (finner == inner)
   11977              :                       break;
   11978      1246352 :                     ix++;
   11979              :                   }
   11980      3983799 :               }
   11981       193431 :             key.index = ix;
   11982              :           }
   11983       193431 :           break;
   11984              : 
   11985         9354 :         case MK_vtable:
   11986         9354 :           {
   11987         9354 :             tree vtable = CLASSTYPE_VTABLES (TREE_TYPE (container));
   11988        12322 :             for (unsigned ix = 0; ; vtable = DECL_CHAIN (vtable), ix++)
   11989        12322 :               if (vtable == decl)
   11990              :                 {
   11991         9354 :                   key.index = ix;
   11992         9354 :                   break;
   11993              :                 }
   11994         9354 :             name = NULL_TREE;
   11995              :           }
   11996         9354 :           break;
   11997              : 
   11998        99255 :         case MK_as_base:
   11999        99255 :           gcc_checking_assert
   12000              :             (decl == TYPE_NAME (CLASSTYPE_AS_BASE (TREE_TYPE (container))));
   12001              :           break;
   12002              : 
   12003        29481 :         case MK_local_friend:
   12004        29481 :           {
   12005              :             /* Find by index on the class's DECL_LIST.  We set TREE_CHAIN to
   12006              :                point to the class in push_template_decl or grokfndecl.  */
   12007        29481 :             unsigned ix = 0;
   12008        29481 :             for (tree decls = CLASSTYPE_DECL_LIST (TREE_CHAIN (decl));
   12009       822840 :                  decls; decls = TREE_CHAIN (decls))
   12010       822840 :               if (!TREE_PURPOSE (decls))
   12011              :                 {
   12012       113028 :                   tree frnd = friend_from_decl_list (TREE_VALUE (decls));
   12013       113028 :                   if (frnd == decl)
   12014              :                     break;
   12015        83547 :                   ix++;
   12016              :                 }
   12017        29481 :             key.index = ix;
   12018        29481 :             name = NULL_TREE;
   12019              :           }
   12020        29481 :           break;
   12021              : 
   12022        22974 :         case MK_local_type:
   12023        22974 :           key_local_type (key, STRIP_TEMPLATE (decl), container);
   12024        22974 :           break;
   12025              : 
   12026         1195 :         case MK_enum:
   12027         1195 :           {
   12028              :             /* Anonymous enums are located by their first identifier,
   12029              :                and underlying type.  */
   12030         1195 :             tree type = TREE_TYPE (decl);
   12031              : 
   12032         1195 :             gcc_checking_assert (UNSCOPED_ENUM_P (type));
   12033              :             /* Using the type name drops the bit precision we might
   12034              :                have been using on the enum.  */
   12035         1195 :             key.ret = TYPE_NAME (ENUM_UNDERLYING_TYPE (type));
   12036         1195 :             if (tree values = TYPE_VALUES (type))
   12037         1195 :               name = DECL_NAME (TREE_VALUE (values));
   12038              :           }
   12039              :           break;
   12040              : 
   12041         1360 :         case MK_keyed:
   12042         1360 :           {
   12043         1360 :             tree scope = get_keyed_decl_scope (inner);
   12044         1360 :             gcc_checking_assert (scope);
   12045              : 
   12046         1360 :             auto *root = keyed_table->get (scope);
   12047         1360 :             unsigned ix = root->length ();
   12048              :             /* If we don't find it, we'll write a really big number
   12049              :                that the reader will ignore.  */
   12050         1484 :             while (ix--)
   12051         1484 :               if ((*root)[ix] == inner)
   12052              :                 break;
   12053              : 
   12054              :             /* Use the keyed-to decl as the 'name'.  */
   12055         1360 :             name = scope;
   12056         1360 :             key.index = ix;
   12057              :           }
   12058         1360 :           break;
   12059              : 
   12060        63717 :         case MK_partial:
   12061        63717 :           {
   12062        63717 :             tree ti = get_template_info (inner);
   12063        63717 :             key.constraints = get_constraints (inner);
   12064        63717 :             key.ret = TI_TEMPLATE (ti);
   12065        63717 :             key.args = TI_ARGS (ti);
   12066              :           }
   12067        63717 :           break;
   12068              :         }
   12069              : 
   12070      2667689 :       tree_node (name);
   12071      2667689 :       if (streaming_p ())
   12072              :         {
   12073              :           /* Check we have enough bits for the index.  */
   12074       949847 :           gcc_checking_assert (key.index < (1u << (sizeof (unsigned) * 8 - 6)));
   12075              : 
   12076       949847 :           unsigned code = ((key.ref_q << 0)
   12077       949847 :                            | (key.coro_disc << 2)
   12078       949847 :                            | (key.iobj_p << 4)
   12079       949847 :                            | (key.xobj_p << 5)
   12080       949847 :                            | (key.index << 6));
   12081       949847 :           u (code);
   12082              :         }
   12083              : 
   12084      2667689 :       if (mk == MK_enum)
   12085         1195 :         tree_node (key.ret);
   12086      2666494 :       else if (mk == MK_partial
   12087      2602777 :                || (mk == MK_named && inner
   12088      2246922 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12089              :         {
   12090      1324768 :           tree_node (key.ret);
   12091      1324768 :           tree arg = key.args;
   12092      1324768 :           if (mk == MK_named)
   12093      3681558 :             while (arg && arg != void_list_node)
   12094              :               {
   12095      2420507 :                 tree_node (TREE_VALUE (arg));
   12096      2420507 :                 arg = TREE_CHAIN (arg);
   12097              :               }
   12098      1324768 :           tree_node (arg);
   12099      1324768 :           tree_node (key.constraints);
   12100              :         }
   12101              :     }
   12102      5048410 : }
   12103              : 
   12104              : /* DECL is a new declaration that may be duplicated in OVL.  Use KEY
   12105              :    to find its clone, or NULL.  If DECL's DECL_NAME is NULL, this
   12106              :    has been found by a proxy.  It will be an enum type located by its
   12107              :    first member.
   12108              : 
   12109              :    We're conservative with matches, so ambiguous decls will be
   12110              :    registered as different, then lead to a lookup error if the two
   12111              :    modules are both visible.  Perhaps we want to do something similar
   12112              :    to duplicate decls to get ODR errors on loading?  We already have
   12113              :    some special casing for namespaces.  */
   12114              : 
   12115              : static tree
   12116       302147 : check_mergeable_decl (merge_kind mk, tree decl, tree ovl, merge_key const &key)
   12117              : {
   12118       302147 :   tree found = NULL_TREE;
   12119       910565 :   for (ovl_iterator iter (ovl); !found && iter; ++iter)
   12120              :     {
   12121       667592 :       tree match = *iter;
   12122              : 
   12123       667592 :       tree d_inner = decl;
   12124       667592 :       tree m_inner = match;
   12125              : 
   12126       771175 :     again:
   12127       771175 :       if (TREE_CODE (d_inner) != TREE_CODE (m_inner))
   12128              :         {
   12129       116293 :           if (TREE_CODE (match) == NAMESPACE_DECL
   12130       116293 :               && !DECL_NAMESPACE_ALIAS (match))
   12131              :             /* Namespaces are never overloaded.  */
   12132              :             found = match;
   12133              : 
   12134       116293 :           continue;
   12135              :         }
   12136              : 
   12137       654882 :       switch (TREE_CODE (d_inner))
   12138              :         {
   12139       245017 :         case TEMPLATE_DECL:
   12140       245017 :           if (template_heads_equivalent_p (d_inner, m_inner))
   12141              :             {
   12142       103583 :               d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12143       103583 :               m_inner = DECL_TEMPLATE_RESULT (m_inner);
   12144       103583 :               if (d_inner == error_mark_node
   12145       103583 :                   && TYPE_DECL_ALIAS_P (m_inner))
   12146              :                 {
   12147              :                   found = match;
   12148              :                   break;
   12149              :                 }
   12150       103583 :               goto again;
   12151              :             }
   12152              :           break;
   12153              : 
   12154       311085 :         case FUNCTION_DECL:
   12155       311085 :           if (tree m_type = TREE_TYPE (m_inner))
   12156       311085 :             if ((!key.ret
   12157       154116 :                  || same_type_p (key.ret, fndecl_declared_return_type (m_inner)))
   12158       285597 :                 && type_memfn_rqual (m_type) == key.ref_q
   12159       285335 :                 && key.iobj_p == DECL_IOBJ_MEMBER_FUNCTION_P (m_inner)
   12160       570410 :                 && key.xobj_p == DECL_XOBJ_MEMBER_FUNCTION_P (m_inner)
   12161       285314 :                 && compparms (key.args, TYPE_ARG_TYPES (m_type))
   12162       129524 :                 && get_coroutine_discriminator (m_inner) == key.coro_disc
   12163              :                 /* Reject if old is a "C" builtin and new is not "C".
   12164              :                    Matches decls_match behaviour.  */
   12165       129521 :                 && (!DECL_IS_UNDECLARED_BUILTIN (m_inner)
   12166         8122 :                     || !DECL_EXTERN_C_P (m_inner)
   12167         7895 :                     || DECL_EXTERN_C_P (d_inner))
   12168              :                 /* Reject if one is a different member of a
   12169              :                    guarded/pre/post fn set.  */
   12170       129496 :                 && (!flag_contracts
   12171       239382 :                     || (DECL_IS_PRE_FN_P (d_inner)
   12172       119691 :                         == DECL_IS_PRE_FN_P (m_inner)))
   12173       440581 :                 && (!flag_contracts
   12174       239382 :                     || (DECL_IS_POST_FN_P (d_inner)
   12175       119691 :                         == DECL_IS_POST_FN_P (m_inner))))
   12176              :               {
   12177       129496 :                 tree m_reqs = get_constraints (m_inner);
   12178       129496 :                 if (m_reqs)
   12179              :                   {
   12180         9485 :                     if (cxx_dialect < cxx20)
   12181            8 :                       m_reqs = CI_ASSOCIATED_CONSTRAINTS (m_reqs);
   12182              :                     else
   12183        18962 :                       m_reqs = CI_DECLARATOR_REQS (m_reqs);
   12184              :                   }
   12185              : 
   12186       129496 :                 if (cp_tree_equal (key.constraints, m_reqs))
   12187       608418 :                   found = match;
   12188              :               }
   12189              :           break;
   12190              : 
   12191        59339 :         case TYPE_DECL:
   12192       118678 :           if (DECL_IMPLICIT_TYPEDEF_P (d_inner)
   12193        59339 :               == DECL_IMPLICIT_TYPEDEF_P (m_inner))
   12194              :             {
   12195        59303 :               if (!IDENTIFIER_ANON_P (DECL_NAME (m_inner)))
   12196        59174 :                 return match;
   12197          129 :               else if (mk == MK_enum
   12198          129 :                        && (TYPE_NAME (ENUM_UNDERLYING_TYPE (TREE_TYPE (m_inner)))
   12199          129 :                            == key.ret))
   12200              :                 found = match;
   12201              :             }
   12202              :           /* With -freflection, typedef struct { } A is now represented the same
   12203              :              as typedef struct A_ { } A except the TYPE_DECL for A_ is invisible
   12204              :              to name lookup, so we won't be able to find and match it directly.
   12205              :              But we will find the in-TU A (m_inner), through which we can obtain
   12206              :              the in-TU A_ when d_inner is the streamed-in A_.  */
   12207           36 :           else if (flag_reflection
   12208           10 :                    && TYPE_DECL_WAS_UNNAMED (d_inner)
   12209           42 :                    && DECL_ORIGINAL_TYPE (m_inner))
   12210              :             {
   12211            6 :               tree orig = TYPE_NAME (DECL_ORIGINAL_TYPE (m_inner));
   12212            6 :               if (TYPE_DECL_WAS_UNNAMED (orig)
   12213           12 :                   && DECL_NAME (orig) == DECL_NAME (d_inner))
   12214              :                 found = orig;
   12215              :             }
   12216              :           break;
   12217              : 
   12218              :         default:
   12219              :           found = match;
   12220              :           break;
   12221              :         }
   12222              :     }
   12223              : 
   12224       242973 :   return found;
   12225              : }
   12226              : 
   12227              : /* DECL, INNER & TYPE are a skeleton set of nodes for a decl.  Only
   12228              :    the bools have been filled in.  Read its merging key and merge it.
   12229              :    Returns the existing decl if there is one.  */
   12230              : 
   12231              : tree
   12232      1228620 : trees_in::key_mergeable (int tag, merge_kind mk, tree decl, tree inner,
   12233              :                          tree type, tree container, bool is_attached,
   12234              :                          bool is_imported_temploid_friend)
   12235              : {
   12236      1228620 :   const char *kind = "new";
   12237      1228620 :   tree existing = NULL_TREE;
   12238              : 
   12239      1228620 :   if (mk & MK_template_mask)
   12240              :     {
   12241              :       // FIXME: We could stream the specialization hash?
   12242       375290 :       spec_entry spec;
   12243       375290 :       spec.tmpl = tree_node ();
   12244       375290 :       spec.args = tree_node ();
   12245              : 
   12246       375290 :       if (get_overrun ())
   12247            0 :         return error_mark_node;
   12248              : 
   12249       375290 :       DECL_NAME (decl) = DECL_NAME (spec.tmpl);
   12250       375290 :       DECL_CONTEXT (decl) = DECL_CONTEXT (spec.tmpl);
   12251       375290 :       DECL_NAME (inner) = DECL_NAME (decl);
   12252       375290 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12253              : 
   12254       375290 :       tree constr = NULL_TREE;
   12255       375290 :       bool is_decl = mk & MK_tmpl_decl_mask;
   12256       375290 :       if (is_decl)
   12257              :         {
   12258       268630 :           if (flag_concepts && TREE_CODE (inner) == VAR_DECL)
   12259              :             {
   12260         5867 :               constr = tree_node ();
   12261         5867 :               if (constr)
   12262            0 :                 set_constraints (inner, constr);
   12263              :             }
   12264       534671 :           spec.spec = (mk & MK_tmpl_tmpl_mask) ? inner : decl;
   12265              :         }
   12266              :       else
   12267       106660 :         spec.spec = type;
   12268       375290 :       existing = match_mergeable_specialization (is_decl, &spec);
   12269       375290 :       if (constr)
   12270              :         /* We'll add these back later, if this is the new decl.  */
   12271            0 :         remove_constraints (inner);
   12272              : 
   12273       375290 :       if (!existing)
   12274              :         ; /* We'll add to the table once read.  */
   12275       150879 :       else if (mk & MK_tmpl_decl_mask)
   12276              :         {
   12277              :           /* A declaration specialization.  */
   12278       104803 :           if (mk & MK_tmpl_tmpl_mask)
   12279         1144 :             existing = DECL_TI_TEMPLATE (existing);
   12280              :         }
   12281              :       else
   12282              :         {
   12283              :           /* A type specialization.  */
   12284        46076 :           if (mk & MK_tmpl_tmpl_mask)
   12285          246 :             existing = CLASSTYPE_TI_TEMPLATE (existing);
   12286              :           else
   12287        45830 :             existing = TYPE_NAME (existing);
   12288              :         }
   12289              :     }
   12290       853330 :   else if (mk == MK_unique)
   12291              :     kind = "unique";
   12292              :   else
   12293              :     {
   12294       631697 :       tree name = tree_node ();
   12295              : 
   12296       631697 :       merge_key key;
   12297       631697 :       unsigned code = u ();
   12298       631697 :       key.ref_q = cp_ref_qualifier ((code >> 0) & 3);
   12299       631697 :       key.coro_disc = (code >> 2) & 3;
   12300       631697 :       key.iobj_p = (code >> 4) & 1;
   12301       631697 :       key.xobj_p = (code >> 5) & 1;
   12302       631697 :       key.index = code >> 6;
   12303              : 
   12304       631697 :       if (mk == MK_enum)
   12305          237 :         key.ret = tree_node ();
   12306       631460 :       else if (mk == MK_partial
   12307       619188 :                || ((mk == MK_named || mk == MK_friend_spec)
   12308       524476 :                    && TREE_CODE (inner) == FUNCTION_DECL))
   12309              :         {
   12310       301793 :           key.ret = tree_node ();
   12311       301793 :           tree arg, *arg_ptr = &key.args;
   12312       301793 :           while ((arg = tree_node ())
   12313       853132 :                  && arg != void_list_node
   12314      1419851 :                  && mk != MK_partial)
   12315              :             {
   12316       552893 :               *arg_ptr = tree_cons (NULL_TREE, arg, NULL_TREE);
   12317       552893 :               arg_ptr = &TREE_CHAIN (*arg_ptr);
   12318              :             }
   12319       301793 :           *arg_ptr = arg;
   12320       301793 :           key.constraints = tree_node ();
   12321              :         }
   12322              : 
   12323       631697 :       if (get_overrun ())
   12324            0 :         return error_mark_node;
   12325              : 
   12326       631697 :       if (mk < MK_indirect_lwm)
   12327              :         {
   12328       619277 :           DECL_NAME (decl) = name;
   12329       619277 :           DECL_CONTEXT (decl) = FROB_CONTEXT (container);
   12330              :         }
   12331       631697 :       DECL_NAME (inner) = DECL_NAME (decl);
   12332       631697 :       DECL_CONTEXT (inner) = DECL_CONTEXT (decl);
   12333              : 
   12334       631697 :       if (mk == MK_partial)
   12335              :         {
   12336        12272 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (key.ret);
   12337        60527 :                spec; spec = TREE_CHAIN (spec))
   12338              :             {
   12339        54411 :               tree tmpl = TREE_VALUE (spec);
   12340        54411 :               tree ti = get_template_info (tmpl);
   12341        54411 :               if (template_args_equal (key.args, TI_ARGS (ti))
   12342        61369 :                   && cp_tree_equal (key.constraints,
   12343              :                                     get_constraints
   12344         6958 :                                     (DECL_TEMPLATE_RESULT (tmpl))))
   12345              :                 {
   12346              :                   existing = tmpl;
   12347              :                   break;
   12348              :                 }
   12349              :             }
   12350              :         }
   12351       619425 :       else if (mk == MK_keyed
   12352          375 :                && DECL_LANG_SPECIFIC (name)
   12353       619800 :                && DECL_MODULE_KEYED_DECLS_P (name))
   12354              :         {
   12355          375 :           gcc_checking_assert (TREE_CODE (container) == NAMESPACE_DECL
   12356              :                                || TREE_CODE (container) == TYPE_DECL
   12357              :                                || TREE_CODE (container) == FUNCTION_DECL);
   12358          375 :           if (auto *set = keyed_table->get (name))
   12359       631846 :             if (key.index < set->length ())
   12360              :               {
   12361          149 :                 existing = (*set)[key.index];
   12362          149 :                 if (existing)
   12363              :                   {
   12364          149 :                     gcc_checking_assert
   12365              :                       (DECL_IMPLICIT_TYPEDEF_P (existing));
   12366          149 :                     if (inner != decl)
   12367           91 :                       existing
   12368           91 :                         = CLASSTYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12369              :                   }
   12370              :               }
   12371              :         }
   12372              :       else
   12373       619050 :         switch (TREE_CODE (container))
   12374              :           {
   12375            0 :           default:
   12376            0 :             gcc_unreachable ();
   12377              : 
   12378       151362 :           case NAMESPACE_DECL:
   12379       151362 :             if (is_attached
   12380       151362 :                 && !is_imported_temploid_friend
   12381       151362 :                 && !(state->is_module () || state->is_partition ()))
   12382              :               kind = "unique";
   12383              :             else
   12384              :               {
   12385       149229 :                 gcc_checking_assert (mk == MK_named || mk == MK_enum);
   12386       149229 :                 tree mvec;
   12387       149229 :                 tree *vslot = mergeable_namespace_slots (container, name,
   12388              :                                                          is_attached, &mvec);
   12389       149229 :                 existing = check_mergeable_decl (mk, decl, *vslot, key);
   12390       149229 :                 if (!existing)
   12391        73590 :                   add_mergeable_namespace_entity (vslot, decl);
   12392              :                 else
   12393              :                   {
   12394              :                     /* Note that we now have duplicates to deal with in
   12395              :                        name lookup.  */
   12396        75639 :                     if (is_attached)
   12397           66 :                       BINDING_VECTOR_PARTITION_DUPS_P (mvec) = true;
   12398              :                     else
   12399        75573 :                       BINDING_VECTOR_GLOBAL_DUPS_P (mvec) = true;
   12400              :                   }
   12401              :               }
   12402       156144 :             break;
   12403              : 
   12404         4782 :           case FUNCTION_DECL:
   12405         4782 :             gcc_checking_assert (mk == MK_local_type);
   12406         4782 :             existing = key_local_type (key, container, name);
   12407         4782 :             if (existing && inner != decl)
   12408         3484 :               existing = TYPE_TI_TEMPLATE (TREE_TYPE (existing));
   12409              :             break;
   12410              : 
   12411       462906 :           case TYPE_DECL:
   12412       462906 :             gcc_checking_assert (!is_imported_temploid_friend);
   12413       462906 :             int use_tmpl = 0;
   12414         5991 :             if (is_attached && !(state->is_module () || state->is_partition ())
   12415              :                 /* Implicit or in-class defaulted member functions
   12416              :                    can come from anywhere.  */
   12417         4303 :                 && !(TREE_CODE (decl) == FUNCTION_DECL
   12418         1271 :                      && !DECL_THUNK_P (decl)
   12419         1271 :                      && DECL_DEFAULTED_FN (decl)
   12420          820 :                      && !DECL_DEFAULTED_OUTSIDE_CLASS_P (decl))
   12421              :                 /* As can members of template specialisations.  */
   12422       466389 :                 && !(node_template_info (container, use_tmpl)
   12423         1307 :                      && use_tmpl != 0))
   12424              :               kind = "unique";
   12425              :             else
   12426              :               {
   12427       459671 :                 tree ctx = TREE_TYPE (container);
   12428              : 
   12429              :                 /* For some reason templated enumeral types are not marked
   12430              :                    as COMPLETE_TYPE_P, even though they have members.
   12431              :                    This may well be a bug elsewhere.  */
   12432       459671 :                 if (TREE_CODE (ctx) == ENUMERAL_TYPE)
   12433        15257 :                   existing = find_enum_member (ctx, name);
   12434       444414 :                 else if (COMPLETE_TYPE_P (ctx))
   12435              :                   {
   12436       192554 :                     switch (mk)
   12437              :                       {
   12438            0 :                       default:
   12439            0 :                         gcc_unreachable ();
   12440              : 
   12441       153378 :                       case MK_named:
   12442       153378 :                         existing = lookup_class_binding (ctx, name);
   12443       153378 :                         if (existing)
   12444              :                           {
   12445       152918 :                             tree inner = decl;
   12446       152918 :                             if (TREE_CODE (inner) == TEMPLATE_DECL
   12447       152918 :                                 && !DECL_MEMBER_TEMPLATE_P (inner))
   12448        67651 :                               inner = DECL_TEMPLATE_RESULT (inner);
   12449              : 
   12450       152918 :                             existing = check_mergeable_decl
   12451       152918 :                               (mk, inner, existing, key);
   12452              : 
   12453       152918 :                             if (!existing && DECL_ALIAS_TEMPLATE_P (decl))
   12454              :                               {} // FIXME: Insert into specialization
   12455              :                             // tables, we'll need the arguments for that!
   12456              :                           }
   12457              :                         break;
   12458              : 
   12459        26583 :                       case MK_field:
   12460        26583 :                         {
   12461        26583 :                           unsigned ix = key.index;
   12462        26583 :                           for (tree field = TYPE_FIELDS (ctx);
   12463       892099 :                                field; field = DECL_CHAIN (field))
   12464              :                             {
   12465       892099 :                               tree finner = STRIP_TEMPLATE (field);
   12466       892099 :                               if (TREE_CODE (finner) == TREE_CODE (inner))
   12467       299361 :                                 if (!ix--)
   12468              :                                   {
   12469              :                                     existing = field;
   12470              :                                     break;
   12471              :                                   }
   12472              :                             }
   12473              :                         }
   12474              :                         break;
   12475              : 
   12476         1492 :                       case MK_vtable:
   12477         1492 :                         {
   12478         1492 :                           unsigned ix = key.index;
   12479         1492 :                           for (tree vtable = CLASSTYPE_VTABLES (ctx);
   12480         1831 :                                vtable; vtable = DECL_CHAIN (vtable))
   12481         1557 :                             if (!ix--)
   12482              :                               {
   12483              :                                 existing = vtable;
   12484              :                                 break;
   12485              :                               }
   12486              :                         }
   12487              :                         break;
   12488              : 
   12489         8076 :                       case MK_as_base:
   12490         8076 :                         {
   12491         8076 :                           tree as_base = CLASSTYPE_AS_BASE (ctx);
   12492         8076 :                           if (as_base && as_base != ctx)
   12493         8076 :                             existing = TYPE_NAME (as_base);
   12494              :                         }
   12495              :                         break;
   12496              : 
   12497         3025 :                       case MK_local_friend:
   12498         3025 :                         {
   12499         3025 :                           unsigned ix = key.index;
   12500         3025 :                           for (tree decls = CLASSTYPE_DECL_LIST (ctx);
   12501        84192 :                                decls; decls = TREE_CHAIN (decls))
   12502        84192 :                             if (!TREE_PURPOSE (decls) && !ix--)
   12503              :                               {
   12504         3025 :                                 existing
   12505         3025 :                                   = friend_from_decl_list (TREE_VALUE (decls));
   12506         3025 :                                 break;
   12507              :                               }
   12508              :                         }
   12509              :                         break;
   12510              :                       }
   12511              : 
   12512       192554 :                     if (existing && mk < MK_indirect_lwm && mk != MK_partial
   12513       188291 :                         && TREE_CODE (decl) == TEMPLATE_DECL
   12514       278871 :                         && !DECL_MEMBER_TEMPLATE_P (decl))
   12515              :                       {
   12516        69667 :                         tree ti;
   12517        69667 :                         if (DECL_IMPLICIT_TYPEDEF_P (existing))
   12518          887 :                           ti = TYPE_TEMPLATE_INFO (TREE_TYPE (existing));
   12519              :                         else
   12520        68780 :                           ti = DECL_TEMPLATE_INFO (existing);
   12521        69667 :                         existing = TI_TEMPLATE (ti);
   12522              :                       }
   12523              :                   }
   12524              :               }
   12525              :           }
   12526              :     }
   12527              : 
   12528      1228620 :   dump (dumper::MERGE)
   12529         3208 :     && dump ("Read:%d's %s merge key (%s) %C:%N", tag, merge_kind_name[mk],
   12530         3208 :              existing ? "matched" : kind, TREE_CODE (decl), decl);
   12531              : 
   12532              :   return existing;
   12533              : }
   12534              : 
   12535              : void
   12536       388332 : trees_out::binfo_mergeable (tree binfo)
   12537              : {
   12538       388332 :   tree dom = binfo;
   12539       490178 :   while (tree parent = BINFO_INHERITANCE_CHAIN (dom))
   12540              :     dom = parent;
   12541       388332 :   tree type = BINFO_TYPE (dom);
   12542       388332 :   gcc_checking_assert (TYPE_BINFO (type) == dom);
   12543       388332 :   tree_node (type);
   12544       388332 :   if (streaming_p ())
   12545              :     {
   12546              :       unsigned ix = 0;
   12547       205913 :       for (; dom != binfo; dom = TREE_CHAIN (dom))
   12548        53683 :         ix++;
   12549       152230 :       u (ix);
   12550              :     }
   12551       388332 : }
   12552              : 
   12553              : unsigned
   12554        95481 : trees_in::binfo_mergeable (tree *type)
   12555              : {
   12556        95481 :   *type = tree_node ();
   12557        95481 :   return u ();
   12558              : }
   12559              : 
   12560              : /* DECL is a just streamed declaration with attributes DATTR that should
   12561              :    have matching ABI tags as EXISTING's attributes EATTR.  Check that the
   12562              :    ABI tags match, and report an error if not.  */
   12563              : 
   12564              : void
   12565       295081 : trees_in::check_abi_tags (tree existing, tree decl, tree &eattr, tree &dattr)
   12566              : {
   12567       295081 :   tree etags = lookup_attribute ("abi_tag", eattr);
   12568       295081 :   tree dtags = lookup_attribute ("abi_tag", dattr);
   12569       295081 :   if ((etags == nullptr) != (dtags == nullptr)
   12570       295081 :       || (etags && !attribute_value_equal (etags, dtags)))
   12571              :     {
   12572           33 :       if (etags)
   12573           24 :         etags = TREE_VALUE (etags);
   12574           33 :       if (dtags)
   12575           24 :         dtags = TREE_VALUE (dtags);
   12576              : 
   12577              :       /* We only error if mangling wouldn't consider the tags equivalent.
   12578              :          Since tags might have been inherited during mangling, ignore
   12579              :          inherited tags if there's a mangled-ness mismatch.  */
   12580           33 :       bool ignore_inherited_p
   12581           33 :         = (DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (existing))
   12582           33 :            != DECL_ASSEMBLER_NAME_SET_P (STRIP_TEMPLATE (decl)));
   12583           33 :       if (!equal_abi_tags (etags, dtags, ignore_inherited_p))
   12584              :         {
   12585           21 :           auto_diagnostic_group d;
   12586           21 :           if (dtags)
   12587           15 :             error_at (DECL_SOURCE_LOCATION (decl),
   12588              :                       "mismatching abi tags for %qD with tags %qE",
   12589              :                       decl, dtags);
   12590              :           else
   12591            6 :             error_at (DECL_SOURCE_LOCATION (decl),
   12592              :                       "mismatching abi tags for %qD with no tags", decl);
   12593           21 :           if (etags)
   12594           15 :             inform (DECL_SOURCE_LOCATION (existing),
   12595              :                     "existing declaration here with tags %qE", etags);
   12596              :           else
   12597            6 :             inform (DECL_SOURCE_LOCATION (existing),
   12598              :                     "existing declaration here with no tags");
   12599           21 :         }
   12600              : 
   12601              :       /* Always use the existing abi_tags as the canonical set so that
   12602              :          later processing doesn't get confused.  */
   12603           33 :       if (dtags)
   12604           24 :         dattr = remove_attribute ("abi_tag", dattr);
   12605           33 :       if (etags)
   12606           24 :         duplicate_one_attribute (&dattr, eattr, "abi_tag");
   12607              :     }
   12608       295081 : }
   12609              : 
   12610              : /* DECL is a just streamed mergeable decl that should match EXISTING.  Check
   12611              :    it does and issue an appropriate diagnostic if not.  Merge any
   12612              :    bits from DECL to EXISTING.  This is stricter matching than
   12613              :    decls_match, because we can rely on ODR-sameness, and we cannot use
   12614              :    decls_match because it can cause instantiations of constraints.  */
   12615              : 
   12616              : bool
   12617       433008 : trees_in::is_matching_decl (tree existing, tree decl, bool is_typedef)
   12618              : {
   12619              :   // FIXME: We should probably do some duplicate decl-like stuff here
   12620              :   // (beware, default parms should be the same?)  Can we just call
   12621              :   // duplicate_decls and teach it how to handle the module-specific
   12622              :   // permitted/required duplications?
   12623              : 
   12624              :   // We know at this point that the decls have matched by key, so we
   12625              :   // can elide some of the checking
   12626       433008 :   gcc_checking_assert (TREE_CODE (existing) == TREE_CODE (decl));
   12627              : 
   12628       433008 :   tree d_inner = decl;
   12629       433008 :   tree e_inner = existing;
   12630       433008 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12631              :     {
   12632       143185 :       d_inner = DECL_TEMPLATE_RESULT (d_inner);
   12633       143185 :       e_inner = DECL_TEMPLATE_RESULT (e_inner);
   12634       143185 :       gcc_checking_assert (TREE_CODE (e_inner) == TREE_CODE (d_inner));
   12635              :     }
   12636              : 
   12637              :   // FIXME: do more precise errors at point of mismatch
   12638       433008 :   const char *mismatch_msg = nullptr;
   12639              : 
   12640       433008 :   if (VAR_OR_FUNCTION_DECL_P (d_inner)
   12641       433008 :       && DECL_EXTERN_C_P (d_inner) != DECL_EXTERN_C_P (e_inner))
   12642              :     {
   12643            6 :       mismatch_msg = G_("conflicting language linkage for imported "
   12644              :                         "declaration %#qD");
   12645            6 :       goto mismatch;
   12646              :     }
   12647       433002 :   else if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12648              :     {
   12649       197896 :       tree e_ret = fndecl_declared_return_type (existing);
   12650       197896 :       tree d_ret = fndecl_declared_return_type (decl);
   12651              : 
   12652        84396 :       if (decl != d_inner && DECL_NAME (d_inner) == fun_identifier
   12653       197920 :           && LAMBDA_TYPE_P (DECL_CONTEXT (d_inner)))
   12654              :         /* This has a recursive type that will compare different.  */;
   12655       197884 :       else if (!same_type_p (d_ret, e_ret))
   12656              :         {
   12657           26 :           mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12658           26 :           goto mismatch;
   12659              :         }
   12660              : 
   12661       197870 :       tree& e_type = TREE_TYPE (e_inner);
   12662       197870 :       tree d_type = TREE_TYPE (d_inner);
   12663              : 
   12664       197870 :       for (tree e_args = TYPE_ARG_TYPES (e_type),
   12665       197870 :              d_args = TYPE_ARG_TYPES (d_type);
   12666       339572 :            e_args != d_args && (e_args || d_args);
   12667       141702 :            e_args = TREE_CHAIN (e_args), d_args = TREE_CHAIN (d_args))
   12668              :         {
   12669       141702 :           if (!(e_args && d_args))
   12670              :             {
   12671            0 :               mismatch_msg = G_("conflicting argument list for imported "
   12672              :                                 "declaration %#qD");
   12673            0 :               goto mismatch;
   12674              :             }
   12675              : 
   12676       141702 :           if (!same_type_p (TREE_VALUE (d_args), TREE_VALUE (e_args)))
   12677              :             {
   12678            0 :               mismatch_msg = G_("conflicting argument types for imported "
   12679              :                                 "declaration %#qD");
   12680            0 :               goto mismatch;
   12681              :             }
   12682              :         }
   12683              : 
   12684              :       /* If EXISTING has an undeduced or uninstantiated exception
   12685              :          specification, but DECL does not, propagate the exception
   12686              :          specification.  Otherwise we end up asserting or trying to
   12687              :          instantiate it in the middle of loading.   */
   12688       197870 :       tree e_spec = TYPE_RAISES_EXCEPTIONS (e_type);
   12689       197870 :       tree d_spec = TYPE_RAISES_EXCEPTIONS (d_type);
   12690       289487 :       if (DECL_MAYBE_DELETED (e_inner) || DEFERRED_NOEXCEPT_SPEC_P (e_spec))
   12691              :         {
   12692        31378 :           if (!(DECL_MAYBE_DELETED (d_inner)
   12693        15632 :                 || DEFERRED_NOEXCEPT_SPEC_P (d_spec))
   12694        46762 :               || (UNEVALUATED_NOEXCEPT_SPEC_P (e_spec)
   12695        25414 :                   && !UNEVALUATED_NOEXCEPT_SPEC_P (d_spec)))
   12696              :             {
   12697          153 :               dump (dumper::MERGE)
   12698            6 :                 && dump ("Propagating instantiated noexcept to %N", existing);
   12699          153 :               gcc_checking_assert (existing == e_inner);
   12700          153 :               e_type = build_exception_variant (e_type, d_spec);
   12701              : 
   12702              :               /* Propagate to existing clones.  */
   12703          153 :               tree clone;
   12704          439 :               FOR_EACH_CLONE (clone, existing)
   12705          286 :                 TREE_TYPE (clone)
   12706          572 :                   = build_exception_variant (TREE_TYPE (clone), d_spec);
   12707              :             }
   12708              :         }
   12709       182124 :       else if (!DECL_MAYBE_DELETED (d_inner)
   12710       259759 :                && !DEFERRED_NOEXCEPT_SPEC_P (d_spec)
   12711       364247 :                && !comp_except_specs (d_spec, e_spec, ce_type))
   12712              :         {
   12713         1721 :           mismatch_msg = G_("conflicting %<noexcept%> specifier for "
   12714              :                             "imported declaration %#qD");
   12715         1721 :           goto mismatch;
   12716              :         }
   12717              : 
   12718              :       /* Similarly if EXISTING has an undeduced return type, but DECL's
   12719              :          is already deduced.  */
   12720       196149 :       bool e_undeduced = undeduced_auto_decl (existing);
   12721       196149 :       bool d_undeduced = undeduced_auto_decl (decl);
   12722       196149 :       if (e_undeduced && !d_undeduced)
   12723              :         {
   12724           16 :           dump (dumper::MERGE)
   12725            0 :             && dump ("Propagating deduced return type to %N", existing);
   12726           16 :           gcc_checking_assert (existing == e_inner);
   12727           16 :           FNDECL_USED_AUTO (existing) = true;
   12728           16 :           DECL_SAVED_AUTO_RETURN_TYPE (existing) = TREE_TYPE (e_type);
   12729           16 :           e_type = change_return_type (TREE_TYPE (d_type), e_type);
   12730              :         }
   12731       196133 :       else if (d_undeduced && !e_undeduced)
   12732              :         /* EXISTING was deduced, leave it alone.  */;
   12733       196130 :       else if (type_uses_auto (d_ret)
   12734       196130 :                && !same_type_p (TREE_TYPE (d_type), TREE_TYPE (e_type)))
   12735              :         {
   12736            9 :           mismatch_msg = G_("conflicting deduced return type for "
   12737              :                             "imported declaration %#qD");
   12738            9 :           goto mismatch;
   12739              :         }
   12740              : 
   12741              :       /* Similarly if EXISTING has undeduced constexpr, but DECL's
   12742              :          is already deduced.  */
   12743       196140 :       if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12744       196140 :           == DECL_DECLARED_CONSTEXPR_P (d_inner))
   12745              :         /* Already matches.  */;
   12746            2 :       else if (DECL_DECLARED_CONSTEXPR_P (d_inner)
   12747            2 :                && (DECL_MAYBE_DELETED (e_inner)
   12748            0 :                    || decl_implicit_constexpr_p (d_inner)))
   12749              :         /* DECL was deduced, copy to EXISTING.  */
   12750              :         {
   12751            1 :           DECL_DECLARED_CONSTEXPR_P (e_inner) = true;
   12752            1 :           if (decl_implicit_constexpr_p (d_inner))
   12753            0 :             DECL_LANG_SPECIFIC (e_inner)->u.fn.implicit_constexpr = true;
   12754              :         }
   12755            1 :       else if (DECL_DECLARED_CONSTEXPR_P (e_inner)
   12756            1 :                && (DECL_MAYBE_DELETED (d_inner)
   12757            0 :                    || decl_implicit_constexpr_p (e_inner)))
   12758              :         /* EXISTING was deduced, leave it alone.  */;
   12759              :       else
   12760              :         {
   12761            0 :           mismatch_msg = G_("conflicting %<constexpr%> for imported "
   12762              :                             "declaration %#qD");
   12763            0 :           goto mismatch;
   12764              :         }
   12765              : 
   12766              :       /* Don't synthesize a defaulted function if we're importing one
   12767              :          we've already determined.  */
   12768       196140 :       if (!DECL_MAYBE_DELETED (d_inner))
   12769       196025 :         DECL_MAYBE_DELETED (e_inner) = false;
   12770              :     }
   12771       235106 :   else if (is_typedef)
   12772              :     {
   12773        83687 :       if (!DECL_ORIGINAL_TYPE (e_inner)
   12774        83687 :           || !same_type_p (DECL_ORIGINAL_TYPE (d_inner),
   12775              :                            DECL_ORIGINAL_TYPE (e_inner)))
   12776              :         {
   12777            3 :           mismatch_msg = G_("conflicting imported declaration %q#D");
   12778            3 :           goto mismatch;
   12779              :         }
   12780              :     }
   12781              :   /* Using cp_tree_equal because we can meet TYPE_ARGUMENT_PACKs
   12782              :      here. I suspect the entities that directly do that are things
   12783              :      that shouldn't go to duplicate_decls (FIELD_DECLs etc).   */
   12784       151419 :   else if (!cp_tree_equal (TREE_TYPE (decl), TREE_TYPE (existing)))
   12785              :     {
   12786              :       mismatch_msg = G_("conflicting type for imported declaration %#qD");
   12787         2513 :     mismatch:
   12788         2513 :       if (DECL_IS_UNDECLARED_BUILTIN (existing))
   12789              :         /* Just like duplicate_decls, presum the user knows what
   12790              :            they're doing in overriding a builtin.   */
   12791         1744 :         TREE_TYPE (existing) = TREE_TYPE (decl);
   12792          769 :       else if (decl_function_context (decl))
   12793              :         /* The type of a mergeable local entity (such as a function scope
   12794              :            capturing lambda's closure type fields) can depend on an
   12795              :            unmergeable local entity (such as a local variable), so type
   12796              :            equality isn't feasible in general for local entities.  */;
   12797              :       else
   12798              :         {
   12799           21 :           gcc_checking_assert (mismatch_msg);
   12800           21 :           auto_diagnostic_group d;
   12801           21 :           error_at (DECL_SOURCE_LOCATION (decl), mismatch_msg, decl);
   12802           21 :           inform (DECL_SOURCE_LOCATION (existing),
   12803              :                   "existing declaration %#qD", existing);
   12804           21 :           return false;
   12805           21 :         }
   12806              :     }
   12807              : 
   12808       432987 :   if (DECL_IS_UNDECLARED_BUILTIN (existing)
   12809       432987 :       && !DECL_IS_UNDECLARED_BUILTIN (decl))
   12810              :     {
   12811              :       /* We're matching a builtin that the user has yet to declare.
   12812              :          We are the one!  This is very much duplicate-decl
   12813              :          shenanigans. */
   12814         2030 :       DECL_SOURCE_LOCATION (existing) = DECL_SOURCE_LOCATION (decl);
   12815         2030 :       if (TREE_CODE (decl) != TYPE_DECL)
   12816              :         {
   12817              :           /* Propagate exceptions etc.  */
   12818         2006 :           TREE_TYPE (existing) = TREE_TYPE (decl);
   12819         2006 :           TREE_NOTHROW (existing) = TREE_NOTHROW (decl);
   12820              :         }
   12821              :       /* This is actually an import! */
   12822         2030 :       DECL_MODULE_IMPORT_P (existing) = true;
   12823              : 
   12824              :       /* Yay, sliced!  */
   12825         2030 :       existing->base = decl->base;
   12826              : 
   12827         2030 :       if (TREE_CODE (decl) == FUNCTION_DECL)
   12828              :         {
   12829              :           /* Ew :(  */
   12830         2006 :           memcpy (&existing->decl_common.size,
   12831              :                   &decl->decl_common.size,
   12832              :                   (offsetof (tree_decl_common, pt_uid)
   12833              :                    - offsetof (tree_decl_common, size)));
   12834         2006 :           auto bltin_class = DECL_BUILT_IN_CLASS (decl);
   12835         2006 :           existing->function_decl.built_in_class = bltin_class;
   12836         2006 :           auto fncode = DECL_UNCHECKED_FUNCTION_CODE (decl);
   12837         2006 :           DECL_UNCHECKED_FUNCTION_CODE (existing) = fncode;
   12838         2006 :           if (existing->function_decl.built_in_class == BUILT_IN_NORMAL)
   12839              :             {
   12840         1796 :               if (builtin_decl_explicit_p (built_in_function (fncode)))
   12841         1796 :                 switch (fncode)
   12842              :                   {
   12843            0 :                   case BUILT_IN_STPCPY:
   12844            0 :                     set_builtin_decl_implicit_p
   12845            0 :                       (built_in_function (fncode), true);
   12846            0 :                     break;
   12847         1796 :                   default:
   12848         1796 :                     set_builtin_decl_declared_p
   12849         1796 :                       (built_in_function (fncode), true);
   12850         1796 :                     break;
   12851              :                   }
   12852         1796 :               copy_attributes_to_builtin (decl);
   12853              :             }
   12854              :         }
   12855              :     }
   12856              : 
   12857       432987 :   if (VAR_OR_FUNCTION_DECL_P (decl)
   12858       432987 :       && DECL_TEMPLATE_INSTANTIATED (decl))
   12859              :     /* Don't instantiate again!  */
   12860         8832 :     DECL_TEMPLATE_INSTANTIATED (existing) = true;
   12861              : 
   12862       432987 :   if (TREE_CODE (d_inner) == FUNCTION_DECL
   12863       432987 :       && DECL_DECLARED_INLINE_P (d_inner))
   12864              :     {
   12865       160366 :       DECL_DECLARED_INLINE_P (e_inner) = true;
   12866       160366 :       if (!DECL_SAVED_TREE (e_inner)
   12867        79938 :           && lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (d_inner))
   12868       160383 :           && !lookup_attribute ("gnu_inline", DECL_ATTRIBUTES (e_inner)))
   12869              :         {
   12870           51 :           DECL_INTERFACE_KNOWN (e_inner)
   12871           17 :             |= DECL_INTERFACE_KNOWN (d_inner);
   12872           17 :           DECL_DISREGARD_INLINE_LIMITS (e_inner)
   12873           17 :             |= DECL_DISREGARD_INLINE_LIMITS (d_inner);
   12874              :           // TODO: we will eventually want to merge all decl attributes
   12875           17 :           duplicate_one_attribute (&DECL_ATTRIBUTES (e_inner),
   12876           17 :                                    DECL_ATTRIBUTES (d_inner), "gnu_inline");
   12877              :         }
   12878              :     }
   12879       432987 :   if (!DECL_EXTERNAL (d_inner))
   12880       202350 :     DECL_EXTERNAL (e_inner) = false;
   12881              : 
   12882       432987 :   if (VAR_OR_FUNCTION_DECL_P (d_inner))
   12883       436450 :     check_abi_tags (existing, decl,
   12884       218225 :                     DECL_ATTRIBUTES (e_inner), DECL_ATTRIBUTES (d_inner));
   12885              : 
   12886       432987 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   12887              :     {
   12888              :       /* Merge default template arguments.  */
   12889       143183 :       tree d_parms = DECL_INNERMOST_TEMPLATE_PARMS (decl);
   12890       143183 :       tree e_parms = DECL_INNERMOST_TEMPLATE_PARMS (existing);
   12891       143183 :       gcc_checking_assert (TREE_VEC_LENGTH (d_parms)
   12892              :                            == TREE_VEC_LENGTH (e_parms));
   12893       410729 :       for (int i = 0; i < TREE_VEC_LENGTH (d_parms); ++i)
   12894              :         {
   12895       267555 :           tree d_default = TREE_PURPOSE (TREE_VEC_ELT (d_parms, i));
   12896       267555 :           tree& e_default = TREE_PURPOSE (TREE_VEC_ELT (e_parms, i));
   12897       267555 :           if (e_default == NULL_TREE)
   12898       226691 :             e_default = d_default;
   12899        40864 :           else if (d_default != NULL_TREE
   12900        40864 :                    && !cp_tree_equal (d_default, e_default))
   12901              :             {
   12902            9 :               auto_diagnostic_group d;
   12903            9 :               tree d_parm = TREE_VALUE (TREE_VEC_ELT (d_parms, i));
   12904            9 :               tree e_parm = TREE_VALUE (TREE_VEC_ELT (e_parms, i));
   12905            9 :               error_at (DECL_SOURCE_LOCATION (d_parm),
   12906              :                         "conflicting default argument for %#qD", d_parm);
   12907            9 :               inform (DECL_SOURCE_LOCATION (e_parm),
   12908              :                       "existing default declared here");
   12909            9 :               return false;
   12910            9 :             }
   12911              :         }
   12912              :     }
   12913              : 
   12914       432978 :   if (TREE_CODE (d_inner) == FUNCTION_DECL)
   12915              :     {
   12916              :       /* Merge default function arguments.  */
   12917       197887 :       tree d_parm = FUNCTION_FIRST_USER_PARMTYPE (d_inner);
   12918       197887 :       tree e_parm = FUNCTION_FIRST_USER_PARMTYPE (e_inner);
   12919       197887 :       int i = 0;
   12920       669005 :       for (; d_parm && d_parm != void_list_node;
   12921       273231 :            d_parm = TREE_CHAIN (d_parm), e_parm = TREE_CHAIN (e_parm), ++i)
   12922              :         {
   12923       273243 :           tree d_default = TREE_PURPOSE (d_parm);
   12924       273243 :           tree& e_default = TREE_PURPOSE (e_parm);
   12925       273243 :           if (e_default == NULL_TREE)
   12926       257003 :             e_default = d_default;
   12927        16240 :           else if (d_default != NULL_TREE
   12928        16240 :                    && !cp_tree_equal (d_default, e_default))
   12929              :             {
   12930           12 :               auto_diagnostic_group d;
   12931           12 :               error_at (get_fndecl_argument_location (d_inner, i),
   12932              :                         "conflicting default argument for parameter %P of %#qD",
   12933              :                         i, decl);
   12934           12 :               inform (get_fndecl_argument_location (e_inner, i),
   12935              :                       "existing default declared here");
   12936           12 :               return false;
   12937           12 :             }
   12938              :         }
   12939              :     }
   12940              : 
   12941              :   return true;
   12942              : }
   12943              : 
   12944              : /* FN is an implicit member function that we've discovered is new to
   12945              :    the class.  Add it to the TYPE_FIELDS chain and the method vector.
   12946              :    Reset the appropriate classtype lazy flag.   */
   12947              : 
   12948              : bool
   12949          964 : trees_in::install_implicit_member (tree fn)
   12950              : {
   12951          964 :   tree ctx = DECL_CONTEXT (fn);
   12952          964 :   tree name = DECL_NAME (fn);
   12953              :   /* We know these are synthesized, so the set of expected prototypes
   12954              :      is quite restricted.  We're not validating correctness, just
   12955              :      distinguishing between the small set of possibilities.  */
   12956          964 :   tree parm_type = TREE_VALUE (FUNCTION_FIRST_USER_PARMTYPE (fn));
   12957          964 :   if (IDENTIFIER_CTOR_P (name))
   12958              :     {
   12959          615 :       if (CLASSTYPE_LAZY_DEFAULT_CTOR (ctx)
   12960          615 :           && VOID_TYPE_P (parm_type))
   12961          147 :         CLASSTYPE_LAZY_DEFAULT_CTOR (ctx) = false;
   12962          468 :       else if (!TYPE_REF_P (parm_type))
   12963              :         return false;
   12964          468 :       else if (CLASSTYPE_LAZY_COPY_CTOR (ctx)
   12965          468 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12966          237 :         CLASSTYPE_LAZY_COPY_CTOR (ctx) = false;
   12967          231 :       else if (CLASSTYPE_LAZY_MOVE_CTOR (ctx))
   12968          231 :         CLASSTYPE_LAZY_MOVE_CTOR (ctx) = false;
   12969              :       else
   12970              :         return false;
   12971              :     }
   12972          349 :   else if (IDENTIFIER_DTOR_P (name))
   12973              :     {
   12974          277 :       if (CLASSTYPE_LAZY_DESTRUCTOR (ctx))
   12975          277 :         CLASSTYPE_LAZY_DESTRUCTOR (ctx) = false;
   12976              :       else
   12977              :         return false;
   12978          277 :       if (DECL_VIRTUAL_P (fn))
   12979              :         /* A virtual dtor should have been created when the class
   12980              :            became complete.  */
   12981              :         return false;
   12982              :     }
   12983           72 :   else if (name == assign_op_identifier)
   12984              :     {
   12985           72 :       if (!TYPE_REF_P (parm_type))
   12986              :         return false;
   12987           72 :       else if (CLASSTYPE_LAZY_COPY_ASSIGN (ctx)
   12988           72 :                && !TYPE_REF_IS_RVALUE (parm_type))
   12989           36 :         CLASSTYPE_LAZY_COPY_ASSIGN (ctx) = false;
   12990           36 :       else if (CLASSTYPE_LAZY_MOVE_ASSIGN (ctx))
   12991           36 :         CLASSTYPE_LAZY_MOVE_ASSIGN (ctx) = false;
   12992              :       else
   12993              :         return false;
   12994              :     }
   12995              :   else
   12996              :     return false;
   12997              : 
   12998         1009 :   dump (dumper::MERGE) && dump ("Adding implicit member %N", fn);
   12999              : 
   13000          964 :   DECL_CHAIN (fn) = TYPE_FIELDS (ctx);
   13001          964 :   TYPE_FIELDS (ctx) = fn;
   13002              : 
   13003          964 :   add_method (ctx, fn, false);
   13004              : 
   13005              :     /* Propagate TYPE_FIELDS.  */
   13006          964 :   fixup_type_variants (ctx);
   13007              : 
   13008          964 :   return true;
   13009              : }
   13010              : 
   13011              : /* Return non-zero if DECL has a definition that would be interesting to
   13012              :    write out.  */
   13013              : 
   13014              : static bool
   13015      2266825 : has_definition (tree decl)
   13016              : {
   13017      2266831 :   bool is_tmpl = TREE_CODE (decl) == TEMPLATE_DECL;
   13018      2266831 :   if (is_tmpl)
   13019       506797 :     decl = DECL_TEMPLATE_RESULT (decl);
   13020              : 
   13021      2266831 :   switch (TREE_CODE (decl))
   13022              :     {
   13023              :     default:
   13024              :       break;
   13025              : 
   13026       853891 :     case FUNCTION_DECL:
   13027       853891 :       if (!DECL_SAVED_TREE (decl))
   13028              :         /* Not defined.  */
   13029              :         break;
   13030              : 
   13031       390711 :       if (DECL_DECLARED_INLINE_P (decl))
   13032              :         return true;
   13033              : 
   13034        31072 :       if (header_module_p ())
   13035              :         /* We always need to write definitions in header modules,
   13036              :            since there's no TU to emit them in otherwise.  */
   13037              :         return true;
   13038              : 
   13039        19691 :       if (DECL_TEMPLATE_INFO (decl))
   13040              :         {
   13041        18400 :           int use_tpl = DECL_USE_TEMPLATE (decl);
   13042              : 
   13043              :           // FIXME: Partial specializations have definitions too.
   13044        18400 :           if (use_tpl < 2)
   13045              :             return true;
   13046              :         }
   13047              : 
   13048              :       /* Coroutine transform functions always need to be emitted
   13049              :          into the importing TU if the ramp function will be.  */
   13050         1351 :       if (DECL_COROUTINE_P (decl))
   13051           12 :         if (tree ramp = DECL_RAMP_FN (decl))
   13052              :           return has_definition (ramp);
   13053              :       break;
   13054              : 
   13055       934251 :     case TYPE_DECL:
   13056       934251 :       {
   13057       934251 :         tree type = TREE_TYPE (decl);
   13058       934251 :         if (type == TYPE_MAIN_VARIANT (type)
   13059       427608 :             && decl == TYPE_NAME (type)
   13060      1361859 :             && (TREE_CODE (type) == ENUMERAL_TYPE
   13061       427608 :                 ? TYPE_VALUES (type) : TYPE_FIELDS (type)))
   13062       211827 :           return true;
   13063              :       }
   13064              :       break;
   13065              : 
   13066       118655 :     case VAR_DECL:
   13067              :       /* DECL_INITIALIZED_P might not be set on a dependent VAR_DECL.  */
   13068       118655 :       if (DECL_LANG_SPECIFIC (decl)
   13069       117745 :           && DECL_TEMPLATE_INFO (decl)
   13070       194692 :           && DECL_INITIAL (decl))
   13071              :         return true;
   13072              :       else
   13073              :         {
   13074        48181 :           if (!DECL_INITIALIZED_P (decl))
   13075              :             /* Not defined.  */
   13076              :             return false;
   13077              : 
   13078        40225 :           if (header_module_p ())
   13079              :             /* We always need to write definitions in header modules,
   13080              :                since there's no TU to emit them in otherwise.  */
   13081              :             return true;
   13082              : 
   13083        14749 :           if (decl_maybe_constant_var_p (decl))
   13084              :             /* We might need its constant value.  */
   13085              :             return true;
   13086              : 
   13087          503 :           if (vague_linkage_p (decl))
   13088              :             /* These are emitted as needed.  */
   13089              :             return true;
   13090              : 
   13091              :           return false;
   13092              :         }
   13093         8538 :       break;
   13094              : 
   13095         8538 :     case CONCEPT_DECL:
   13096         8538 :       if (DECL_INITIAL (decl))
   13097         8538 :         return true;
   13098              : 
   13099              :       break;
   13100              :     }
   13101              : 
   13102              :   return false;
   13103              : }
   13104              : 
   13105              : uintptr_t *
   13106       649002 : trees_in::find_duplicate (tree existing)
   13107              : {
   13108       302937 :   if (!duplicates)
   13109              :     return NULL;
   13110              : 
   13111       431587 :   return duplicates->get (existing);
   13112              : }
   13113              : 
   13114              : /* We're starting to read a duplicate DECL.  EXISTING is the already
   13115              :    known node.  */
   13116              : 
   13117              : void
   13118       617200 : trees_in::register_duplicate (tree decl, tree existing)
   13119              : {
   13120       617200 :   if (!duplicates)
   13121       102086 :     duplicates = new duplicate_hash_map (40);
   13122              : 
   13123       617200 :   bool existed;
   13124       617200 :   uintptr_t &slot = duplicates->get_or_insert (existing, &existed);
   13125       617200 :   gcc_checking_assert (!existed);
   13126       617200 :   slot = reinterpret_cast<uintptr_t> (decl);
   13127              : 
   13128       617200 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   13129              :     /* Also register the DECL_TEMPLATE_RESULT as a duplicate so
   13130              :        that passing decl's _RESULT to maybe_duplicate naturally
   13131              :        gives us existing's _RESULT back.  */
   13132       286370 :     register_duplicate (DECL_TEMPLATE_RESULT (decl),
   13133       143185 :                         DECL_TEMPLATE_RESULT (existing));
   13134       617200 : }
   13135              : 
   13136              : /* We've read a definition of MAYBE_EXISTING.  If not a duplicate,
   13137              :    return MAYBE_EXISTING (into which the definition should be
   13138              :    installed).  Otherwise return NULL if already known bad, or the
   13139              :    duplicate we read (for ODR checking, or extracting additional merge
   13140              :    information).  */
   13141              : 
   13142              : tree
   13143       346065 : trees_in::odr_duplicate (tree maybe_existing, bool has_defn)
   13144              : {
   13145       346065 :   tree res = NULL_TREE;
   13146              : 
   13147       546333 :   if (uintptr_t *dup = find_duplicate (maybe_existing))
   13148              :     {
   13149       145051 :       if (!(*dup & 1))
   13150       145048 :         res = reinterpret_cast<tree> (*dup);
   13151              :     }
   13152              :   else
   13153              :     res = maybe_existing;
   13154              : 
   13155       346065 :   assert_definition (maybe_existing, res && !has_defn);
   13156              : 
   13157              :   // FIXME: We probably need to return the template, so that the
   13158              :   // template header can be checked?
   13159       346065 :   return res ? STRIP_TEMPLATE (res) : NULL_TREE;
   13160              : }
   13161              : 
   13162              : /* The following writer functions rely on the current behaviour of
   13163              :    depset::hash::add_dependency making the decl and defn depset nodes
   13164              :    depend on each other.  That way we don't have to worry about seeding
   13165              :    the tree map with named decls that cannot be looked up by name (I.e
   13166              :    template and function parms).  We know the decl and definition will
   13167              :    be in the same cluster, which is what we want.  */
   13168              : 
   13169              : void
   13170       654235 : trees_out::write_function_def (tree decl)
   13171              : {
   13172       654235 :   tree_node (DECL_RESULT (decl));
   13173              : 
   13174       654235 :   {
   13175              :     /* The function body for a non-inline function or function template
   13176              :        is ignored for determining exposures.  This should only matter
   13177              :        for templates (we don't emit the bodies of non-inline functions
   13178              :        to begin with).  */
   13179       654235 :     auto ovr = dep_hash->ignore_exposure_if (!DECL_DECLARED_INLINE_P (decl));
   13180       654235 :     tree_node (DECL_INITIAL (decl));
   13181       654235 :     tree_node (DECL_SAVED_TREE (decl));
   13182       654235 :   }
   13183              : 
   13184      1298924 :   tree_node (DECL_FRIEND_CONTEXT (decl));
   13185              : 
   13186       654235 :   constexpr_fundef *cexpr = retrieve_constexpr_fundef (decl);
   13187              : 
   13188       654235 :   if (streaming_p ())
   13189       327076 :     u (cexpr != nullptr);
   13190       654235 :   if (cexpr)
   13191              :     {
   13192       149663 :       chained_decls (cexpr->parms);
   13193       149663 :       tree_node (cexpr->result);
   13194       149663 :       tree_node (cexpr->body);
   13195              :     }
   13196              : 
   13197       654235 :   function* f = DECL_STRUCT_FUNCTION (decl);
   13198              : 
   13199       654235 :   if (streaming_p ())
   13200              :     {
   13201       327076 :       unsigned flags = 0;
   13202              : 
   13203              :       /* Whether the importer should emit this definition, if used.  */
   13204       327076 :       flags |= 1 * (DECL_NOT_REALLY_EXTERN (decl)
   13205       327076 :                     && (get_importer_interface (decl)
   13206              :                         != importer_interface::external));
   13207              : 
   13208              :       /* Make sure DECL_REALLY_EXTERN and DECL_INTERFACE_KNOWN are consistent
   13209              :          on non-templates or we'll crash later in import_export_decl.  */
   13210       219457 :       gcc_checking_assert (flags || DECL_INTERFACE_KNOWN (decl)
   13211              :                            || (DECL_LANG_SPECIFIC (decl)
   13212              :                                && DECL_LOCAL_DECL_P (decl)
   13213              :                                && DECL_OMP_DECLARE_REDUCTION_P (decl))
   13214              :                            || (DECL_LANG_SPECIFIC (decl)
   13215              :                                && DECL_TEMPLATE_INFO (decl)
   13216              :                                && uses_template_parms (DECL_TI_ARGS (decl))));
   13217              : 
   13218       327076 :       if (f)
   13219              :         {
   13220       325737 :           flags |= 2;
   13221              :           /* These flags are needed in tsubst_lambda_expr.  */
   13222       325737 :           flags |= 4 * f->language->returns_value;
   13223       325737 :           flags |= 8 * f->language->returns_null;
   13224       325737 :           flags |= 16 * f->language->returns_abnormally;
   13225       325737 :           flags |= 32 * f->language->infinite_loop;
   13226              :         }
   13227              : 
   13228       327076 :       u (flags);
   13229              :     }
   13230              : 
   13231       654235 :   if (state && f)
   13232              :     {
   13233       651557 :       state->write_location (*this, f->function_start_locus);
   13234       651557 :       state->write_location (*this, f->function_end_locus);
   13235              :     }
   13236              : 
   13237       654235 :   if (DECL_COROUTINE_P (decl))
   13238              :     {
   13239           44 :       tree ramp = DECL_RAMP_FN (decl);
   13240           44 :       tree_node (ramp);
   13241           44 :       if (!ramp)
   13242              :         {
   13243           32 :           tree_node (DECL_ACTOR_FN (decl));
   13244           32 :           tree_node (DECL_DESTROY_FN (decl));
   13245              :         }
   13246              :     }
   13247       654235 : }
   13248              : 
   13249              : void
   13250            0 : trees_out::mark_function_def (tree)
   13251              : {
   13252            0 : }
   13253              : 
   13254              : bool
   13255       226693 : trees_in::read_function_def (tree decl, tree maybe_template)
   13256              : {
   13257       227243 :   dump () && dump ("Reading function definition %N", decl);
   13258       226693 :   tree result = tree_node ();
   13259       226693 :   tree initial = tree_node ();
   13260       226693 :   tree saved = tree_node ();
   13261       226693 :   tree context = tree_node ();
   13262       226693 :   post_process_data pdata {};
   13263       226693 :   pdata.decl = maybe_template;
   13264              : 
   13265       226693 :   tree maybe_dup = odr_duplicate (maybe_template, DECL_SAVED_TREE (decl));
   13266       453383 :   bool installing = maybe_dup && !DECL_SAVED_TREE (decl);
   13267              : 
   13268       226693 :   constexpr_fundef cexpr;
   13269       226693 :   if (u ())
   13270              :     {
   13271        52644 :       cexpr.parms = chained_decls ();
   13272        52644 :       cexpr.result = tree_node ();
   13273        52644 :       cexpr.body = tree_node ();
   13274        52644 :       cexpr.decl = decl;
   13275              :     }
   13276              :   else
   13277              :     cexpr.decl = NULL_TREE;
   13278              : 
   13279       226693 :   unsigned flags = u ();
   13280       226693 :   if (flags & 2)
   13281              :     {
   13282       225800 :       pdata.start_locus = state->read_location (*this);
   13283       225800 :       pdata.end_locus = state->read_location (*this);
   13284       225800 :       pdata.returns_value = flags & 4;
   13285       225800 :       pdata.returns_null = flags & 8;
   13286       225800 :       pdata.returns_abnormally = flags & 16;
   13287       225800 :       pdata.infinite_loop = flags & 32;
   13288              :     }
   13289              : 
   13290       226693 :   tree coro_actor = NULL_TREE;
   13291       226693 :   tree coro_destroy = NULL_TREE;
   13292       226693 :   tree coro_ramp = NULL_TREE;
   13293       226693 :   if (DECL_COROUTINE_P (decl))
   13294              :     {
   13295           17 :       coro_ramp = tree_node ();
   13296           17 :       if (!coro_ramp)
   13297              :         {
   13298           11 :           coro_actor = tree_node ();
   13299           11 :           coro_destroy = tree_node ();
   13300           11 :           if ((coro_actor == NULL_TREE) != (coro_destroy == NULL_TREE))
   13301            0 :             set_overrun ();
   13302              :         }
   13303              :     }
   13304              : 
   13305       226693 :   if (get_overrun ())
   13306              :     return NULL_TREE;
   13307              : 
   13308       226693 :   if (installing)
   13309              :     {
   13310       141830 :       DECL_NOT_REALLY_EXTERN (decl) = flags & 1;
   13311       141830 :       DECL_RESULT (decl) = result;
   13312       141830 :       DECL_INITIAL (decl) = initial;
   13313       141830 :       DECL_SAVED_TREE (decl) = saved;
   13314              : 
   13315              :       /* Some entities (like anticipated builtins) were declared without
   13316              :          DECL_ARGUMENTS, so update them now.  But don't do it if there's
   13317              :          already an argument list, because we've already built the
   13318              :          definition referencing those merged PARM_DECLs.  */
   13319       141830 :       if (!DECL_ARGUMENTS (decl))
   13320         7099 :         DECL_ARGUMENTS (decl) = DECL_ARGUMENTS (maybe_dup);
   13321              : 
   13322       141830 :       if (context)
   13323         5951 :         SET_DECL_FRIEND_CONTEXT (decl, context);
   13324       141830 :       if (cexpr.decl)
   13325        38682 :         register_constexpr_fundef (cexpr);
   13326              : 
   13327       141830 :       if (coro_ramp)
   13328            6 :         coro_set_ramp_function (decl, coro_ramp);
   13329       141824 :       else if (coro_actor && coro_destroy)
   13330            3 :         coro_set_transform_functions (decl, coro_actor, coro_destroy);
   13331              : 
   13332       141830 :       if (DECL_LOCAL_DECL_P (decl))
   13333              :         /* Block-scope OMP UDRs aren't real functions, and don't need a
   13334              :            function structure to be allocated or to be expanded.  */
   13335            3 :         gcc_checking_assert (DECL_OMP_DECLARE_REDUCTION_P (decl));
   13336              :       else
   13337       141827 :         post_process (pdata);
   13338              :     }
   13339              :   else if (maybe_dup)
   13340              :     {
   13341              :       // FIXME:QOI Check matching defn
   13342              :     }
   13343              : 
   13344              :   return true;
   13345              : }
   13346              : 
   13347              : /* Also for CONCEPT_DECLs.  */
   13348              : 
   13349              : void
   13350       151176 : trees_out::write_var_def (tree decl)
   13351              : {
   13352              :   /* The initializer of a non-inline variable or variable template is
   13353              :      ignored for determining exposures.  */
   13354       151176 :   auto ovr = dep_hash->ignore_exposure_if (VAR_P (decl)
   13355       151176 :                                            && !DECL_INLINE_VAR_P (decl));
   13356              : 
   13357       151176 :   tree init = DECL_INITIAL (decl);
   13358       151176 :   tree_node (init);
   13359       151176 :   if (!init)
   13360              :     {
   13361         1904 :       tree dyn_init = NULL_TREE;
   13362              : 
   13363              :       /* We only need to write initializers in header modules.  */
   13364         3088 :       if (header_module_p () && DECL_NONTRIVIALLY_INITIALIZED_P (decl))
   13365              :         {
   13366          450 :           dyn_init = value_member (decl,
   13367          450 :                                    CP_DECL_THREAD_LOCAL_P (decl)
   13368              :                                    ? tls_aggregates : static_aggregates);
   13369          450 :           gcc_checking_assert (dyn_init);
   13370              :           /* Mark it so write_inits knows this is needed.  */
   13371          450 :           TREE_LANG_FLAG_0 (dyn_init) = true;
   13372          450 :           dyn_init = TREE_PURPOSE (dyn_init);
   13373              :         }
   13374         1904 :       tree_node (dyn_init);
   13375              :     }
   13376       151176 : }
   13377              : 
   13378              : void
   13379            0 : trees_out::mark_var_def (tree)
   13380              : {
   13381            0 : }
   13382              : 
   13383              : bool
   13384        46488 : trees_in::read_var_def (tree decl, tree maybe_template)
   13385              : {
   13386              :   /* Do not mark the virtual table entries as used.  */
   13387        46488 :   bool vtable = VAR_P (decl) && DECL_VTABLE_OR_VTT_P (decl);
   13388        46488 :   unused += vtable;
   13389        46488 :   tree init = tree_node ();
   13390        46488 :   tree dyn_init = init ? NULL_TREE : tree_node ();
   13391        46488 :   unused -= vtable;
   13392              : 
   13393        46488 :   if (get_overrun ())
   13394              :     return false;
   13395              : 
   13396        46488 :   bool initialized = (VAR_P (decl) ? bool (DECL_INITIALIZED_P (decl))
   13397        46488 :                       : bool (DECL_INITIAL (decl)));
   13398        46488 :   tree maybe_dup = odr_duplicate (maybe_template, initialized);
   13399        46488 :   bool installing = maybe_dup && !initialized;
   13400        46488 :   if (installing)
   13401              :     {
   13402        29814 :       DECL_INITIAL (decl) = init;
   13403        29814 :       if (DECL_EXTERNAL (decl))
   13404         3656 :         DECL_NOT_REALLY_EXTERN (decl) = true;
   13405        29814 :       if (VAR_P (decl))
   13406              :         {
   13407        26162 :           DECL_INITIALIZED_P (decl) = true;
   13408        26162 :           if (maybe_dup && DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (maybe_dup))
   13409        25721 :             DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
   13410        26162 :           tentative_decl_linkage (decl);
   13411        26162 :           if (DECL_EXPLICIT_INSTANTIATION (decl)
   13412        26162 :               && !DECL_EXTERNAL (decl))
   13413            9 :             setup_explicit_instantiation_definition_linkage (decl);
   13414              :           /* Class non-template static members are handled in read_class_def.
   13415              :              But still handle specialisations of member templates.  */
   13416        52324 :           if ((!DECL_CLASS_SCOPE_P (decl)
   13417        16707 :                || primary_template_specialization_p (decl))
   13418        35757 :               && (DECL_IMPLICIT_INSTANTIATION (decl)
   13419         9515 :                   || (DECL_EXPLICIT_INSTANTIATION (decl)
   13420           21 :                       && !DECL_EXTERNAL (decl))))
   13421           89 :             note_vague_linkage_variable (decl);
   13422              :         }
   13423        29814 :       if (!dyn_init)
   13424              :         ;
   13425          216 :       else if (CP_DECL_THREAD_LOCAL_P (decl))
   13426           96 :         tls_aggregates = tree_cons (dyn_init, decl, tls_aggregates);
   13427              :       else
   13428          120 :         static_aggregates = tree_cons (dyn_init, decl, static_aggregates);
   13429              :     }
   13430              :   else if (maybe_dup)
   13431              :     {
   13432              :       // FIXME:QOI Check matching defn
   13433              :     }
   13434              : 
   13435              :   return true;
   13436              : }
   13437              : 
   13438              : /* If MEMBER doesn't have an independent life outside the class,
   13439              :    return it (or its TEMPLATE_DECL).  Otherwise NULL.  */
   13440              : 
   13441              : static tree
   13442       312396 : member_owned_by_class (tree member)
   13443              : {
   13444       312396 :   gcc_assert (DECL_P (member));
   13445              : 
   13446              :   /* Clones are owned by their origin.  */
   13447       312396 :   if (DECL_CLONED_FUNCTION_P (member))
   13448              :     return NULL;
   13449              : 
   13450       312396 :   if (TREE_CODE (member) == FIELD_DECL)
   13451              :     /* FIELD_DECLS can have template info in some cases.  We always
   13452              :        want the FIELD_DECL though, as there's never a TEMPLATE_DECL
   13453              :        wrapping them.  */
   13454              :     return member;
   13455              : 
   13456       134061 :   int use_tpl = -1;
   13457       134061 :   if (tree ti = node_template_info (member, use_tpl))
   13458              :     {
   13459              :       // FIXME: Don't bail on things that CANNOT have their own
   13460              :       // template header.  No, make sure they're in the same cluster.
   13461            0 :       if (use_tpl > 0)
   13462              :         return NULL_TREE;
   13463              : 
   13464            0 :       if (DECL_TEMPLATE_RESULT (TI_TEMPLATE (ti)) == member)
   13465       312396 :         member = TI_TEMPLATE (ti);
   13466              :     }
   13467              :   return member;
   13468              : }
   13469              : 
   13470              : void
   13471       220572 : trees_out::write_class_def (tree defn)
   13472              : {
   13473       220572 :   gcc_assert (DECL_P (defn));
   13474       220572 :   if (streaming_p ())
   13475       110683 :     dump () && dump ("Writing class definition %N", defn);
   13476              : 
   13477       220572 :   tree type = TREE_TYPE (defn);
   13478       220572 :   tree_node (TYPE_SIZE (type));
   13479       220572 :   tree_node (TYPE_SIZE_UNIT (type));
   13480       220572 :   tree_node (TYPE_VFIELD (type));
   13481       220572 :   tree_node (TYPE_BINFO (type));
   13482              : 
   13483       220572 :   vec_chained_decls (TYPE_FIELDS (type));
   13484              : 
   13485              :   /* Every class but __as_base has a type-specific.  */
   13486       437214 :   gcc_checking_assert (!TYPE_LANG_SPECIFIC (type) == IS_FAKE_BASE_TYPE (type));
   13487              : 
   13488       220572 :   if (TYPE_LANG_SPECIFIC (type))
   13489              :     {
   13490       216642 :       {
   13491       216642 :         vec<tree, va_gc> *v = CLASSTYPE_MEMBER_VEC (type);
   13492       216642 :         if (!v)
   13493              :           {
   13494        50505 :             gcc_checking_assert (!streaming_p ());
   13495              :             /* Force a class vector.  */
   13496        50505 :             v = set_class_bindings (type, -1);
   13497        50505 :             gcc_checking_assert (v);
   13498              :           }
   13499              : 
   13500       216642 :         unsigned len = v->length ();
   13501       216642 :         if (streaming_p ())
   13502       108298 :           u (len);
   13503      1854574 :         for (unsigned ix = 0; ix != len; ix++)
   13504              :           {
   13505      1637932 :             tree m = (*v)[ix];
   13506      1637932 :             if (TREE_CODE (m) == TYPE_DECL
   13507       472157 :                 && DECL_ARTIFICIAL (m)
   13508      1873455 :                 && TYPE_STUB_DECL (TREE_TYPE (m)) == m)
   13509              :               /* This is a using-decl for a type, or an anonymous
   13510              :                  struct (maybe with a typedef name).  Write the type.  */
   13511        17512 :               m = TREE_TYPE (m);
   13512      1637932 :             tree_node (m);
   13513              :           }
   13514              :       }
   13515       216642 :       tree_node (CLASSTYPE_LAMBDA_EXPR (type));
   13516              : 
   13517              :       /* TYPE_CONTAINS_VPTR_P looks at the vbase vector, which the
   13518              :          reader won't know at this point.  */
   13519       216642 :       int has_vptr = TYPE_CONTAINS_VPTR_P (type);
   13520              : 
   13521       216642 :       if (streaming_p ())
   13522              :         {
   13523       108298 :           unsigned nvbases = vec_safe_length (CLASSTYPE_VBASECLASSES (type));
   13524       108298 :           u (nvbases);
   13525       108298 :           i (has_vptr);
   13526              :         }
   13527              : 
   13528       216642 :       if (has_vptr)
   13529              :         {
   13530         7836 :           tree_vec (CLASSTYPE_PURE_VIRTUALS (type));
   13531         7836 :           tree_pair_vec (CLASSTYPE_VCALL_INDICES (type));
   13532         7836 :           tree_node (CLASSTYPE_KEY_METHOD (type));
   13533              :         }
   13534              :     }
   13535              : 
   13536       220572 :   if (TYPE_LANG_SPECIFIC (type))
   13537              :     {
   13538       216642 :       tree_node (CLASSTYPE_PRIMARY_BINFO (type));
   13539              : 
   13540       216642 :       tree as_base = CLASSTYPE_AS_BASE (type);
   13541       216642 :       if (as_base)
   13542       112643 :         as_base = TYPE_NAME (as_base);
   13543       216642 :       tree_node (as_base);
   13544              : 
   13545              :       /* Write the vtables.  */
   13546       216642 :       tree vtables = CLASSTYPE_VTABLES (type);
   13547       216642 :       vec_chained_decls (vtables);
   13548       442638 :       for (; vtables; vtables = TREE_CHAIN (vtables))
   13549         9354 :         write_definition (vtables);
   13550              : 
   13551       216642 :       {
   13552              :         /* Friend declarations in class definitions are ignored when
   13553              :            determining exposures.  */
   13554       216642 :         auto ovr = dep_hash->ignore_exposure_if (true);
   13555              : 
   13556              :         /* Write the friend classes.  */
   13557       216642 :         tree_list (CLASSTYPE_FRIEND_CLASSES (type), false);
   13558              : 
   13559              :         /* Write the friend functions.  */
   13560       216642 :         for (tree friends = DECL_FRIENDLIST (defn);
   13561       249600 :              friends; friends = TREE_CHAIN (friends))
   13562              :           {
   13563        32958 :             tree_node (FRIEND_NAME (friends));
   13564        32958 :             tree_list (FRIEND_DECLS (friends), false);
   13565              :           }
   13566              :         /* End of friend fns.  */
   13567       216642 :         tree_node (NULL_TREE);
   13568       216642 :       }
   13569              : 
   13570              :       /* Write the decl list.  We don't need to ignore exposures of friend
   13571              :          decls here as any such decls should already have been added and
   13572              :          ignored above.  */
   13573       216642 :       tree_list (CLASSTYPE_DECL_LIST (type), true);
   13574              : 
   13575       216642 :       if (TYPE_CONTAINS_VPTR_P (type))
   13576              :         {
   13577              :           /* Write the thunks.  */
   13578         7836 :           for (tree decls = TYPE_FIELDS (type);
   13579       223078 :                decls; decls = DECL_CHAIN (decls))
   13580       215242 :             if (TREE_CODE (decls) == FUNCTION_DECL
   13581       155108 :                 && DECL_VIRTUAL_P (decls)
   13582       255922 :                 && DECL_THUNKS (decls))
   13583              :               {
   13584         1464 :                 tree_node (decls);
   13585              :                 /* Thunks are always unique, so chaining is ok.  */
   13586         1464 :                 chained_decls (DECL_THUNKS (decls));
   13587              :               }
   13588         7836 :           tree_node (NULL_TREE);
   13589              :         }
   13590              :     }
   13591       220572 : }
   13592              : 
   13593              : void
   13594       312396 : trees_out::mark_class_member (tree member, bool do_defn)
   13595              : {
   13596       312396 :   gcc_assert (DECL_P (member));
   13597              : 
   13598       312396 :   member = member_owned_by_class (member);
   13599       312396 :   if (member)
   13600       624792 :     mark_declaration (member, do_defn && has_definition (member));
   13601       312396 : }
   13602              : 
   13603              : void
   13604       220600 : trees_out::mark_class_def (tree defn)
   13605              : {
   13606       220600 :   gcc_assert (DECL_P (defn));
   13607       220600 :   tree type = TREE_TYPE (defn);
   13608              :   /* Mark the class members that are not type-decls and cannot have
   13609              :      independent definitions.  */
   13610      2583630 :   for (tree member = TYPE_FIELDS (type); member; member = DECL_CHAIN (member))
   13611      2363030 :     if (TREE_CODE (member) == FIELD_DECL
   13612      2363030 :         || TREE_CODE (member) == USING_DECL
   13613              :         /* A cloned enum-decl from 'using enum unrelated;'   */
   13614      2363030 :         || (TREE_CODE (member) == CONST_DECL
   13615        18220 :             && DECL_CONTEXT (member) == type))
   13616              :       {
   13617       312396 :         mark_class_member (member);
   13618       312396 :         if (TREE_CODE (member) == FIELD_DECL)
   13619       178335 :           if (tree repr = DECL_BIT_FIELD_REPRESENTATIVE (member))
   13620              :             /* If we're marking a class template definition, then
   13621              :                this'll contain the width (as set by grokbitfield)
   13622              :                instead of a decl.  */
   13623         3284 :             if (DECL_P (repr))
   13624         2540 :               mark_declaration (repr, false);
   13625              :       }
   13626              : 
   13627              :   /* Mark the binfo hierarchy.  */
   13628       519328 :   for (tree child = TYPE_BINFO (type); child; child = TREE_CHAIN (child))
   13629       298728 :     mark_by_value (child);
   13630              : 
   13631       220600 :   if (TYPE_LANG_SPECIFIC (type))
   13632              :     {
   13633       216646 :       for (tree vtable = CLASSTYPE_VTABLES (type);
   13634       226000 :            vtable; vtable = TREE_CHAIN (vtable))
   13635         9354 :         mark_declaration (vtable, true);
   13636              : 
   13637       216646 :       if (TYPE_CONTAINS_VPTR_P (type))
   13638              :         /* Mark the thunks, they belong to the class definition,
   13639              :            /not/ the thunked-to function.  */
   13640         7836 :         for (tree decls = TYPE_FIELDS (type);
   13641       223078 :              decls; decls = DECL_CHAIN (decls))
   13642       215242 :           if (TREE_CODE (decls) == FUNCTION_DECL)
   13643       155108 :             for (tree thunks = DECL_THUNKS (decls);
   13644       157052 :                  thunks; thunks = DECL_CHAIN (thunks))
   13645         1944 :               mark_declaration (thunks, false);
   13646              :     }
   13647       220600 : }
   13648              : 
   13649              : /* Nop sorting, needed for resorting the member vec.  */
   13650              : 
   13651              : static void
   13652     11185906 : nop (void *, void *, void *)
   13653              : {
   13654     11185906 : }
   13655              : 
   13656              : bool
   13657        69683 : trees_in::read_class_def (tree defn, tree maybe_template)
   13658              : {
   13659        69683 :   gcc_assert (DECL_P (defn));
   13660        70338 :   dump () && dump ("Reading class definition %N", defn);
   13661        69683 :   tree type = TREE_TYPE (defn);
   13662        69683 :   tree size = tree_node ();
   13663        69683 :   tree size_unit = tree_node ();
   13664        69683 :   tree vfield = tree_node ();
   13665        69683 :   tree binfo = tree_node ();
   13666        69683 :   vec<tree, va_gc> *vbase_vec = NULL;
   13667        69683 :   vec<tree, va_gc> *member_vec = NULL;
   13668        69683 :   vec<tree, va_gc> *pure_virts = NULL;
   13669        69683 :   vec<tree_pair_s, va_gc> *vcall_indices = NULL;
   13670        69683 :   tree key_method = NULL_TREE;
   13671        69683 :   tree lambda = NULL_TREE;
   13672              : 
   13673              :   /* Read the fields.  */
   13674        69683 :   vec<tree, va_heap> *fields = vec_chained_decls ();
   13675              : 
   13676        69683 :   if (TYPE_LANG_SPECIFIC (type))
   13677              :     {
   13678        68311 :       if (unsigned len = u ())
   13679              :         {
   13680        68311 :           vec_alloc (member_vec, len);
   13681       675237 :           for (unsigned ix = 0; ix != len; ix++)
   13682              :             {
   13683       538615 :               tree m = tree_node ();
   13684       538615 :               if (get_overrun ())
   13685              :                 break;
   13686       538615 :               if (TYPE_P (m))
   13687         5539 :                 m = TYPE_STUB_DECL (m);
   13688       538615 :               member_vec->quick_push (m);
   13689              :             }
   13690              :         }
   13691        68311 :       lambda = tree_node ();
   13692              : 
   13693        68311 :       if (!get_overrun ())
   13694              :         {
   13695        68311 :           unsigned nvbases = u ();
   13696        68311 :           if (nvbases)
   13697              :             {
   13698          292 :               vec_alloc (vbase_vec, nvbases);
   13699         1342 :               for (tree child = binfo; child; child = TREE_CHAIN (child))
   13700         1050 :                 if (BINFO_VIRTUAL_P (child))
   13701          292 :                   vbase_vec->quick_push (child);
   13702              :             }
   13703              :         }
   13704              : 
   13705        68311 :       if (!get_overrun ())
   13706              :         {
   13707        68311 :           int has_vptr = i ();
   13708        68311 :           if (has_vptr)
   13709              :             {
   13710         2699 :               pure_virts = tree_vec ();
   13711         2699 :               vcall_indices = tree_pair_vec ();
   13712         2699 :               key_method = tree_node ();
   13713              :             }
   13714              :         }
   13715              :     }
   13716              : 
   13717        69683 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_SIZE (type));
   13718        69683 :   bool installing = maybe_dup && !TYPE_SIZE (type);
   13719        39935 :   if (installing)
   13720              :     {
   13721        39935 :       if (maybe_dup != defn)
   13722              :         {
   13723              :           // FIXME: This is needed on other defns too, almost
   13724              :           // duplicate-decl like?  See is_matching_decl too.
   13725              :           /* Copy flags from the duplicate.  */
   13726          227 :           tree type_dup = TREE_TYPE (maybe_dup);
   13727              : 
   13728              :           /* Core pieces.  */
   13729          227 :           TYPE_MODE_RAW (type) = TYPE_MODE_RAW (type_dup);
   13730          227 :           TYPE_ALIGN_RAW (type) = TYPE_ALIGN_RAW (type_dup);
   13731          454 :           TYPE_WARN_IF_NOT_ALIGN_RAW (type)
   13732          227 :             = TYPE_WARN_IF_NOT_ALIGN_RAW (type_dup);
   13733          227 :           TYPE_USER_ALIGN (type) = TYPE_USER_ALIGN (type_dup);
   13734              : 
   13735          227 :           SET_DECL_MODE (defn, DECL_MODE (maybe_dup));
   13736          227 :           DECL_SIZE (defn) = DECL_SIZE (maybe_dup);
   13737          227 :           DECL_SIZE_UNIT (defn) = DECL_SIZE_UNIT (maybe_dup);
   13738          227 :           DECL_ALIGN_RAW (defn) = DECL_ALIGN_RAW (maybe_dup);
   13739          454 :           DECL_WARN_IF_NOT_ALIGN_RAW (defn)
   13740          227 :             = DECL_WARN_IF_NOT_ALIGN_RAW (maybe_dup);
   13741          227 :           DECL_USER_ALIGN (defn) = DECL_USER_ALIGN (maybe_dup);
   13742              : 
   13743          227 :           TYPE_TYPELESS_STORAGE (type) = TYPE_TYPELESS_STORAGE (type_dup);
   13744          227 :           TYPE_CXX_ODR_P (type) = TYPE_CXX_ODR_P (type_dup);
   13745          227 :           TYPE_NO_FORCE_BLK (type) = TYPE_NO_FORCE_BLK (type_dup);
   13746          227 :           TYPE_TRANSPARENT_AGGR (type) = TYPE_TRANSPARENT_AGGR (type_dup);
   13747          454 :           TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type)
   13748          227 :             = TYPE_CONTAINS_PLACEHOLDER_INTERNAL (type_dup);
   13749              : 
   13750          227 :           TYPE_EMPTY_P (type) = TYPE_EMPTY_P (type_dup);
   13751          227 :           TREE_ADDRESSABLE (type) = TREE_ADDRESSABLE (type_dup);
   13752              : 
   13753              :           /* C++ pieces.  */
   13754          227 :           TYPE_POLYMORPHIC_P (type) = TYPE_POLYMORPHIC_P (type_dup);
   13755          227 :           CLASSTYPE_FINAL (type) = CLASSTYPE_FINAL (type_dup);
   13756              : 
   13757          454 :           TYPE_HAS_USER_CONSTRUCTOR (type)
   13758          227 :             = TYPE_HAS_USER_CONSTRUCTOR (type_dup);
   13759          454 :           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
   13760          227 :             = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type_dup);
   13761          454 :           TYPE_NEEDS_CONSTRUCTING (type)
   13762          227 :             = TYPE_NEEDS_CONSTRUCTING (type_dup);
   13763              : 
   13764          227 :           if (auto ls = TYPE_LANG_SPECIFIC (type_dup))
   13765              :             {
   13766          227 :               if (TYPE_LANG_SPECIFIC (type))
   13767              :                 {
   13768          681 :                   CLASSTYPE_BEFRIENDING_CLASSES (type_dup)
   13769          227 :                     = CLASSTYPE_BEFRIENDING_CLASSES (type);
   13770          454 :                   SET_CLASSTYPE_TYPEINFO_VAR (type_dup,
   13771              :                                               CLASSTYPE_TYPEINFO_VAR (type));
   13772              :                 }
   13773          886 :               for (tree v = type; v; v = TYPE_NEXT_VARIANT (v))
   13774          659 :                 TYPE_LANG_SPECIFIC (v) = ls;
   13775              :             }
   13776              :         }
   13777              : 
   13778        39935 :       TYPE_SIZE (type) = size;
   13779        39935 :       TYPE_SIZE_UNIT (type) = size_unit;
   13780              : 
   13781        39935 :       if (fields)
   13782              :         {
   13783        39935 :           tree *chain = &TYPE_FIELDS (type);
   13784        39935 :           unsigned len = fields->length ();
   13785       500472 :           for (unsigned ix = 0; ix != len; ix++)
   13786              :             {
   13787       460537 :               tree decl = (*fields)[ix];
   13788              : 
   13789       460537 :               if (!decl)
   13790              :                 {
   13791              :                   /* An anonymous struct with typedef name.  */
   13792            3 :                   tree tdef = (*fields)[ix+1];
   13793            3 :                   decl = TYPE_STUB_DECL (TREE_TYPE (tdef));
   13794            3 :                   gcc_checking_assert (IDENTIFIER_ANON_P (DECL_NAME (decl))
   13795              :                                        && decl != tdef);
   13796              :                 }
   13797              : 
   13798       832137 :               gcc_checking_assert (!*chain == !DECL_CLONED_FUNCTION_P (decl));
   13799       460537 :               *chain = decl;
   13800       460537 :               chain = &DECL_CHAIN (decl);
   13801              : 
   13802       460537 :               if (TREE_CODE (decl) == FIELD_DECL
   13803       460537 :                   && ANON_AGGR_TYPE_P (TREE_TYPE (decl)))
   13804              :                 {
   13805          271 :                   tree anon_type = TYPE_MAIN_VARIANT (TREE_TYPE (decl));
   13806          271 :                   if (DECL_NAME (defn) == as_base_identifier)
   13807              :                     /* ANON_AGGR_TYPE_FIELD should already point to the
   13808              :                        original FIELD_DECL; don't overwrite it to point
   13809              :                        to the as-base FIELD_DECL copy.  */
   13810           24 :                     gcc_checking_assert (ANON_AGGR_TYPE_FIELD (anon_type));
   13811              :                   else
   13812          259 :                     SET_ANON_AGGR_TYPE_FIELD (anon_type, decl);
   13813              :                 }
   13814              : 
   13815       460537 :               if (TREE_CODE (decl) == USING_DECL
   13816       460537 :                   && TREE_CODE (USING_DECL_SCOPE (decl)) == RECORD_TYPE)
   13817              :                 {
   13818              :                   /* Reconstruct DECL_ACCESS.  */
   13819        20090 :                   tree decls = USING_DECL_DECLS (decl);
   13820        20090 :                   tree access = declared_access (decl);
   13821              : 
   13822        23484 :                   for (ovl_iterator iter (decls); iter; ++iter)
   13823              :                     {
   13824         2183 :                       tree d = *iter;
   13825              : 
   13826         2183 :                       retrofit_lang_decl (d);
   13827         2183 :                       tree list = DECL_ACCESS (d);
   13828              : 
   13829         2183 :                       if (!purpose_member (type, list))
   13830         2609 :                         DECL_ACCESS (d) = tree_cons (type, access, list);
   13831              :                     }
   13832              :                 }
   13833              : 
   13834       460537 :               if (TREE_CODE (decl) == VAR_DECL
   13835        14081 :                   && TREE_CODE (maybe_template) != TEMPLATE_DECL)
   13836        12236 :                 note_vague_linkage_variable (decl);
   13837              :             }
   13838              :         }
   13839              : 
   13840        39935 :       TYPE_VFIELD (type) = vfield;
   13841        39935 :       TYPE_BINFO (type) = binfo;
   13842              : 
   13843        39935 :       if (TYPE_LANG_SPECIFIC (type))
   13844              :         {
   13845        39089 :           if (!TYPE_POLYMORPHIC_P (type))
   13846        37477 :             SET_CLASSTYPE_LAMBDA_EXPR (type, lambda);
   13847              :           else
   13848         1612 :             gcc_checking_assert (lambda == NULL_TREE);
   13849              : 
   13850        39089 :           CLASSTYPE_MEMBER_VEC (type) = member_vec;
   13851        39089 :           CLASSTYPE_PURE_VIRTUALS (type) = pure_virts;
   13852        39089 :           CLASSTYPE_VCALL_INDICES (type) = vcall_indices;
   13853              : 
   13854        39089 :           if (TYPE_POLYMORPHIC_P (type))
   13855         1612 :             SET_CLASSTYPE_KEY_METHOD (type, key_method);
   13856              :           else
   13857        37477 :             gcc_checking_assert (key_method == NULL_TREE);
   13858              : 
   13859        39089 :           CLASSTYPE_VBASECLASSES (type) = vbase_vec;
   13860              : 
   13861              :           /* Resort the member vector.  */
   13862        39089 :           resort_type_member_vec (member_vec, NULL, nop, NULL);
   13863              :         }
   13864              :     }
   13865              :   else if (maybe_dup)
   13866              :     {
   13867              :       // FIXME:QOI Check matching defn
   13868              :     }
   13869              : 
   13870        69683 :   if (TYPE_LANG_SPECIFIC (type))
   13871              :     {
   13872        68311 :       tree primary = tree_node ();
   13873        68311 :       tree as_base = tree_node ();
   13874              : 
   13875        68311 :       if (as_base)
   13876        34868 :         as_base = TREE_TYPE (as_base);
   13877              : 
   13878              :       /* Read the vtables.  */
   13879        68311 :       vec<tree, va_heap> *vtables = vec_chained_decls ();
   13880        68311 :       if (vtables)
   13881              :         {
   13882         2659 :           unsigned len = vtables->length ();
   13883         5820 :           for (unsigned ix = 0; ix != len; ix++)
   13884              :             {
   13885         3161 :               tree vtable = (*vtables)[ix];
   13886         3161 :               read_var_def (vtable, vtable);
   13887              :             }
   13888              :         }
   13889              : 
   13890        68311 :       tree friend_classes = tree_list (false);
   13891        68311 :       tree friend_functions = NULL_TREE;
   13892        68311 :       for (tree *chain = &friend_functions;
   13893        80310 :            tree name = tree_node (); chain = &TREE_CHAIN (*chain))
   13894              :         {
   13895        11999 :           tree val = tree_list (false);
   13896        11999 :           *chain = build_tree_list (name, val);
   13897        11999 :         }
   13898        68311 :       tree decl_list = tree_list (true);
   13899              : 
   13900        68311 :       if (installing)
   13901              :         {
   13902        39089 :           CLASSTYPE_PRIMARY_BINFO (type) = primary;
   13903        39089 :           CLASSTYPE_AS_BASE (type) = as_base;
   13904              : 
   13905        39089 :           if (vtables)
   13906              :             {
   13907         1624 :               if ((!CLASSTYPE_KEY_METHOD (type)
   13908              :                    /* Sneaky user may have defined it inline
   13909              :                       out-of-class.  */
   13910         1168 :                    || DECL_DECLARED_INLINE_P (CLASSTYPE_KEY_METHOD (type)))
   13911              :                   /* An imported non-template class attached to a module
   13912              :                      doesn't need to have its vtables emitted here.  */
   13913         1783 :                   && (CLASSTYPE_USE_TEMPLATE (type)
   13914          292 :                       || !DECL_MODULE_ATTACH_P (defn)))
   13915         1151 :                 vec_safe_push (keyed_classes, type);
   13916         1624 :               unsigned len = vtables->length ();
   13917         1624 :               tree *chain = &CLASSTYPE_VTABLES (type);
   13918         3567 :               for (unsigned ix = 0; ix != len; ix++)
   13919              :                 {
   13920         1943 :                   tree vtable = (*vtables)[ix];
   13921         1943 :                   gcc_checking_assert (!*chain);
   13922         1943 :                   *chain = vtable;
   13923         1943 :                   chain = &DECL_CHAIN (vtable);
   13924              :                 }
   13925              :             }
   13926        39089 :           CLASSTYPE_FRIEND_CLASSES (type) = friend_classes;
   13927        39089 :           DECL_FRIENDLIST (defn) = friend_functions;
   13928        39089 :           CLASSTYPE_DECL_LIST (type) = decl_list;
   13929              : 
   13930        41990 :           for (; friend_classes; friend_classes = TREE_CHAIN (friend_classes))
   13931              :             {
   13932         2901 :               tree f = TREE_VALUE (friend_classes);
   13933         2901 :               if (TREE_CODE (f) == TEMPLATE_DECL)
   13934         1204 :                 f = TREE_TYPE (f);
   13935              : 
   13936         2901 :               if (CLASS_TYPE_P (f))
   13937              :                 {
   13938         2865 :                   CLASSTYPE_BEFRIENDING_CLASSES (f)
   13939         5730 :                     = tree_cons (NULL_TREE, type,
   13940         2865 :                                  CLASSTYPE_BEFRIENDING_CLASSES (f));
   13941         2907 :                   dump () && dump ("Class %N befriending %C:%N",
   13942            6 :                                    type, TREE_CODE (f), f);
   13943              :                 }
   13944              :             }
   13945              : 
   13946        46253 :           for (; friend_functions;
   13947         7164 :                friend_functions = TREE_CHAIN (friend_functions))
   13948         7164 :             for (tree friend_decls = TREE_VALUE (friend_functions);
   13949        16222 :                  friend_decls; friend_decls = TREE_CHAIN (friend_decls))
   13950              :               {
   13951         9058 :                 tree f = TREE_VALUE (friend_decls);
   13952         9058 :                 if (TREE_CODE (f) == TU_LOCAL_ENTITY)
   13953           36 :                   continue;
   13954              : 
   13955         9022 :                 DECL_BEFRIENDING_CLASSES (f)
   13956         9022 :                   = tree_cons (NULL_TREE, type, DECL_BEFRIENDING_CLASSES (f));
   13957         9088 :                 dump () && dump ("Class %N befriending %C:%N",
   13958           30 :                                  type, TREE_CODE (f), f);
   13959              :               }
   13960              :         }
   13961              : 
   13962        68311 :       if (TYPE_CONTAINS_VPTR_P (type))
   13963              :         /* Read and install the thunks.  */
   13964         3155 :         while (tree vfunc = tree_node ())
   13965              :           {
   13966          456 :             tree thunks = chained_decls ();
   13967          456 :             if (installing)
   13968          288 :               SET_DECL_THUNKS (vfunc, thunks);
   13969              :           }
   13970              : 
   13971        68311 :       vec_free (vtables);
   13972              :     }
   13973              : 
   13974              :   /* Propagate to all variants.  */
   13975        69683 :   if (installing)
   13976        39935 :     fixup_type_variants (type, /*update_align=*/true);
   13977              : 
   13978              :   /* IS_FAKE_BASE_TYPE is inaccurate at this point, because if this is
   13979              :      the fake base, we've not hooked it into the containing class's
   13980              :      data structure yet.  Fortunately it has a unique name.  */
   13981        39935 :   if (installing
   13982        39935 :       && DECL_NAME (defn) != as_base_identifier
   13983        39089 :       && (!CLASSTYPE_TEMPLATE_INFO (type)
   13984        33128 :           || !uses_template_parms (TI_ARGS (CLASSTYPE_TEMPLATE_INFO (type)))))
   13985              :     /* Emit debug info.  It'd be nice to know if the interface TU
   13986              :        already emitted this.  */
   13987        21142 :     rest_of_type_compilation (type, !LOCAL_CLASS_P (type));
   13988              : 
   13989        69683 :   vec_free (fields);
   13990              : 
   13991        69683 :   return !get_overrun ();
   13992              : }
   13993              : 
   13994              : void
   13995        10006 : trees_out::write_enum_def (tree decl)
   13996              : {
   13997        10006 :   tree type = TREE_TYPE (decl);
   13998              : 
   13999        10006 :   tree_node (TYPE_VALUES (type));
   14000              :   /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   14001              :      ENUMERAL_TYPE.  */
   14002        10006 : }
   14003              : 
   14004              : void
   14005        10006 : trees_out::mark_enum_def (tree decl)
   14006              : {
   14007        10006 :   tree type = TREE_TYPE (decl);
   14008              : 
   14009        53016 :   for (tree values = TYPE_VALUES (type); values; values = TREE_CHAIN (values))
   14010              :     {
   14011        43010 :       tree cst = TREE_VALUE (values);
   14012        43010 :       mark_by_value (cst);
   14013              :       /* We must mark the init to avoid circularity in tt_enum_int.  */
   14014        43010 :       if (tree init = DECL_INITIAL (cst))
   14015        42510 :         if (TREE_CODE (init) == INTEGER_CST)
   14016        41746 :           mark_by_value (init);
   14017              :     }
   14018        10006 : }
   14019              : 
   14020              : bool
   14021         3201 : trees_in::read_enum_def (tree defn, tree maybe_template)
   14022              : {
   14023         3201 :   tree type = TREE_TYPE (defn);
   14024         3201 :   tree values = tree_node ();
   14025              : 
   14026         3201 :   if (get_overrun ())
   14027              :     return false;
   14028              : 
   14029         3201 :   tree maybe_dup = odr_duplicate (maybe_template, TYPE_VALUES (type));
   14030         6402 :   bool installing = maybe_dup && !TYPE_VALUES (type);
   14031              : 
   14032         3201 :   if (installing)
   14033              :     {
   14034         1671 :       TYPE_VALUES (type) = values;
   14035              :       /* Note that we stream TYPE_MIN/MAX_VALUE directly as part of the
   14036              :          ENUMERAL_TYPE.  */
   14037              : 
   14038         2749 :       rest_of_type_compilation (type, DECL_NAMESPACE_SCOPE_P (defn));
   14039              :     }
   14040         1530 :   else if (maybe_dup)
   14041              :     {
   14042         1530 :       tree known = TYPE_VALUES (type);
   14043         8465 :       for (; known && values;
   14044         6935 :            known = TREE_CHAIN (known), values = TREE_CHAIN (values))
   14045              :         {
   14046         6944 :           tree known_decl = TREE_VALUE (known);
   14047         6944 :           tree new_decl = TREE_VALUE (values);
   14048              : 
   14049         6944 :           if (DECL_NAME (known_decl) != DECL_NAME (new_decl))
   14050              :             break;
   14051              : 
   14052         6938 :           new_decl = maybe_duplicate (new_decl);
   14053              : 
   14054         6938 :           if (!cp_tree_equal (DECL_INITIAL (known_decl),
   14055         6938 :                               DECL_INITIAL (new_decl)))
   14056              :             break;
   14057              :         }
   14058              : 
   14059         1530 :       if (known || values)
   14060              :         {
   14061           12 :           auto_diagnostic_group d;
   14062           12 :           error_at (DECL_SOURCE_LOCATION (maybe_dup),
   14063              :                     "definition of %qD does not match", maybe_dup);
   14064           12 :           inform (DECL_SOURCE_LOCATION (defn),
   14065              :                   "existing definition %qD", defn);
   14066              : 
   14067           12 :           tree known_decl = NULL_TREE, new_decl = NULL_TREE;
   14068              : 
   14069           12 :           if (known)
   14070            9 :             known_decl = TREE_VALUE (known);
   14071           12 :           if (values)
   14072           12 :             new_decl = maybe_duplicate (TREE_VALUE (values));
   14073              : 
   14074           12 :           if (known_decl && new_decl)
   14075              :             {
   14076            9 :               inform (DECL_SOURCE_LOCATION (new_decl),
   14077              :                       "enumerator %qD does not match ...", new_decl);
   14078            9 :               inform (DECL_SOURCE_LOCATION (known_decl),
   14079              :                       "... this enumerator %qD", known_decl);
   14080              :             }
   14081            3 :           else if (known_decl || new_decl)
   14082              :             {
   14083            3 :               tree extra = known_decl ? known_decl : new_decl;
   14084            3 :               inform (DECL_SOURCE_LOCATION (extra),
   14085              :                       "additional enumerators beginning with %qD", extra);
   14086              :             }
   14087              :           else
   14088            0 :             inform (DECL_SOURCE_LOCATION (maybe_dup),
   14089              :                     "enumeration range differs");
   14090              : 
   14091              :           /* Mark it bad.  */
   14092           12 :           unmatched_duplicate (maybe_template);
   14093           12 :         }
   14094              :     }
   14095              : 
   14096              :   return true;
   14097              : }
   14098              : 
   14099              : /* Write out the body of DECL.  See above circularity note.  */
   14100              : 
   14101              : void
   14102      1035989 : trees_out::write_definition (tree decl, bool refs_tu_local)
   14103              : {
   14104      1035989 :   auto ovr = make_temp_override (writing_local_entities,
   14105      1035989 :                                  writing_local_entities || refs_tu_local);
   14106              : 
   14107      1035989 :   if (streaming_p ())
   14108              :     {
   14109       517904 :       assert_definition (decl);
   14110       517904 :       dump ()
   14111          952 :         && dump ("Writing definition %C:%N", TREE_CODE (decl), decl);
   14112              :     }
   14113              :   else
   14114       518085 :     dump (dumper::DEPEND)
   14115           96 :       && dump ("Depending definition %C:%N", TREE_CODE (decl), decl);
   14116              : 
   14117      1035989 :  again:
   14118      1624358 :   switch (TREE_CODE (decl))
   14119              :     {
   14120            0 :     default:
   14121            0 :       gcc_unreachable ();
   14122              : 
   14123       588369 :     case TEMPLATE_DECL:
   14124       588369 :       decl = DECL_TEMPLATE_RESULT (decl);
   14125       588369 :       goto again;
   14126              : 
   14127       654235 :     case FUNCTION_DECL:
   14128       654235 :       write_function_def (decl);
   14129       654235 :       break;
   14130              : 
   14131       230578 :     case TYPE_DECL:
   14132       230578 :       {
   14133       230578 :         tree type = TREE_TYPE (decl);
   14134       230578 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14135              :                     && TYPE_NAME (type) == decl);
   14136       230578 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14137        10006 :           write_enum_def (decl);
   14138              :         else
   14139       220572 :           write_class_def (decl);
   14140              :       }
   14141              :       break;
   14142              : 
   14143       151176 :     case VAR_DECL:
   14144       151176 :     case CONCEPT_DECL:
   14145       151176 :       write_var_def (decl);
   14146       151176 :       break;
   14147              :     }
   14148      1035989 : }
   14149              : 
   14150              : /* Mark a declaration for by-value walking.  If DO_DEFN is true, mark
   14151              :    its body too.  */
   14152              : 
   14153              : void
   14154      4353075 : trees_out::mark_declaration (tree decl, bool do_defn)
   14155              : {
   14156      4353075 :   mark_by_value (decl);
   14157              : 
   14158      4353075 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14159      1449723 :     decl = DECL_TEMPLATE_RESULT (decl);
   14160              : 
   14161      4353075 :   if (!do_defn)
   14162              :     return;
   14163              : 
   14164      1036011 :   switch (TREE_CODE (decl))
   14165              :     {
   14166            0 :     default:
   14167            0 :       gcc_unreachable ();
   14168              : 
   14169              :     case FUNCTION_DECL:
   14170              :       mark_function_def (decl);
   14171              :       break;
   14172              : 
   14173       230606 :     case TYPE_DECL:
   14174       230606 :       {
   14175       230606 :         tree type = TREE_TYPE (decl);
   14176       230606 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14177              :                     && TYPE_NAME (type) == decl);
   14178       230606 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14179        10006 :           mark_enum_def (decl);
   14180              :         else
   14181       220600 :           mark_class_def (decl);
   14182              :       }
   14183              :       break;
   14184              : 
   14185              :     case VAR_DECL:
   14186              :     case CONCEPT_DECL:
   14187              :       mark_var_def (decl);
   14188              :       break;
   14189              :     }
   14190              : }
   14191              : 
   14192              : /* Read in the body of DECL.  See above circularity note.  */
   14193              : 
   14194              : bool
   14195       342904 : trees_in::read_definition (tree decl)
   14196              : {
   14197       344244 :   dump () && dump ("Reading definition %C %N", TREE_CODE (decl), decl);
   14198              : 
   14199       342904 :   tree maybe_template = decl;
   14200              : 
   14201       545722 :  again:
   14202       545722 :   switch (TREE_CODE (decl))
   14203              :     {
   14204              :     default:
   14205              :       break;
   14206              : 
   14207       202818 :     case TEMPLATE_DECL:
   14208       202818 :       decl = DECL_TEMPLATE_RESULT (decl);
   14209       202818 :       goto again;
   14210              : 
   14211       226693 :     case FUNCTION_DECL:
   14212       226693 :       return read_function_def (decl, maybe_template);
   14213              : 
   14214        72884 :     case TYPE_DECL:
   14215        72884 :       {
   14216        72884 :         tree type = TREE_TYPE (decl);
   14217        72884 :         gcc_assert (TYPE_MAIN_VARIANT (type) == type
   14218              :                     && TYPE_NAME (type) == decl);
   14219        72884 :         if (TREE_CODE (type) == ENUMERAL_TYPE)
   14220         3201 :           return read_enum_def (decl, maybe_template);
   14221              :         else
   14222        69683 :           return read_class_def (decl, maybe_template);
   14223              :       }
   14224        43327 :       break;
   14225              : 
   14226        43327 :     case VAR_DECL:
   14227        43327 :     case CONCEPT_DECL:
   14228        43327 :       return read_var_def (decl, maybe_template);
   14229              :     }
   14230              : 
   14231              :   return false;
   14232              : }
   14233              : 
   14234              : /* Lookup an maybe insert a slot for depset for KEY.  */
   14235              : 
   14236              : depset **
   14237     21872361 : depset::hash::entity_slot (tree entity, bool insert)
   14238              : {
   14239     21872361 :   traits::compare_type key (entity, NULL);
   14240     33010691 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14241              :                                        insert ? INSERT : NO_INSERT);
   14242              : 
   14243     21872361 :   return slot;
   14244              : }
   14245              : 
   14246              : depset **
   14247       244233 : depset::hash::binding_slot (tree ctx, tree name, bool insert)
   14248              : {
   14249       244233 :   traits::compare_type key (ctx, name);
   14250       324395 :   depset **slot = find_slot_with_hash (key, traits::hash (key),
   14251              :                                        insert ? INSERT : NO_INSERT);
   14252              : 
   14253       244233 :   return slot;
   14254              : }
   14255              : 
   14256              : depset *
   14257     10631533 : depset::hash::find_dependency (tree decl)
   14258              : {
   14259     10631533 :   depset **slot = entity_slot (decl, false);
   14260              : 
   14261     10631533 :   return slot ? *slot : NULL;
   14262              : }
   14263              : 
   14264              : depset *
   14265        80162 : depset::hash::find_binding (tree ctx, tree name)
   14266              : {
   14267        80162 :   depset **slot = binding_slot (ctx, name, false);
   14268              : 
   14269        80162 :   return slot ? *slot : NULL;
   14270              : }
   14271              : 
   14272              : static bool is_tu_local_entity (tree decl, bool explain = false);
   14273              : static bool is_tu_local_value (tree decl, tree expr, bool explain = false);
   14274              : static bool has_tu_local_tmpl_arg (tree decl, tree args, bool explain);
   14275              : 
   14276              : /* Returns true if DECL is a TU-local entity, as defined by [basic.link].
   14277              :    If EXPLAIN is true, emit an informative note about why DECL is TU-local.  */
   14278              : 
   14279              : static bool
   14280      4986945 : is_tu_local_entity (tree decl, bool explain/*=false*/)
   14281              : {
   14282      5173785 :   gcc_checking_assert (DECL_P (decl));
   14283      5173785 :   location_t loc = DECL_SOURCE_LOCATION (decl);
   14284      5173785 :   tree type = TREE_TYPE (decl);
   14285              : 
   14286              :   /* Only types, functions, variables, and template (specialisations)
   14287              :      can be TU-local.  */
   14288      5173785 :   if (TREE_CODE (decl) != TYPE_DECL
   14289              :       && TREE_CODE (decl) != FUNCTION_DECL
   14290              :       && TREE_CODE (decl) != VAR_DECL
   14291              :       && TREE_CODE (decl) != TEMPLATE_DECL)
   14292              :     return false;
   14293              : 
   14294              :   /* An explicit type alias is not an entity; we don't want to stream
   14295              :      such aliases if they refer to TU-local entities, so propagate this
   14296              :      from the original type. The built-in declarations of 'int' and such
   14297              :      are never TU-local.  */
   14298      5169756 :   if (TREE_CODE (decl) == TYPE_DECL
   14299      1733926 :       && !DECL_SELF_REFERENCE_P (decl)
   14300      6830000 :       && !DECL_IMPLICIT_TYPEDEF_P (decl))
   14301              :     {
   14302       882970 :       tree orig = DECL_ORIGINAL_TYPE (decl);
   14303       882970 :       if (orig && TYPE_NAME (orig))
   14304              :         {
   14305       186840 :           if (explain)
   14306           11 :             inform (loc, "%qD is an alias of TU-local type %qT", decl, orig);
   14307       186840 :           return is_tu_local_entity (TYPE_NAME (orig), explain);
   14308              :         }
   14309              :       else
   14310              :         return false;
   14311              :     }
   14312              : 
   14313              :   /* Check specializations first for slightly better explanations.  */
   14314      4286786 :   int use_tpl = -1;
   14315      4286786 :   tree ti = node_template_info (decl, use_tpl);
   14316      5239699 :   if (use_tpl > 0 && TREE_CODE (TI_TEMPLATE (ti)) == TEMPLATE_DECL)
   14317              :     {
   14318              :       /* A specialization of a TU-local template.  */
   14319       952730 :       tree tmpl = TI_TEMPLATE (ti);
   14320       952730 :       if (is_tu_local_entity (tmpl))
   14321              :         {
   14322           72 :           if (explain)
   14323              :             {
   14324           18 :               inform (loc, "%qD is a specialization of TU-local template %qD",
   14325              :                       decl, tmpl);
   14326           18 :               is_tu_local_entity (tmpl, /*explain=*/true);
   14327              :             }
   14328              :           return true;
   14329              :         }
   14330              : 
   14331              :       /* A specialization of a template with any TU-local template argument.  */
   14332       952658 :       if (has_tu_local_tmpl_arg (decl, TI_ARGS (ti), explain))
   14333              :         return true;
   14334              : 
   14335              :       /* FIXME A specialization of a template whose (possibly instantiated)
   14336              :          declaration is an exposure.  This should always be covered by the
   14337              :          above cases??  */
   14338              :     }
   14339              : 
   14340              :   /* A type, function, variable, or template with internal linkage.  */
   14341      4286682 :   linkage_kind kind = decl_linkage (decl);
   14342      4286682 :   if (kind == lk_internal
   14343              :       /* But although weakrefs are marked static, don't consider them
   14344              :          to be TU-local.  */
   14345      4286682 :       && !lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
   14346              :     {
   14347          853 :       if (explain)
   14348          168 :         inform (loc, "%qD declared with internal linkage", decl);
   14349              :       return true;
   14350              :     }
   14351              : 
   14352              :   /* Does not have a name with linkage and is declared, or introduced by a
   14353              :      lambda-expression, within the definition of a TU-local entity.  */
   14354      4285829 :   if (kind == lk_none)
   14355              :     {
   14356       440275 :       tree ctx = CP_DECL_CONTEXT (decl);
   14357       561061 :       if (LAMBDA_TYPE_P (type))
   14358        86980 :         if (tree extra = LAMBDA_TYPE_EXTRA_SCOPE (type))
   14359       440275 :           ctx = extra;
   14360              : 
   14361       440275 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   14362              :         {
   14363           31 :           if (!TREE_PUBLIC (ctx))
   14364              :             {
   14365            0 :               if (explain)
   14366            0 :                 inform (loc, "%qD has no linkage and is declared in an "
   14367              :                         "anonymous namespace", decl);
   14368              :               return true;
   14369              :             }
   14370              :         }
   14371       440244 :       else if (TYPE_P (ctx))
   14372              :         {
   14373        49825 :           tree ctx_decl = TYPE_MAIN_DECL (ctx);
   14374        49825 :           if (is_tu_local_entity (ctx_decl))
   14375              :             {
   14376            6 :               if (explain)
   14377              :                 {
   14378            0 :                   inform (loc, "%qD has no linkage and is declared within "
   14379              :                           "TU-local entity %qT", decl, ctx);
   14380            0 :                   is_tu_local_entity (ctx_decl, /*explain=*/true);
   14381              :                 }
   14382              :               return true;
   14383              :             }
   14384              :         }
   14385       390419 :       else if (is_tu_local_entity (ctx))
   14386              :         {
   14387           33 :           if (explain)
   14388              :             {
   14389            6 :               inform (loc, "%qD has no linkage and is declared within "
   14390              :                       "TU-local entity %qD", decl, ctx);
   14391            6 :               is_tu_local_entity (ctx, /*explain=*/true);
   14392              :             }
   14393              :           return true;
   14394              :         }
   14395              :     }
   14396              : 
   14397              :   /* A type with no name that is defined outside a class-specifier, function
   14398              :      body, or initializer; or is introduced by a defining-type-specifier that
   14399              :      is used to declare only TU-local entities.
   14400              : 
   14401              :      We consider types with names for linkage purposes as having names, since
   14402              :      these aren't really TU-local.  */
   14403      4285790 :   tree inner = STRIP_TEMPLATE (decl);
   14404      1814716 :   if (inner
   14405      4285790 :       && TREE_CODE (inner) == TYPE_DECL
   14406      3235659 :       && TYPE_ANON_P (type)
   14407        47348 :       && !DECL_SELF_REFERENCE_P (inner)
   14408              :       /* An enum with an enumerator name for linkage.  */
   14409      1857095 :       && !(UNSCOPED_ENUM_P (type) && TYPE_VALUES (type)))
   14410              :     {
   14411        40091 :       tree main_decl = TYPE_MAIN_DECL (type);
   14412        79638 :       if (LAMBDA_TYPE_P (type))
   14413              :         {
   14414              :           /* A lambda expression is, in practice, TU-local iff it has no
   14415              :              mangling scope.  This currently doesn't line up exactly with
   14416              :              the standard's definition due to some ABI issues, but it's
   14417              :              pretty close, and avoids other issues down the line.  */
   14418        78944 :           if (!LAMBDA_TYPE_EXTRA_SCOPE (type))
   14419              :             {
   14420            4 :               if (explain)
   14421            2 :                 inform (loc, "%qT has no name and cannot be differentiated "
   14422              :                         "from similar lambdas in other TUs", type);
   14423              :               return true;
   14424              :             }
   14425              :         }
   14426         1238 :       else if (!DECL_CLASS_SCOPE_P (main_decl)
   14427          649 :                && !decl_function_context (main_decl))
   14428              :         {
   14429           30 :           if (explain)
   14430           12 :             inform (loc, "%qT has no name and is not defined within a class, "
   14431              :                     "function, or initializer", type);
   14432              :           return true;
   14433              :         }
   14434              : 
   14435              :       // FIXME introduced by a defining-type-specifier only declaring TU-local
   14436              :       // entities; does this refer to e.g. 'static struct {} a;"?  I can't
   14437              :       // think of any cases where this isn't covered by earlier cases.  */
   14438              :     }
   14439              : 
   14440              :   return false;
   14441              : }
   14442              : 
   14443              : /* Helper for is_tu_local_entity.  Returns true if one of the ARGS of
   14444              :    DECL is TU-local.  Emits an explanation if EXPLAIN is true.  */
   14445              : 
   14446              : static bool
   14447      1082083 : has_tu_local_tmpl_arg (tree decl, tree args, bool explain)
   14448              : {
   14449      1082083 :   if (!args || TREE_CODE (args) != TREE_VEC)
   14450              :     return false;
   14451              : 
   14452      2929246 :   for (tree a : tree_vec_range (args))
   14453              :     {
   14454      1847195 :       if (TREE_CODE (a) == TREE_VEC)
   14455              :         {
   14456       129425 :           if (has_tu_local_tmpl_arg (decl, a, explain))
   14457           32 :             return true;
   14458              :         }
   14459              :       else if (!WILDCARD_TYPE_P (a))
   14460              :         {
   14461      1563877 :           if (DECL_P (a) && is_tu_local_entity (a))
   14462              :             {
   14463            0 :               if (explain)
   14464              :                 {
   14465            0 :                   inform (DECL_SOURCE_LOCATION (decl),
   14466              :                           "%qD has TU-local template argument %qD",
   14467              :                           decl, a);
   14468            0 :                   is_tu_local_entity (a, /*explain=*/true);
   14469              :                 }
   14470              :               return true;
   14471              :             }
   14472              : 
   14473      1563877 :           if (TYPE_P (a) && TYPE_NAME (a) && is_tu_local_entity (TYPE_NAME (a)))
   14474              :             {
   14475           17 :               if (explain)
   14476              :                 {
   14477            1 :                   inform (DECL_SOURCE_LOCATION (decl),
   14478              :                           "%qD has TU-local template argument %qT",
   14479              :                           decl, a);
   14480            1 :                   is_tu_local_entity (TYPE_NAME (a), /*explain=*/true);
   14481              :                 }
   14482              :               return true;
   14483              :             }
   14484              : 
   14485      1563860 :           if (EXPR_P (a) && is_tu_local_value (decl, a, explain))
   14486              :             return true;
   14487              :         }
   14488              :     }
   14489              : 
   14490      1082051 :   return false;
   14491              : }
   14492              : 
   14493              : /* Returns true if EXPR (part of the initializer for DECL) is a TU-local value
   14494              :    or object.  Emits an explanation if EXPLAIN is true.  */
   14495              : 
   14496              : static bool
   14497       124440 : is_tu_local_value (tree decl, tree expr, bool explain/*=false*/)
   14498              : {
   14499       124440 :   if (!expr)
   14500              :     return false;
   14501              : 
   14502       122344 :   tree e = expr;
   14503       122344 :   STRIP_ANY_LOCATION_WRAPPER (e);
   14504       122344 :   STRIP_NOPS (e);
   14505       122344 :   if (TREE_CODE (e) == TARGET_EXPR)
   14506            0 :     e = TARGET_EXPR_INITIAL (e);
   14507            0 :   if (!e)
   14508              :     return false;
   14509              : 
   14510              :   /* It is, or is a pointer to, a TU-local function or the object associated
   14511              :      with a TU-local variable.  */
   14512       122344 :   tree object = NULL_TREE;
   14513       122344 :   if (TREE_CODE (e) == ADDR_EXPR)
   14514         4586 :     object = TREE_OPERAND (e, 0);
   14515       117758 :   else if (TREE_CODE (e) == PTRMEM_CST)
   14516            0 :     object = PTRMEM_CST_MEMBER (e);
   14517       117758 :   else if (VAR_OR_FUNCTION_DECL_P (e))
   14518              :     object = e;
   14519              : 
   14520         4586 :   if (object
   14521         5358 :       && VAR_OR_FUNCTION_DECL_P (object)
   14522         5445 :       && is_tu_local_entity (object))
   14523              :     {
   14524           54 :       if (explain)
   14525              :         {
   14526              :           /* We've lost a lot of location information by the time we get here,
   14527              :              so let's just do our best effort.  */
   14528           18 :           auto loc = cp_expr_loc_or_loc (expr, DECL_SOURCE_LOCATION (decl));
   14529           18 :           if (VAR_P (object))
   14530            9 :             inform (loc, "%qD refers to TU-local object %qD", decl, object);
   14531              :           else
   14532            9 :             inform (loc, "%qD refers to TU-local function %qD", decl, object);
   14533           18 :           is_tu_local_entity (object, true);
   14534              :         }
   14535              :       return true;
   14536              :     }
   14537              : 
   14538              :   /* It is an object of class or array type and any of its subobjects or
   14539              :      any of the objects or functions to which its non-static data members
   14540              :      of reference type refer is TU-local and is usable in constant
   14541              :      expressions.  */
   14542       122290 :   if (TREE_CODE (e) == CONSTRUCTOR && AGGREGATE_TYPE_P (TREE_TYPE (e)))
   14543        70796 :     for (auto &f : CONSTRUCTOR_ELTS (e))
   14544        54438 :       if (is_tu_local_value (decl, f.value, explain))
   14545              :         return true;
   14546              : 
   14547              :   return false;
   14548              : }
   14549              : 
   14550              : /* Complains if DECL is a TU-local entity imported from a named module.
   14551              :    Returns TRUE if instantiation should fail.  */
   14552              : 
   14553              : bool
   14554   9153120000 : instantiating_tu_local_entity (tree decl)
   14555              : {
   14556   9153120000 :   if (!modules_p ())
   14557              :     return false;
   14558              : 
   14559     36450869 :   if (TREE_CODE (decl) == TU_LOCAL_ENTITY)
   14560              :     {
   14561           92 :       auto_diagnostic_group d;
   14562           92 :       error ("instantiation exposes TU-local entity %qD",
   14563           92 :              TU_LOCAL_ENTITY_NAME (decl));
   14564           92 :       inform (TU_LOCAL_ENTITY_LOCATION (decl), "declared here");
   14565           92 :       return true;
   14566           92 :     }
   14567              : 
   14568              :   /* Currently, only TU-local variables and functions, or possibly
   14569              :      templates thereof, will be emitted from named modules.  */
   14570     36450777 :   tree inner = STRIP_TEMPLATE (decl);
   14571     36450777 :   if (!VAR_OR_FUNCTION_DECL_P (inner))
   14572              :     return false;
   14573              : 
   14574              :   /* From this point we will only be emitting warnings; if we're not
   14575              :      warning about this case then there's no need to check further.  */
   14576      1521817 :   if (!warn_expose_global_module_tu_local
   14577      3043634 :       || !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   14578      1521817 :                               OPT_Wexpose_global_module_tu_local))
   14579              :     return false;
   14580              : 
   14581      1509833 :   if (!is_tu_local_entity (decl))
   14582              :     return false;
   14583              : 
   14584           65 :   if (!DECL_LANG_SPECIFIC (inner)
   14585          120 :       || !DECL_MODULE_IMPORT_P (inner))
   14586              :     return false;
   14587              : 
   14588              :   /* Referencing TU-local entities from a header is generally OK.
   14589              :      We don't have an easy way to detect if this declaration came
   14590              :      from a header via a separate named module, but we can just
   14591              :      ignore that case for warning purposes.  */
   14592            9 :   unsigned index = import_entity_index (decl);
   14593            9 :   module_state *mod = import_entity_module (index);
   14594            9 :   if (mod->is_header ())
   14595              :     return false;
   14596              : 
   14597            9 :   auto_diagnostic_group d;
   14598            9 :   pedwarn (input_location, OPT_Wexpose_global_module_tu_local,
   14599              :            "instantiation exposes TU-local entity %qD", decl);
   14600            9 :   inform (DECL_SOURCE_LOCATION (decl), "declared here");
   14601              : 
   14602              :   /* We treat TU-local entities from the GMF as not actually being
   14603              :      TU-local as an extension, so allow instantiation to proceed.  */
   14604            9 :   return false;
   14605            9 : }
   14606              : 
   14607              : /* DECL is a newly discovered dependency.  Create the depset, if it
   14608              :    doesn't already exist.  Add it to the worklist if so.
   14609              : 
   14610              :    DECL will be an OVL_USING_P OVERLOAD, if it's from a binding that's
   14611              :    a using decl.
   14612              : 
   14613              :    We do not have to worry about adding the same dependency more than
   14614              :    once.  First it's harmless, but secondly the TREE_VISITED marking
   14615              :    prevents us wanting to do it anyway.  */
   14616              : 
   14617              : depset *
   14618      9403489 : depset::hash::make_dependency (tree decl, entity_kind ek)
   14619              : {
   14620              :   /* Make sure we're being told consistent information.  */
   14621     17613022 :   gcc_checking_assert ((ek == EK_NAMESPACE)
   14622              :                        == (TREE_CODE (decl) == NAMESPACE_DECL
   14623              :                            && !DECL_NAMESPACE_ALIAS (decl)));
   14624      9403489 :   gcc_checking_assert (ek != EK_BINDING && ek != EK_REDIRECT);
   14625      9403489 :   gcc_checking_assert (TREE_CODE (decl) != FIELD_DECL
   14626              :                        && (TREE_CODE (decl) != USING_DECL
   14627              :                            || TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL));
   14628      9403489 :   gcc_checking_assert (!is_key_order ());
   14629      9403489 :   if (ek == EK_USING)
   14630        41586 :     gcc_checking_assert (TREE_CODE (decl) == OVERLOAD);
   14631      9403489 :   if (ek == EK_TU_LOCAL)
   14632           93 :     gcc_checking_assert (DECL_DECLARES_FUNCTION_P (decl));
   14633              : 
   14634      9403489 :   if (TREE_CODE (decl) == TEMPLATE_DECL)
   14635              :     /* The template should have copied these from its result decl.  */
   14636      3679130 :     gcc_checking_assert (DECL_MODULE_EXPORT_P (decl)
   14637              :                          == DECL_MODULE_EXPORT_P (DECL_TEMPLATE_RESULT (decl)));
   14638              : 
   14639      9403489 :   depset **slot = entity_slot (decl, true);
   14640      9403489 :   depset *dep = *slot;
   14641      9403489 :   bool for_binding = ek == EK_FOR_BINDING;
   14642              : 
   14643      9403489 :   if (!dep)
   14644              :     {
   14645       778125 :       if ((DECL_IMPLICIT_TYPEDEF_P (decl)
   14646              :            /* ... not an enum, for instance.  */
   14647       385389 :            && RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl))
   14648       380120 :            && TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
   14649       347001 :            && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (decl)) == 2)
   14650      2687161 :           || (VAR_P (decl)
   14651       103888 :               && DECL_LANG_SPECIFIC (decl)
   14652       103828 :               && DECL_USE_TEMPLATE (decl) == 2))
   14653              :         {
   14654              :           /* A partial or explicit specialization. Partial
   14655              :              specializations might not be in the hash table, because
   14656              :              there can be multiple differently-constrained variants.
   14657              : 
   14658              :              template<typename T> class silly;
   14659              :              template<typename T> requires true class silly {};
   14660              : 
   14661              :              We need to find them, insert their TEMPLATE_DECL in the
   14662              :              dep_hash, and then convert the dep we just found into a
   14663              :              redirect.  */
   14664              : 
   14665        51120 :           tree ti = get_template_info (decl);
   14666        51120 :           tree tmpl = TI_TEMPLATE (ti);
   14667        51120 :           tree partial = NULL_TREE;
   14668        51120 :           for (tree spec = DECL_TEMPLATE_SPECIALIZATIONS (tmpl);
   14669       188863 :                spec; spec = TREE_CHAIN (spec))
   14670       165295 :             if (DECL_TEMPLATE_RESULT (TREE_VALUE (spec)) == decl)
   14671              :               {
   14672              :                 partial = TREE_VALUE (spec);
   14673              :                 break;
   14674              :               }
   14675              : 
   14676        51120 :           if (partial)
   14677              :             {
   14678              :               /* Eagerly create an empty redirect.  The following
   14679              :                  make_dependency call could cause hash reallocation,
   14680              :                  and invalidate slot's value.  */
   14681        27552 :               depset *redirect = make_entity (decl, EK_REDIRECT);
   14682              : 
   14683              :               /* Redirects are never reached -- always snap to their target.  */
   14684        27552 :               redirect->set_flag_bit<DB_UNREACHED_BIT> ();
   14685              : 
   14686        27552 :               *slot = redirect;
   14687              : 
   14688        27552 :               depset *tmpl_dep = make_dependency (partial, EK_PARTIAL);
   14689        27552 :               gcc_checking_assert (tmpl_dep->get_entity_kind () == EK_PARTIAL);
   14690              : 
   14691        27552 :               redirect->deps.safe_push (tmpl_dep);
   14692              : 
   14693        27552 :               return redirect;
   14694              :             }
   14695              :         }
   14696              : 
   14697      1923424 :       bool has_def = ek != EK_USING && has_definition (decl);
   14698      1881838 :       if (ek > EK_BINDING)
   14699       157792 :         ek = EK_DECL;
   14700              : 
   14701              :       /* The only OVERLOADS we should see are USING decls from
   14702              :          bindings.  */
   14703      1923424 :       *slot = dep = make_entity (decl, ek, has_def);
   14704              : 
   14705      1923424 :       if (CHECKING_P && TREE_CODE (decl) == TEMPLATE_DECL)
   14706              :         /* The template_result should otherwise not be in the
   14707              :            table, or be an empty redirect (created above).  */
   14708       506797 :         if (auto *eslot = entity_slot (DECL_TEMPLATE_RESULT (decl), false))
   14709        27552 :           gcc_checking_assert ((*eslot)->get_entity_kind () == EK_REDIRECT
   14710              :                                && !(*eslot)->deps.length ());
   14711              : 
   14712      1923424 :       if (ignore_exposure)
   14713        54365 :         dep->set_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14714              : 
   14715      1923424 :       if (ek != EK_USING)
   14716              :         {
   14717      1881838 :           tree not_tmpl = STRIP_TEMPLATE (decl);
   14718      1881838 :           bool imported_from_module_p = false;
   14719              : 
   14720      1881838 :           if (DECL_LANG_SPECIFIC (not_tmpl)
   14721      3577882 :               && DECL_MODULE_IMPORT_P (not_tmpl))
   14722              :             {
   14723              :               /* Store the module number and index in cluster/section,
   14724              :                  so we don't have to look them up again.  */
   14725        67309 :               unsigned index = import_entity_index (decl);
   14726        67309 :               module_state *from = import_entity_module (index);
   14727              :               /* Remap will be zero for imports from partitions, which
   14728              :                  we want to treat as-if declared in this TU.  */
   14729        67309 :               if (from->remap)
   14730              :                 {
   14731        66552 :                   dep->cluster = index - from->entity_lwm;
   14732        66552 :                   dep->section = from->remap;
   14733        66552 :                   dep->set_flag_bit<DB_IMPORTED_BIT> ();
   14734              : 
   14735        66552 :                   if (!from->is_header ())
   14736      1881838 :                     imported_from_module_p = true;
   14737              :                 }
   14738              :             }
   14739              : 
   14740              :           /* Check for TU-local entities.  This is unnecessary in header
   14741              :              units because we can export internal-linkage decls, and
   14742              :              no declarations are exposures.  Similarly, if the decl was
   14743              :              imported from a non-header module we know it cannot have
   14744              :              been TU-local.  */
   14745      1881838 :           if (!header_module_p () && !imported_from_module_p)
   14746              :             {
   14747       917091 :               if (is_tu_local_entity (decl))
   14748          309 :                 dep->set_flag_bit<DB_TU_LOCAL_BIT> ();
   14749              : 
   14750       917091 :               if (VAR_P (decl)
   14751        48514 :                   && decl_maybe_constant_var_p (decl)
   14752       964256 :                   && is_tu_local_value (decl, DECL_INITIAL (decl)))
   14753              :                 {
   14754              :                   /* A potentially-constant variable initialized to a TU-local
   14755              :                      value is not usable in constant expressions within other
   14756              :                      translation units.  We can achieve this by simply not
   14757              :                      streaming the definition in such cases.  */
   14758           24 :                   dep->clear_flag_bit<DB_DEFN_BIT> ();
   14759              : 
   14760           24 :                   if (DECL_DECLARED_CONSTEXPR_P (decl)
   14761           39 :                       || DECL_INLINE_VAR_P (decl))
   14762              :                     /* A constexpr variable initialized to a TU-local value,
   14763              :                        or an inline value (PR c++/119996), is an exposure.
   14764              : 
   14765              :                        For simplicity, we don't support "non-strict" TU-local
   14766              :                        values: even if the TU-local entity we refer to in the
   14767              :                        initialiser is in the GMF, we still won't consider this
   14768              :                        valid in constant expressions in other TUs, and so
   14769              :                        complain accordingly.  */
   14770           15 :                     dep->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14771              :                 }
   14772              :             }
   14773              : 
   14774              :           /* A namespace-scope type may be declared in one module unit
   14775              :              and defined in another; make sure that we're found when
   14776              :              completing the class.  */
   14777      1881838 :           if (ek == EK_DECL
   14778       751166 :               && !dep->is_import ()
   14779       747005 :               && dep->has_defn ()
   14780       398054 :               && DECL_NAMESPACE_SCOPE_P (not_tmpl)
   14781       143734 :               && DECL_IMPLICIT_TYPEDEF_P (not_tmpl)
   14782              :               /* Anonymous types can't be forward-declared.  */
   14783      1919228 :               && !IDENTIFIER_ANON_P (DECL_NAME (not_tmpl)))
   14784        36870 :             dep->set_flag_bit<DB_IS_PENDING_BIT> ();
   14785              : 
   14786              :           /* Namespace-scope functions can be found by ADL by template
   14787              :              instantiations in this module.  We need to create bindings
   14788              :              for them so that name lookup recognises they exist, if they
   14789              :              won't be discarded.  add_binding_entity is too early to do
   14790              :              this for GM functions, because if nobody ends up using them
   14791              :              we'll have leftover bindings laying around, and it's tricky
   14792              :              to delete them and any namespaces they've implicitly created
   14793              :              deps on.  The downside is this means we don't pick up on
   14794              :              using-decls, but by [module.global.frag] p3.6 we don't have
   14795              :              to.  */
   14796      1881838 :           if (ek == EK_DECL
   14797      1881838 :               && !for_binding
   14798       593386 :               && !dep->is_import ()
   14799       589231 :               && !dep->is_tu_local ()
   14800       589127 :               && DECL_NAMESPACE_SCOPE_P (decl)
   14801        76324 :               && DECL_DECLARES_FUNCTION_P (decl)
   14802              :               /* Compiler-generated functions won't participate in ADL.  */
   14803        59169 :               && !DECL_ARTIFICIAL (decl)
   14804              :               /* A hidden friend doesn't need a binding.  */
   14805      1932587 :               && !(DECL_LANG_SPECIFIC (not_tmpl)
   14806        50749 :                    && DECL_UNIQUE_FRIEND_P (not_tmpl)))
   14807              :             {
   14808              :               /* This will only affect GM functions.  */
   14809        65614 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (not_tmpl)
   14810              :                                    || !DECL_MODULE_PURVIEW_P (not_tmpl));
   14811              :               /* We shouldn't see any instantiations or specialisations.  */
   14812        32807 :               gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   14813              :                                    || !DECL_USE_TEMPLATE (decl));
   14814              : 
   14815        32807 :               tree ns = CP_DECL_CONTEXT (decl);
   14816        32807 :               tree name = DECL_NAME (decl);
   14817        32807 :               depset *binding = find_binding (ns, name);
   14818        32807 :               if (!binding)
   14819              :                 {
   14820        10182 :                   binding = make_binding (ns, name);
   14821        10182 :                   add_namespace_context (binding, ns);
   14822              : 
   14823        10182 :                   depset **slot = binding_slot (ns, name, /*insert=*/true);
   14824        10182 :                   *slot = binding;
   14825              :                 }
   14826              : 
   14827        32807 :               binding->deps.safe_push (dep);
   14828        32807 :               dep->deps.safe_push (binding);
   14829        32842 :               dump (dumper::DEPEND)
   14830            9 :                 && dump ("Built ADL binding for %C:%N",
   14831            9 :                          TREE_CODE (decl), decl);
   14832              :             }
   14833              :         }
   14834              : 
   14835      1923424 :       if (!dep->is_import ())
   14836      1856872 :         worklist.safe_push (dep);
   14837              :     }
   14838      7452513 :   else if (!ignore_exposure)
   14839      6573762 :     dep->clear_flag_bit<DB_IGNORED_EXPOSURE_BIT> ();
   14840              : 
   14841      9375937 :   dump (dumper::DEPEND)
   14842        37499 :     && dump ("%s on %s %C:%N found",
   14843              :              ek == EK_REDIRECT ? "Redirect"
   14844        37499 :              : (for_binding || ek == EK_TU_LOCAL) ? "Binding"
   14845              :              : "Dependency",
   14846        37499 :              dep->entity_kind_name (), TREE_CODE (decl), decl);
   14847              : 
   14848      9375937 :   return dep;
   14849              : }
   14850              : 
   14851              : /* Whether REF is an exposure of a member type of SOURCE.
   14852              : 
   14853              :    This comes up with exposures of class-scope lambdas, that we currently
   14854              :    treat as TU-local due to ABI reasons.  In such a case the type of the
   14855              :    lambda will be exposed in two places, first by the class type it is in
   14856              :    the TYPE_FIELDS list of, and second by the actual member declaring that
   14857              :    lambda.  We only want the second case to warn.  */
   14858              : 
   14859              : static bool
   14860          253 : is_exposure_of_member_type (depset *source, depset *ref)
   14861              : {
   14862          253 :   gcc_checking_assert (source->refs_tu_local (/*strict=*/true)
   14863              :                        && ref->is_tu_local (/*strict=*/true));
   14864          253 :   tree source_entity = STRIP_TEMPLATE (source->get_entity ());
   14865          253 :   tree ref_entity = STRIP_TEMPLATE (ref->get_entity ());
   14866              : 
   14867          253 :   if (!source->is_tu_local (/*strict=*/true)
   14868          241 :       && source_entity
   14869          241 :       && ref_entity
   14870          241 :       && DECL_IMPLICIT_TYPEDEF_P (source_entity)
   14871            2 :       && DECL_IMPLICIT_TYPEDEF_P (ref_entity)
   14872            2 :       && DECL_CLASS_SCOPE_P (ref_entity)
   14873          255 :       && DECL_CONTEXT (ref_entity) == TREE_TYPE (source_entity))
   14874              :     {
   14875            4 :       gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (ref_entity)));
   14876              :       return true;
   14877              :     }
   14878              :   else
   14879              :     return false;
   14880              : }
   14881              : 
   14882              : /* DEP is a newly discovered dependency.  Append it to current's
   14883              :    depset.  */
   14884              : 
   14885              : void
   14886      7104428 : depset::hash::add_dependency (depset *dep)
   14887              : {
   14888      7104428 :   gcc_checking_assert (current && !is_key_order ());
   14889      7104428 :   current->deps.safe_push (dep);
   14890              : 
   14891      7104428 :   if (dep->is_tu_local (/*strict=*/true))
   14892              :     {
   14893          329 :       if (dep->is_tu_local ())
   14894          260 :         current->set_flag_bit<DB_REF_PURVIEW_BIT> ();
   14895              :       else
   14896           69 :         current->set_flag_bit<DB_REF_GLOBAL_BIT> ();
   14897              : 
   14898          329 :       if (!ignore_exposure && !is_exposure_of_member_type (current, dep))
   14899              :         {
   14900          133 :           if (dep->is_tu_local ())
   14901           91 :             current->set_flag_bit<DB_EXPOSE_PURVIEW_BIT> ();
   14902              :           else
   14903           42 :             current->set_flag_bit<DB_EXPOSE_GLOBAL_BIT> ();
   14904              :         }
   14905              :     }
   14906              : 
   14907      7104428 :   if (current->get_entity_kind () == EK_USING
   14908        41699 :       && DECL_IMPLICIT_TYPEDEF_P (dep->get_entity ())
   14909      7109786 :       && TREE_CODE (TREE_TYPE (dep->get_entity ())) == ENUMERAL_TYPE)
   14910              :     {
   14911              :       /* CURRENT is an unwrapped using-decl and DECL is an enum's
   14912              :          implicit typedef.  Is CURRENT a member of the enum?  */
   14913         5084 :       tree c_decl = OVL_FUNCTION (current->get_entity ());
   14914              : 
   14915         5084 :       if (TREE_CODE (c_decl) == CONST_DECL
   14916        10149 :           && (current->deps[0]->get_entity ()
   14917         5065 :               == CP_DECL_CONTEXT (dep->get_entity ())))
   14918              :         /* Make DECL depend on CURRENT.  */
   14919         5011 :         dep->deps.safe_push (current);
   14920              :     }
   14921              : 
   14922              :   /* If two dependencies recursively depend on each other existing within
   14923              :      their own merge keys, we must ensure that the first dep we saw while
   14924              :      walking is written first in this cluster.  See sort_cluster for more
   14925              :      details.  */
   14926      7104428 :   if (writing_merge_key)
   14927              :     {
   14928      1279831 :       if (!dep->is_maybe_recursive () && !current->is_maybe_recursive ())
   14929        66887 :         current->set_flag_bit<DB_ENTRY_BIT> ();
   14930      1279831 :       dep->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14931      1279831 :       current->set_flag_bit<DB_MAYBE_RECURSIVE_BIT> ();
   14932              :     }
   14933              : 
   14934      7104428 :   if (dep->is_unreached ())
   14935              :     {
   14936              :       /* The dependency is reachable now.  */
   14937       515844 :       reached_unreached = true;
   14938       515844 :       dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   14939       515844 :       dump (dumper::DEPEND)
   14940           30 :         && dump ("Reaching unreached %s %C:%N", dep->entity_kind_name (),
   14941           30 :                  TREE_CODE (dep->get_entity ()), dep->get_entity ());
   14942              :     }
   14943      7104428 : }
   14944              : 
   14945              : depset *
   14946     10460241 : depset::hash::add_dependency (tree decl, entity_kind ek)
   14947              : {
   14948     10460241 :   depset *dep;
   14949              : 
   14950     10460241 :   if (is_key_order ())
   14951              :     {
   14952      3288220 :       dep = find_dependency (decl);
   14953      3288220 :       if (dep)
   14954              :         {
   14955      1492553 :           current->deps.safe_push (dep);
   14956      1492553 :           dump (dumper::MERGE)
   14957          723 :             && dump ("Key dependency on %s %C:%N found",
   14958          723 :                      dep->entity_kind_name (), TREE_CODE (decl), decl);
   14959              :         }
   14960              :       else
   14961              :         {
   14962              :           /* It's not a mergeable decl, look for it in the original
   14963              :              table.  */
   14964      1795667 :           dep = chain->find_dependency (decl);
   14965      1795667 :           gcc_checking_assert (dep);
   14966              :         }
   14967              :     }
   14968              :   else
   14969              :     {
   14970      7172021 :       dep = make_dependency (decl, ek);
   14971      7172021 :       if (dep->get_entity_kind () != EK_REDIRECT)
   14972      7104428 :         add_dependency (dep);
   14973              :     }
   14974              : 
   14975     10460241 :   return dep;
   14976              : }
   14977              : 
   14978              : void
   14979       787871 : depset::hash::add_namespace_context (depset *dep, tree ns)
   14980              : {
   14981       787871 :   depset *ns_dep = make_dependency (ns, depset::EK_NAMESPACE);
   14982       787871 :   dep->deps.safe_push (ns_dep);
   14983              : 
   14984              :   /* Mark it as special if imported so we don't walk connect when
   14985              :      SCCing.  */
   14986       787871 :   if (!dep->is_binding () && ns_dep->is_import ())
   14987            0 :     dep->set_special ();
   14988       787871 : }
   14989              : 
   14990              : struct add_binding_data
   14991              : {
   14992              :   tree ns;
   14993              :   bitmap partitions;
   14994              :   depset *binding;
   14995              :   depset::hash *hash;
   14996              :   bool met_namespace;
   14997              : };
   14998              : 
   14999              : /* Return true if we are, or contain something that is exported.  */
   15000              : 
   15001              : bool
   15002      6004273 : depset::hash::add_binding_entity (tree decl, WMB_Flags flags, void *data_)
   15003              : {
   15004      6004273 :   auto data = static_cast <add_binding_data *> (data_);
   15005      6004273 :   decl = strip_using_decl (decl);
   15006              : 
   15007      6004273 :   if (!(TREE_CODE (decl) == NAMESPACE_DECL && !DECL_NAMESPACE_ALIAS (decl)))
   15008              :     {
   15009      5995484 :       tree inner = decl;
   15010              : 
   15011      5995484 :       if (TREE_CODE (inner) == CONST_DECL
   15012        11057 :           && TREE_CODE (DECL_CONTEXT (inner)) == ENUMERAL_TYPE
   15013              :           /* A using-decl could make a CONST_DECL purview for a non-purview
   15014              :              enumeration.  */
   15015      6006541 :           && (!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner)))
   15016        11016 :         inner = TYPE_NAME (DECL_CONTEXT (inner));
   15017      5984468 :       else if (TREE_CODE (inner) == TEMPLATE_DECL)
   15018       152617 :         inner = DECL_TEMPLATE_RESULT (inner);
   15019              : 
   15020     11799498 :       if ((!DECL_LANG_SPECIFIC (inner) || !DECL_MODULE_PURVIEW_P (inner))
   15021     11621196 :           && !((flags & WMB_Using) && (flags & WMB_Purview)))
   15022              :         /* Ignore entities not within the module purview.  We'll need to
   15023              :            create bindings for any non-discarded function calls for ADL,
   15024              :            but it's simpler to handle that at the point of use rather
   15025              :            than trying to clear out bindings after the fact.  */
   15026              :         return false;
   15027              : 
   15028       203553 :       if ((flags & WMB_Hidden)
   15029         5609 :           && DECL_LANG_SPECIFIC (inner)
   15030       209162 :           && DECL_UNIQUE_FRIEND_P (inner))
   15031              :         /* Hidden friends will be found via ADL on the class type,
   15032              :            and so do not need to have bindings.  Anticipated builtin
   15033              :            functions and the hidden decl underlying a DECL_LOCAL_DECL_P
   15034              :            also don't need exporting, but we should create a binding
   15035              :            anyway so that we can have a common decl to match against.  */
   15036              :         return false;
   15037              : 
   15038       198033 :       bool internal_decl = false;
   15039       198033 :       if (!header_module_p () && is_tu_local_entity (decl)
   15040       198297 :           && !((flags & WMB_Using) && (flags & WMB_Export)))
   15041              :         {
   15042              :           /* A TU-local entity.  For ADL we still need to create bindings
   15043              :              for internal-linkage functions attached to a named module.  */
   15044          153 :           if (DECL_DECLARES_FUNCTION_P (inner)
   15045          105 :               && DECL_LANG_SPECIFIC (inner)
   15046          363 :               && DECL_MODULE_ATTACH_P (inner))
   15047              :             {
   15048           93 :               gcc_checking_assert (!DECL_MODULE_EXPORT_P (inner));
   15049              :               internal_decl = true;
   15050              :             }
   15051              :           else
   15052              :             return false;
   15053              :         }
   15054              : 
   15055       197868 :       if ((TREE_CODE (decl) == VAR_DECL
   15056       197868 :            || TREE_CODE (decl) == TYPE_DECL)
   15057       197868 :           && DECL_TINFO_P (decl))
   15058              :         /* Ignore TINFO things.  */
   15059              :         return false;
   15060              : 
   15061       197868 :       if (TREE_CODE (decl) == VAR_DECL && DECL_NTTP_OBJECT_P (decl))
   15062              :         /* Ignore NTTP objects.  */
   15063              :         return false;
   15064              : 
   15065       197868 :       if (deduction_guide_p (decl))
   15066              :         {
   15067              :           /* Ignore deduction guides, bindings for them will be created within
   15068              :              find_dependencies for their class template.  But still build a dep
   15069              :              for them so that we don't discard them.  */
   15070         1616 :           data->hash->make_dependency (decl, EK_FOR_BINDING);
   15071         1616 :           return false;
   15072              :         }
   15073              : 
   15074       196252 :       if (!(flags & WMB_Using) && CP_DECL_CONTEXT (decl) != data->ns)
   15075              :         {
   15076              :           /* An unscoped enum constant implicitly brought into the containing
   15077              :              namespace.  We treat this like a using-decl.  */
   15078         4187 :           gcc_checking_assert (TREE_CODE (decl) == CONST_DECL);
   15079              : 
   15080         4187 :           flags = WMB_Flags (flags | WMB_Using);
   15081         4187 :           if (DECL_MODULE_EXPORT_P (TYPE_NAME (TREE_TYPE (decl)))
   15082              :               /* A using-decl can make an enum constant exported for a
   15083              :                  non-exported enumeration.  */
   15084         4187 :               || (DECL_LANG_SPECIFIC (decl) && DECL_MODULE_EXPORT_P (decl)))
   15085         3956 :             flags = WMB_Flags (flags | WMB_Export);
   15086              :         }
   15087              : 
   15088       196252 :       if (!data->binding)
   15089              :         /* No binding to check.  */;
   15090        43488 :       else if (flags & WMB_Using)
   15091              :         {
   15092              :           /* Look in the binding to see if we already have this
   15093              :              using.  */
   15094       181811 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15095              :             {
   15096       147176 :               depset *d = data->binding->deps[ix];
   15097       294352 :               if (d->get_entity_kind () == EK_USING
   15098       147176 :                   && OVL_FUNCTION (d->get_entity ()) == decl)
   15099              :                 {
   15100            3 :                   if (!(flags & WMB_Hidden))
   15101            3 :                     d->clear_hidden_binding ();
   15102            3 :                   OVL_PURVIEW_P (d->get_entity ()) = true;
   15103            3 :                   if (flags & WMB_Export)
   15104            3 :                     OVL_EXPORT_P (d->get_entity ()) = true;
   15105            3 :                   return bool (flags & WMB_Export);
   15106              :                 }
   15107              :             }
   15108              :         }
   15109        26169 :       else if (flags & WMB_Dups)
   15110              :         {
   15111              :           /* Look in the binding to see if we already have this decl.  */
   15112           78 :           for (unsigned ix = data->binding->deps.length (); --ix;)
   15113              :             {
   15114           39 :               depset *d = data->binding->deps[ix];
   15115           39 :               if (d->get_entity () == decl)
   15116              :                 {
   15117           33 :                   if (!(flags & WMB_Hidden))
   15118           33 :                     d->clear_hidden_binding ();
   15119              :                   return false;
   15120              :                 }
   15121              :             }
   15122              :         }
   15123              : 
   15124              :       /* We're adding something.  */
   15125       196216 :       if (!data->binding)
   15126              :         {
   15127       152764 :           data->binding = make_binding (data->ns, DECL_NAME (decl));
   15128       152764 :           data->hash->add_namespace_context (data->binding, data->ns);
   15129              : 
   15130       152764 :           depset **slot = data->hash->binding_slot (data->ns,
   15131       152764 :                                                     DECL_NAME (decl), true);
   15132       152764 :           gcc_checking_assert (!*slot);
   15133       152764 :           *slot = data->binding;
   15134              :         }
   15135              : 
   15136              :       /* Make sure nobody left a tree visited lying about.  */
   15137       196216 :       gcc_checking_assert (!TREE_VISITED (decl));
   15138              : 
   15139       196216 :       if (flags & WMB_Using)
   15140              :         {
   15141        41586 :           decl = ovl_make (decl, NULL_TREE);
   15142        41586 :           OVL_USING_P (decl) = true;
   15143        41586 :           OVL_PURVIEW_P (decl) = true;
   15144        41586 :           if (flags & WMB_Export)
   15145        40387 :             OVL_EXPORT_P (decl) = true;
   15146              :         }
   15147              : 
   15148       196216 :       entity_kind ek = EK_FOR_BINDING;
   15149       196216 :       if (internal_decl)
   15150              :         ek = EK_TU_LOCAL;
   15151       196123 :       else if (flags & WMB_Using)
   15152        41586 :         ek = EK_USING;
   15153              : 
   15154       196216 :       depset *dep = data->hash->make_dependency (decl, ek);
   15155       196216 :       if (flags & WMB_Hidden)
   15156           89 :         dep->set_hidden_binding ();
   15157       196216 :       data->binding->deps.safe_push (dep);
   15158              :       /* Binding and contents are mutually dependent.  */
   15159       196216 :       dep->deps.safe_push (data->binding);
   15160              : 
   15161       196216 :       return (flags & WMB_Using
   15162       196216 :               ? flags & WMB_Export : DECL_MODULE_EXPORT_P (decl));
   15163              :     }
   15164         8789 :   else if (!data->met_namespace)
   15165              :     {
   15166              :       /* Namespace, walk exactly once.  */
   15167         8780 :       data->met_namespace = true;
   15168         8780 :       if (data->hash->add_namespace_entities (decl, data->partitions))
   15169              :         {
   15170              :           /* It contains an exported thing, so it is exported.  */
   15171         1735 :           gcc_checking_assert (DECL_MODULE_PURVIEW_P (decl));
   15172         1735 :           gcc_checking_assert (TREE_PUBLIC (decl) || header_module_p ());
   15173         1735 :           DECL_MODULE_EXPORT_P (decl) = true;
   15174              :         }
   15175              : 
   15176         8780 :       if (DECL_MODULE_PURVIEW_P (decl))
   15177              :         {
   15178         2104 :           data->hash->make_dependency (decl, depset::EK_NAMESPACE);
   15179              : 
   15180         2104 :           return DECL_MODULE_EXPORT_P (decl);
   15181              :         }
   15182              :     }
   15183              : 
   15184              :   return false;
   15185              : }
   15186              : 
   15187              : /* Recursively find all the namespace bindings of NS.  Add a depset
   15188              :    for every binding that contains an export or module-linkage entity.
   15189              :    Add a defining depset for every such decl that we need to write a
   15190              :    definition.  Such defining depsets depend on the binding depset.
   15191              :    Returns true if we contain something exported.  */
   15192              : 
   15193              : bool
   15194        11624 : depset::hash::add_namespace_entities (tree ns, bitmap partitions)
   15195              : {
   15196        12838 :   dump () && dump ("Looking for writables in %N", ns);
   15197        11624 :   dump.indent ();
   15198              : 
   15199        11624 :   unsigned count = 0;
   15200        11624 :   add_binding_data data;
   15201        11624 :   data.ns = ns;
   15202        11624 :   data.partitions = partitions;
   15203        11624 :   data.hash = this;
   15204              : 
   15205      7791783 :   for (tree binding : *DECL_NAMESPACE_BINDINGS (ns))
   15206              :     {
   15207      7780159 :       data.binding = nullptr;
   15208      7780159 :       data.met_namespace = false;
   15209      7780159 :       if (walk_module_binding (binding, partitions, add_binding_entity, &data))
   15210       144875 :         count++;
   15211              :     }
   15212              : 
   15213              :   /* Seed any using-directives so that we emit the relevant namespaces.  */
   15214        12263 :   for (tree udir : NAMESPACE_LEVEL (ns)->using_directives)
   15215          219 :     if (TREE_CODE (udir) == USING_DECL && DECL_MODULE_PURVIEW_P (udir))
   15216              :       {
   15217              :         /* Unless it's a (TU-local) anonymous namespace.
   15218              : 
   15219              :            FIXME instead of checking here, they should be
   15220              :            is_tu_local_entity.  */
   15221          182 :         if (!TREE_PUBLIC (USING_DECL_DECLS (udir)))
   15222           73 :           continue;
   15223          109 :         make_dependency (USING_DECL_DECLS (udir), depset::EK_NAMESPACE);
   15224          109 :         if (DECL_MODULE_EXPORT_P (udir))
   15225           80 :           count++;
   15226              :       }
   15227              : 
   15228        11624 :   if (count)
   15229         4094 :     dump () && dump ("Found %u entries", count);
   15230        11624 :   dump.outdent ();
   15231              : 
   15232        11624 :   return count != 0;
   15233              : }
   15234              : 
   15235              : void
   15236          216 : depset::hash::add_partial_entities (vec<tree, va_gc> *partial_classes)
   15237              : {
   15238        23385 :   for (unsigned ix = 0; ix != partial_classes->length (); ix++)
   15239              :     {
   15240        23169 :       tree inner = (*partial_classes)[ix];
   15241              : 
   15242        23169 :       depset *dep = make_dependency (inner, depset::EK_DECL);
   15243              : 
   15244        23169 :       if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15245              :         {
   15246        23169 :           dep = dep->deps[0];
   15247              :           /* We should have recorded the template as a partial
   15248              :              specialization.  */
   15249        23169 :           gcc_checking_assert (dep->get_entity_kind ()
   15250              :                                == depset::EK_PARTIAL);
   15251              : 
   15252              :           /* Only emit GM entities if reached.  */
   15253        23169 :           if (!DECL_LANG_SPECIFIC (inner)
   15254        36022 :               || !DECL_MODULE_PURVIEW_P (inner))
   15255        11444 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15256              :         }
   15257              :       else
   15258              :         {
   15259              :           /* It was an explicit specialization, not a partial one.
   15260              :              We should have already added this.  */
   15261            0 :           gcc_checking_assert (dep->get_entity_kind ()
   15262              :                                == depset::EK_SPECIALIZATION);
   15263            0 :           gcc_checking_assert (dep->is_special ());
   15264              :         }
   15265              :     }
   15266          216 : }
   15267              : 
   15268              : /* Add the members of imported classes that we defined in this TU.
   15269              :    This will also include lazily created implicit member function
   15270              :    declarations.  (All others will be definitions.)  */
   15271              : 
   15272              : void
   15273           12 : depset::hash::add_class_entities (vec<tree, va_gc> *class_members)
   15274              : {
   15275           24 :   for (unsigned ix = 0; ix != class_members->length (); ix++)
   15276              :     {
   15277           12 :       tree defn = (*class_members)[ix];
   15278           12 :       depset *dep = make_dependency (defn, EK_INNER_DECL);
   15279              : 
   15280           12 :       if (dep->get_entity_kind () == EK_REDIRECT)
   15281            0 :         dep = dep->deps[0];
   15282              : 
   15283              :       /* Only non-instantiations need marking as pendings.  */
   15284           24 :       if (dep->get_entity_kind () == EK_DECL)
   15285           12 :         dep->set_flag_bit <DB_IS_PENDING_BIT> ();
   15286              :     }
   15287           12 : }
   15288              : 
   15289              : /* Add any entities found via dependent ADL.  */
   15290              : 
   15291              : void
   15292      9021301 : depset::hash::add_dependent_adl_entities (tree expr)
   15293              : {
   15294      9021301 :   gcc_checking_assert (!is_key_order ());
   15295              : 
   15296              :   /* This is not needed for header units where everything is
   15297              :      visible to name lookup, nothing is discarded.  */
   15298      9021301 :   if (header_module_p ())
   15299      8973256 :     return;
   15300              : 
   15301      4407802 :   if (TREE_CODE (current->get_entity ()) != TEMPLATE_DECL)
   15302              :     return;
   15303              : 
   15304      2452539 :   dep_adl_info info;
   15305      2452539 :   auto_vec<tree, 3> args;
   15306      2452539 :   switch (TREE_CODE (expr))
   15307              :     {
   15308       259391 :     case CALL_EXPR:
   15309       259391 :       if (!KOENIG_LOOKUP_P (expr)
   15310       259391 :           || !type_dependent_expression_p_push (expr))
   15311              :         return;
   15312         8982 :       info.name = CALL_EXPR_FN (expr);
   15313         8982 :       if (!info.name)
   15314              :         return;
   15315         8982 :       if (TREE_CODE (info.name) == TEMPLATE_ID_EXPR)
   15316         1472 :         info.name = TREE_OPERAND (info.name, 0);
   15317         8982 :       if (TREE_CODE (info.name) == TU_LOCAL_ENTITY)
   15318              :         return;
   15319        15742 :       if (!identifier_p (info.name))
   15320         8808 :         info.name = OVL_NAME (info.name);
   15321        28783 :       for (int ix = 0; ix < call_expr_nargs (expr); ix++)
   15322        19801 :         args.safe_push (CALL_EXPR_ARG (expr, ix));
   15323              :       break;
   15324              : 
   15325        19105 :     case LE_EXPR:
   15326        19105 :     case GE_EXPR:
   15327        19105 :     case LT_EXPR:
   15328        19105 :     case GT_EXPR:
   15329        19105 :       info.rewrite = SPACESHIP_EXPR;
   15330        19105 :       goto overloadable_expr;
   15331              : 
   15332        10333 :     case NE_EXPR:
   15333        10333 :       info.rewrite = EQ_EXPR;
   15334        10333 :       goto overloadable_expr;
   15335              : 
   15336        21032 :     case EQ_EXPR:
   15337              :       /* Not strictly a rewrite candidate, but we need to ensure
   15338              :          that lookup of a matching NE_EXPR can succeed if that
   15339              :          would inhibit a rewrite with reversed parameters.  */
   15340        21032 :       info.rewrite = NE_EXPR;
   15341        21032 :       goto overloadable_expr;
   15342              : 
   15343       154283 :     case COMPOUND_EXPR:
   15344       154283 :     case MEMBER_REF:
   15345       154283 :     case MULT_EXPR:
   15346       154283 :     case TRUNC_DIV_EXPR:
   15347       154283 :     case TRUNC_MOD_EXPR:
   15348       154283 :     case PLUS_EXPR:
   15349       154283 :     case MINUS_EXPR:
   15350       154283 :     case LSHIFT_EXPR:
   15351       154283 :     case RSHIFT_EXPR:
   15352       154283 :     case SPACESHIP_EXPR:
   15353       154283 :     case BIT_AND_EXPR:
   15354       154283 :     case BIT_XOR_EXPR:
   15355       154283 :     case BIT_IOR_EXPR:
   15356       154283 :     case TRUTH_ANDIF_EXPR:
   15357       154283 :     case TRUTH_ORIF_EXPR:
   15358       154283 :     overloadable_expr:
   15359       154283 :       if (!type_dependent_expression_p_push (expr))
   15360              :         return;
   15361       111905 :       info.name = ovl_op_identifier (TREE_CODE (expr));
   15362       111905 :       gcc_checking_assert (tree_operand_length (expr) == 2);
   15363       111905 :       args.safe_push (TREE_OPERAND (expr, 0));
   15364       111905 :       args.safe_push (TREE_OPERAND (expr, 1));
   15365       111905 :       break;
   15366              : 
   15367              :     default:
   15368              :       return;
   15369              :     }
   15370              : 
   15371              :   /* If all arguments are type-dependent we don't need to do
   15372              :      anything further, we won't find new entities.  */
   15373       120887 :   bool all_type_dependent = true;
   15374       538892 :   for (tree arg : args)
   15375       224276 :     if (!type_dependent_expression_p_push (arg))
   15376              :       {
   15377              :         all_type_dependent = false;
   15378              :         break;
   15379              :       }
   15380       120887 :   if (all_type_dependent)
   15381              :     return;
   15382              : 
   15383        48045 :   gcc_checking_assert (!info.args);
   15384        48045 :   info.args = make_tree_vector ();
   15385       244283 :   for (tree arg : args)
   15386       100148 :     vec_safe_push (info.args, arg);
   15387              : 
   15388              :   /* We need to defer name lookup until after walking, otherwise
   15389              :      we get confused by stray TREE_VISITEDs.  */
   15390        48045 :   dep_adl_entity_list.safe_push (info);
   15391      2452539 : }
   15392              : 
   15393              : /* We add the partial & explicit specializations, and the explicit
   15394              :    instantiations.  */
   15395              : 
   15396              : static void
   15397      1130041 : specialization_add (bool decl_p, spec_entry *entry, void *data_)
   15398              : {
   15399      1130041 :   vec<spec_entry *> *data = reinterpret_cast <vec<spec_entry *> *> (data_);
   15400              : 
   15401      1130041 :   if (!decl_p)
   15402              :     {
   15403              :       /* We exclusively use decls to locate things.  Make sure there's
   15404              :          no mismatch between the two specialization tables we keep.
   15405              :          pt.cc optimizes instantiation lookup using a complicated
   15406              :          heuristic.  We don't attempt to replicate that algorithm, but
   15407              :          observe its behaviour and reproduce it upon read back.  */
   15408              : 
   15409       333837 :        gcc_checking_assert (TREE_CODE (entry->spec) == ENUMERAL_TYPE
   15410              :                            || DECL_CLASS_TEMPLATE_P (entry->tmpl));
   15411              : 
   15412       333837 :        gcc_checking_assert (!match_mergeable_specialization (true, entry));
   15413              :     }
   15414       796204 :   else if (VAR_OR_FUNCTION_DECL_P (entry->spec))
   15415       391201 :     gcc_checking_assert (!DECL_LOCAL_DECL_P (entry->spec));
   15416              : 
   15417      1130041 :   data->safe_push (entry);
   15418      1130041 : }
   15419              : 
   15420              : /* Arbitrary stable comparison.  */
   15421              : 
   15422              : static int
   15423     68860262 : specialization_cmp (const void *a_, const void *b_)
   15424              : {
   15425     68860262 :   const spec_entry *ea = *reinterpret_cast<const spec_entry *const *> (a_);
   15426     68860262 :   const spec_entry *eb = *reinterpret_cast<const spec_entry *const *> (b_);
   15427              : 
   15428     68860262 :   if (ea == eb)
   15429              :     return 0;
   15430              : 
   15431     68860262 :   tree a = ea->spec;
   15432     68860262 :   tree b = eb->spec;
   15433     68860262 :   if (TYPE_P (a))
   15434              :     {
   15435     19307078 :       a = TYPE_NAME (a);
   15436     19307078 :       b = TYPE_NAME (b);
   15437              :     }
   15438              : 
   15439     68860262 :   if (a == b)
   15440              :     /* This can happen with friend specializations.  Just order by
   15441              :        entry address.  See note in depset_cmp.  */
   15442            0 :     return ea < eb ? -1 : +1;
   15443              : 
   15444     68860262 :   return DECL_UID (a) < DECL_UID (b) ? -1 : +1;
   15445              : }
   15446              : 
   15447              : /* We add all kinds of specialializations.  Implicit specializations
   15448              :    should only streamed and walked if they are reachable from
   15449              :    elsewhere.  Hence the UNREACHED flag.  This is making the
   15450              :    assumption that it is cheaper to reinstantiate them on demand
   15451              :    elsewhere, rather than stream them in when we instantiate their
   15452              :    general template.  Also, if we do stream them, we can only do that
   15453              :    if they are not internal (which they can become if they themselves
   15454              :    touch an internal entity?).  */
   15455              : 
   15456              : void
   15457         5688 : depset::hash::add_specializations (bool decl_p)
   15458              : {
   15459         5688 :   vec<spec_entry *> data;
   15460         5688 :   data.create (100);
   15461         5688 :   walk_specializations (decl_p, specialization_add, &data);
   15462         5688 :   data.qsort (specialization_cmp);
   15463      1135729 :   while (data.length ())
   15464              :     {
   15465      1130041 :       spec_entry *entry = data.pop ();
   15466      1130041 :       tree spec = entry->spec;
   15467      1130041 :       int use_tpl = 0;
   15468      1130041 :       bool is_friend = false;
   15469              : 
   15470      1130041 :       if (decl_p && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (entry->tmpl))
   15471              :         /* A friend of a template.  This is keyed to the
   15472              :            instantiation.  */
   15473              :         is_friend = true;
   15474              : 
   15475      1130041 :       if (decl_p)
   15476              :         {
   15477       796204 :           if (tree ti = DECL_TEMPLATE_INFO (spec))
   15478              :             {
   15479       796204 :               tree tmpl = TI_TEMPLATE (ti);
   15480              : 
   15481       796204 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15482       796204 :               if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15483              :                 {
   15484         5981 :                   spec = tmpl;
   15485         5981 :                   gcc_checking_assert (DECL_USE_TEMPLATE (spec) == use_tpl);
   15486              :                 }
   15487       790223 :               else if (is_friend)
   15488              :                 {
   15489         5759 :                   if (TI_TEMPLATE (ti) != entry->tmpl
   15490         5759 :                       || !template_args_equal (TI_ARGS (ti), entry->tmpl))
   15491         5759 :                     goto template_friend;
   15492              :                 }
   15493              :             }
   15494              :           else
   15495              :             {
   15496            0 :             template_friend:;
   15497         5759 :               gcc_checking_assert (is_friend);
   15498              :               /* This is a friend of a template class, but not the one
   15499              :                  that generated entry->spec itself (i.e. it's an
   15500              :                  equivalent clone).  We do not need to record
   15501              :                  this.  */
   15502         5759 :               continue;
   15503              :             }
   15504              :         }
   15505              :       else
   15506              :         {
   15507       333837 :           if (TREE_CODE (spec) == ENUMERAL_TYPE)
   15508              :             {
   15509         1539 :               tree ctx = DECL_CONTEXT (TYPE_NAME (spec));
   15510              : 
   15511         1539 :               if (TYPE_P (ctx))
   15512         1533 :                 use_tpl = CLASSTYPE_USE_TEMPLATE (ctx);
   15513              :               else
   15514            6 :                 use_tpl = DECL_USE_TEMPLATE (ctx);
   15515              :             }
   15516              :           else
   15517       332298 :             use_tpl = CLASSTYPE_USE_TEMPLATE (spec);
   15518              : 
   15519       333837 :           tree ti = TYPE_TEMPLATE_INFO (spec);
   15520       333837 :           tree tmpl = TI_TEMPLATE (ti);
   15521              : 
   15522       333837 :           spec = TYPE_NAME (spec);
   15523       333837 :           if (spec == DECL_TEMPLATE_RESULT (tmpl))
   15524              :             {
   15525         1714 :               spec = tmpl;
   15526         1714 :               use_tpl = DECL_USE_TEMPLATE (spec);
   15527              :             }
   15528              :         }
   15529              : 
   15530      1124282 :       bool needs_reaching = false;
   15531      1124282 :       if (use_tpl == 1)
   15532              :         /* Implicit instantiations only walked if we reach them.  */
   15533              :         needs_reaching = true;
   15534        89570 :       else if (!DECL_LANG_SPECIFIC (STRIP_TEMPLATE (spec))
   15535       159920 :                || !DECL_MODULE_PURVIEW_P (STRIP_TEMPLATE (spec)))
   15536              :         /* Likewise, GMF explicit or partial specializations.  */
   15537              :         needs_reaching = true;
   15538              : 
   15539              : #if false && CHECKING_P
   15540              :       /* The instantiation isn't always on
   15541              :          DECL_TEMPLATE_INSTANTIATIONS, */
   15542              :       // FIXME: we probably need to remember this information?
   15543              :       /* Verify the specialization is on the
   15544              :          DECL_TEMPLATE_INSTANTIATIONS of the template.  */
   15545              :       for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (entry->tmpl);
   15546              :            cons; cons = TREE_CHAIN (cons))
   15547              :         if (TREE_VALUE (cons) == entry->spec)
   15548              :           {
   15549              :             gcc_assert (entry->args == TREE_PURPOSE (cons));
   15550              :             goto have_spec;
   15551              :           }
   15552              :       gcc_unreachable ();
   15553              :     have_spec:;
   15554              : #endif
   15555              : 
   15556              :       /* Make sure nobody left a tree visited lying about.  */
   15557      1124282 :       gcc_checking_assert (!TREE_VISITED (spec));
   15558      1124282 :       depset *dep = make_dependency (spec, depset::EK_SPECIALIZATION);
   15559      1124282 :       if (dep->is_special ())
   15560            0 :         gcc_unreachable ();
   15561              :       else
   15562              :         {
   15563      1124282 :           if (dep->get_entity_kind () == depset::EK_REDIRECT)
   15564        26603 :             dep = dep->deps[0];
   15565      1097679 :           else if (dep->get_entity_kind () == depset::EK_SPECIALIZATION)
   15566              :             {
   15567      1097679 :               dep->set_special ();
   15568      1097679 :               dep->deps.safe_push (reinterpret_cast<depset *> (entry));
   15569      1097679 :               if (!decl_p)
   15570       311617 :                 dep->set_flag_bit<DB_TYPE_SPEC_BIT> ();
   15571              :             }
   15572              : 
   15573      1124282 :           if (needs_reaching)
   15574      1078795 :             dep->set_flag_bit<DB_UNREACHED_BIT> ();
   15575      1124282 :           if (is_friend)
   15576            0 :             dep->set_flag_bit<DB_FRIEND_SPEC_BIT> ();
   15577              :         }
   15578              :     }
   15579         5688 :   data.release ();
   15580         5688 : }
   15581              : 
   15582              : /* Add a depset into the mergeable hash.  */
   15583              : 
   15584              : void
   15585      1330542 : depset::hash::add_mergeable (depset *mergeable)
   15586              : {
   15587      1330542 :   gcc_checking_assert (is_key_order ());
   15588      1330542 :   entity_kind ek = mergeable->get_entity_kind ();
   15589      1330542 :   tree decl = mergeable->get_entity ();
   15590      1330542 :   gcc_checking_assert (ek < EK_DIRECT_HWM);
   15591              : 
   15592      1330542 :   depset **slot = entity_slot (decl, true);
   15593      1330542 :   gcc_checking_assert (!*slot);
   15594      1330542 :   depset *dep = make_entity (decl, ek);
   15595      1330542 :   *slot = dep;
   15596              : 
   15597      1330542 :   worklist.safe_push (dep);
   15598              : 
   15599              :   /* So we can locate the mergeable depset this depset refers to,
   15600              :      mark the first dep.  */
   15601      1330542 :   dep->set_special ();
   15602      1330542 :   dep->deps.safe_push (mergeable);
   15603      1330542 : }
   15604              : 
   15605              : /* Find the innermost-namespace scope of DECL, and that
   15606              :    namespace-scope decl.  */
   15607              : 
   15608              : tree
   15609     41668538 : find_pending_key (tree decl, tree *decl_p = nullptr)
   15610              : {
   15611     41668538 :   tree ns = decl;
   15612     50613179 :   do
   15613              :     {
   15614     50613179 :       decl = ns;
   15615     50613179 :       ns = CP_DECL_CONTEXT (ns);
   15616     50613179 :       if (TYPE_P (ns))
   15617      5551171 :         ns = TYPE_NAME (ns);
   15618              :     }
   15619     50613179 :   while (TREE_CODE (ns) != NAMESPACE_DECL);
   15620              : 
   15621     41668538 :   if (decl_p)
   15622     41047502 :     *decl_p = decl;
   15623              : 
   15624     41668538 :   return ns;
   15625              : }
   15626              : 
   15627              : /* Creates bindings and dependencies for all deduction guides of
   15628              :    the given class template DECL as needed.  */
   15629              : 
   15630              : void
   15631        61683 : depset::hash::add_deduction_guides (tree decl)
   15632              : {
   15633              :   /* Alias templates never have deduction guides.  */
   15634        61683 :   if (DECL_ALIAS_TEMPLATE_P (decl))
   15635        60552 :     return;
   15636              : 
   15637              :   /* We don't need to do anything for class-scope deduction guides,
   15638              :      as they will be added as members anyway.  */
   15639        61683 :   if (!DECL_NAMESPACE_SCOPE_P (decl))
   15640              :     return;
   15641              : 
   15642        47355 :   tree ns = CP_DECL_CONTEXT (decl);
   15643        47355 :   tree name = dguide_name (decl);
   15644              : 
   15645              :   /* We always add all deduction guides with a given name at once,
   15646              :      so if there's already a binding there's nothing to do.  */
   15647        47355 :   if (find_binding (ns, name))
   15648              :     return;
   15649              : 
   15650        43840 :   tree guides = lookup_qualified_name (ns, name, LOOK_want::NORMAL,
   15651              :                                        /*complain=*/false);
   15652        43840 :   if (guides == error_mark_node)
   15653              :     return;
   15654              : 
   15655         1131 :   depset *binding = nullptr;
   15656         5505 :   for (tree t : lkp_range (guides))
   15657              :     {
   15658         3243 :       gcc_checking_assert (!TREE_VISITED (t));
   15659         3243 :       depset *dep = make_dependency (t, EK_FOR_BINDING);
   15660              : 
   15661              :       /* We don't want to create bindings for imported deduction guides, as
   15662              :          this would potentially cause name lookup to return duplicates.  */
   15663         3243 :       if (dep->is_import ())
   15664            6 :         continue;
   15665              : 
   15666         3237 :       if (!binding)
   15667              :         {
   15668              :           /* We have bindings to add.  */
   15669         1125 :           binding = make_binding (ns, name);
   15670         1125 :           add_namespace_context (binding, ns);
   15671              : 
   15672         1125 :           depset **slot = binding_slot (ns, name, /*insert=*/true);
   15673         1125 :           *slot = binding;
   15674              :         }
   15675              : 
   15676         3237 :       binding->deps.safe_push (dep);
   15677         3237 :       dep->deps.safe_push (binding);
   15678         3237 :       dump (dumper::DEPEND)
   15679            0 :         && dump ("Built binding for deduction guide %C:%N",
   15680            0 :                  TREE_CODE (decl), decl);
   15681              :     }
   15682              : }
   15683              : 
   15684              : /* Iteratively find dependencies.  During the walk we may find more
   15685              :    entries on the same binding that need walking.  */
   15686              : 
   15687              : void
   15688       340527 : depset::hash::find_dependencies (module_state *module)
   15689              : {
   15690       340527 :   trees_out walker (NULL, module, *this);
   15691       340527 :   vec<depset *> unreached;
   15692       681054 :   unreached.create (worklist.length ());
   15693              : 
   15694       342865 :   for (;;)
   15695              :     {
   15696       341696 :       reached_unreached = false;
   15697      6118567 :       while (worklist.length ())
   15698              :         {
   15699      5776871 :           depset *item = worklist.pop ();
   15700              : 
   15701      5776871 :           gcc_checking_assert (!item->is_binding ());
   15702      5776871 :           if (item->is_unreached ())
   15703      3068143 :             unreached.quick_push (item);
   15704              :           else
   15705              :             {
   15706      2708728 :               current = item;
   15707      2708728 :               tree decl = current->get_entity ();
   15708      2708728 :               dump (is_key_order () ? dumper::MERGE : dumper::DEPEND)
   15709      2710051 :                 && dump ("Dependencies of %s %C:%N",
   15710         1323 :                          is_key_order () ? "key-order"
   15711         1323 :                          : current->entity_kind_name (), TREE_CODE (decl), decl);
   15712      2708728 :               dump.indent ();
   15713      2708728 :               walker.begin ();
   15714      2708728 :               if (current->get_entity_kind () == EK_USING)
   15715        41586 :                 walker.tree_node (OVL_FUNCTION (decl));
   15716      2667142 :               else if (current->get_entity_kind () == EK_TU_LOCAL)
   15717              :                 /* We only stream its name and location.  */
   15718           93 :                 module->note_location (DECL_SOURCE_LOCATION (decl));
   15719      2667049 :               else if (TREE_VISITED (decl))
   15720              :                 /* A global tree.  */;
   15721      2664465 :               else if (current->get_entity_kind () == EK_NAMESPACE)
   15722              :                 {
   15723         2764 :                   module->note_location (DECL_SOURCE_LOCATION (decl));
   15724         2764 :                   add_namespace_context (current, CP_DECL_CONTEXT (decl));
   15725              :                 }
   15726              :               else
   15727              :                 {
   15728      2661701 :                   walker.mark_declaration (decl, current->has_defn ());
   15729              : 
   15730      2661701 :                   if (!is_key_order ()
   15731      2661701 :                       && item->is_pending_entity ())
   15732              :                     {
   15733       621036 :                       tree ns = find_pending_key (decl, nullptr);
   15734       621036 :                       add_namespace_context (item, ns);
   15735              :                     }
   15736              : 
   15737      2661701 :                   auto ovr = make_temp_override
   15738      2661701 :                     (ignore_exposure, item->is_ignored_exposure_context ());
   15739      2661701 :                   walker.decl_value (decl, current);
   15740      2661701 :                   if (current->has_defn ())
   15741       513405 :                     walker.write_definition (decl, current->refs_tu_local ());
   15742      2661701 :                 }
   15743      2708728 :               walker.end ();
   15744              : 
   15745              :               /* If we see either a class template or a deduction guide, make
   15746              :                  sure to add all visible deduction guides.  We need to check
   15747              :                  both in case they have been added in separate modules, or
   15748              :                  one is in the GMF and would have otherwise been discarded.  */
   15749      2708728 :               if (!is_key_order ()
   15750      2708728 :                   && DECL_CLASS_TEMPLATE_P (decl))
   15751        58440 :                 add_deduction_guides (decl);
   15752      2708728 :               if (!is_key_order ()
   15753      2708728 :                   && deduction_guide_p (decl))
   15754         3243 :                 add_deduction_guides (TYPE_NAME (TREE_TYPE (TREE_TYPE (decl))));
   15755              : 
   15756              :               /* Handle dependent ADL for [module.global.frag] p3.3.  */
   15757      2708728 :               if (!is_key_order () && !dep_adl_entity_list.is_empty ())
   15758              :                 {
   15759        25697 :                   processing_template_decl_sentinel ptds;
   15760        25697 :                   ++processing_template_decl;
   15761        73742 :                   for (auto &info : dep_adl_entity_list)
   15762              :                     {
   15763        48045 :                       tree lookup = lookup_arg_dependent (info.name, NULL_TREE,
   15764              :                                                           info.args, true);
   15765       146550 :                       for (tree fn : lkp_range (lookup))
   15766              :                         /* We don't need to add_dependency, just have
   15767              :                            make_dependency build an ADL binding.  */
   15768        50460 :                         make_dependency (fn, EK_DECL);
   15769              : 
   15770        48045 :                       if (info.rewrite)
   15771              :                         {
   15772        11534 :                           tree rewrite_name = ovl_op_identifier (info.rewrite);
   15773        11534 :                           lookup = lookup_arg_dependent (rewrite_name, NULL_TREE,
   15774              :                                                          info.args, true);
   15775        37902 :                           for (tree fn : lkp_range (lookup))
   15776        14834 :                             make_dependency (fn, EK_DECL);
   15777              :                         }
   15778        48045 :                       release_tree_vector (info.args);
   15779              :                     }
   15780        25697 :                   dep_adl_entity_list.truncate (0);
   15781        25697 :                 }
   15782              : 
   15783      2708728 :               if (!is_key_order ()
   15784      1378186 :                   && TREE_CODE (decl) == TEMPLATE_DECL
   15785      3192027 :                   && !DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   15786              :                 {
   15787              :                   /* Mark all the explicit & partial specializations as
   15788              :                      reachable.  We search both specialization lists as some
   15789              :                      constrained partial specializations for class types are
   15790              :                      only found in DECL_TEMPLATE_SPECIALIZATIONS.  */
   15791      1388923 :                   auto mark_reached = [this](tree spec)
   15792              :                     {
   15793       915361 :                       if (TYPE_P (spec))
   15794       262415 :                         spec = TYPE_NAME (spec);
   15795       915361 :                       int use_tpl;
   15796       915361 :                       node_template_info (spec, use_tpl);
   15797       915361 :                       if (use_tpl & 2)
   15798              :                         {
   15799        90797 :                           depset *spec_dep = find_dependency (spec);
   15800        90797 :                           if (spec_dep->get_entity_kind () == EK_REDIRECT)
   15801        20479 :                             spec_dep = spec_dep->deps[0];
   15802        90797 :                           if (spec_dep->is_unreached ())
   15803              :                             {
   15804        22428 :                               reached_unreached = true;
   15805        22428 :                               spec_dep->clear_flag_bit<DB_UNREACHED_BIT> ();
   15806        22428 :                               dump (dumper::DEPEND)
   15807            0 :                                 && dump ("Reaching unreached specialization"
   15808            0 :                                          " %C:%N", TREE_CODE (spec), spec);
   15809              :                             }
   15810              :                         }
   15811      1388923 :                     };
   15812              : 
   15813       473562 :                   for (tree cons = DECL_TEMPLATE_INSTANTIATIONS (decl);
   15814      1367679 :                        cons; cons = TREE_CHAIN (cons))
   15815       894117 :                     mark_reached (TREE_VALUE (cons));
   15816       473562 :                   for (tree cons = DECL_TEMPLATE_SPECIALIZATIONS (decl);
   15817       494806 :                        cons; cons = TREE_CHAIN (cons))
   15818        21244 :                     mark_reached (TREE_VALUE (cons));
   15819              :                 }
   15820              : 
   15821      2708728 :               dump.outdent ();
   15822      2708728 :               current = NULL;
   15823              :             }
   15824              :         }
   15825              : 
   15826       341696 :       if (!reached_unreached)
   15827              :         break;
   15828              : 
   15829              :       /* It's possible the we reached the unreached before we
   15830              :          processed it in the above loop, so we'll be doing this an
   15831              :          extra time.  However, to avoid that we have to do some
   15832              :          bit shuffling that also involves a scan of the list.
   15833              :          Swings & roundabouts I guess.  */
   15834         1169 :       std::swap (worklist, unreached);
   15835         1169 :     }
   15836              : 
   15837       340527 :   unreached.release ();
   15838       340527 : }
   15839              : 
   15840              : /* Compare two entries of a single binding.  TYPE_DECL before
   15841              :    non-exported before exported.  */
   15842              : 
   15843              : static int
   15844      1086568 : binding_cmp (const void *a_, const void *b_)
   15845              : {
   15846      1086568 :   depset *a = *(depset *const *)a_;
   15847      1086568 :   depset *b = *(depset *const *)b_;
   15848              : 
   15849      1086568 :   tree a_ent = a->get_entity ();
   15850      1086568 :   tree b_ent = b->get_entity ();
   15851      1086568 :   gcc_checking_assert (a_ent != b_ent
   15852              :                        && !a->is_binding ()
   15853              :                        && !b->is_binding ());
   15854              : 
   15855              :   /* Implicit typedefs come first.  */
   15856      1086568 :   bool a_implicit = DECL_IMPLICIT_TYPEDEF_P (a_ent);
   15857      1086568 :   bool b_implicit = DECL_IMPLICIT_TYPEDEF_P (b_ent);
   15858      1086434 :   if (a_implicit || b_implicit)
   15859              :     {
   15860              :       /* A binding with two implicit type decls?  That's unpossible!  */
   15861          268 :       gcc_checking_assert (!(a_implicit && b_implicit));
   15862          268 :       return a_implicit ? -1 : +1;  /* Implicit first.  */
   15863              :     }
   15864              : 
   15865              :   /* TU-local before non-TU-local.  */
   15866      1086300 :   bool a_internal = a->get_entity_kind () == depset::EK_TU_LOCAL;
   15867      1086300 :   bool b_internal = b->get_entity_kind () == depset::EK_TU_LOCAL;
   15868      1086300 :   if (a_internal != b_internal)
   15869            0 :     return a_internal ? -1 : +1;  /* Internal first.  */
   15870              : 
   15871              :   /* Hidden before non-hidden.  */
   15872      1086300 :   bool a_hidden = a->is_hidden ();
   15873      1086300 :   bool b_hidden = b->is_hidden ();
   15874      1086300 :   if (a_hidden != b_hidden)
   15875            0 :     return a_hidden ? -1 : +1;
   15876              : 
   15877      1086300 :   bool a_using = a->get_entity_kind () == depset::EK_USING;
   15878      1086300 :   bool a_export;
   15879      1086300 :   if (a_using)
   15880              :     {
   15881       347125 :       a_export = OVL_EXPORT_P (a_ent);
   15882       347125 :       a_ent = OVL_FUNCTION (a_ent);
   15883              :     }
   15884       739175 :   else if (TREE_CODE (a_ent) == CONST_DECL
   15885            0 :            && DECL_LANG_SPECIFIC (a_ent)
   15886       739175 :            && DECL_MODULE_EXPORT_P (a_ent))
   15887              :     a_export = true;
   15888              :   else
   15889       739175 :     a_export = DECL_MODULE_EXPORT_P (TREE_CODE (a_ent) == CONST_DECL
   15890              :                                      ? TYPE_NAME (TREE_TYPE (a_ent))
   15891              :                                      : STRIP_TEMPLATE (a_ent));
   15892              : 
   15893      1086300 :   bool b_using = b->get_entity_kind () == depset::EK_USING;
   15894      1086300 :   bool b_export;
   15895      1086300 :   if (b_using)
   15896              :     {
   15897       368422 :       b_export = OVL_EXPORT_P (b_ent);
   15898       368422 :       b_ent = OVL_FUNCTION (b_ent);
   15899              :     }
   15900       717878 :   else if (TREE_CODE (b_ent) == CONST_DECL
   15901            0 :            && DECL_LANG_SPECIFIC (b_ent)
   15902       717878 :            && DECL_MODULE_EXPORT_P (b_ent))
   15903              :     b_export = true;
   15904              :   else
   15905       717878 :     b_export = DECL_MODULE_EXPORT_P (TREE_CODE (b_ent) == CONST_DECL
   15906              :                                      ? TYPE_NAME (TREE_TYPE (b_ent))
   15907              :                                      : STRIP_TEMPLATE (b_ent));
   15908              : 
   15909              :   /* Non-exports before exports.  */
   15910      1086300 :   if (a_export != b_export)
   15911       199527 :     return a_export ? +1 : -1;
   15912              : 
   15913              :   /* At this point we don't care, but want a stable sort.  */
   15914              : 
   15915       886773 :   if (a_using != b_using)
   15916              :     /* using first.  */
   15917        16715 :     return a_using? -1 : +1;
   15918              : 
   15919       870058 :   return DECL_UID (a_ent) < DECL_UID (b_ent) ? -1 : +1;
   15920              : }
   15921              : 
   15922              : /* True iff TMPL has an explicit instantiation definition.
   15923              : 
   15924              :    This is local to module.cc because register_specialization skips adding most
   15925              :    instantiations unless module_maybe_has_cmi_p.  */
   15926              : 
   15927              : static bool
   15928           76 : template_has_explicit_inst (tree tmpl)
   15929              : {
   15930           88 :   for (tree t = DECL_TEMPLATE_INSTANTIATIONS (tmpl); t; t = TREE_CHAIN (t))
   15931              :     {
   15932           24 :       tree spec = TREE_VALUE (t);
   15933           24 :       if (DECL_EXPLICIT_INSTANTIATION (spec)
   15934           24 :           && !DECL_REALLY_EXTERN (spec))
   15935              :         return true;
   15936              :     }
   15937              :   return false;
   15938              : }
   15939              : 
   15940              : /* Complain about DEP that exposes a TU-local entity.
   15941              : 
   15942              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   15943              :    if we explained anything.  */
   15944              : 
   15945              : bool
   15946          127 : depset::hash::diagnose_bad_internal_ref (depset *dep, bool strict)
   15947              : {
   15948          127 :   tree decl = dep->get_entity ();
   15949              : 
   15950              :   /* Don't need to walk if we're not going to be emitting
   15951              :      any diagnostics anyway.  */
   15952          148 :   if (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   15953           21 :                                      OPT_Wexpose_global_module_tu_local))
   15954            0 :     return false;
   15955              : 
   15956          523 :   for (depset *rdep : dep->deps)
   15957          135 :     if (!rdep->is_binding () && rdep->is_tu_local (strict)
   15958          369 :         && !is_exposure_of_member_type (dep, rdep))
   15959              :       {
   15960              :         // FIXME:QOI Better location information?  We're
   15961              :         // losing, so it doesn't matter about efficiency.
   15962          118 :         tree exposed = rdep->get_entity ();
   15963          118 :         auto_diagnostic_group d;
   15964          118 :         if (strict)
   15965              :           {
   15966              :             /* Allow suppressing the warning from the point of declaration
   15967              :                of the otherwise-exposed decl, for cases we know that
   15968              :                exposures will never be 'bad'.  */
   15969           27 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (exposed),
   15970           27 :                                     OPT_Wexpose_global_module_tu_local)
   15971           45 :                 && pedwarn (DECL_SOURCE_LOCATION (decl),
   15972           18 :                             OPT_Wexpose_global_module_tu_local,
   15973              :                             "%qD exposes TU-local entity %qD", decl, exposed))
   15974              :               {
   15975           18 :                 bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15976           18 :                 gcc_checking_assert (informed);
   15977              :                 return true;
   15978              :               }
   15979              :           }
   15980              :         else
   15981              :           {
   15982           91 :             error_at (DECL_SOURCE_LOCATION (decl),
   15983              :                       "%qD exposes TU-local entity %qD", decl, exposed);
   15984           91 :             bool informed = is_tu_local_entity (exposed, /*explain=*/true);
   15985           91 :             gcc_checking_assert (informed);
   15986           91 :             if (dep->is_tu_local (/*strict=*/true))
   15987            3 :               inform (DECL_SOURCE_LOCATION (decl),
   15988              :                       "%qD is also TU-local but has been exposed elsewhere",
   15989              :                       decl);
   15990              :             return true;
   15991              :           }
   15992          118 :       }
   15993              : 
   15994              :   return false;
   15995              : }
   15996              : 
   15997              : /* Warn about a template DEP that references a TU-local entity.
   15998              : 
   15999              :    If STRICT, DEP only referenced entities from the GMF.  Returns TRUE
   16000              :    if we explained anything.  */
   16001              : 
   16002              : bool
   16003           94 : depset::hash::diagnose_template_names_tu_local (depset *dep, bool strict)
   16004              : {
   16005           94 :   tree decl = dep->get_entity ();
   16006              : 
   16007              :   /* Don't bother walking if we know we won't be emitting anything.  */
   16008           94 :   if (!warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   16009           94 :                            OPT_Wtemplate_names_tu_local)
   16010              :       /* Only warn strictly if users haven't silenced this warning here.  */
   16011          121 :       || (strict && !warning_enabled_at (DECL_SOURCE_LOCATION (decl),
   16012           27 :                                          OPT_Wexpose_global_module_tu_local)))
   16013              :     return false;
   16014              : 
   16015              :   /* Friend decls in a class body are ignored, but this is harmless:
   16016              :      it should not impact any consumers.  */
   16017           94 :   if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (decl)))
   16018              :     return false;
   16019              : 
   16020              :   /* We should now only be warning about templates.  */
   16021           76 :   gcc_checking_assert
   16022              :     (TREE_CODE (decl) == TEMPLATE_DECL
   16023              :      && VAR_OR_FUNCTION_DECL_P (DECL_TEMPLATE_RESULT (decl)));
   16024              : 
   16025              :   /* Don't warn if we've seen any explicit instantiation definitions,
   16026              :      the intent might be for importers to only use those.  */
   16027           76 :   if (template_has_explicit_inst (decl))
   16028              :     return false;
   16029              : 
   16030          268 :   for (depset *rdep : dep->deps)
   16031          134 :     if (!rdep->is_binding () && rdep->is_tu_local (strict))
   16032              :       {
   16033           67 :         tree ref = rdep->get_entity ();
   16034           67 :         auto_diagnostic_group d;
   16035           67 :         if (strict)
   16036              :           {
   16037           15 :             if (warning_enabled_at (DECL_SOURCE_LOCATION (ref),
   16038           15 :                                     OPT_Wexpose_global_module_tu_local)
   16039           21 :                 && warning_at (DECL_SOURCE_LOCATION (decl),
   16040            6 :                                OPT_Wtemplate_names_tu_local,
   16041              :                                "%qD refers to TU-local entity %qD, which may "
   16042              :                                "cause issues when instantiating in other TUs",
   16043              :                                decl, ref))
   16044              :               {
   16045            6 :                 is_tu_local_entity (ref, /*explain=*/true);
   16046            6 :                 return true;
   16047              :               }
   16048              :           }
   16049           52 :         else if (warning_at (DECL_SOURCE_LOCATION (decl),
   16050           52 :                              OPT_Wtemplate_names_tu_local,
   16051              :                              "%qD refers to TU-local entity %qD and cannot "
   16052              :                              "be instantiated in other TUs", decl, ref))
   16053              :           {
   16054           52 :             is_tu_local_entity (ref, /*explain=*/true);
   16055           52 :             return true;
   16056              :           }
   16057           67 :       }
   16058              : 
   16059              :   return false;
   16060              : }
   16061              : 
   16062              : /* Sort the bindings, issue errors about bad internal refs.  */
   16063              : 
   16064              : bool
   16065         2844 : depset::hash::finalize_dependencies ()
   16066              : {
   16067         2844 :   bool ok = true;
   16068      2117891 :   for (depset *dep : *this)
   16069              :     {
   16070      2115047 :       if (dep->is_binding ())
   16071              :         {
   16072              :           /* Keep the containing namespace dep first.  */
   16073       164071 :           gcc_checking_assert (dep->deps.length () > 1
   16074              :                                && (dep->deps[0]->get_entity_kind ()
   16075              :                                    == EK_NAMESPACE)
   16076              :                                && (dep->deps[0]->get_entity ()
   16077              :                                    == dep->get_entity ()));
   16078       164071 :           if (dep->deps.length () > 2)
   16079        16693 :             gcc_qsort (&dep->deps[1], dep->deps.length () - 1,
   16080              :                        sizeof (dep->deps[1]), binding_cmp);
   16081              : 
   16082              :           /* Bindings shouldn't refer to imported entities.  */
   16083       164071 :           if (CHECKING_P)
   16084       888544 :             for (depset *entity : dep->deps)
   16085       396331 :               gcc_checking_assert (!entity->is_import ());
   16086       164071 :           continue;
   16087       164071 :         }
   16088              : 
   16089              :       /* Otherwise, we'll check for bad internal refs.
   16090              :          Don't complain about any references from TU-local entities.  */
   16091      1950976 :       if (dep->is_tu_local ())
   16092          264 :         continue;
   16093              : 
   16094              :       /* We already complained about usings of non-external entities in
   16095              :          check_can_export_using_decl, don't do it again here.  */
   16096      1950712 :       if (dep->get_entity_kind () == EK_USING)
   16097        41586 :         continue;
   16098              : 
   16099      1909126 :       if (dep->is_exposure ())
   16100              :         {
   16101          106 :           bool explained = diagnose_bad_internal_ref (dep);
   16102              : 
   16103              :           /* A TU-local variable will always be considered an exposure,
   16104              :              so we don't have to worry about strict-only handling.  */
   16105          106 :           tree decl = dep->get_entity ();
   16106          106 :           if (!explained
   16107           15 :               && VAR_P (decl)
   16108          121 :               && (DECL_DECLARED_CONSTEXPR_P (decl)
   16109            6 :                   || DECL_INLINE_VAR_P (decl)))
   16110              :             {
   16111           15 :               auto_diagnostic_group d;
   16112           15 :               if (DECL_DECLARED_CONSTEXPR_P (decl))
   16113            9 :                 error_at (DECL_SOURCE_LOCATION (decl),
   16114              :                           "%qD is declared %<constexpr%> and is initialized to "
   16115              :                           "a TU-local value", decl);
   16116              :               else
   16117              :                 {
   16118              :                   /* This can only occur with references.  */
   16119            6 :                   gcc_checking_assert (TYPE_REF_P (TREE_TYPE (decl)));
   16120            6 :                   error_at (DECL_SOURCE_LOCATION (decl),
   16121              :                             "%qD is a reference declared %<inline%> and is "
   16122              :                             "constant-initialized to a TU-local value", decl);
   16123              :                 }
   16124           15 :               bool informed = is_tu_local_value (decl, DECL_INITIAL (decl),
   16125              :                                                  /*explain=*/true);
   16126           15 :               gcc_checking_assert (informed);
   16127           15 :               explained = true;
   16128           15 :             }
   16129              : 
   16130              :           /* We should have emitted an error above, unless the warning was
   16131              :              silenced.  */
   16132          106 :           gcc_checking_assert (explained);
   16133          106 :           ok = false;
   16134          106 :           continue;
   16135          106 :         }
   16136              : 
   16137              :       /* In all other cases, we're just warning (rather than erroring).
   16138              :          We don't want to do too much warning, so let's just bail after
   16139              :          the first warning we successfully emit.  */
   16140      1909038 :       if (warn_expose_global_module_tu_local
   16141      1909020 :           && !dep->is_tu_local (/*strict=*/true)
   16142      1908978 :           && dep->is_exposure (/*strict=*/true)
   16143      1909041 :           && diagnose_bad_internal_ref (dep, /*strict=*/true))
   16144           18 :         continue;
   16145              : 
   16146      1909054 :       if (warn_template_names_tu_local
   16147      1909017 :           && dep->refs_tu_local ()
   16148      1909069 :           && diagnose_template_names_tu_local (dep))
   16149           52 :         continue;
   16150              : 
   16151      1908950 :       if (warn_template_names_tu_local
   16152       272863 :           && warn_expose_global_module_tu_local
   16153       272863 :           && !dep->is_tu_local (/*strict=*/true)
   16154       272839 :           && dep->refs_tu_local (/*strict=*/true)
   16155           30 :           && !dep->is_exposure (/*strict=*/true)
   16156      1908977 :           && diagnose_template_names_tu_local (dep, /*strict=*/true))
   16157              :         continue;
   16158              :     }
   16159              : 
   16160         2844 :   return ok;
   16161              : }
   16162              : 
   16163              : /* Core of TARJAN's algorithm to find Strongly Connected Components
   16164              :    within a graph.  See https://en.wikipedia.org/wiki/
   16165              :    Tarjan%27s_strongly_connected_components_algorithm for details.
   16166              : 
   16167              :    We use depset::section as lowlink.  Completed nodes have
   16168              :    depset::cluster containing the cluster number, with the top
   16169              :    bit set.
   16170              : 
   16171              :    A useful property is that the output vector is a reverse
   16172              :    topological sort of the resulting DAG.  In our case that means
   16173              :    dependent SCCs are found before their dependers.  We make use of
   16174              :    that property.  */
   16175              : 
   16176              : void
   16177      2871782 : depset::tarjan::connect (depset *v)
   16178              : {
   16179      2871782 :   gcc_checking_assert (v->is_binding ()
   16180              :                        || !(v->is_tu_local ()
   16181              :                             || v->is_unreached ()
   16182              :                             || v->is_import ()));
   16183              : 
   16184      2871782 :   v->cluster = v->section = ++index;
   16185      2871782 :   stack.safe_push (v);
   16186              : 
   16187              :   /* Walk all our dependencies, ignore a first marked slot  */
   16188     25437715 :   for (unsigned ix = v->is_special (); ix != v->deps.length (); ix++)
   16189              :     {
   16190      9852793 :       depset *dep = v->deps[ix];
   16191              : 
   16192      9852793 :       if (dep->is_binding ()
   16193     19473631 :           || !(dep->is_import () || dep->is_tu_local ()))
   16194              :         {
   16195      9848049 :           unsigned lwm = dep->cluster;
   16196              : 
   16197      9848049 :           if (!dep->cluster)
   16198              :             {
   16199              :               /* A new node.  Connect it.  */
   16200      1609398 :               connect (dep);
   16201      1609398 :               lwm = dep->section;
   16202              :             }
   16203              : 
   16204      9848049 :           if (dep->section && v->section > lwm)
   16205      1532668 :             v->section = lwm;
   16206              :         }
   16207              :     }
   16208              : 
   16209      2871782 :   if (v->section == v->cluster)
   16210              :     {
   16211              :       /* Root of a new SCC.  Push all the members onto the result list. */
   16212              :       unsigned num = v->cluster;
   16213      2871782 :       depset *p;
   16214      2871782 :       do
   16215              :         {
   16216      2871782 :           p = stack.pop ();
   16217      2871782 :           p->cluster = num;
   16218      2871782 :           p->section = 0;
   16219      2871782 :           result.quick_push (p);
   16220              :         }
   16221      2871782 :       while (p != v);
   16222              :     }
   16223      2871782 : }
   16224              : 
   16225              : /* Compare two depsets.  The specific ordering is unimportant, we're
   16226              :    just trying to get consistency.  */
   16227              : 
   16228              : static int
   16229    138857210 : depset_cmp (const void *a_, const void *b_)
   16230              : {
   16231    138857210 :   depset *a = *(depset *const *)a_;
   16232    138857210 :   depset *b = *(depset *const *)b_;
   16233              : 
   16234    138857210 :   depset::entity_kind a_kind = a->get_entity_kind ();
   16235    138857210 :   depset::entity_kind b_kind = b->get_entity_kind ();
   16236              : 
   16237    138857210 :   if  (a_kind != b_kind)
   16238              :     /* Different entity kinds, order by that.  */
   16239      4912060 :     return a_kind < b_kind ? -1 : +1;
   16240              : 
   16241    133945150 :   tree a_decl = a->get_entity ();
   16242    133945150 :   tree b_decl = b->get_entity ();
   16243    133945150 :   if (a_kind == depset::EK_USING)
   16244              :     {
   16245              :       /* If one is a using, the other must be too.  */
   16246      2400814 :       a_decl = OVL_FUNCTION (a_decl);
   16247      2400814 :       b_decl = OVL_FUNCTION (b_decl);
   16248              :     }
   16249              : 
   16250    133945150 :   if (a_decl != b_decl)
   16251              :     /* Different entities, order by their UID.  */
   16252    125875995 :     return DECL_UID (a_decl) < DECL_UID (b_decl) ? -1 : +1;
   16253              : 
   16254      8069155 :   if (a_kind == depset::EK_BINDING)
   16255              :     {
   16256              :       /* Both are bindings.  Order by identifier hash.  */
   16257      8065871 :       gcc_checking_assert (a->get_name () != b->get_name ());
   16258      8065871 :       hashval_t ah = IDENTIFIER_HASH_VALUE (a->get_name ());
   16259      8065871 :       hashval_t bh = IDENTIFIER_HASH_VALUE (b->get_name ());
   16260      8065871 :       return (ah == bh ? 0 : ah < bh ? -1 : +1);
   16261              :     }
   16262              : 
   16263              :   /* They are the same decl.  This can happen with two using decls
   16264              :      pointing to the same target.  The best we can aim for is
   16265              :      consistently telling qsort how to order them.  Hopefully we'll
   16266              :      never have to debug a case that depends on this.  Oh, who am I
   16267              :      kidding?  Good luck.  */
   16268         3284 :   gcc_checking_assert (a_kind == depset::EK_USING);
   16269              : 
   16270              :   /* Order by depset address.  Not the best, but it is something.  */
   16271         3284 :   return a < b ? -1 : +1;
   16272              : }
   16273              : 
   16274              : /* Sort the clusters in SCC such that those that depend on one another
   16275              :    are placed later.   */
   16276              : 
   16277              : // FIXME: I am not convinced this is needed and, if needed,
   16278              : // sufficient.  We emit the decls in this order but that emission
   16279              : // could walk into later decls (from the body of the decl, or default
   16280              : // arg-like things).  Why doesn't that walk do the right thing?  And
   16281              : // if it DTRT why do we need to sort here -- won't things naturally
   16282              : // work?  I think part of the issue is that when we're going to refer
   16283              : // to an entity by name, and that entity is in the same cluster as us,
   16284              : // we need to actually walk that entity, if we've not already walked
   16285              : // it.
   16286              : static void
   16287       337683 : sort_cluster (depset::hash *original, depset *scc[], unsigned size)
   16288              : {
   16289       337683 :   depset::hash table (size, original);
   16290              : 
   16291       337683 :   dump.indent ();
   16292              : 
   16293              :   /* Place bindings last, usings before that.  It's not strictly
   16294              :      necessary, but it does make things neater.  Says Mr OCD.  */
   16295       337683 :   unsigned bind_lwm = size;
   16296       337683 :   unsigned use_lwm = size;
   16297      1873622 :   for (unsigned ix = 0; ix != use_lwm;)
   16298              :     {
   16299      1535939 :       depset *dep = scc[ix];
   16300      1535939 :       switch (dep->get_entity_kind ())
   16301              :         {
   16302       163814 :         case depset::EK_BINDING:
   16303              :           /* Move to end.  No increment.  Notice this could be moving
   16304              :              a using decl, which we'll then move again.  */
   16305       163814 :           if (--bind_lwm != ix)
   16306              :             {
   16307        94895 :               scc[ix] = scc[bind_lwm];
   16308        94895 :               scc[bind_lwm] = dep;
   16309              :             }
   16310       163814 :           if (use_lwm > bind_lwm)
   16311              :             {
   16312       130467 :               use_lwm--;
   16313       130467 :               break;
   16314              :             }
   16315              :           /* We must have copied a using or TU-local, so move it too.  */
   16316        33347 :           dep = scc[ix];
   16317        33347 :           gcc_checking_assert
   16318              :             (dep->get_entity_kind () == depset::EK_USING
   16319              :              || dep->get_entity_kind () == depset::EK_TU_LOCAL);
   16320              :           /* FALLTHROUGH  */
   16321              : 
   16322        74930 :         case depset::EK_USING:
   16323        74930 :         case depset::EK_TU_LOCAL:
   16324        74930 :           if (--use_lwm != ix)
   16325              :             {
   16326        58542 :               scc[ix] = scc[use_lwm];
   16327        58542 :               scc[use_lwm] = dep;
   16328              :             }
   16329              :           break;
   16330              : 
   16331      1330542 :         case depset::EK_DECL:
   16332      1330542 :         case depset::EK_SPECIALIZATION:
   16333      1330542 :         case depset::EK_PARTIAL:
   16334      1330542 :           table.add_mergeable (dep);
   16335      1330542 :           ix++;
   16336      1330542 :           break;
   16337              : 
   16338            0 :         default:
   16339            0 :           gcc_unreachable ();
   16340              :         }
   16341              :     }
   16342              : 
   16343       337683 :   gcc_checking_assert (use_lwm <= bind_lwm);
   16344       337971 :   dump (dumper::MERGE) && dump ("Ordering %u/%u depsets", use_lwm, size);
   16345              : 
   16346       337683 :   table.find_dependencies (nullptr);
   16347              : 
   16348       337683 :   auto_vec<depset *> order = table.connect ();
   16349       675366 :   gcc_checking_assert (order.length () == use_lwm);
   16350              : 
   16351              :   /* Now rewrite entries [0,lwm), in the dependency order we
   16352              :      discovered.  Usually each entity is in its own cluster.  Rarely,
   16353              :      we can get multi-entity clusters, in which case all but one must
   16354              :      only be reached from within the cluster.  This happens for
   16355              :      something like:
   16356              : 
   16357              :      template<typename T>
   16358              :      auto Foo (const T &arg) -> TPL<decltype (arg)>;
   16359              : 
   16360              :      The instantiation of TPL will be in the specialization table, and
   16361              :      refer to Foo via arg.  But we can only get to that specialization
   16362              :      from Foo's declaration, so we only need to treat Foo as mergeable
   16363              :      (We'll do structural comparison of TPL<decltype (arg)>).
   16364              : 
   16365              :      We approximate finding the single cluster entry dep by checking for
   16366              :      entities recursively depending on a dep first seen when streaming
   16367              :      its own merge key; the first dep we see in such a cluster should be
   16368              :      the first one streamed.  */
   16369              :   unsigned entry_pos = ~0u;
   16370              :   unsigned cluster = ~0u;
   16371      3336450 :   for (unsigned ix = 0; ix != order.length (); ix++)
   16372              :     {
   16373      1330542 :       gcc_checking_assert (order[ix]->is_special ());
   16374      1330542 :       bool tight = order[ix]->cluster == cluster;
   16375      1330542 :       depset *dep = order[ix]->deps[0];
   16376      1331535 :       dump (dumper::MERGE)
   16377         1983 :         && dump ("Mergeable %u is %N%s%s", ix, dep->get_entity (),
   16378          993 :                  tight ? " (tight)" : "", dep->is_entry () ? " (entry)" : "");
   16379      1330542 :       scc[ix] = dep;
   16380      1330542 :       if (tight)
   16381              :         {
   16382          134 :           gcc_checking_assert (dep->is_maybe_recursive ());
   16383          134 :           if (dep->is_entry ())
   16384              :             {
   16385              :               /* There should only be one entry dep in a cluster.  */
   16386            9 :               gcc_checking_assert (!scc[entry_pos]->is_entry ());
   16387            9 :               gcc_checking_assert (scc[entry_pos]->is_maybe_recursive ());
   16388            9 :               scc[ix] = scc[entry_pos];
   16389            9 :               scc[entry_pos] = dep;
   16390              :             }
   16391              :         }
   16392              :       else
   16393              :         entry_pos = ix;
   16394      1330542 :       cluster = order[ix]->cluster;
   16395              :     }
   16396              : 
   16397       337971 :   dump (dumper::MERGE) && dump ("Ordered %u keys", order.length ());
   16398       337683 :   dump.outdent ();
   16399       337683 : }
   16400              : 
   16401              : /* Reduce graph to SCCS clusters.  SCCS will be populated with the
   16402              :    depsets in dependency order.  Each depset's CLUSTER field contains
   16403              :    its cluster number.  Each SCC has a unique cluster number, and are
   16404              :    contiguous in SCCS. Cluster numbers are otherwise arbitrary.  */
   16405              : 
   16406              : vec<depset *>
   16407       340498 : depset::hash::connect ()
   16408              : {
   16409       340498 :   tarjan connector (size ());
   16410       340498 :   vec<depset *> deps;
   16411       340498 :   deps.create (size ());
   16412      3785164 :   for (depset *item : *this)
   16413              :     {
   16414      3444666 :       entity_kind kind = item->get_entity_kind ();
   16415      3280852 :       if (kind == EK_BINDING
   16416      3280852 :           || !(kind == EK_REDIRECT
   16417      3253300 :                || item->is_tu_local ()
   16418      3253169 :                || item->is_unreached ()
   16419      2712226 :                || item->is_import ()))
   16420      2871782 :         deps.quick_push (item);
   16421              :     }
   16422              : 
   16423              :   /* Iteration over the hash table is an unspecified ordering.  While
   16424              :      that has advantages, it causes 2 problems.  Firstly repeatable
   16425              :      builds are tricky.  Secondly creating testcases that check
   16426              :      dependencies are correct by making sure a bad ordering would
   16427              :      happen if that was wrong.  */
   16428       340498 :   deps.qsort (depset_cmp);
   16429              : 
   16430      3552778 :   while (deps.length ())
   16431              :     {
   16432      2871782 :       depset *v = deps.pop ();
   16433      2871782 :       dump (dumper::CLUSTER) &&
   16434         1800 :         (v->is_binding ()
   16435          210 :          ? dump ("Connecting binding %P", v->get_entity (), v->get_name ())
   16436         1590 :          : dump ("Connecting %s %s %C:%N",
   16437         1590 :                  is_key_order () ? "key-order"
   16438          870 :                  : !v->has_defn () ? "declaration" : "definition",
   16439         1590 :                  v->entity_kind_name (), TREE_CODE (v->get_entity ()),
   16440              :                  v->get_entity ()));
   16441      2871782 :       if (!v->cluster)
   16442      1262384 :         connector.connect (v);
   16443              :     }
   16444              : 
   16445       340498 :   deps.release ();
   16446       680996 :   return connector.result;
   16447       340498 : }
   16448              : 
   16449              : /* Initialize location spans.  */
   16450              : 
   16451              : void
   16452         5036 : loc_spans::init (const line_maps *lmaps, const line_map_ordinary *map)
   16453              : {
   16454         5036 :   gcc_checking_assert (!init_p ());
   16455         5036 :   spans = new vec<span> ();
   16456         5036 :   spans->reserve (20);
   16457              : 
   16458         5036 :   span interval;
   16459         5036 :   interval.ordinary.first = 0;
   16460         5036 :   interval.macro.second = MAX_LOCATION_T + 1;
   16461         5036 :   interval.ordinary_delta = interval.macro_delta = 0;
   16462              : 
   16463              :   /* A span for reserved fixed locs.  */
   16464         5036 :   interval.ordinary.second
   16465         5036 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   16466         5036 :   interval.macro.first = interval.macro.second;
   16467         5036 :   dump (dumper::LOCATION)
   16468           42 :     && dump ("Fixed span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16469              :              interval.ordinary.first, interval.ordinary.second,
   16470              :              interval.macro.first, interval.macro.second);
   16471         5036 :   spans->quick_push (interval);
   16472              : 
   16473              :   /* A span for command line & forced headers.  */
   16474         5036 :   interval.ordinary.first = interval.ordinary.second;
   16475         5036 :   interval.macro.second = interval.macro.first;
   16476         5036 :   if (map)
   16477              :     {
   16478         5030 :       interval.ordinary.second = map->start_location;
   16479         5030 :       interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (lmaps);
   16480              :     }
   16481         5036 :   dump (dumper::LOCATION)
   16482           21 :     && dump ("Pre span %u ordinary:[%K,%K) macro:[%K,%K)", spans->length (),
   16483              :              interval.ordinary.first, interval.ordinary.second,
   16484              :              interval.macro.first, interval.macro.second);
   16485         5036 :   spans->quick_push (interval);
   16486              : 
   16487              :   /* Start an interval for the main file.  */
   16488         5036 :   interval.ordinary.first = interval.ordinary.second;
   16489         5036 :   interval.macro.second = interval.macro.first;
   16490         5036 :   dump (dumper::LOCATION)
   16491           21 :     && dump ("Main span %u ordinary:[%K,*) macro:[*,%K)", spans->length (),
   16492              :              interval.ordinary.first, interval.macro.second);
   16493         5036 :   spans->quick_push (interval);
   16494         5036 : }
   16495              : 
   16496              : /* Reopen the span, if we want the about-to-be-inserted set of maps to
   16497              :    be propagated in our own location table.  I.e. we are the primary
   16498              :    interface and we're importing a partition.  */
   16499              : 
   16500              : bool
   16501         3123 : loc_spans::maybe_propagate (module_state *import, location_t hwm)
   16502              : {
   16503         3123 :   bool opened = (module_interface_p () && !module_partition_p ()
   16504         3591 :                  && import->is_partition ());
   16505          178 :   if (opened)
   16506          178 :     open (hwm);
   16507         3123 :   return opened;
   16508              : }
   16509              : 
   16510              : /* Open a new linemap interval.  The just-created ordinary map is the
   16511              :    first map of the interval.  */
   16512              : 
   16513              : void
   16514         1087 : loc_spans::open (location_t hwm)
   16515              : {
   16516         1087 :   span interval;
   16517         1087 :   interval.ordinary.first = interval.ordinary.second = hwm;
   16518         2174 :   interval.macro.first = interval.macro.second
   16519         1087 :     = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16520         1087 :   interval.ordinary_delta = interval.macro_delta = 0;
   16521         1087 :   dump (dumper::LOCATION)
   16522            0 :     && dump ("Opening span %u ordinary:[%K,... macro:...,%K)",
   16523            0 :              spans->length (), interval.ordinary.first,
   16524              :              interval.macro.second);
   16525         1087 :   if (spans->length ())
   16526              :     {
   16527              :       /* No overlapping!  */
   16528         1087 :       auto &last = spans->last ();
   16529         1087 :       gcc_checking_assert (interval.ordinary.first >= last.ordinary.second);
   16530         1087 :       gcc_checking_assert (interval.macro.second <= last.macro.first);
   16531              :     }
   16532         1087 :   spans->safe_push (interval);
   16533         1087 : }
   16534              : 
   16535              : /* Close out the current linemap interval.  The last maps are within
   16536              :    the interval.  */
   16537              : 
   16538              : void
   16539         6120 : loc_spans::close ()
   16540              : {
   16541         6120 :   span &interval = spans->last ();
   16542              : 
   16543         6120 :   interval.ordinary.second
   16544         6120 :     = ((line_table->highest_location
   16545         6120 :         + (loc_one << line_table->default_range_bits))
   16546         6120 :        & ~((loc_one << line_table->default_range_bits) - 1));
   16547         6120 :   interval.macro.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   16548         6120 :   dump (dumper::LOCATION)
   16549           21 :     && dump ("Closing span %u ordinary:[%K,%K) macro:[%K,%K)",
   16550           21 :              spans->length () - 1,
   16551              :              interval.ordinary.first,interval.ordinary.second,
   16552              :              interval.macro.first, interval.macro.second);
   16553         6120 : }
   16554              : 
   16555              : /* Given an ordinary location LOC, return the lmap_interval it resides
   16556              :    in.  NULL if it is not in an interval.  */
   16557              : 
   16558              : const loc_spans::span *
   16559     44378755 : loc_spans::ordinary (location_t loc)
   16560              : {
   16561     44378755 :   unsigned len = spans->length ();
   16562     44378755 :   unsigned pos = 0;
   16563    133014189 :   while (len)
   16564              :     {
   16565     88625645 :       unsigned half = len / 2;
   16566     88625645 :       const span &probe = (*spans)[pos + half];
   16567     88625645 :       if (loc < probe.ordinary.first)
   16568              :         len = half;
   16569     88618140 :       else if (loc < probe.ordinary.second)
   16570              :         return &probe;
   16571              :       else
   16572              :         {
   16573     44249174 :           pos += half + 1;
   16574     44249174 :           len = len - (half + 1);
   16575              :         }
   16576              :     }
   16577              :   return NULL;
   16578              : }
   16579              : 
   16580              : /* Likewise, given a macro location LOC, return the lmap interval it
   16581              :    resides in.   */
   16582              : 
   16583              : const loc_spans::span *
   16584      3337502 : loc_spans::macro (location_t loc)
   16585              : {
   16586      3337502 :   unsigned len = spans->length ();
   16587      3337502 :   unsigned pos = 0;
   16588     10009458 :   while (len)
   16589              :     {
   16590      6671944 :       unsigned half = len / 2;
   16591      6671944 :       const span &probe = (*spans)[pos + half];
   16592      6671944 :       if (loc >= probe.macro.second)
   16593              :         len = half;
   16594      6671944 :       else if (loc >= probe.macro.first)
   16595              :         return &probe;
   16596              :       else
   16597              :         {
   16598      3334454 :           pos += half + 1;
   16599      3334454 :           len = len - (half + 1);
   16600              :         }
   16601              :     }
   16602              :   return NULL;
   16603              : }
   16604              : 
   16605              : /* Return the ordinary location closest to FROM.  */
   16606              : 
   16607              : static location_t
   16608         7003 : ordinary_loc_of (line_maps *lmaps, location_t from)
   16609              : {
   16610        14009 :   while (!IS_ORDINARY_LOC (from))
   16611              :     {
   16612            3 :       if (IS_ADHOC_LOC (from))
   16613            3 :         from = get_location_from_adhoc_loc (lmaps, from);
   16614            3 :       if (from >= LINEMAPS_MACRO_LOWEST_LOCATION (lmaps))
   16615              :         {
   16616              :           /* Find the ordinary location nearest FROM.  */
   16617            0 :           const line_map *map = linemap_lookup (lmaps, from);
   16618            0 :           const line_map_macro *mac_map = linemap_check_macro (map);
   16619            0 :           from = mac_map->get_expansion_point_location ();
   16620              :         }
   16621              :     }
   16622         7003 :   return from;
   16623              : }
   16624              : 
   16625              : static module_state **
   16626        12597 : get_module_slot (tree name, module_state *parent, bool partition, bool insert)
   16627              : {
   16628        12597 :   module_state_hash::compare_type ct (name, uintptr_t (parent) | partition);
   16629        12597 :   hashval_t hv = module_state_hash::hash (ct);
   16630              : 
   16631        12597 :   return modules_hash->find_slot_with_hash (ct, hv, insert ? INSERT : NO_INSERT);
   16632              : }
   16633              : 
   16634              : static module_state *
   16635       173469 : get_primary (module_state *parent)
   16636              : {
   16637       177592 :   while (parent->is_partition ())
   16638          691 :     parent = parent->parent;
   16639              : 
   16640       176901 :   if (!parent->name)
   16641              :     // Implementation unit has null name
   16642       109105 :     parent = parent->parent;
   16643              : 
   16644       164372 :   return parent;
   16645              : }
   16646              : 
   16647              : /* Find or create module NAME & PARENT in the hash table.  */
   16648              : 
   16649              : module_state *
   16650        12597 : get_module (tree name, module_state *parent, bool partition)
   16651              : {
   16652              :   /* We might be given an empty NAME if preprocessing fails to handle
   16653              :      a header-name token.  */
   16654        12597 :   if (name && TREE_CODE (name) == STRING_CST
   16655        15491 :       && TREE_STRING_LENGTH (name) == 0)
   16656              :     return nullptr;
   16657              : 
   16658        12597 :   if (partition)
   16659              :     {
   16660         1123 :       if (!parent)
   16661          238 :         parent = get_primary (this_module ());
   16662              : 
   16663         1123 :       if (!parent->is_partition () && !parent->flatname)
   16664          265 :         parent->set_flatname ();
   16665              :     }
   16666              : 
   16667        12597 :   module_state **slot = get_module_slot (name, parent, partition, true);
   16668        12597 :   module_state *state = *slot;
   16669        12597 :   if (!state)
   16670              :     {
   16671         6817 :       state = (new (ggc_alloc<module_state> ())
   16672         6817 :                module_state (name, parent, partition));
   16673         6817 :       *slot = state;
   16674              :     }
   16675              :   return state;
   16676              : }
   16677              : 
   16678              : /* Process string name PTR into a module_state.  */
   16679              : 
   16680              : static module_state *
   16681          454 : get_module (const char *ptr)
   16682              : {
   16683              :   /* On DOS based file systems, there is an ambiguity with A:B which can be
   16684              :      interpreted as a module Module:Partition or Drive:PATH.  Interpret strings
   16685              :      which clearly starts as pathnames as header-names and everything else is
   16686              :      treated as a (possibly malformed) named moduled.  */
   16687          454 :   if (IS_DIR_SEPARATOR (ptr[ptr[0] == '.']) // ./FOO or /FOO
   16688              : #if HAVE_DOS_BASED_FILE_SYSTEM
   16689              :       || (HAS_DRIVE_SPEC (ptr) && IS_DIR_SEPARATOR (ptr[2])) // A:/FOO
   16690              : #endif
   16691              :       || false)
   16692              :     /* A header name.  */
   16693          109 :     return get_module (build_string (strlen (ptr), ptr));
   16694              : 
   16695              :   bool partition = false;
   16696              :   module_state *mod = NULL;
   16697              : 
   16698         1357 :   for (const char *probe = ptr;; probe++)
   16699         1702 :     if (!*probe || *probe == '.' || *probe == ':')
   16700              :       {
   16701          435 :         if (probe == ptr)
   16702              :           return NULL;
   16703              : 
   16704          435 :         mod = get_module (get_identifier_with_length (ptr, probe - ptr),
   16705              :                           mod, partition);
   16706          435 :         ptr = probe;
   16707          435 :         if (*ptr == ':')
   16708              :           {
   16709           87 :             if (partition)
   16710              :               return NULL;
   16711              :             partition = true;
   16712              :           }
   16713              : 
   16714          435 :         if (!*ptr++)
   16715              :           break;
   16716              :       }
   16717         1267 :     else if (!(ISALPHA (*probe) || *probe == '_'
   16718           18 :                || (probe != ptr && ISDIGIT (*probe))))
   16719              :       return NULL;
   16720              : 
   16721              :   return mod;
   16722              : }
   16723              : 
   16724              : /* Create a new mapper connecting to OPTION.  */
   16725              : 
   16726              : module_client *
   16727         5036 : make_mapper (location_t loc, class mkdeps *deps)
   16728              : {
   16729         5036 :   timevar_start (TV_MODULE_MAPPER);
   16730         5036 :   const char *option = module_mapper_name;
   16731         5036 :   if (!option)
   16732         4988 :     option = getenv ("CXX_MODULE_MAPPER");
   16733              : 
   16734        10072 :   mapper = module_client::open_module_client
   16735         5036 :     (loc, option, deps, &set_cmi_repo,
   16736         5036 :      (save_decoded_options[0].opt_index == OPT_SPECIAL_program_name)
   16737         5036 :      && save_decoded_options[0].arg != progname
   16738              :      ? save_decoded_options[0].arg : nullptr);
   16739              : 
   16740         5036 :   timevar_stop (TV_MODULE_MAPPER);
   16741              : 
   16742         5036 :   return mapper;
   16743              : }
   16744              : 
   16745              : static unsigned lazy_snum;
   16746              : 
   16747              : static bool
   16748        11888 : recursive_lazy (unsigned snum = ~0u)
   16749              : {
   16750        11888 :   if (lazy_snum)
   16751              :     {
   16752            0 :       error_at (input_location, "recursive lazy load");
   16753            0 :       return true;
   16754              :     }
   16755              : 
   16756        11888 :   lazy_snum = snum;
   16757        11888 :   return false;
   16758              : }
   16759              : 
   16760              : /* If THIS has an interface dependency on itself, report an error and
   16761              :    return false.  */
   16762              : 
   16763              : bool
   16764         2967 : module_state::check_circular_import (location_t from)
   16765              : {
   16766         2967 :   if (this == this_module ())
   16767              :     {
   16768              :       /* Cannot import the current module.  */
   16769            9 :       auto_diagnostic_group d;
   16770            9 :       error_at (from, "module %qs depends on itself", get_flatname ());
   16771            9 :       if (!header_module_p ())
   16772            6 :         inform (loc, "module %qs declared here", get_flatname ());
   16773            9 :       return false;
   16774            9 :     }
   16775              :   return true;
   16776              : }
   16777              : 
   16778              : /* Module name substitutions.  */
   16779              : static vec<module_state *,va_heap> substs;
   16780              : 
   16781              : void
   16782         9232 : module_state::mangle (bool include_partition)
   16783              : {
   16784         9232 :   if (subst)
   16785          425 :     mangle_module_substitution (subst);
   16786              :   else
   16787              :     {
   16788         8807 :       if (parent)
   16789          888 :         parent->mangle (include_partition);
   16790         8807 :       if (include_partition || !is_partition ())
   16791              :         {
   16792              :           // Partitions are significant for global initializer
   16793              :           // functions
   16794         8601 :           bool partition = is_partition () && !parent->is_partition ();
   16795         8601 :           subst = mangle_module_component (name, partition);
   16796         8601 :           substs.safe_push (this);
   16797              :         }
   16798              :     }
   16799         9232 : }
   16800              : 
   16801              : void
   16802         8344 : mangle_module (int mod, bool include_partition)
   16803              : {
   16804         8344 :   module_state *imp = (*modules)[mod];
   16805              : 
   16806         8344 :   gcc_checking_assert (!imp->is_header ());
   16807              : 
   16808         8344 :   if (!imp->name)
   16809              :     /* Set when importing the primary module interface.  */
   16810          223 :     imp = imp->parent;
   16811              : 
   16812              :   /* Ensure this is actually a module unit.  */
   16813          223 :   gcc_checking_assert (imp);
   16814              : 
   16815         8344 :   imp->mangle (include_partition);
   16816         8344 : }
   16817              : 
   16818              : /* Clean up substitutions.  */
   16819              : void
   16820         7873 : mangle_module_fini ()
   16821              : {
   16822        16474 :   while (substs.length ())
   16823         8601 :     substs.pop ()->subst = 0;
   16824         7873 : }
   16825              : 
   16826              : /* Announce WHAT about the module.  */
   16827              : 
   16828              : void
   16829        12927 : module_state::announce (const char *what) const
   16830              : {
   16831        12927 :   if (noisy_p ())
   16832              :     {
   16833            0 :       fprintf (stderr, " %s:%s", what, get_flatname ());
   16834            0 :       fflush (stderr);
   16835              :     }
   16836        12927 : }
   16837              : 
   16838              : /* A human-readable README section.  The contents of this section to
   16839              :    not contribute to the CRC, so the contents can change per
   16840              :    compilation.  That allows us to embed CWD, hostname, build time and
   16841              :    what not.  It is a STRTAB that may be extracted with:
   16842              :      readelf -pgnu.c++.README $(module).gcm */
   16843              : 
   16844              : void
   16845         2815 : module_state::write_readme (elf_out *to, cpp_reader *reader, const char *dialect)
   16846              : {
   16847         2815 :   bytes_out readme (to);
   16848              : 
   16849         2815 :   readme.begin (false);
   16850              : 
   16851         2815 :   readme.printf ("GNU C++ %s",
   16852         2815 :                  is_header () ? "header unit"
   16853         1906 :                  : !is_partition () ? "primary interface"
   16854          208 :                  : is_interface () ? "interface partition"
   16855              :                  : "internal partition");
   16856              : 
   16857              :   /* Compiler's version.  */
   16858         2815 :   readme.printf ("compiler: %s", version_string);
   16859              : 
   16860              :   /* Module format version.  */
   16861         2815 :   verstr_t string;
   16862         2815 :   version2string (MODULE_VERSION, string);
   16863         2815 :   readme.printf ("version: %s", string);
   16864              : 
   16865              :   /* Module information.  */
   16866         2815 :   readme.printf ("module: %s", get_flatname ());
   16867         2815 :   readme.printf ("source: %s", main_input_filename);
   16868         2815 :   readme.printf ("dialect: %s", dialect);
   16869         2815 :   if (extensions)
   16870           30 :     readme.printf ("extensions: %s%s%s",
   16871              :                    extensions & SE_OPENMP ? "-fopenmp"
   16872            6 :                    : extensions & SE_OPENMP_SIMD ? "-fopenmp-simd" : "",
   16873              :                    (extensions & SE_OPENACC)
   16874            3 :                    && (extensions & (SE_OPENMP | SE_OPENMP_SIMD))
   16875              :                    ? " " : "",
   16876           12 :                    extensions & SE_OPENACC ? "-fopenacc" : "");
   16877              : 
   16878              :   /* The following fields could be expected to change between
   16879              :      otherwise identical compilations.  Consider a distributed build
   16880              :      system.  We should have a way of overriding that.  */
   16881         2815 :   if (char *cwd = getcwd (NULL, 0))
   16882              :     {
   16883         2815 :       readme.printf ("cwd: %s", cwd);
   16884         2815 :       free (cwd);
   16885              :     }
   16886         5630 :   readme.printf ("repository: %s", cmi_repo ? cmi_repo : ".");
   16887              : #if NETWORKING
   16888              :   {
   16889              :     char hostname[64];
   16890              :     if (!gethostname (hostname, sizeof (hostname)))
   16891              :       readme.printf ("host: %s", hostname);
   16892              :   }
   16893              : #endif
   16894         2815 :   {
   16895              :     /* This of course will change!  */
   16896         2815 :     time_t stampy;
   16897         2815 :     auto kind = cpp_get_date (reader, &stampy);
   16898         2815 :     if (kind != CPP_time_kind::UNKNOWN)
   16899              :       {
   16900         2815 :         struct tm *time;
   16901              : 
   16902         2815 :         time = gmtime (&stampy);
   16903         2815 :         readme.print_time ("build", time, "UTC");
   16904              : 
   16905         2815 :         if (kind == CPP_time_kind::DYNAMIC)
   16906              :           {
   16907         2815 :             time = localtime (&stampy);
   16908         2815 :             readme.print_time ("local", time,
   16909              : #if defined (__USE_MISC) || defined (__USE_BSD) /* Is there a better way?  */
   16910              :                                time->tm_zone
   16911              : #else
   16912              :                                ""
   16913              : #endif
   16914              :                                );
   16915              :           }
   16916              :       }
   16917              :   }
   16918              : 
   16919              :   /* Its direct imports.  */
   16920         3493 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   16921              :     {
   16922          678 :       module_state *state = (*modules)[ix];
   16923              : 
   16924          678 :       if (state->is_direct ())
   16925          997 :         readme.printf ("%s: %s %s", state->exported_p ? "export" : "import",
   16926              :                        state->get_flatname (), state->filename);
   16927              :     }
   16928              : 
   16929         2815 :   readme.end (to, to->name (MOD_SNAME_PFX ".README"), NULL);
   16930         2815 : }
   16931              : 
   16932              : /* Sort environment var names in reverse order.  */
   16933              : 
   16934              : static int
   16935            0 : env_var_cmp (const void *a_, const void *b_)
   16936              : {
   16937            0 :   const unsigned char *a = *(const unsigned char *const *)a_;
   16938            0 :   const unsigned char *b = *(const unsigned char *const *)b_;
   16939              : 
   16940            0 :   for (unsigned ix = 0; ; ix++)
   16941              :     {
   16942            0 :       bool a_end = !a[ix] || a[ix] == '=';
   16943            0 :       if (a[ix] == b[ix])
   16944              :         {
   16945            0 :           if (a_end)
   16946              :             break;
   16947              :         }
   16948              :       else
   16949              :         {
   16950            0 :           bool b_end = !b[ix] || b[ix] == '=';
   16951              : 
   16952            0 :           if (!a_end && !b_end)
   16953            0 :             return a[ix] < b[ix] ? +1 : -1;
   16954            0 :           if (a_end && b_end)
   16955              :             break;
   16956            0 :           return a_end ? +1 : -1;
   16957              :         }
   16958            0 :     }
   16959              : 
   16960              :   return 0;
   16961              : }
   16962              : 
   16963              : /* Write the environment. It is a STRTAB that may be extracted with:
   16964              :      readelf -pgnu.c++.ENV $(module).gcm */
   16965              : 
   16966              : void
   16967            0 : module_state::write_env (elf_out *to)
   16968              : {
   16969            0 :   vec<const char *> vars;
   16970            0 :   vars.create (20);
   16971              : 
   16972            0 :   extern char **environ;
   16973            0 :   while (const char *var = environ[vars.length ()])
   16974            0 :     vars.safe_push (var);
   16975            0 :   vars.qsort (env_var_cmp);
   16976              : 
   16977            0 :   bytes_out env (to);
   16978            0 :   env.begin (false);
   16979            0 :   while (vars.length ())
   16980            0 :     env.printf ("%s", vars.pop ());
   16981            0 :   env.end (to, to->name (MOD_SNAME_PFX ".ENV"), NULL);
   16982              : 
   16983            0 :   vars.release ();
   16984            0 : }
   16985              : 
   16986              : /* Write the direct or indirect imports.
   16987              :    u:N
   16988              :    {
   16989              :      u:index
   16990              :      s:name
   16991              :      u32:crc
   16992              :      s:filename (direct)
   16993              :      u:exported (direct)
   16994              :    } imports[N]
   16995              :  */
   16996              : 
   16997              : void
   16998          916 : module_state::write_imports (bytes_out &sec, bool direct)
   16999              : {
   17000          916 :   unsigned count = 0;
   17001              : 
   17002         1934 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   17003              :     {
   17004         1018 :       module_state *imp = (*modules)[ix];
   17005              : 
   17006         1018 :       if (imp->remap && imp->is_direct () == direct)
   17007          488 :         count++;
   17008              :     }
   17009              : 
   17010          916 :   gcc_assert (!direct || count);
   17011              : 
   17012          916 :   sec.u (count);
   17013         2850 :   for (unsigned ix = 1; ix < modules->length (); ix++)
   17014              :     {
   17015         1018 :       module_state *imp = (*modules)[ix];
   17016              : 
   17017         1018 :       if (imp->remap && imp->is_direct () == direct)
   17018              :         {
   17019          650 :           dump () && dump ("Writing %simport:%u->%u %M (crc=%x)",
   17020              :                            !direct ? "indirect "
   17021           81 :                            : imp->exported_p ? "exported " : "",
   17022              :                            ix, imp->remap, imp, imp->crc);
   17023          488 :           sec.u (imp->remap);
   17024          488 :           sec.str (imp->get_flatname ());
   17025          488 :           sec.u32 (imp->crc);
   17026          488 :           if (direct)
   17027              :             {
   17028          480 :               write_location (sec, imp->imported_from ());
   17029          480 :               sec.str (imp->filename);
   17030          480 :               int exportedness = 0;
   17031          480 :               if (imp->exported_p)
   17032              :                 exportedness = +1;
   17033          286 :               else if (!imp->is_purview_direct ())
   17034           12 :                 exportedness = -1;
   17035          480 :               sec.i (exportedness);
   17036              :             }
   17037              :         }
   17038              :     }
   17039          916 : }
   17040              : 
   17041              : /* READER, LMAPS  != NULL == direct imports,
   17042              :    == NUL == indirect imports.  */
   17043              : 
   17044              : unsigned
   17045          776 : module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
   17046              : {
   17047          776 :   unsigned count = sec.u ();
   17048          776 :   unsigned loaded = 0;
   17049              : 
   17050         1961 :   while (count--)
   17051              :     {
   17052          409 :       unsigned ix = sec.u ();
   17053          409 :       if (ix >= slurp->remap->length () || !ix || (*slurp->remap)[ix])
   17054              :         {
   17055            0 :           sec.set_overrun ();
   17056            0 :           break;
   17057              :         }
   17058              : 
   17059          409 :       const char *name = sec.str (NULL);
   17060          409 :       module_state *imp = get_module (name);
   17061          409 :       unsigned crc = sec.u32 ();
   17062          409 :       int exportedness = 0;
   17063              : 
   17064              :       /* If the import is a partition, it must be the same primary
   17065              :          module as this TU.  */
   17066          409 :       if (imp && imp->is_partition () &&
   17067              :           (!named_module_p ()
   17068          144 :            || (get_primary (this_module ()) != get_primary (imp))))
   17069              :         imp = NULL;
   17070              : 
   17071          409 :       if (!imp)
   17072            0 :         sec.set_overrun ();
   17073          409 :       if (sec.get_overrun ())
   17074              :         break;
   17075              : 
   17076          409 :       if (lmaps)
   17077              :         {
   17078              :           /* A direct import, maybe load it.  */
   17079          406 :           location_t floc = read_location (sec);
   17080          406 :           const char *fname = sec.str (NULL);
   17081          406 :           exportedness = sec.i ();
   17082              : 
   17083          406 :           if (sec.get_overrun ())
   17084              :             break;
   17085              : 
   17086          406 :           if (!imp->check_circular_import (floc))
   17087            3 :             continue;
   17088              : 
   17089          403 :           if (imp->loadedness == ML_NONE)
   17090              :             {
   17091          316 :               imp->loc = floc;
   17092          316 :               imp->crc = crc;
   17093          316 :               if (!imp->get_flatname ())
   17094          274 :                 imp->set_flatname ();
   17095              : 
   17096          316 :               unsigned n = dump.push (imp);
   17097              : 
   17098          316 :               if (!imp->filename && fname)
   17099          274 :                 imp->filename = xstrdup (fname);
   17100              : 
   17101          316 :               if (imp->is_partition ())
   17102           33 :                 dump () && dump ("Importing elided partition %M", imp);
   17103              : 
   17104          316 :               if (!imp->do_import (reader, false))
   17105            3 :                 imp = NULL;
   17106          316 :               dump.pop (n);
   17107          316 :               if (!imp)
   17108            3 :                 continue;
   17109              :             }
   17110              : 
   17111          400 :           if (is_partition ())
   17112              :             {
   17113           69 :               if (!imp->is_direct () && !imp->is_partition_direct ())
   17114              :                 {
   17115           30 :                   imp->directness = MD_PARTITION_DIRECT;
   17116           30 :                   linemap_module_reparent (line_table, imp->loc, floc);
   17117              :                 }
   17118           69 :               if (exportedness > 0)
   17119            6 :                 imp->exported_p = true;
   17120              :             }
   17121              :         }
   17122              :       else
   17123              :         {
   17124              :           /* An indirect import, find it, it should already be here.  */
   17125            3 :           if (imp->loadedness == ML_NONE)
   17126              :             {
   17127            0 :               error_at (loc, "indirect import %qs is not already loaded", name);
   17128            0 :               continue;
   17129              :             }
   17130              :         }
   17131              : 
   17132          403 :       if (imp->crc != crc)
   17133            0 :         error_at (loc, "import %qs has CRC mismatch", imp->get_flatname ());
   17134              : 
   17135          403 :       (*slurp->remap)[ix] = (imp->mod << 1) | (lmaps != NULL);
   17136              : 
   17137          403 :       if (lmaps && exportedness >= 0)
   17138          388 :         set_import (imp, bool (exportedness));
   17139          583 :       dump () && dump ("Found %simport:%u %M->%u", !lmaps ? "indirect "
   17140           90 :                        : exportedness > 0 ? "exported "
   17141           51 :                        : exportedness < 0 ? "gmf" : "", ix, imp,
   17142              :                        imp->mod);
   17143          403 :       loaded++;
   17144              :     }
   17145              : 
   17146          776 :   return loaded;
   17147              : }
   17148              : 
   17149              : /* Write the import table to MOD_SNAME_PFX.imp.  */
   17150              : 
   17151              : void
   17152          458 : module_state::write_imports (elf_out *to, unsigned *crc_ptr)
   17153              : {
   17154          536 :   dump () && dump ("Writing imports");
   17155          458 :   dump.indent ();
   17156              : 
   17157          458 :   bytes_out sec (to);
   17158          458 :   sec.begin ();
   17159              : 
   17160          458 :   write_imports (sec, true);
   17161          458 :   write_imports (sec, false);
   17162              : 
   17163          458 :   sec.end (to, to->name (MOD_SNAME_PFX ".imp"), crc_ptr);
   17164          458 :   dump.outdent ();
   17165          458 : }
   17166              : 
   17167              : bool
   17168          388 : module_state::read_imports (cpp_reader *reader, line_maps *lmaps)
   17169              : {
   17170          388 :   bytes_in sec;
   17171              : 
   17172          388 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".imp"))
   17173              :     return false;
   17174              : 
   17175          475 :   dump () && dump ("Reading %u imports", slurp->remap->length () - 1);
   17176          388 :   dump.indent ();
   17177              : 
   17178              :   /* Read the imports.  */
   17179          388 :   unsigned direct = read_imports (sec, reader, lmaps);
   17180          388 :   unsigned indirect = read_imports (sec, NULL, NULL);
   17181          388 :   if (direct + indirect + 1 != slurp->remap->length ())
   17182            6 :     from ()->set_error (elf::E_BAD_IMPORT);
   17183              : 
   17184          388 :   dump.outdent ();
   17185          388 :   if (!sec.end (from ()))
   17186              :     return false;
   17187              :   return true;
   17188          388 : }
   17189              : 
   17190              : /* We're the primary module interface, but have partitions.  Document
   17191              :    them so that non-partition module implementation units know which
   17192              :    have already been loaded.  */
   17193              : 
   17194              : void
   17195          136 : module_state::write_partitions (elf_out *to, unsigned count, unsigned *crc_ptr)
   17196              : {
   17197          160 :   dump () && dump ("Writing %u elided partitions", count);
   17198          136 :   dump.indent ();
   17199              : 
   17200          136 :   bytes_out sec (to);
   17201          136 :   sec.begin ();
   17202              : 
   17203          486 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   17204              :     {
   17205          214 :       module_state *imp = (*modules)[ix];
   17206          214 :       if (imp->is_partition ())
   17207              :         {
   17208          229 :           dump () && dump ("Writing elided partition %M (crc=%x)",
   17209              :                            imp, imp->crc);
   17210          190 :           sec.str (imp->get_flatname ());
   17211          190 :           sec.u32 (imp->crc);
   17212          371 :           write_location (sec, imp->is_direct ()
   17213          181 :                           ? imp->imported_from () : UNKNOWN_LOCATION);
   17214          190 :           sec.str (imp->filename);
   17215              :         }
   17216              :     }
   17217              : 
   17218          136 :   sec.end (to, to->name (MOD_SNAME_PFX ".prt"), crc_ptr);
   17219          136 :   dump.outdent ();
   17220          136 : }
   17221              : 
   17222              : bool
   17223           27 : module_state::read_partitions (unsigned count)
   17224              : {
   17225           27 :   bytes_in sec;
   17226           27 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".prt"))
   17227              :     return false;
   17228              : 
   17229           33 :   dump () && dump ("Reading %u elided partitions", count);
   17230           27 :   dump.indent ();
   17231              : 
   17232           66 :   while (count--)
   17233              :     {
   17234           39 :       const char *name = sec.str (NULL);
   17235           39 :       unsigned crc = sec.u32 ();
   17236           39 :       location_t floc = read_location (sec);
   17237           39 :       const char *fname = sec.str (NULL);
   17238              : 
   17239           39 :       if (sec.get_overrun ())
   17240              :         break;
   17241              : 
   17242           48 :       dump () && dump ("Reading elided partition %s (crc=%x)", name, crc);
   17243              : 
   17244           39 :       module_state *imp = get_module (name);
   17245           39 :       if (!imp  /* Partition should be ...  */
   17246           39 :           || !imp->is_partition () /* a partition ...  */
   17247           39 :           || imp->loadedness != ML_NONE  /* that is not yet loaded ...  */
   17248           78 :           || get_primary (imp) != this) /* whose primary is this.  */
   17249              :         {
   17250            0 :           sec.set_overrun ();
   17251            0 :           break;
   17252              :         }
   17253              : 
   17254           39 :       if (!imp->has_location ())
   17255           30 :         imp->loc = floc;
   17256           39 :       imp->crc = crc;
   17257           39 :       if (!imp->filename && fname[0])
   17258           30 :         imp->filename = xstrdup (fname);
   17259              :     }
   17260              : 
   17261           27 :   dump.outdent ();
   17262           27 :   if (!sec.end (from ()))
   17263              :     return false;
   17264              :   return true;
   17265           27 : }
   17266              : 
   17267              : /* Data for config reading and writing.  */
   17268              : struct module_state_config {
   17269              :   const char *dialect_str = get_dialect ();
   17270              :   line_map_uint_t ordinary_locs = 0;
   17271              :   line_map_uint_t macro_locs = 0;
   17272              :   unsigned num_imports = 0;
   17273              :   unsigned num_partitions = 0;
   17274              :   unsigned num_entities = 0;
   17275              :   unsigned loc_range_bits = 0;
   17276              :   unsigned active_init = 0;
   17277              : 
   17278        99305 :   static void release ()
   17279              :   {
   17280        99305 :     XDELETEVEC (dialect);
   17281        99305 :     dialect = NULL;
   17282              :   }
   17283              : 
   17284              : private:
   17285              :   static const char *get_dialect ();
   17286              :   static char *dialect;
   17287              : };
   17288              : 
   17289              : char *module_state_config::dialect;
   17290              : 
   17291              : /* Generate a string of the significant compilation options.
   17292              :    Generally assume the user knows what they're doing, in the same way
   17293              :    that object files can be mixed.  */
   17294              : 
   17295              : const char *
   17296         6070 : module_state_config::get_dialect ()
   17297              : {
   17298         6070 :   if (!dialect)
   17299         9634 :     dialect = concat (get_cxx_dialect_name (cxx_dialect),
   17300              :                       /* C++ implies these, only show if disabled.  */
   17301         4817 :                       flag_exceptions ? "" : "/no-exceptions",
   17302         4817 :                       flag_rtti ? "" : "/no-rtti",
   17303         4817 :                       flag_new_inheriting_ctors ? "" : "/old-inheriting-ctors",
   17304              :                       /* C++ 20 implies concepts and coroutines.  */
   17305         1505 :                       cxx_dialect < cxx20 && flag_concepts ? "/concepts" : "",
   17306         4817 :                       (cxx_dialect < cxx20 && flag_coroutines
   17307              :                        ? "/coroutines" : ""),
   17308         4817 :                       flag_module_implicit_inline ? "/implicit-inline" : "",
   17309         4817 :                       flag_contracts ? "/contracts" : "",
   17310         4817 :                       flag_reflection ? "/reflection" : "",
   17311              :                       NULL);
   17312              : 
   17313         6070 :   return dialect;
   17314              : }
   17315              : 
   17316              : /* Contents of a cluster.  */
   17317              : enum cluster_tag {
   17318              :   ct_decl,      /* A decl.  */
   17319              :   ct_defn,      /* A definition.  */
   17320              :   ct_bind,      /* A binding.  */
   17321              :   ct_hwm
   17322              : };
   17323              : 
   17324              : /* Binding modifiers.  */
   17325              : enum ct_bind_flags
   17326              : {
   17327              :   cbf_export = 0x1,     /* An exported decl.  */
   17328              :   cbf_hidden = 0x2,     /* A hidden (friend) decl.  */
   17329              :   cbf_using = 0x4,      /* A using decl.  */
   17330              :   cbf_internal = 0x8,   /* A TU-local decl.  */
   17331              : };
   17332              : 
   17333              : /* DEP belongs to a different cluster, seed it to prevent
   17334              :    unfortunately timed duplicate import.  */
   17335              : // FIXME: QOI For inter-cluster references we could just only pick
   17336              : // one entity from an earlier cluster.  Even better track
   17337              : // dependencies between earlier clusters
   17338              : 
   17339              : void
   17340      9966953 : module_state::intercluster_seed (trees_out &sec, unsigned index_hwm, depset *dep)
   17341              : {
   17342      9966953 :   if (dep->is_tu_local ())
   17343              :     /* We only stream placeholders for TU-local entities anyway.  */;
   17344      9966744 :   else if (dep->is_import () || dep->cluster < index_hwm)
   17345              :     {
   17346      4295438 :       tree ent = dep->get_entity ();
   17347      4295438 :       if (!TREE_VISITED (ent))
   17348              :         {
   17349      1842617 :           sec.tree_node (ent);
   17350      1843067 :           dump (dumper::CLUSTER)
   17351          450 :             && dump ("Seeded %s %N",
   17352          450 :                      dep->is_import () ? "import" : "intercluster", ent);
   17353              :         }
   17354              :     }
   17355      9966953 : }
   17356              : 
   17357              : /* Write the cluster of depsets in SCC[0-SIZE).
   17358              :    dep->section -> section number
   17359              :    dep->cluster -> entity number
   17360              :  */
   17361              : 
   17362              : unsigned
   17363       337683 : module_state::write_cluster (elf_out *to, depset *scc[], unsigned size,
   17364              :                              depset::hash &table, unsigned *counts,
   17365              :                              unsigned *crc_ptr)
   17366              : {
   17367       339217 :   dump () && dump ("Writing section:%u %u depsets", table.section, size);
   17368       337683 :   dump.indent ();
   17369              : 
   17370       337683 :   trees_out sec (to, this, table, table.section);
   17371       337683 :   sec.begin ();
   17372       337683 :   unsigned index_lwm = counts[MSC_entities];
   17373              : 
   17374              :   /* Determine entity numbers, mark for writing.   */
   17375       337992 :   dump (dumper::CLUSTER) && dump ("Cluster members:") && (dump.indent (), true);
   17376      1873622 :   for (unsigned ix = 0; ix != size; ix++)
   17377              :     {
   17378      1535939 :       depset *b = scc[ix];
   17379              : 
   17380      1535939 :       switch (b->get_entity_kind ())
   17381              :         {
   17382            0 :         default:
   17383            0 :           gcc_unreachable ();
   17384              : 
   17385       163814 :         case depset::EK_BINDING:
   17386       163814 :           {
   17387       163814 :             dump (dumper::CLUSTER)
   17388          210 :               && dump ("[%u]=%s %P", ix, b->entity_kind_name (),
   17389              :                        b->get_entity (), b->get_name ());
   17390       163814 :             depset *ns_dep = b->deps[0];
   17391       163814 :             gcc_checking_assert (ns_dep->get_entity_kind ()
   17392              :                                  == depset::EK_NAMESPACE
   17393              :                                  && ns_dep->get_entity () == b->get_entity ());
   17394       395817 :             for (unsigned jx = b->deps.length (); --jx;)
   17395              :               {
   17396       232003 :                 depset *dep = b->deps[jx];
   17397              :                 // We could be declaring something that is also a
   17398              :                 // (merged) import
   17399       273634 :                 gcc_checking_assert (dep->is_import ()
   17400              :                                      || TREE_VISITED (dep->get_entity ())
   17401              :                                      || (dep->get_entity_kind ()
   17402              :                                          == depset::EK_USING)
   17403              :                                      || (dep->get_entity_kind ()
   17404              :                                          == depset::EK_TU_LOCAL));
   17405              :               }
   17406              :           }
   17407              :           break;
   17408              : 
   17409      1330542 :         case depset::EK_DECL:
   17410      1330542 :         case depset::EK_SPECIALIZATION:
   17411      1330542 :         case depset::EK_PARTIAL:
   17412      1330542 :           b->cluster = counts[MSC_entities]++;
   17413      1330542 :           sec.mark_declaration (b->get_entity (), b->has_defn ());
   17414              :           /* FALLTHROUGH  */
   17415              : 
   17416      1372125 :         case depset::EK_USING:
   17417      1372125 :         case depset::EK_TU_LOCAL:
   17418      2744250 :           gcc_checking_assert (!b->is_import ()
   17419              :                                && !b->is_unreached ());
   17420      1539113 :           dump (dumper::CLUSTER)
   17421         1161 :             && dump ("[%u]=%s %s %N", ix, b->entity_kind_name (),
   17422          729 :                      b->has_defn () ? "definition" : "declaration",
   17423              :                      b->get_entity ());
   17424              :           break;
   17425              :         }
   17426              :     }
   17427       337992 :   dump (dumper::CLUSTER) && (dump.outdent (), true);
   17428              : 
   17429              :   /* Ensure every out-of-cluster decl is referenced before we start
   17430              :      streaming.  We must do both imports *and* earlier clusters,
   17431              :      because the latter could reach into the former and cause a
   17432              :      duplicate loop.   */
   17433       337683 :   sec.set_importing (+1);
   17434      1873622 :   for (unsigned ix = 0; ix != size; ix++)
   17435              :     {
   17436      1535939 :       depset *b = scc[ix];
   17437     19777986 :       for (unsigned jx = b->is_special (); jx != b->deps.length (); jx++)
   17438              :         {
   17439      8357494 :           depset *dep = b->deps[jx];
   17440              : 
   17441      8357494 :           if (dep->is_binding ())
   17442              :             {
   17443      2073369 :               for (unsigned ix = dep->deps.length (); --ix;)
   17444              :                 {
   17445      1609459 :                   depset *bind = dep->deps[ix];
   17446      1609459 :                   if (bind->get_entity_kind () == depset::EK_USING)
   17447       576691 :                     bind = bind->deps[1];
   17448              : 
   17449      1609459 :                   intercluster_seed (sec, index_lwm, bind);
   17450              :                 }
   17451              :               /* Also check the namespace itself.  */
   17452       231955 :               dep = dep->deps[0];
   17453              :             }
   17454              : 
   17455      8357494 :           intercluster_seed (sec, index_lwm, dep);
   17456              :         }
   17457              :     }
   17458       337683 :   sec.tree_node (NULL_TREE);
   17459              :   /* We're done importing now.  */
   17460       337683 :   sec.set_importing (-1);
   17461              : 
   17462              :   /* Write non-definitions.  */
   17463      1873622 :   for (unsigned ix = 0; ix != size; ix++)
   17464              :     {
   17465      1535939 :       depset *b = scc[ix];
   17466      1535939 :       tree decl = b->get_entity ();
   17467      1535939 :       switch (b->get_entity_kind ())
   17468              :         {
   17469            0 :         default:
   17470            0 :           gcc_unreachable ();
   17471       163814 :           break;
   17472              : 
   17473       163814 :         case depset::EK_BINDING:
   17474       163814 :           {
   17475       163814 :             gcc_assert (TREE_CODE (decl) == NAMESPACE_DECL);
   17476       165012 :             dump () && dump ("Depset:%u binding %C:%P", ix, TREE_CODE (decl),
   17477              :                              decl, b->get_name ());
   17478       163814 :             sec.u (ct_bind);
   17479       163814 :             sec.tree_node (decl);
   17480       163814 :             sec.tree_node (b->get_name ());
   17481              : 
   17482              :             /* Write in reverse order, so reading will see the exports
   17483              :                first, thus building the overload chain will be
   17484              :                optimized.  */
   17485       559631 :             for (unsigned jx = b->deps.length (); --jx;)
   17486              :               {
   17487       232003 :                 depset *dep = b->deps[jx];
   17488       232003 :                 tree bound = dep->get_entity ();
   17489       232003 :                 unsigned flags = 0;
   17490       232003 :                 if (dep->get_entity_kind () == depset::EK_TU_LOCAL)
   17491              :                   flags |= cbf_internal;
   17492       231955 :                 else if (dep->get_entity_kind () == depset::EK_USING)
   17493              :                   {
   17494        41583 :                     tree ovl = bound;
   17495        41583 :                     bound = OVL_FUNCTION (bound);
   17496        41583 :                     if (!(TREE_CODE (bound) == CONST_DECL
   17497         5062 :                           && UNSCOPED_ENUM_P (TREE_TYPE (bound))
   17498         4220 :                           && decl == TYPE_NAME (TREE_TYPE (bound))))
   17499              :                       /* An unscoped enumerator in its enumeration's
   17500              :                          scope is not a using.  */
   17501              :                       flags |= cbf_using;
   17502        41583 :                     if (OVL_EXPORT_P (ovl))
   17503        40387 :                       flags |= cbf_export;
   17504              :                   }
   17505              :                 else
   17506              :                   {
   17507              :                     /* An implicit typedef must be at one.  */
   17508       190372 :                     gcc_assert (!DECL_IMPLICIT_TYPEDEF_P (bound) || jx == 1);
   17509       190372 :                     if (dep->is_hidden ())
   17510              :                       flags |= cbf_hidden;
   17511       190283 :                     else if (DECL_MODULE_EXPORT_P (STRIP_TEMPLATE (bound)))
   17512       144798 :                       flags |= cbf_export;
   17513              :                   }
   17514              : 
   17515       232003 :                 gcc_checking_assert (DECL_P (bound));
   17516              : 
   17517       232003 :                 sec.i (flags);
   17518       232003 :                 if (flags & cbf_internal)
   17519              :                   {
   17520           48 :                     sec.tree_node (name_for_tu_local_decl (bound));
   17521           48 :                     write_location (sec, DECL_SOURCE_LOCATION (bound));
   17522              :                   }
   17523              :                 else
   17524       231955 :                   sec.tree_node (bound);
   17525              :               }
   17526              : 
   17527              :             /* Terminate the list.  */
   17528       163814 :             sec.i (-1);
   17529              :           }
   17530       163814 :           break;
   17531              : 
   17532        41583 :         case depset::EK_USING:
   17533        41583 :         case depset::EK_TU_LOCAL:
   17534        41610 :           dump () && dump ("Depset:%u %s %C:%N", ix, b->entity_kind_name (),
   17535           27 :                            TREE_CODE (decl), decl);
   17536              :           break;
   17537              : 
   17538      1330542 :         case depset::EK_SPECIALIZATION:
   17539      1330542 :         case depset::EK_PARTIAL:
   17540      1330542 :         case depset::EK_DECL:
   17541      1333689 :           dump () && dump ("Depset:%u %s entity:%u %C:%N", ix,
   17542              :                            b->entity_kind_name (), b->cluster,
   17543         3147 :                            TREE_CODE (decl), decl);
   17544              : 
   17545      1330542 :           sec.u (ct_decl);
   17546      1330542 :           sec.tree_node (decl);
   17547              : 
   17548      1539086 :           dump () && dump ("Wrote declaration entity:%u %C:%N",
   17549         3147 :                            b->cluster, TREE_CODE (decl), decl);
   17550              :           break;
   17551              :         }
   17552              :     }
   17553              : 
   17554              :   depset *namer = NULL;
   17555              : 
   17556              :   /* Write out definitions  */
   17557      1873622 :   for (unsigned ix = 0; ix != size; ix++)
   17558              :     {
   17559      1535939 :       depset *b = scc[ix];
   17560      1535939 :       tree decl = b->get_entity ();
   17561      1535939 :       switch (b->get_entity_kind ())
   17562              :         {
   17563              :         default:
   17564              :           break;
   17565              : 
   17566      1330542 :         case depset::EK_SPECIALIZATION:
   17567      1330542 :         case depset::EK_PARTIAL:
   17568      1330542 :         case depset::EK_DECL:
   17569      1330542 :           if (!namer)
   17570       323596 :             namer = b;
   17571              : 
   17572      1330542 :           if (b->has_defn ())
   17573              :             {
   17574       513224 :               sec.u (ct_defn);
   17575       513224 :               sec.tree_node (decl);
   17576       514167 :               dump () && dump ("Writing definition %N", decl);
   17577       513224 :               sec.write_definition (decl, b->refs_tu_local ());
   17578              : 
   17579       513224 :               if (!namer->has_defn ())
   17580      1535939 :                 namer = b;
   17581              :             }
   17582              :           break;
   17583              :         }
   17584              :     }
   17585              : 
   17586              :   /* We don't find the section by name.  Use depset's decl's name for
   17587              :      human friendliness.  */
   17588       337683 :   unsigned name = 0;
   17589       337683 :   tree naming_decl = NULL_TREE;
   17590       337683 :   if (namer)
   17591              :     {
   17592       323596 :       naming_decl = namer->get_entity ();
   17593       323596 :       if (namer->get_entity_kind () == depset::EK_USING)
   17594              :         /* This unfortunately names the section from the target of the
   17595              :            using decl.  But the name is only a guide, so Do Not Care.  */
   17596            0 :         naming_decl = OVL_FUNCTION (naming_decl);
   17597       323596 :       if (DECL_IMPLICIT_TYPEDEF_P (naming_decl))
   17598              :         /* Lose any anonymousness.  */
   17599       114660 :         naming_decl = TYPE_NAME (TREE_TYPE (naming_decl));
   17600       323596 :       name = to->qualified_name (naming_decl, namer->has_defn ());
   17601              :     }
   17602              : 
   17603       337683 :   unsigned bytes = sec.pos;
   17604       337683 :   unsigned snum = sec.end (to, name, crc_ptr);
   17605              : 
   17606      2211305 :   for (unsigned ix = size; ix--;)
   17607      1535939 :     gcc_checking_assert (scc[ix]->section == snum);
   17608              : 
   17609       337683 :   dump.outdent ();
   17610       339217 :   dump () && dump ("Wrote section:%u named-by:%N", table.section, naming_decl);
   17611              : 
   17612       337683 :   return bytes;
   17613       337683 : }
   17614              : 
   17615              : /* Read a cluster from section SNUM.  */
   17616              : 
   17617              : bool
   17618       215723 : module_state::read_cluster (unsigned snum)
   17619              : {
   17620       215723 :   trees_in sec (this);
   17621              : 
   17622       215723 :   if (!sec.begin (loc, from (), snum))
   17623              :     return false;
   17624              : 
   17625       217041 :   dump () && dump ("Reading section:%u", snum);
   17626       215723 :   dump.indent ();
   17627              : 
   17628              :   /* We care about structural equality.  */
   17629       215723 :   comparing_dependent_aliases++;
   17630              : 
   17631              :   /* First seed the imports.  */
   17632      1347271 :   while (tree import = sec.tree_node ())
   17633      1133173 :     dump (dumper::CLUSTER) && dump ("Seeded import %N", import);
   17634              : 
   17635      1551501 :   while (!sec.get_overrun () && sec.more_p ())
   17636              :     {
   17637      1335778 :       unsigned ct = sec.u ();
   17638      1335778 :       switch (ct)
   17639              :         {
   17640            0 :         default:
   17641            0 :           sec.set_overrun ();
   17642            0 :           break;
   17643              : 
   17644       107105 :         case ct_bind:
   17645              :           /* A set of namespace bindings.  */
   17646       107105 :           {
   17647       107105 :             tree ns = sec.tree_node ();
   17648       107105 :             tree name = sec.tree_node ();
   17649       107105 :             tree decls = NULL_TREE;
   17650       107105 :             tree visible = NULL_TREE;
   17651       107105 :             tree internal = NULL_TREE;
   17652       107105 :             tree type = NULL_TREE;
   17653       107105 :             bool dedup = false;
   17654       107105 :             bool global_p = is_header ();
   17655              : 
   17656              :             /* We rely on the bindings being in the reverse order of
   17657              :                the resulting overload set.  */
   17658       256698 :             for (;;)
   17659              :               {
   17660       256698 :                 int flags = sec.i ();
   17661       256698 :                 if (flags < 0)
   17662              :                   break;
   17663              : 
   17664       149593 :                 if ((flags & cbf_hidden)
   17665           56 :                     && (flags & (cbf_using | cbf_export)))
   17666            0 :                   sec.set_overrun ();
   17667       149593 :                 if ((flags & cbf_internal)
   17668           15 :                     && flags != cbf_internal)
   17669            0 :                   sec.set_overrun ();
   17670              : 
   17671            0 :                 if (flags & cbf_internal)
   17672              :                   {
   17673           15 :                     tree name = sec.tree_node ();
   17674           15 :                     location_t loc = read_location (sec);
   17675           15 :                     if (sec.get_overrun ())
   17676              :                       break;
   17677              : 
   17678           15 :                     tree decl = make_node (TU_LOCAL_ENTITY);
   17679           15 :                     TU_LOCAL_ENTITY_NAME (decl) = name;
   17680           15 :                     TU_LOCAL_ENTITY_LOCATION (decl) = loc;
   17681           15 :                     internal = tree_cons (NULL_TREE, decl, internal);
   17682           15 :                     continue;
   17683           15 :                   }
   17684              : 
   17685       149578 :                 tree decl = sec.tree_node ();
   17686       149578 :                 if (sec.get_overrun ())
   17687              :                   break;
   17688              : 
   17689       149578 :                 if (!global_p)
   17690              :                   {
   17691              :                     /* Check if the decl could require GM merging.  */
   17692        17230 :                     tree orig = get_originating_module_decl (decl);
   17693        17230 :                     tree inner = STRIP_TEMPLATE (orig);
   17694        17230 :                     if (!DECL_LANG_SPECIFIC (inner)
   17695        34460 :                         || !DECL_MODULE_ATTACH_P (inner))
   17696              :                       global_p = true;
   17697              :                   }
   17698              : 
   17699       149578 :                 if (decls && TREE_CODE (decl) == TYPE_DECL)
   17700              :                   {
   17701              :                     /* Stat hack.  */
   17702           54 :                     if (type || !DECL_IMPLICIT_TYPEDEF_P (decl))
   17703            0 :                       sec.set_overrun ();
   17704              : 
   17705           54 :                     if (flags & cbf_using)
   17706              :                       {
   17707            3 :                         type = build_lang_decl_loc (UNKNOWN_LOCATION,
   17708              :                                                     USING_DECL,
   17709            3 :                                                     DECL_NAME (decl),
   17710              :                                                     NULL_TREE);
   17711            3 :                         USING_DECL_DECLS (type) = decl;
   17712            3 :                         USING_DECL_SCOPE (type) = CP_DECL_CONTEXT (decl);
   17713            3 :                         DECL_CONTEXT (type) = ns;
   17714              : 
   17715            3 :                         DECL_MODULE_PURVIEW_P (type) = true;
   17716            3 :                         if (flags & cbf_export)
   17717            3 :                           DECL_MODULE_EXPORT_P (type) = true;
   17718              :                       }
   17719              :                     else
   17720              :                       type = decl;
   17721              :                   }
   17722              :                 else
   17723              :                   {
   17724       149524 :                     if ((flags & cbf_using) &&
   17725        21865 :                         !DECL_DECLARES_FUNCTION_P (decl))
   17726              :                       {
   17727              :                         /* We should only see a single non-function using-decl
   17728              :                            for a binding; more than that would clash.  */
   17729         4667 :                         if (decls)
   17730            0 :                           sec.set_overrun ();
   17731              : 
   17732              :                         /* FIXME: Propagate the location of the using-decl
   17733              :                            for use in diagnostics.  */
   17734         4667 :                         decls = build_lang_decl_loc (UNKNOWN_LOCATION,
   17735              :                                                      USING_DECL,
   17736         4667 :                                                      DECL_NAME (decl),
   17737              :                                                      NULL_TREE);
   17738         4667 :                         USING_DECL_DECLS (decls) = decl;
   17739              :                         /* We don't currently record the actual scope of the
   17740              :                            using-declaration, but this approximation should
   17741              :                            generally be good enough.  */
   17742         4667 :                         USING_DECL_SCOPE (decls) = CP_DECL_CONTEXT (decl);
   17743         4667 :                         DECL_CONTEXT (decls) = ns;
   17744              : 
   17745         4667 :                         DECL_MODULE_PURVIEW_P (decls) = true;
   17746         4667 :                         if (flags & cbf_export)
   17747         4545 :                           DECL_MODULE_EXPORT_P (decls) = true;
   17748              :                       }
   17749       144857 :                     else if (decls
   17750       102423 :                              || (flags & (cbf_hidden | cbf_using))
   17751       239156 :                              || DECL_FUNCTION_TEMPLATE_P (decl))
   17752              :                       {
   17753        66262 :                         decls = ovl_make (decl, decls);
   17754        66262 :                         if (flags & cbf_using)
   17755              :                           {
   17756        17198 :                             dedup = true;
   17757        17198 :                             OVL_USING_P (decls) = true;
   17758        17198 :                             OVL_PURVIEW_P (decls) = true;
   17759        17198 :                             if (flags & cbf_export)
   17760        17183 :                               OVL_EXPORT_P (decls) = true;
   17761              :                           }
   17762              : 
   17763        66262 :                         if (flags & cbf_hidden)
   17764           56 :                           OVL_HIDDEN_P (decls) = true;
   17765        66206 :                         else if (dedup)
   17766        25218 :                           OVL_DEDUP_P (decls) = true;
   17767              :                       }
   17768              :                     else
   17769              :                       decls = decl;
   17770              : 
   17771       149402 :                     if (flags & cbf_export
   17772       149524 :                         || (!(flags & cbf_hidden)
   17773        18495 :                             && (is_module () || is_partition ())))
   17774       149593 :                       visible = decls;
   17775              :                   }
   17776              :               }
   17777              : 
   17778       107105 :             if (!decls && !internal)
   17779            0 :               sec.set_overrun ();
   17780              : 
   17781       107105 :             if (sec.get_overrun ())
   17782              :               break; /* Bail.  */
   17783              : 
   17784       108017 :             dump () && dump ("Binding of %P", ns, name);
   17785       107105 :             if (!set_module_binding (ns, name, mod, global_p,
   17786       107105 :                                      is_module () || is_partition (),
   17787              :                                      decls, type, visible, internal))
   17788            0 :               sec.set_overrun ();
   17789              :           }
   17790              :           break;
   17791              : 
   17792       885772 :         case ct_decl:
   17793              :           /* A decl.  */
   17794       885772 :           {
   17795       885772 :             tree decl = sec.tree_node ();
   17796      1339586 :             dump () && dump ("Read declaration of %N", decl);
   17797              :           }
   17798              :           break;
   17799              : 
   17800       342901 :         case ct_defn:
   17801       342901 :           {
   17802       342901 :             tree decl = sec.tree_node ();
   17803       344241 :             dump () && dump ("Reading definition of %N", decl);
   17804       342901 :             sec.read_definition (decl);
   17805              :           }
   17806       342901 :           break;
   17807              :         }
   17808              :     }
   17809              : 
   17810              :   /* When lazy loading is in effect, we can be in the middle of
   17811              :      parsing or instantiating a function.  Save it away.
   17812              :      push_function_context does too much work.   */
   17813       215723 :   tree old_cfd = current_function_decl;
   17814       215723 :   struct function *old_cfun = cfun;
   17815       405724 :   for (const post_process_data& pdata : sec.post_process ())
   17816              :     {
   17817       141827 :       tree decl = pdata.decl;
   17818              : 
   17819       141827 :       bool abstract = false;
   17820       141827 :       if (TREE_CODE (decl) == TEMPLATE_DECL)
   17821              :         {
   17822        87328 :           abstract = true;
   17823        87328 :           decl = DECL_TEMPLATE_RESULT (decl);
   17824              :         }
   17825              : 
   17826       141827 :       current_function_decl = decl;
   17827       141827 :       allocate_struct_function (decl, abstract);
   17828       141827 :       cfun->language = ggc_cleared_alloc<language_function> ();
   17829       141827 :       cfun->language->base.x_stmt_tree.stmts_are_full_exprs_p = 1;
   17830       141827 :       cfun->function_start_locus = pdata.start_locus;
   17831       141827 :       cfun->function_end_locus = pdata.end_locus;
   17832       141827 :       cfun->language->returns_value = pdata.returns_value;
   17833       141827 :       cfun->language->returns_null = pdata.returns_null;
   17834       141827 :       cfun->language->returns_abnormally = pdata.returns_abnormally;
   17835       141827 :       cfun->language->infinite_loop = pdata.infinite_loop;
   17836       141827 :       cfun->coroutine_component = DECL_COROUTINE_P (decl);
   17837              : 
   17838              :       /* Make sure we emit explicit instantiations.
   17839              :          FIXME do we want to do this in expand_or_defer_fn instead?  */
   17840       141827 :       if (DECL_EXPLICIT_INSTANTIATION (decl)
   17841       141827 :           && !DECL_EXTERNAL (decl))
   17842           27 :         setup_explicit_instantiation_definition_linkage (decl);
   17843              : 
   17844       141827 :       if (abstract)
   17845              :         ;
   17846        54499 :       else if (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl))
   17847         8959 :         vec_safe_push (post_load_decls, decl);
   17848              :       else
   17849              :         {
   17850        45540 :           bool aggr = aggregate_value_p (DECL_RESULT (decl), decl);
   17851              : #ifdef PCC_STATIC_STRUCT_RETURN
   17852              :           cfun->returns_pcc_struct = aggr;
   17853              : #endif
   17854        45540 :           cfun->returns_struct = aggr;
   17855        45540 :           expand_or_defer_fn (decl);
   17856              : 
   17857              :           /* If we first see this function after at_eof, it doesn't get
   17858              :              note_vague_linkage_fn from tentative_decl_linkage, so the loop in
   17859              :              c_parse_final_cleanups won't consider it.  But with DECL_COMDAT we
   17860              :              can just clear DECL_EXTERNAL and let cgraph decide.
   17861              :              FIXME handle this outside module.cc after GCC 15.  */
   17862         1884 :           if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   17863        45707 :               && DECL_NOT_REALLY_EXTERN (decl))
   17864          131 :             DECL_EXTERNAL (decl) = false;
   17865              :         }
   17866              : 
   17867              :     }
   17868       215791 :   for (const tree& type : sec.post_process_type ())
   17869              :     {
   17870              :       /* Attempt to complete an array type now in case its element type
   17871              :          had a definition streamed later in the cluster.  */
   17872           32 :       gcc_checking_assert (TREE_CODE (type) == ARRAY_TYPE);
   17873           32 :       complete_type (type);
   17874              :     }
   17875       215723 :   set_cfun (old_cfun);
   17876       215723 :   current_function_decl = old_cfd;
   17877       215723 :   comparing_dependent_aliases--;
   17878              : 
   17879       215723 :   dump.outdent ();
   17880       217041 :   dump () && dump ("Read section:%u", snum);
   17881              : 
   17882       215723 :   loaded_clusters++;
   17883              : 
   17884       215723 :   if (!sec.end (from ()))
   17885              :     return false;
   17886              : 
   17887              :   return true;
   17888       215723 : }
   17889              : 
   17890              : void
   17891       166882 : module_state::write_namespace (bytes_out &sec, depset *dep)
   17892              : {
   17893       166882 :   unsigned ns_num = dep->cluster;
   17894       166882 :   unsigned ns_import = 0;
   17895              : 
   17896       166882 :   if (dep->is_import ())
   17897            0 :     ns_import = dep->section;
   17898       166882 :   else if (dep->get_entity () != global_namespace)
   17899       110553 :     ns_num++;
   17900              : 
   17901       166882 :   sec.u (ns_import);
   17902       166882 :   sec.u (ns_num);
   17903       166882 : }
   17904              : 
   17905              : tree
   17906       175811 : module_state::read_namespace (bytes_in &sec)
   17907              : {
   17908       175811 :   unsigned ns_import = sec.u ();
   17909       175811 :   unsigned ns_num = sec.u ();
   17910       175811 :   tree ns = NULL_TREE;
   17911              : 
   17912       175811 :   if (ns_import || ns_num)
   17913              :     {
   17914       113787 :       if (!ns_import)
   17915       113787 :         ns_num--;
   17916              : 
   17917       113787 :       if (unsigned origin = slurp->remap_module (ns_import))
   17918              :         {
   17919       113787 :           module_state *from = (*modules)[origin];
   17920       113787 :           if (ns_num < from->entity_num)
   17921              :             {
   17922       113787 :               binding_slot &slot = (*entity_ary)[from->entity_lwm + ns_num];
   17923              : 
   17924       113787 :               if (!slot.is_lazy ())
   17925       113787 :                 ns = slot;
   17926              :             }
   17927              :         }
   17928              :       else
   17929            0 :         sec.set_overrun ();
   17930              :     }
   17931              :   else
   17932        62024 :     ns = global_namespace;
   17933              : 
   17934       175811 :   return ns;
   17935              : }
   17936              : 
   17937              : /* SPACES is a sorted vector of namespaces.  Write out the namespaces
   17938              :    to MOD_SNAME_PFX.nms section.   */
   17939              : 
   17940              : void
   17941          597 : module_state::write_namespaces (elf_out *to, vec<depset *> spaces,
   17942              :                                 unsigned num, unsigned *crc_p)
   17943              : {
   17944          655 :   dump () && dump ("Writing namespaces");
   17945          597 :   dump.indent ();
   17946              : 
   17947          597 :   bytes_out sec (to);
   17948          597 :   sec.begin ();
   17949              : 
   17950         3940 :   for (unsigned ix = 0; ix != num; ix++)
   17951              :     {
   17952         2746 :       depset *b = spaces[ix];
   17953         2746 :       tree ns = b->get_entity ();
   17954              : 
   17955              :       /* This could be an anonymous namespace even for a named module,
   17956              :          since we can still emit no-linkage decls.  */
   17957         2746 :       gcc_checking_assert (TREE_CODE (ns) == NAMESPACE_DECL);
   17958              : 
   17959         2746 :       unsigned flags = 0;
   17960         2746 :       if (TREE_PUBLIC (ns))
   17961         2685 :         flags |= 1;
   17962         2746 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17963          524 :         flags |= 2;
   17964         2746 :       if (DECL_MODULE_PURVIEW_P (ns))
   17965         2089 :         flags |= 4;
   17966         2746 :       if (DECL_MODULE_EXPORT_P (ns))
   17967         1866 :         flags |= 8;
   17968         2746 :       if (TREE_DEPRECATED (ns))
   17969           18 :         flags |= 16;
   17970              : 
   17971         2886 :       dump () && dump ("Writing namespace:%u %N%s%s%s%s",
   17972              :                        b->cluster, ns,
   17973          140 :                        flags & 1 ? ", public" : "",
   17974          140 :                        flags & 2 ? ", inline" : "",
   17975          140 :                        flags & 4 ? ", purview" : "",
   17976          140 :                        flags & 8 ? ", export" : "",
   17977          140 :                        flags & 16 ? ", deprecated" : "");
   17978         2746 :       sec.u (b->cluster);
   17979         2746 :       sec.u (to->name (DECL_NAME (ns)));
   17980         2746 :       write_namespace (sec, b->deps[0]);
   17981              : 
   17982         2746 :       sec.u (flags);
   17983         2746 :       write_location (sec, DECL_SOURCE_LOCATION (ns));
   17984              : 
   17985         2746 :       if (DECL_NAMESPACE_INLINE_P (ns))
   17986              :         {
   17987          524 :           if (tree attr = lookup_attribute ("abi_tag", DECL_ATTRIBUTES (ns)))
   17988              :             {
   17989          131 :               tree tags = TREE_VALUE (attr);
   17990          131 :               sec.u (list_length (tags));
   17991          396 :               for (tree tag = tags; tag; tag = TREE_CHAIN (tag))
   17992          134 :                 sec.str (TREE_STRING_POINTER (TREE_VALUE (tag)));
   17993              :             }
   17994              :           else
   17995          393 :             sec.u (0);
   17996              :         }
   17997              :     }
   17998              : 
   17999          597 :   sec.end (to, to->name (MOD_SNAME_PFX ".nms"), crc_p);
   18000          597 :   dump.outdent ();
   18001          597 : }
   18002              : 
   18003              : /* Read the namespace hierarchy from MOD_SNAME_PFX.namespace.  Fill in
   18004              :    SPACES from that data.  */
   18005              : 
   18006              : bool
   18007          612 : module_state::read_namespaces (unsigned num)
   18008              : {
   18009          612 :   bytes_in sec;
   18010              : 
   18011          612 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".nms"))
   18012              :     return false;
   18013              : 
   18014          685 :   dump () && dump ("Reading namespaces");
   18015          612 :   dump.indent ();
   18016              : 
   18017         3412 :   for (unsigned ix = 0; ix != num; ix++)
   18018              :     {
   18019         2800 :       unsigned entity_index = sec.u ();
   18020         2800 :       unsigned name = sec.u ();
   18021              : 
   18022         2800 :       tree parent = read_namespace (sec);
   18023              : 
   18024              :       /* See comment in write_namespace about why not bits.  */
   18025         2800 :       unsigned flags = sec.u ();
   18026         2800 :       location_t src_loc = read_location (sec);
   18027         2800 :       unsigned tags_count = (flags & 2) ? sec.u () : 0;
   18028              : 
   18029         2800 :       if (entity_index >= entity_num
   18030         2800 :           || !parent
   18031         2800 :           || (flags & 0xc) == 0x8)
   18032            0 :         sec.set_overrun ();
   18033              : 
   18034         2800 :       tree tags = NULL_TREE;
   18035         2939 :       while (tags_count--)
   18036              :         {
   18037          139 :           size_t len;
   18038          139 :           const char *str = sec.str (&len);
   18039          139 :           tags = tree_cons (NULL_TREE, build_string (len + 1, str), tags);
   18040          139 :           tags = nreverse (tags);
   18041              :         }
   18042              : 
   18043         2800 :       if (sec.get_overrun ())
   18044              :         break;
   18045              : 
   18046         5536 :       tree id = name ? get_identifier (from ()->name (name)) : NULL_TREE;
   18047              : 
   18048         3020 :       dump () && dump ("Read namespace:%u %P%s%s%s%s",
   18049              :                        entity_index, parent, id,
   18050          110 :                        flags & 1 ? ", public" : "",
   18051              :                        flags & 2 ? ", inline" : "",
   18052          110 :                        flags & 4 ? ", purview" : "",
   18053          110 :                        flags & 8 ? ", export" : "",
   18054          110 :                        flags & 16 ? ", deprecated" : "");
   18055         2800 :       bool visible_p = ((flags & 8)
   18056         2800 :                         || ((flags & 1)
   18057          636 :                             && (flags & 4)
   18058          101 :                             && (is_partition () || is_module ())));
   18059         2800 :       tree inner = add_imported_namespace (parent, id, src_loc, mod,
   18060              :                                            bool (flags & 2), visible_p);
   18061         2800 :       if (!inner)
   18062              :         {
   18063            0 :           sec.set_overrun ();
   18064            0 :           break;
   18065              :         }
   18066              : 
   18067         2800 :       if (is_partition ())
   18068              :         {
   18069           54 :           if (flags & 4)
   18070           48 :             DECL_MODULE_PURVIEW_P (inner) = true;
   18071           54 :           if (flags & 8)
   18072           27 :             DECL_MODULE_EXPORT_P (inner) = true;
   18073              :         }
   18074              : 
   18075         2800 :       if (flags & 16)
   18076           16 :         TREE_DEPRECATED (inner) = true;
   18077              : 
   18078         2800 :       if (tags)
   18079          136 :         DECL_ATTRIBUTES (inner)
   18080          272 :           = tree_cons (get_identifier ("abi_tag"), tags, DECL_ATTRIBUTES (inner));
   18081              : 
   18082              :       /* Install the namespace.  */
   18083         2800 :       (*entity_ary)[entity_lwm + entity_index] = inner;
   18084         2800 :       if (DECL_MODULE_IMPORT_P (inner))
   18085              :         {
   18086            0 :           bool existed;
   18087            0 :           unsigned *slot = &entity_map->get_or_insert
   18088            0 :             (DECL_UID (inner), &existed);
   18089            0 :           if (existed)
   18090              :             /* If it existed, it should match.  */
   18091            0 :             gcc_checking_assert (inner == (*entity_ary)[*slot]);
   18092              :           else
   18093            0 :             *slot = entity_lwm + entity_index;
   18094              :         }
   18095              :     }
   18096              : 
   18097          612 :   dump.outdent ();
   18098          612 :   if (!sec.end (from ()))
   18099              :     return false;
   18100              :   return true;
   18101          612 : }
   18102              : 
   18103              : unsigned
   18104          597 : module_state::write_using_directives (elf_out *to, depset::hash &table,
   18105              :                                       vec<depset *> spaces, unsigned *crc_p)
   18106              : {
   18107          655 :   dump () && dump ("Writing using-directives");
   18108          597 :   dump.indent ();
   18109              : 
   18110          597 :   bytes_out sec (to);
   18111          597 :   sec.begin ();
   18112              : 
   18113          597 :   unsigned num = 0;
   18114         3940 :   auto emit_one_ns = [&](depset *parent_dep)
   18115              :     {
   18116         3343 :       tree parent = parent_dep->get_entity ();
   18117         3889 :       for (auto udir : NAMESPACE_LEVEL (parent)->using_directives)
   18118              :         {
   18119          186 :           if (TREE_CODE (udir) != USING_DECL || !DECL_MODULE_PURVIEW_P (udir))
   18120           19 :             continue;
   18121          167 :           bool exported = DECL_MODULE_EXPORT_P (udir);
   18122          167 :           tree target = USING_DECL_DECLS (udir);
   18123          167 :           depset *target_dep = table.find_dependency (target);
   18124              : 
   18125              :           /* An using-directive imported from a different module might not
   18126              :              have been walked earlier (PR c++/122915).  But importers will
   18127              :              be able to just refer to the decl in that module unless it was
   18128              :              a partition anyway, so we don't have anything to do here.  */
   18129          167 :           if (!target_dep)
   18130              :             {
   18131            6 :               gcc_checking_assert (DECL_MODULE_IMPORT_P (udir));
   18132            6 :               continue;
   18133              :             }
   18134              : 
   18135          185 :           dump () && dump ("Writing using-directive in %N for %N",
   18136              :                            parent, target);
   18137          161 :           sec.u (exported);
   18138          161 :           write_namespace (sec, parent_dep);
   18139          161 :           write_namespace (sec, target_dep);
   18140          161 :           ++num;
   18141              :         }
   18142         3940 :     };
   18143              : 
   18144          597 :   emit_one_ns (table.find_dependency (global_namespace));
   18145         4537 :   for (depset *parent_dep : spaces)
   18146         2746 :     emit_one_ns (parent_dep);
   18147              : 
   18148          597 :   sec.end (to, to->name (MOD_SNAME_PFX ".udi"), crc_p);
   18149          597 :   dump.outdent ();
   18150              : 
   18151          597 :   return num;
   18152          597 : }
   18153              : 
   18154              : bool
   18155          162 : module_state::read_using_directives (unsigned num)
   18156              : {
   18157          162 :   if (!bitmap_bit_p (this_module ()->imports, mod))
   18158              :     {
   18159           11 :       dump () && dump ("Ignoring using-directives because module %M "
   18160              :                        "is not visible in this TU", this);
   18161              :       return true;
   18162              :     }
   18163              : 
   18164          154 :   bytes_in sec;
   18165              : 
   18166          154 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".udi"))
   18167              :     return false;
   18168              : 
   18169          166 :   dump () && dump ("Reading using-directives");
   18170          154 :   dump.indent ();
   18171              : 
   18172          335 :   for (unsigned ix = 0; ix != num; ++ix)
   18173              :     {
   18174          181 :       bool exported = sec.u ();
   18175          181 :       tree parent = read_namespace (sec);
   18176          181 :       tree target = read_namespace (sec);
   18177          181 :       if (sec.get_overrun ())
   18178              :         break;
   18179              : 
   18180          193 :       dump () && dump ("Read using-directive in %N for %N", parent, target);
   18181          181 :       if (exported || is_module () || is_partition ())
   18182          154 :         add_imported_using_namespace (parent, target);
   18183              :     }
   18184              : 
   18185          154 :   dump.outdent ();
   18186          154 :   if (!sec.end (from ()))
   18187              :     return false;
   18188              :   return true;
   18189          154 : }
   18190              : 
   18191              : /* Write the binding TABLE to MOD_SNAME_PFX.bnd   */
   18192              : 
   18193              : unsigned
   18194         2815 : module_state::write_bindings (elf_out *to, vec<depset *> sccs, unsigned *crc_p)
   18195              : {
   18196         3115 :   dump () && dump ("Writing binding table");
   18197         2815 :   dump.indent ();
   18198              : 
   18199         2815 :   unsigned num = 0;
   18200         2815 :   bytes_out sec (to);
   18201         2815 :   sec.begin ();
   18202              : 
   18203      3090925 :   for (unsigned ix = 0; ix != sccs.length (); ix++)
   18204              :     {
   18205      1541240 :       depset *b = sccs[ix];
   18206      1541240 :       if (b->is_binding ())
   18207              :         {
   18208       163814 :           tree ns = b->get_entity ();
   18209       165012 :           dump () && dump ("Bindings %P section:%u", ns, b->get_name (),
   18210              :                            b->section);
   18211       163814 :           sec.u (to->name (b->get_name ()));
   18212       163814 :           write_namespace (sec, b->deps[0]);
   18213       163814 :           sec.u (b->section);
   18214       163814 :           num++;
   18215              :         }
   18216              :     }
   18217              : 
   18218         2815 :   sec.end (to, to->name (MOD_SNAME_PFX ".bnd"), crc_p);
   18219         2815 :   dump.outdent ();
   18220              : 
   18221         2815 :   return num;
   18222         2815 : }
   18223              : 
   18224              : /* Read the binding table from MOD_SNAME_PFX.bind.  */
   18225              : 
   18226              : bool
   18227         2997 : module_state::read_bindings (unsigned num, unsigned lwm, unsigned hwm)
   18228              : {
   18229         2997 :   bytes_in sec;
   18230              : 
   18231         2997 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".bnd"))
   18232              :     return false;
   18233              : 
   18234         3529 :   dump () && dump ("Reading binding table");
   18235         2997 :   dump.indent ();
   18236       175646 :   for (; !sec.get_overrun () && num--;)
   18237              :     {
   18238       172649 :       const char *name = from ()->name (sec.u ());
   18239       172649 :       tree ns = read_namespace (sec);
   18240       172649 :       unsigned snum = sec.u ();
   18241              : 
   18242       172649 :       if (!ns || !name || (snum - lwm) >= (hwm - lwm))
   18243            0 :         sec.set_overrun ();
   18244       172649 :       if (!sec.get_overrun ())
   18245              :         {
   18246       172649 :           tree id = get_identifier (name);
   18247       174199 :           dump () && dump ("Bindings %P section:%u", ns, id, snum);
   18248       172649 :           if (mod && !import_module_binding (ns, id, mod, snum))
   18249              :             break;
   18250              :         }
   18251              :     }
   18252              : 
   18253         2997 :   dump.outdent ();
   18254         2997 :   if (!sec.end (from ()))
   18255              :     return false;
   18256              :   return true;
   18257         2997 : }
   18258              : 
   18259              : /* Write the entity table to MOD_SNAME_PFX.ent
   18260              : 
   18261              :    Each entry is a section number.  */
   18262              : 
   18263              : void
   18264         2549 : module_state::write_entities (elf_out *to, vec<depset *> depsets,
   18265              :                               unsigned count, unsigned *crc_p)
   18266              : {
   18267         2796 :   dump () && dump ("Writing entities");
   18268         2549 :   dump.indent ();
   18269              : 
   18270         2549 :   bytes_out sec (to);
   18271         2549 :   sec.begin ();
   18272              : 
   18273         2549 :   unsigned current = 0;
   18274      1546317 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18275              :     {
   18276      1541219 :       depset *d = depsets[ix];
   18277              : 
   18278      2877056 :       switch (d->get_entity_kind ())
   18279              :         {
   18280              :         default:
   18281              :           break;
   18282              : 
   18283         5295 :         case depset::EK_NAMESPACE:
   18284         5295 :           if (!d->is_import () && d->get_entity () != global_namespace)
   18285              :             {
   18286         2746 :               gcc_checking_assert (d->cluster == current);
   18287         2746 :               current++;
   18288         2746 :               sec.u (0);
   18289              :             }
   18290              :           break;
   18291              : 
   18292      1330542 :         case depset::EK_DECL:
   18293      1330542 :         case depset::EK_SPECIALIZATION:
   18294      1330542 :         case depset::EK_PARTIAL:
   18295      2661084 :           gcc_checking_assert (!d->is_unreached ()
   18296              :                                && !d->is_import ()
   18297              :                                && d->cluster == current
   18298              :                                && d->section);
   18299      1330542 :           current++;
   18300      1330542 :           sec.u (d->section);
   18301      1330542 :           break;
   18302              :         }
   18303              :     }
   18304         2549 :   gcc_assert (count == current);
   18305         2549 :   sec.end (to, to->name (MOD_SNAME_PFX ".ent"), crc_p);
   18306         2549 :   dump.outdent ();
   18307         2549 : }
   18308              : 
   18309              : bool
   18310         2768 : module_state::read_entities (unsigned count, unsigned lwm, unsigned hwm)
   18311              : {
   18312         2768 :   trees_in sec (this);
   18313              : 
   18314         2768 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".ent"))
   18315              :     return false;
   18316              : 
   18317         3246 :   dump () && dump ("Reading entities");
   18318         2768 :   dump.indent ();
   18319              : 
   18320      1276378 :   for (binding_slot *slot = entity_ary->begin () + entity_lwm; count--; slot++)
   18321              :     {
   18322      1273610 :       unsigned snum = sec.u ();
   18323      1273610 :       if (snum && (snum - lwm) >= (hwm - lwm))
   18324            0 :         sec.set_overrun ();
   18325      1273610 :       if (sec.get_overrun ())
   18326              :         break;
   18327              : 
   18328      1273610 :       if (snum)
   18329      1270810 :         slot->set_lazy (snum << 2);
   18330              :     }
   18331              : 
   18332         2768 :   dump.outdent ();
   18333         2768 :   if (!sec.end (from ()))
   18334              :     return false;
   18335              :   return true;
   18336         2768 : }
   18337              : 
   18338              : /* Write the pending table to MOD_SNAME_PFX.pnd
   18339              : 
   18340              :    The pending table holds information about clusters that need to be
   18341              :    loaded because they contain information about something that is not
   18342              :    found by namespace-scope lookup.
   18343              : 
   18344              :    The three cases are:
   18345              : 
   18346              :    (a) Template (maybe-partial) specializations that we have
   18347              :    instantiated or defined.  When an importer needs to instantiate
   18348              :    that template, they /must have/ the partial, explicit & extern
   18349              :    specializations available.  If they have the other specializations
   18350              :    available, they'll have less work to do.  Thus, when we're about to
   18351              :    instantiate FOO, we have to be able to ask 'are there any
   18352              :    specialization of FOO in our imports?'.
   18353              : 
   18354              :    (b) (Maybe-implicit) member functions definitions.  A class could
   18355              :    be defined in one header, and an inline member defined in a
   18356              :    different header (this occurs in the STL).  Similarly, like the
   18357              :    specialization case, an implicit member function could have been
   18358              :    'instantiated' in one module, and it'd be nice to not have to
   18359              :    reinstantiate it in another.
   18360              : 
   18361              :    (c) Classes completed elsewhere.  A class could be declared in one
   18362              :    header and defined in another.  We need to know to load the class
   18363              :    definition before looking in it.  It does highlight an issue --
   18364              :    there could be an intermediate import between the outermost containing
   18365              :    namespace-scope class and the innermost being-defined class.  This is
   18366              :    actually possible with all of these cases, so be aware -- we're not
   18367              :    just talking of one level of import to get to the innermost namespace.
   18368              : 
   18369              :    This gets complicated fast, it took me multiple attempts to even
   18370              :    get something remotely working.  Partially because I focussed on
   18371              :    optimizing what I think turns out to be a smaller problem, given
   18372              :    the known need to do the more general case *anyway*.  I document
   18373              :    the smaller problem, because it does appear to be the natural way
   18374              :    to do it.  It's trap!
   18375              : 
   18376              :    **** THE TRAP
   18377              : 
   18378              :    Let's refer to the primary template or the containing class as the
   18379              :    KEY.  And the specialization or member as the PENDING-ENTITY.  (To
   18380              :    avoid having to say those mouthfuls all the time.)
   18381              : 
   18382              :    In either case, we have an entity and we need some way of mapping
   18383              :    that to a set of entities that need to be loaded before we can
   18384              :    proceed with whatever processing of the entity we were going to do.
   18385              : 
   18386              :    We need to link the key to the pending-entity in some way.  Given a
   18387              :    key, tell me the pending-entities I need to have loaded.  However
   18388              :    we tie the key to the pending-entity must not rely on the key being
   18389              :    loaded -- that'd defeat the lazy loading scheme.
   18390              : 
   18391              :    As the key will be an import in we know its entity number (either
   18392              :    because we imported it, or we're writing it out too).  Thus we can
   18393              :    generate a map of key-indices to pending-entities.  The
   18394              :    pending-entity indices will be into our span of the entity table,
   18395              :    and thus allow them to be lazily loaded.  The key index will be
   18396              :    into another slot of the entity table.  Notice that this checking
   18397              :    could be expensive, we don't want to iterate over a bunch of
   18398              :    pending-entity indices (across multiple imports), every time we're
   18399              :    about do to the thing with the key.  We need to quickly determine
   18400              :    'definitely nothing needed'.
   18401              : 
   18402              :    That's almost good enough, except that key indices are not unique
   18403              :    in a couple of cases :( Specifically the Global Module or a module
   18404              :    partition can result in multiple modules assigning an entity index
   18405              :    for the key.  The decl-merging on loading will detect that so we
   18406              :    only have one Key loaded, and in the entity hash it'll indicate the
   18407              :    entity index of first load.  Which might be different to how we
   18408              :    know it.  Notice this is restricted to GM entities or this-module
   18409              :    entities.  Foreign imports cannot have this.
   18410              : 
   18411              :    We can simply resolve this in the direction of how this module
   18412              :    referred to the key to how the importer knows it.  Look in the
   18413              :    entity table slot that we nominate, maybe lazy load it, and then
   18414              :    lookup the resultant entity in the entity hash to learn how the
   18415              :    importer knows it.
   18416              : 
   18417              :    But we need to go in the other direction :( Given the key, find all
   18418              :    the index-aliases of that key.  We can partially solve that by
   18419              :    adding an alias hash table.  Whenever we load a merged decl, add or
   18420              :    augment a mapping from the entity (or its entity-index) to the
   18421              :    newly-discovered index.  Then when we look for pending entities of
   18422              :    a key, we also iterate over this aliases this mapping provides.
   18423              : 
   18424              :    But that requires the alias to be loaded.  And that's not
   18425              :    necessarily true.
   18426              : 
   18427              :    *** THE SIMPLER WAY
   18428              : 
   18429              :    The remaining fixed thing we have is the innermost namespace
   18430              :    containing the ultimate namespace-scope container of the key and
   18431              :    the name of that container (which might be the key itself).  I.e. a
   18432              :    namespace-decl/identifier/module tuple.  Let's call this the
   18433              :    top-key.  We'll discover that the module is not important here,
   18434              :    because of cross-module possibilities mentioned in case #c above.
   18435              :    We can't markup namespace-binding slots.  The best we can do is
   18436              :    mark the binding vector with 'there's something here', and have
   18437              :    another map from namespace/identifier pairs to a vector of pending
   18438              :    entity indices.
   18439              : 
   18440              :    Maintain a pending-entity map.  This is keyed by top-key, and
   18441              :    maps to a vector of pending-entity indices.  On the binding vector
   18442              :    have flags saying whether the pending-name-entity map has contents.
   18443              :    (We might want to further extend the key to be GM-vs-Partition and
   18444              :    specialization-vs-member, but let's not get ahead of ourselves.)
   18445              : 
   18446              :    For every key-like entity, find the outermost namespace-scope
   18447              :    name.  Use that to lookup in the pending-entity map and then make
   18448              :    sure the specified entities are loaded.
   18449              : 
   18450              :    An optimization might be to have a flag in each key-entity saying
   18451              :    that its top key might be in the entity table.  It's not clear to
   18452              :    me how to set that flag cheaply -- cheaper than just looking.
   18453              : 
   18454              :    FIXME: It'd be nice to have a bit in decls to tell us whether to
   18455              :    even try this.  We can have a 'already done' flag, that we set when
   18456              :    we've done KLASS's lazy pendings.  When we import a module that
   18457              :    registers pendings on the same top-key as KLASS we need to clear
   18458              :    the flag.  A recursive walk of the top-key clearing the bit will
   18459              :    suffice.  Plus we only need to recurse on classes that have the bit
   18460              :    set.  (That means we need to set the bit on parents of KLASS here,
   18461              :    don't forget.)  However, first: correctness, second: efficiency.  */
   18462              : 
   18463              : unsigned
   18464         2815 : module_state::write_pendings (elf_out *to, vec<depset *> depsets,
   18465              :                               depset::hash &table, unsigned *crc_p)
   18466              : {
   18467         3115 :   dump () && dump ("Writing pending-entities");
   18468         2815 :   dump.indent ();
   18469              : 
   18470         2815 :   trees_out sec (to, this, table);
   18471         2815 :   sec.begin ();
   18472              : 
   18473         2815 :   unsigned count = 0;
   18474         2815 :   tree cache_ns = NULL_TREE;
   18475         2815 :   tree cache_id = NULL_TREE;
   18476         2815 :   unsigned cache_section = ~0;
   18477      1546870 :   for (unsigned ix = 0; ix < depsets.length (); ix++)
   18478              :     {
   18479      1541240 :       depset *d = depsets[ix];
   18480              : 
   18481      1541240 :       if (d->is_binding ())
   18482       920274 :         continue;
   18483              : 
   18484      1377426 :       if (d->is_import ())
   18485            0 :         continue;
   18486              : 
   18487      1377426 :       if (!d->is_pending_entity ())
   18488       756460 :         continue;
   18489              : 
   18490       620966 :       tree key_decl = nullptr;
   18491       620966 :       tree key_ns = find_pending_key (d->get_entity (), &key_decl);
   18492       620966 :       tree key_name = DECL_NAME (key_decl);
   18493              : 
   18494       620966 :       if (IDENTIFIER_ANON_P (key_name))
   18495              :         {
   18496            6 :           gcc_checking_assert (IDENTIFIER_LAMBDA_P (key_name));
   18497           12 :           if (tree attached = LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (key_decl)))
   18498            6 :             key_name = DECL_NAME (attached);
   18499              :           else
   18500              :             {
   18501              :               /* There's nothing to attach it to.  Must
   18502              :                  always reinstantiate.  */
   18503            0 :               dump ()
   18504            0 :                 && dump ("Unattached lambda %N[%u] section:%u",
   18505            0 :                          d->get_entity_kind () == depset::EK_DECL
   18506              :                          ? "Member" : "Specialization", d->get_entity (),
   18507              :                          d->cluster, d->section);
   18508            0 :               continue;
   18509              :             }
   18510              :         }
   18511              : 
   18512       620966 :       char const *also = "";
   18513       620966 :       if (d->section == cache_section
   18514       416363 :           && key_ns == cache_ns
   18515       416363 :           && key_name == cache_id)
   18516              :         /* Same section & key as previous, no need to repeat ourselves.  */
   18517              :         also = "also ";
   18518              :       else
   18519              :         {
   18520       278941 :           cache_ns = key_ns;
   18521       278941 :           cache_id = key_name;
   18522       278941 :           cache_section = d->section;
   18523       278941 :           gcc_checking_assert (table.find_dependency (cache_ns));
   18524       278941 :           sec.tree_node (cache_ns);
   18525       278941 :           sec.tree_node (cache_id);
   18526       278941 :           sec.u (d->cluster);
   18527       278941 :           count++;
   18528              :         }
   18529       622422 :       dump () && dump ("Pending %s %N entity:%u section:%u %skeyed to %P",
   18530          728 :                        d->get_entity_kind () == depset::EK_DECL
   18531              :                        ? "member" : "specialization", d->get_entity (),
   18532              :                        d->cluster, cache_section, also, cache_ns, cache_id);
   18533              :       }
   18534         2815 :   sec.end (to, to->name (MOD_SNAME_PFX ".pnd"), crc_p);
   18535         2815 :   dump.outdent ();
   18536              : 
   18537         2815 :   return count;
   18538         2815 : }
   18539              : 
   18540              : bool
   18541         1604 : module_state::read_pendings (unsigned count)
   18542              : {
   18543         1604 :   trees_in sec (this);
   18544              : 
   18545         1604 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".pnd"))
   18546              :     return false;
   18547              : 
   18548         1926 :   dump () && dump ("Reading %u pendings", count);
   18549         1604 :   dump.indent ();
   18550              : 
   18551       267568 :   for (unsigned ix = 0; ix != count; ix++)
   18552              :     {
   18553       265964 :       pending_key key;
   18554       265964 :       unsigned index;
   18555              : 
   18556       265964 :       key.ns = sec.tree_node ();
   18557       265964 :       key.id = sec.tree_node ();
   18558       265964 :       index = sec.u ();
   18559              : 
   18560       265964 :       if (!key.ns || !key.id
   18561       265964 :           || !(TREE_CODE (key.ns) == NAMESPACE_DECL
   18562       265964 :                && !DECL_NAMESPACE_ALIAS (key.ns))
   18563       265964 :           || !identifier_p (key.id)
   18564       531928 :           || index >= entity_num)
   18565            0 :         sec.set_overrun ();
   18566              : 
   18567       265964 :       if (sec.get_overrun ())
   18568              :         break;
   18569              : 
   18570       266773 :       dump () && dump ("Pending:%u keyed to %P", index, key.ns, key.id);
   18571              : 
   18572       265964 :       index += entity_lwm;
   18573       265964 :       auto &vec = pending_table->get_or_insert (key);
   18574       265964 :       vec.safe_push (index);
   18575              :     }
   18576              : 
   18577         1604 :   dump.outdent ();
   18578         1604 :   if (!sec.end (from ()))
   18579              :     return false;
   18580              :   return true;
   18581         1604 : }
   18582              : 
   18583              : /* Read & write locations.  */
   18584              : enum loc_kind {
   18585              :   LK_ORDINARY,
   18586              :   LK_MACRO,
   18587              :   LK_IMPORT_ORDINARY,
   18588              :   LK_IMPORT_MACRO,
   18589              :   LK_ADHOC,
   18590              :   LK_RESERVED,
   18591              : };
   18592              : 
   18593              : static const module_state *
   18594         4892 : module_for_ordinary_loc (location_t loc)
   18595              : {
   18596         4892 :   unsigned pos = 0;
   18597         4892 :   unsigned len = ool->length () - pos;
   18598              : 
   18599         9787 :   while (len)
   18600              :     {
   18601         4895 :       unsigned half = len / 2;
   18602         4895 :       module_state *probe = (*ool)[pos + half];
   18603         4895 :       if (loc < probe->ordinary_locs.first)
   18604              :         len = half;
   18605         4892 :       else if (loc < probe->ordinary_locs.first + probe->ordinary_locs.second)
   18606              :         return probe;
   18607              :       else
   18608              :         {
   18609            0 :           pos += half + 1;
   18610            0 :           len = len - (half + 1);
   18611              :         }
   18612              :     }
   18613              : 
   18614              :   return nullptr;
   18615              : }
   18616              : 
   18617              : static const module_state *
   18618            6 : module_for_macro_loc (location_t loc)
   18619              : {
   18620            6 :   unsigned pos = 1;
   18621            6 :   unsigned len = modules->length () - pos;
   18622              : 
   18623            6 :   while (len)
   18624              :     {
   18625            6 :       unsigned half = len / 2;
   18626            6 :       module_state *probe = (*modules)[pos + half];
   18627            6 :       if (loc < probe->macro_locs.first)
   18628              :         {
   18629            0 :           pos += half + 1;
   18630            0 :           len = len - (half + 1);
   18631              :         }
   18632            6 :       else if (loc >= probe->macro_locs.first + probe->macro_locs.second)
   18633              :         len = half;
   18634              :       else
   18635              :         return probe;
   18636              :     }
   18637              : 
   18638              :   return NULL;
   18639              : }
   18640              : 
   18641              : location_t
   18642         1329 : module_state::imported_from () const
   18643              : {
   18644         1329 :   location_t from = loc;
   18645         1329 :   line_map_ordinary const *fmap
   18646         1329 :     = linemap_check_ordinary (linemap_lookup (line_table, from));
   18647              : 
   18648         1329 :   if (MAP_MODULE_P (fmap))
   18649         1329 :     from = linemap_included_from (fmap);
   18650              : 
   18651         1329 :   return from;
   18652              : }
   18653              : 
   18654              : /* Note that LOC will need writing.  This allows us to prune locations
   18655              :    that are not needed.  */
   18656              : 
   18657              : bool
   18658     30679110 : module_state::note_location (location_t loc)
   18659              : {
   18660     30679110 :   bool added = false;
   18661     30679110 :   if (!macro_loc_table && !ord_loc_table)
   18662              :     ;
   18663     30679110 :   else if (loc < RESERVED_LOCATION_COUNT)
   18664              :     ;
   18665     27935256 :   else if (IS_ADHOC_LOC (loc))
   18666              :     {
   18667      3538394 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18668      3538394 :       note_location (locus);
   18669      3538394 :       source_range range = get_range_from_loc (line_table, loc);
   18670      3538394 :       if (range.m_start != locus)
   18671      3411175 :         note_location (range.m_start);
   18672      3538394 :       note_location (range.m_finish);
   18673              :     }
   18674     24396862 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18675              :     {
   18676      1673838 :       if (spans.macro (loc))
   18677              :         {
   18678      1673832 :           const line_map *map = linemap_lookup (line_table, loc);
   18679      1673832 :           const line_map_macro *mac_map = linemap_check_macro (map);
   18680      1673832 :           hashval_t hv = macro_loc_traits::hash (mac_map);
   18681      1673832 :           macro_loc_info *slot
   18682      1673832 :             = macro_loc_table->find_slot_with_hash (mac_map, hv, INSERT);
   18683      1673832 :           if (!slot->src)
   18684              :             {
   18685       172618 :               slot->src = mac_map;
   18686       172618 :               slot->remap = 0;
   18687              :               // Expansion locations could themselves be from a
   18688              :               // macro, we need to note them all.
   18689       172618 :               note_location (mac_map->m_expansion);
   18690       172618 :               gcc_checking_assert (mac_map->n_tokens);
   18691       172618 :               location_t tloc = UNKNOWN_LOCATION;
   18692      6564488 :               for (unsigned ix = mac_map->n_tokens * 2; ix--;)
   18693      6391870 :                 if (mac_map->macro_locations[ix] != tloc)
   18694              :                   {
   18695      3372635 :                     tloc = mac_map->macro_locations[ix];
   18696      3372635 :                     note_location (tloc);
   18697              :                   }
   18698              :               added = true;
   18699              :             }
   18700              :         }
   18701              :     }
   18702     22723024 :   else if (IS_ORDINARY_LOC (loc))
   18703              :     {
   18704     22723024 :       if (spans.ordinary (loc))
   18705              :         {
   18706     22718127 :           const line_map *map = linemap_lookup (line_table, loc);
   18707     22718127 :           const line_map_ordinary *ord_map = linemap_check_ordinary (map);
   18708     22718127 :           ord_loc_info lkup;
   18709     22718127 :           lkup.src = ord_map;
   18710     22718127 :           lkup.span = loc_one << ord_map->m_column_and_range_bits;
   18711     22718127 :           lkup.offset = (loc - MAP_START_LOCATION (ord_map)) & ~(lkup.span - 1);
   18712     22718127 :           lkup.remap = 0;
   18713     22718127 :           ord_loc_info *slot = (ord_loc_table->find_slot_with_hash
   18714     22718127 :                                 (lkup, ord_loc_traits::hash (lkup), INSERT));
   18715     22718127 :           if (!slot->src)
   18716              :             {
   18717      2558407 :               *slot = lkup;
   18718      2558407 :               added = true;
   18719              :             }
   18720              :         }
   18721              :     }
   18722              :   else
   18723            0 :     gcc_unreachable ();
   18724     30679110 :   return added;
   18725              : }
   18726              : 
   18727              : /* If we're not streaming, record that we need location LOC.
   18728              :    Otherwise stream it.  */
   18729              : 
   18730              : void
   18731     45955349 : module_state::write_location (bytes_out &sec, location_t loc)
   18732              : {
   18733     45955349 :   if (!sec.streaming_p ())
   18734              :     {
   18735     16359016 :       note_location (loc);
   18736     16359016 :       return;
   18737              :     }
   18738              : 
   18739     29596333 :   if (loc < RESERVED_LOCATION_COUNT)
   18740              :     {
   18741      2822228 :       dump (dumper::LOCATION) && dump ("Reserved location %K", loc);
   18742      2822210 :       sec.loc (LK_RESERVED + loc);
   18743              :     }
   18744     26774123 :   else if (IS_ADHOC_LOC (loc))
   18745              :     {
   18746      3454731 :       dump (dumper::LOCATION) && dump ("Adhoc location");
   18747      3454728 :       sec.u (LK_ADHOC);
   18748      3454728 :       location_t locus = get_location_from_adhoc_loc (line_table, loc);
   18749      3454728 :       write_location (sec, locus);
   18750      3454728 :       source_range range = get_range_from_loc (line_table, loc);
   18751      3454728 :       if (range.m_start == locus)
   18752              :         /* Compress.  */
   18753       119605 :         range.m_start = UNKNOWN_LOCATION;
   18754      3454728 :       write_location (sec, range.m_start);
   18755      3454728 :       write_location (sec, range.m_finish);
   18756      3454728 :       unsigned discriminator = get_discriminator_from_adhoc_loc (line_table, loc);
   18757      3454728 :       sec.u (discriminator);
   18758              :     }
   18759     23319395 :   else if (loc >= LINEMAPS_MACRO_LOWEST_LOCATION (line_table))
   18760              :     {
   18761      1663664 :       const macro_loc_info *info = nullptr;
   18762      1663664 :       line_map_uint_t offset = 0;
   18763      1663664 :       if (unsigned hwm = macro_loc_remap->length ())
   18764              :         {
   18765      1663658 :           info = macro_loc_remap->begin ();
   18766     24489428 :           while (hwm != 1)
   18767              :             {
   18768     21162112 :               unsigned mid = hwm / 2;
   18769     21162112 :               if (MAP_START_LOCATION (info[mid].src) <= loc)
   18770              :                 {
   18771     10688358 :                   info += mid;
   18772     10688358 :                   hwm -= mid;
   18773              :                 }
   18774              :               else
   18775              :                 hwm = mid;
   18776              :             }
   18777      1663658 :           offset = loc - MAP_START_LOCATION (info->src);
   18778      1663658 :           if (offset > info->src->n_tokens)
   18779            0 :             info = nullptr;
   18780              :         }
   18781              : 
   18782      1663664 :       gcc_checking_assert (bool (info) == bool (spans.macro (loc)));
   18783              : 
   18784      1663664 :       if (info)
   18785              :         {
   18786      1663658 :           offset += info->remap;
   18787      1663658 :           sec.u (LK_MACRO);
   18788      1663658 :           sec.loc (offset);
   18789      1663658 :           dump (dumper::LOCATION)
   18790            9 :             && dump ("Macro location %K output %K", loc, offset);
   18791              :         }
   18792            6 :       else if (const module_state *import = module_for_macro_loc (loc))
   18793              :         {
   18794            6 :           auto off = loc - import->macro_locs.first;
   18795            6 :           sec.u (LK_IMPORT_MACRO);
   18796            6 :           sec.u (import->remap);
   18797            6 :           sec.loc (off);
   18798            6 :           dump (dumper::LOCATION)
   18799            0 :             && dump ("Imported macro location %K output %u:%K",
   18800            0 :                      loc, import->remap, off);
   18801              :         }
   18802              :       else
   18803            0 :         gcc_unreachable ();
   18804              :     }
   18805     21655731 :   else if (IS_ORDINARY_LOC (loc))
   18806              :     {
   18807              :       /* If we ran out of locations for imported decls, this location could
   18808              :          be a module unit's location.  In that case, remap the location
   18809              :          to be where we imported the module from.  */
   18810     21655731 :       if (spans.locations_exhausted_p () || CHECKING_P)
   18811              :         {
   18812     21655731 :           const line_map_ordinary *map
   18813     21655731 :             = linemap_check_ordinary (linemap_lookup (line_table, loc));
   18814     21655731 :           if (MAP_MODULE_P (map) && loc == MAP_START_LOCATION (map))
   18815              :             {
   18816            0 :               gcc_checking_assert (spans.locations_exhausted_p ());
   18817            0 :               write_location (sec, linemap_included_from (map));
   18818            0 :               return;
   18819              :             }
   18820              :         }
   18821              : 
   18822     21655731 :       const ord_loc_info *info = nullptr;
   18823     21655731 :       line_map_uint_t offset = 0;
   18824     21655731 :       if (line_map_uint_t hwm = ord_loc_remap->length ())
   18825              :         {
   18826     21655731 :           info = ord_loc_remap->begin ();
   18827    322965636 :           while (hwm != 1)
   18828              :             {
   18829    279654174 :               auto mid = hwm / 2;
   18830    279654174 :               if (MAP_START_LOCATION (info[mid].src) + info[mid].offset <= loc)
   18831              :                 {
   18832    146390469 :                   info += mid;
   18833    146390469 :                   hwm -= mid;
   18834              :                 }
   18835              :               else
   18836              :                 hwm = mid;
   18837              :             }
   18838     21655731 :           offset = loc - MAP_START_LOCATION (info->src) - info->offset;
   18839     21655731 :           if (offset > info->span)
   18840         4892 :             info = nullptr;
   18841              :         }
   18842              : 
   18843     21655731 :       gcc_checking_assert (bool (info) == bool (spans.ordinary (loc)));
   18844              : 
   18845     21655731 :       if (info)
   18846              :         {
   18847     21650839 :           offset += info->remap;
   18848     21650839 :           sec.u (LK_ORDINARY);
   18849     21650839 :           sec.loc (offset);
   18850              : 
   18851     21650839 :           dump (dumper::LOCATION)
   18852           78 :             && dump ("Ordinary location %K output %K", loc, offset);
   18853              :         }
   18854         4892 :       else if (const module_state *import = module_for_ordinary_loc (loc))
   18855              :         {
   18856         4892 :           auto off = loc - import->ordinary_locs.first;
   18857         4892 :           sec.u (LK_IMPORT_ORDINARY);
   18858         4892 :           sec.u (import->remap);
   18859         4892 :           sec.loc (off);
   18860         4892 :           dump (dumper::LOCATION)
   18861            0 :             && dump ("Imported ordinary location %K output %u:%K",
   18862            0 :                      loc, import->remap, off);
   18863              :         }
   18864              :       else
   18865            0 :         gcc_unreachable ();
   18866              :     }
   18867              :   else
   18868            0 :     gcc_unreachable ();
   18869              : }
   18870              : 
   18871              : location_t
   18872     21253293 : module_state::read_location (bytes_in &sec) const
   18873              : {
   18874     21253293 :   location_t locus = UNKNOWN_LOCATION;
   18875     21253293 :   unsigned kind = sec.u ();
   18876     21253293 :   switch (kind)
   18877              :      {
   18878      1961276 :     default:
   18879      1961276 :       {
   18880      1961276 :         if (kind < LK_RESERVED + RESERVED_LOCATION_COUNT)
   18881      1961276 :           locus = location_t (kind - LK_RESERVED);
   18882              :         else
   18883            0 :           sec.set_overrun ();
   18884      1961276 :         dump (dumper::LOCATION)
   18885            0 :           && dump ("Reserved location %K", locus);
   18886              :       }
   18887              :       break;
   18888              : 
   18889      2380903 :      case LK_ADHOC:
   18890      2380903 :       {
   18891      2380903 :         dump (dumper::LOCATION) && dump ("Adhoc location");
   18892      2380903 :         locus = read_location (sec);
   18893      2380903 :         source_range range;
   18894      2380903 :         range.m_start = read_location (sec);
   18895      2380903 :         if (range.m_start == UNKNOWN_LOCATION)
   18896        76299 :           range.m_start = locus;
   18897      2380903 :         range.m_finish = read_location (sec);
   18898      2380903 :         unsigned discriminator = sec.u ();
   18899      2380903 :         if (locus != loc && range.m_start != loc && range.m_finish != loc)
   18900      2380903 :           locus = line_table->get_or_create_combined_loc (locus, range,
   18901              :                                                           nullptr, discriminator);
   18902              :       }
   18903              :       break;
   18904              : 
   18905      1403676 :     case LK_MACRO:
   18906      1403676 :       {
   18907      1403676 :         auto off = sec.loc ();
   18908              : 
   18909      1403676 :         if (macro_locs.second)
   18910              :           {
   18911      1403676 :             if (off < macro_locs.second)
   18912      1403676 :               locus = off + macro_locs.first;
   18913              :             else
   18914            0 :               sec.set_overrun ();
   18915              :           }
   18916              :         else
   18917            0 :           locus = loc;
   18918      1403676 :         dump (dumper::LOCATION)
   18919            0 :           && dump ("Macro %K becoming %K", off, locus);
   18920              :       }
   18921              :       break;
   18922              : 
   18923     15505463 :     case LK_ORDINARY:
   18924     15505463 :       {
   18925     15505463 :         auto off = sec.loc ();
   18926     15505463 :         if (ordinary_locs.second)
   18927              :           {
   18928     15505463 :             if (off < ordinary_locs.second)
   18929     15505463 :               locus = off + ordinary_locs.first;
   18930              :             else
   18931            0 :               sec.set_overrun ();
   18932              :           }
   18933              :         else
   18934            0 :           locus = loc;
   18935              : 
   18936     15505463 :         dump (dumper::LOCATION)
   18937            0 :           && dump ("Ordinary location %K becoming %K", off, locus);
   18938              :       }
   18939              :       break;
   18940              : 
   18941         1975 :      case LK_IMPORT_MACRO:
   18942         1975 :      case LK_IMPORT_ORDINARY:
   18943         1975 :        {
   18944         1975 :          unsigned mod = sec.u ();
   18945         1975 :          location_t off = sec.loc ();
   18946         1975 :          const module_state *import = NULL;
   18947              : 
   18948         1975 :          if (!mod && !slurp->remap)
   18949              :            /* This is an early read of a partition location during the
   18950              :               read of our ordinary location map.  */
   18951              :            import = this;
   18952              :          else
   18953              :            {
   18954         1975 :              mod = slurp->remap_module (mod);
   18955         1975 :              if (!mod)
   18956            0 :                sec.set_overrun ();
   18957              :              else
   18958         1975 :                import = (*modules)[mod];
   18959              :            }
   18960              : 
   18961         1975 :          if (import)
   18962              :            {
   18963         1975 :              if (kind == LK_IMPORT_MACRO)
   18964              :                {
   18965            6 :                  if (!import->macro_locs.second)
   18966            0 :                    locus = import->loc;
   18967            6 :                  else if (off < import->macro_locs.second)
   18968            6 :                    locus = off + import->macro_locs.first;
   18969              :                  else
   18970            0 :                    sec.set_overrun ();
   18971              :                }
   18972              :              else
   18973              :                {
   18974         1969 :                  if (!import->ordinary_locs.second)
   18975            0 :                    locus = import->loc;
   18976         1969 :                  else if (off < import->ordinary_locs.second)
   18977         1969 :                    locus = import->ordinary_locs.first + off;
   18978              :                  else
   18979            0 :                    sec.set_overrun ();
   18980              :                }
   18981              :            }
   18982              :        }
   18983              :        break;
   18984              :     }
   18985              : 
   18986     21253293 :   return locus;
   18987              : }
   18988              : 
   18989              : /* Allocate hash tables to record needed locations.  */
   18990              : 
   18991              : void
   18992         2844 : module_state::write_init_maps ()
   18993              : {
   18994         2844 :   macro_loc_table = new hash_table<macro_loc_traits> (EXPERIMENT (1, 400));
   18995         2844 :   ord_loc_table = new hash_table<ord_loc_traits> (EXPERIMENT (1, 400));
   18996         2844 : }
   18997              : 
   18998              : /* Prepare the span adjustments.  We prune unneeded locations -- at
   18999              :    this point every needed location must have been seen by
   19000              :    note_location.  */
   19001              : 
   19002              : range_t
   19003         2815 : module_state::write_prepare_maps (module_state_config *cfg, bool has_partitions)
   19004              : {
   19005         3115 :   dump () && dump ("Preparing locations");
   19006         2815 :   dump.indent ();
   19007              : 
   19008         3115 :   dump () && dump ("Reserved locations [%K,%K) macro [%K,%K)",
   19009          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.first,
   19010          300 :                    spans[loc_spans::SPAN_RESERVED].ordinary.second,
   19011          300 :                    spans[loc_spans::SPAN_RESERVED].macro.first,
   19012          300 :                    spans[loc_spans::SPAN_RESERVED].macro.second);
   19013              : 
   19014         2815 :   range_t info {0, 0};
   19015              : 
   19016              :   // Sort the noted lines.
   19017         2815 :   vec_alloc (ord_loc_remap, ord_loc_table->size ());
   19018         2815 :   for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   19019      2546199 :        iter != end; ++iter)
   19020      2543384 :     ord_loc_remap->quick_push (*iter);
   19021         2815 :   ord_loc_remap->qsort (&ord_loc_info::compare);
   19022              : 
   19023              :   // Note included-from maps.
   19024         2815 :   bool added = false;
   19025         2815 :   const line_map_ordinary *current = nullptr;
   19026      2551829 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19027      2546199 :        iter != end; ++iter)
   19028      2543384 :     if (iter->src != current)
   19029              :       {
   19030        36027 :         current = iter->src;
   19031        14541 :         for (auto probe = current;
   19032        36027 :              auto from = linemap_included_from (probe);
   19033        14541 :              probe = linemap_check_ordinary (linemap_lookup (line_table, from)))
   19034              :           {
   19035        32820 :             if (has_partitions)
   19036              :               {
   19037              :                 // Partition locations need to elide their module map
   19038              :                 // entry.
   19039          223 :                 probe
   19040          223 :                   = linemap_check_ordinary (linemap_lookup (line_table, from));
   19041          223 :                 if (MAP_MODULE_P (probe))
   19042          190 :                   from = linemap_included_from (probe);
   19043              :               }
   19044              : 
   19045        32820 :             if (!note_location (from))
   19046              :               break;
   19047        14541 :             added = true;
   19048        14541 :           }
   19049              :       }
   19050         2815 :   if (added)
   19051              :     {
   19052              :       // Reconstruct the line array as we added items to the hash table.
   19053          529 :       vec_free (ord_loc_remap);
   19054          529 :       vec_alloc (ord_loc_remap, ord_loc_table->size ());
   19055          529 :       for (auto iter = ord_loc_table->begin (), end = ord_loc_table->end ();
   19056      2546402 :            iter != end; ++iter)
   19057      2545873 :         ord_loc_remap->quick_push (*iter);
   19058          529 :       ord_loc_remap->qsort (&ord_loc_info::compare);
   19059              :     }
   19060         2815 :   delete ord_loc_table;
   19061         2815 :   ord_loc_table = nullptr;
   19062              : 
   19063              :   // Merge (sufficiently) adjacent spans, and calculate remapping.
   19064         2815 :   constexpr line_map_uint_t adjacency = 2; // Allow 2 missing lines.
   19065         5630 :   auto begin = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19066         2815 :   auto dst = begin;
   19067         2815 :   line_map_uint_t offset = 0;
   19068         2815 :   unsigned range_bits = 0;
   19069         2815 :   ord_loc_info *base = nullptr;
   19070      2560740 :   for (auto iter = begin; iter != end; ++iter)
   19071              :     {
   19072      2557925 :       if (base && iter->src == base->src)
   19073              :         {
   19074      4801264 :           if (base->offset + base->span +
   19075      2526781 :               ((adjacency << base->src->m_column_and_range_bits)
   19076              :                // If there are few c&r bits, allow further separation.
   19077      2526781 :                | (adjacency << 4))
   19078      2526781 :               >= iter->offset)
   19079              :             {
   19080              :               // Merge.
   19081      2274483 :               offset -= base->span;
   19082      2274483 :               base->span = iter->offset + iter->span - base->offset;
   19083      2274483 :               offset += base->span;
   19084      2274483 :               continue;
   19085              :             }
   19086              :         }
   19087        31144 :       else if (range_bits < iter->src->m_range_bits)
   19088         2716 :         range_bits = iter->src->m_range_bits;
   19089              : 
   19090       283442 :       offset += ((loc_one << iter->src->m_range_bits) - 1);
   19091       283442 :       offset &= ~((loc_one << iter->src->m_range_bits) - 1);
   19092       283442 :       iter->remap = offset;
   19093       283442 :       offset += iter->span;
   19094       283442 :       base = dst;
   19095       283442 :       *dst++ = *iter;
   19096              :     }
   19097         2815 :   ord_loc_remap->truncate (dst - begin);
   19098              : 
   19099         2815 :   info.first = ord_loc_remap->length ();
   19100         2815 :   cfg->ordinary_locs = offset;
   19101         2815 :   cfg->loc_range_bits = range_bits;
   19102         3115 :   dump () && dump ("Ordinary maps:%K locs:%K range_bits:%u",
   19103              :                    info.first,
   19104              :                    cfg->ordinary_locs,
   19105              :                    cfg->loc_range_bits);
   19106              : 
   19107              :   // Remap the macro locations.
   19108         2815 :   vec_alloc (macro_loc_remap, macro_loc_table->size ());
   19109         2815 :   for (auto iter = macro_loc_table->begin (), end = macro_loc_table->end ();
   19110       175433 :        iter != end; ++iter)
   19111       172618 :     macro_loc_remap->quick_push (*iter);
   19112         2815 :   delete macro_loc_table;
   19113         2815 :   macro_loc_table = nullptr;
   19114              : 
   19115         2815 :   macro_loc_remap->qsort (&macro_loc_info::compare);
   19116         2815 :   offset = 0;
   19117         8445 :   for (auto iter = macro_loc_remap->begin (), end = macro_loc_remap->end ();
   19118       175433 :        iter != end; ++iter)
   19119              :     {
   19120       172618 :       auto mac = iter->src;
   19121       172618 :       iter->remap = offset;
   19122       172618 :       offset += mac->n_tokens;
   19123              :     }
   19124         2815 :   info.second = macro_loc_remap->length ();
   19125         2815 :   cfg->macro_locs = offset;
   19126              : 
   19127         3115 :   dump () && dump ("Macro maps:%K locs:%K", info.second, cfg->macro_locs);
   19128              : 
   19129         2815 :   dump.outdent ();
   19130              : 
   19131              :   // If we have no ordinary locs, we must also have no macro locs.
   19132         2815 :   gcc_checking_assert (cfg->ordinary_locs || !cfg->macro_locs);
   19133              : 
   19134         2815 :   return info;
   19135              : }
   19136              : 
   19137              : bool
   19138         3048 : module_state::read_prepare_maps (const module_state_config *cfg)
   19139              : {
   19140         3048 :   location_t ordinary = line_table->highest_location + 1;
   19141         3048 :   ordinary += cfg->ordinary_locs;
   19142              : 
   19143         3048 :   location_t macro = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19144         3048 :   macro -= cfg->macro_locs;
   19145              : 
   19146         3048 :   if (ordinary < LINE_MAP_MAX_LOCATION_WITH_COLS
   19147         3048 :       && macro >= LINE_MAP_MAX_LOCATION)
   19148              :     /* OK, we have enough locations.  */
   19149              :     return true;
   19150              : 
   19151            0 :   ordinary_locs.first = ordinary_locs.second = 0;
   19152            0 :   macro_locs.first = macro_locs.second = 0;
   19153              : 
   19154            0 :   spans.report_location_exhaustion (loc);
   19155              : 
   19156              :   return false;
   19157              : }
   19158              : 
   19159              : /* Write & read the location maps. Not called if there are no
   19160              :    locations.   */
   19161              : 
   19162              : void
   19163         2716 : module_state::write_ordinary_maps (elf_out *to, range_t &info,
   19164              :                                    bool has_partitions, unsigned *crc_p)
   19165              : {
   19166         2994 :   dump () && dump ("Writing ordinary location maps");
   19167         2716 :   dump.indent ();
   19168              : 
   19169         2716 :   vec<const char *> filenames;
   19170         2716 :   filenames.create (20);
   19171              : 
   19172              :   /* Determine the unique filenames.  */
   19173         2716 :   const line_map_ordinary *current = nullptr;
   19174       291590 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19175       286158 :        iter != end; ++iter)
   19176       283442 :     if (iter->src != current)
   19177              :       {
   19178        31144 :         current = iter->src;
   19179        31144 :         const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19180              : 
   19181              :         /* We should never find a module linemap in an interval.  */
   19182        31144 :         gcc_checking_assert (!MAP_MODULE_P (iter->src));
   19183              : 
   19184              :         /* We expect very few filenames, so just an array.
   19185              :            (Not true when headers are still in play :()  */
   19186      2372513 :         for (unsigned jx = filenames.length (); jx--;)
   19187              :           {
   19188      2325008 :             const char *name = filenames[jx];
   19189      2325008 :             if (0 == strcmp (name, fname))
   19190              :               {
   19191              :                 /* Reset the linemap's name, because for things like
   19192              :                    preprocessed input we could have multiple instances
   19193              :                    of the same name, and we'd rather not percolate
   19194              :                    that.  */
   19195        14783 :                 const_cast<line_map_ordinary *> (iter->src)->to_file = name;
   19196        14783 :                 fname = NULL;
   19197        14783 :                 break;
   19198              :               }
   19199              :           }
   19200        31144 :         if (fname)
   19201        16361 :           filenames.safe_push (fname);
   19202              :       }
   19203              : 
   19204         2716 :   bytes_out sec (to);
   19205         2716 :   sec.begin ();
   19206              : 
   19207              :   /* Write the filenames.  */
   19208         2716 :   unsigned len = filenames.length ();
   19209         2716 :   sec.u (len);
   19210         2994 :   dump () && dump ("%u source file names", len);
   19211        19077 :   for (unsigned ix = 0; ix != len; ix++)
   19212              :     {
   19213        16361 :       const char *fname = filenames[ix];
   19214        16376 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19215        16361 :       sec.str (fname);
   19216              :     }
   19217              : 
   19218         2716 :   sec.loc (info.first); /* Num maps.  */
   19219         2716 :   const ord_loc_info *base = nullptr;
   19220       291590 :   for (auto iter = ord_loc_remap->begin (), end = ord_loc_remap->end ();
   19221       286158 :        iter != end; ++iter)
   19222              :     {
   19223       283442 :       dump (dumper::LOCATION)
   19224           36 :         && dump ("Span:%K ordinary [%K+%K,+%K)->[%K,+%K)",
   19225           36 :                  (location_t) (iter - ord_loc_remap->begin ()),
   19226           18 :                  MAP_START_LOCATION (iter->src),
   19227              :                  iter->offset, iter->span, iter->remap,
   19228              :                  iter->span);
   19229              : 
   19230       283442 :       if (!base || iter->src != base->src)
   19231        31144 :         base = iter;
   19232       283442 :       sec.loc (iter->offset - base->offset);
   19233       283442 :       if (base == iter)
   19234              :         {
   19235        31144 :           sec.u (iter->src->sysp);
   19236        31144 :           sec.u (iter->src->m_range_bits);
   19237        31144 :           sec.u (iter->src->m_column_and_range_bits - iter->src->m_range_bits);
   19238              : 
   19239        31144 :           const char *fname = ORDINARY_MAP_FILE_NAME (iter->src);
   19240      7155670 :           for (unsigned ix = 0; ix != filenames.length (); ix++)
   19241      3577835 :             if (filenames[ix] == fname)
   19242              :               {
   19243        31144 :                 sec.u (ix);
   19244        31144 :                 break;
   19245              :               }
   19246        31144 :           unsigned line = ORDINARY_MAP_STARTING_LINE_NUMBER (iter->src);
   19247        31144 :           line += iter->offset >> iter->src->m_column_and_range_bits;
   19248        31144 :           sec.u (line);
   19249              :         }
   19250       283442 :       sec.loc (iter->remap);
   19251       283442 :       if (base == iter)
   19252              :         {
   19253              :           /* Write the included from location, which means reading it
   19254              :              while reading in the ordinary maps.  So we'd better not
   19255              :              be getting ahead of ourselves.  */
   19256        31144 :           location_t from = linemap_included_from (iter->src);
   19257        31144 :           gcc_checking_assert (from < MAP_START_LOCATION (iter->src));
   19258        31144 :           if (from != UNKNOWN_LOCATION && has_partitions)
   19259              :             {
   19260              :               /* A partition's span will have a from pointing at a
   19261              :                  MODULE_INC.  Find that map's from.  */
   19262          217 :               line_map_ordinary const *fmap
   19263          217 :                 = linemap_check_ordinary (linemap_lookup (line_table, from));
   19264          217 :               if (MAP_MODULE_P (fmap))
   19265          184 :                 from = linemap_included_from (fmap);
   19266              :             }
   19267        31144 :           write_location (sec, from);
   19268              :         }
   19269              :     }
   19270              : 
   19271         2716 :   filenames.release ();
   19272              : 
   19273         2716 :   sec.end (to, to->name (MOD_SNAME_PFX ".olm"), crc_p);
   19274         2716 :   dump.outdent ();
   19275         2716 : }
   19276              : 
   19277              : /* Return the prefix to use for dumping a #pragma diagnostic change to DK.  */
   19278              : 
   19279              : static const char *
   19280         1044 : dk_string (enum diagnostics::kind dk)
   19281              : {
   19282         1044 :   gcc_assert (dk > diagnostics::kind::unspecified
   19283              :               && dk < diagnostics::kind::last_diagnostic_kind);
   19284         1044 :   if (dk == diagnostics::kind::ignored)
   19285              :     /* diagnostics/kinds.def has an empty string for ignored.  */
   19286              :     return "ignored: ";
   19287              :   else
   19288            0 :     return diagnostics::get_text_for_kind (dk);
   19289              : }
   19290              : 
   19291              : /* Dump one #pragma GCC diagnostic entry.  */
   19292              : 
   19293              : static bool
   19294         2132 : dump_dc_change (unsigned index, unsigned opt, enum diagnostics::kind dk)
   19295              : {
   19296         2132 :   if (dk == diagnostics::kind::pop)
   19297         1088 :     return dump (" Index %u: pop from %d", index, opt);
   19298              :   else
   19299         1044 :     return dump (" Index %u: %s%s", index, dk_string (dk),
   19300         2088 :                  cl_options[opt].opt_text);
   19301              : }
   19302              : 
   19303              : /* Write out any #pragma GCC diagnostic info to the .dgc section.  */
   19304              : 
   19305              : void
   19306         5531 : module_state::write_diagnostic_classification (elf_out *to,
   19307              :                                                diagnostics::context *dc,
   19308              :                                                unsigned *crc_p)
   19309              : {
   19310         5531 :   auto &changes = dc->get_classification_history ();
   19311              : 
   19312         5531 :   bytes_out sec (to);
   19313         5531 :   if (sec.streaming_p ())
   19314              :     {
   19315         2716 :       sec.begin ();
   19316         2994 :       dump () && dump ("Writing diagnostic change locations");
   19317         2716 :       dump.indent ();
   19318              :     }
   19319              : 
   19320         5531 :   unsigned len = changes.length ();
   19321              : 
   19322              :   /* We don't want to write out any entries that came from one of our imports.
   19323              :      But then we need to adjust the total, and change diagnostics::kind::pop
   19324              :      targets to match the index in our actual output.  So remember how many
   19325              :      lines we had skipped at each step, where -1 means this line itself
   19326              :      is skipped.  */
   19327         5531 :   int skips = 0;
   19328         5531 :   auto_vec<int> skips_at (len);
   19329         5531 :   skips_at.safe_grow (len);
   19330              : 
   19331        79610 :   for (unsigned i = 0; i < len; ++i)
   19332              :     {
   19333        68548 :       const auto &c = changes[i];
   19334        68548 :       skips_at[i] = skips;
   19335        68548 :       if (linemap_location_from_module_p (line_table, c.location))
   19336              :         {
   19337        13318 :           ++skips;
   19338        13318 :           skips_at[i] = -1;
   19339        13318 :           continue;
   19340              :         }
   19341              :     }
   19342              : 
   19343         5531 :   if (sec.streaming_p ())
   19344              :     {
   19345         2716 :       sec.u (len - skips);
   19346         2994 :       dump () && dump ("Diagnostic changes: %u", len - skips);
   19347              :     }
   19348              : 
   19349        74079 :   for (unsigned i = 0; i < len; ++i)
   19350              :     {
   19351        68548 :       if (skips_at[i] == -1)
   19352        13318 :         continue;
   19353              : 
   19354        55230 :       const auto &c = changes[i];
   19355        55230 :       write_location (sec, c.location);
   19356        55230 :       if (sec.streaming_p ())
   19357              :         {
   19358        27615 :           unsigned opt = c.option;
   19359        27615 :           if (c.kind == diagnostics::kind::pop)
   19360        14205 :             opt -= skips_at[opt];
   19361        27615 :           sec.u (opt);
   19362        27615 :           sec.u (static_cast<unsigned> (c.kind));
   19363        70613 :           dump () && dump_dc_change (i - skips_at[i], opt, c.kind);
   19364              :         }
   19365              :     }
   19366              : 
   19367         5531 :   if (sec.streaming_p ())
   19368              :     {
   19369         2716 :       sec.end (to, to->name (MOD_SNAME_PFX ".dgc"), crc_p);
   19370         2716 :       dump.outdent ();
   19371              :     }
   19372         5531 : }
   19373              : 
   19374              : /* Read any #pragma GCC diagnostic info from the .dgc section.  */
   19375              : 
   19376              : bool
   19377         2990 : module_state::read_diagnostic_classification (diagnostics::context *dc)
   19378              : {
   19379         2990 :   bytes_in sec;
   19380              : 
   19381         2990 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".dgc"))
   19382              :     return false;
   19383              : 
   19384         3510 :   dump () && dump ("Reading diagnostic change locations");
   19385         2990 :   dump.indent ();
   19386              : 
   19387         2990 :   unsigned len = sec.u ();
   19388         3510 :   dump () && dump ("Diagnostic changes: %u", len);
   19389              : 
   19390         2990 :   auto &changes = dc->get_classification_history ();
   19391         2990 :   int offset = changes.length ();
   19392         2990 :   changes.reserve (len + 1);
   19393        31008 :   for (unsigned i = 0; i < len; ++i)
   19394              :     {
   19395        25028 :       location_t loc = read_location (sec);
   19396        25028 :       int opt = sec.u ();
   19397        25028 :       enum diagnostics::kind kind = (enum diagnostics::kind) sec.u ();
   19398        25028 :       if (kind == diagnostics::kind::pop)
   19399              :         /* For a pop, opt is the 'changes' index to return to.  */
   19400        12865 :         opt += offset;
   19401        25028 :       changes.quick_push ({ loc, opt, kind });
   19402        25095 :       dump () && dump_dc_change (changes.length () - 1, opt, kind);
   19403              :     }
   19404              : 
   19405              :   /* Did the import pop all its diagnostic changes?  */
   19406         2990 :   bool last_was_reset = (len == 0);
   19407         2990 :   if (len)
   19408          232 :     for (int i = changes.length () - 1; ; --i)
   19409              :       {
   19410        11060 :         gcc_checking_assert (i >= offset);
   19411              : 
   19412        11060 :         const auto &c = changes[i];
   19413        11060 :         if (c.kind != diagnostics::kind::pop)
   19414              :           break;
   19415        11051 :         else if (c.option == offset)
   19416              :           {
   19417              :             last_was_reset = true;
   19418              :             break;
   19419              :           }
   19420              :         else
   19421              :           /* As in update_effective_level_from_pragmas, the loop will decrement
   19422              :              i so we actually jump to c.option - 1.  */
   19423        10944 :           i = c.option;
   19424        10944 :       }
   19425         2990 :   if (!last_was_reset)
   19426              :     {
   19427              :       /* It didn't, so add a pop at its last location to avoid affecting later
   19428              :          imports.  */
   19429            9 :       location_t last_loc = ordinary_locs.first + ordinary_locs.second - 1;
   19430            9 :       changes.quick_push ({ last_loc, offset, diagnostics::kind::pop });
   19431           15 :       dump () && dump (" Adding final pop from index %d", offset);
   19432              :     }
   19433              : 
   19434         2990 :   dump.outdent ();
   19435         2990 :   if (!sec.end (from ()))
   19436              :     return false;
   19437              : 
   19438              :   return true;
   19439         2990 : }
   19440              : 
   19441              : void
   19442          128 : module_state::write_macro_maps (elf_out *to, range_t &info, unsigned *crc_p)
   19443              : {
   19444          140 :   dump () && dump ("Writing macro location maps");
   19445          128 :   dump.indent ();
   19446              : 
   19447          128 :   bytes_out sec (to);
   19448          128 :   sec.begin ();
   19449              : 
   19450          140 :   dump () && dump ("Macro maps:%K", info.second);
   19451          128 :   sec.loc (info.second);
   19452              : 
   19453          128 :   line_map_uint_t macro_num = 0;
   19454          256 :   for (auto iter = macro_loc_remap->end (), begin = macro_loc_remap->begin ();
   19455       172746 :        iter-- != begin;)
   19456              :     {
   19457       172618 :       auto mac = iter->src;
   19458       172618 :       sec.loc (iter->remap);
   19459       172618 :       sec.u (mac->n_tokens);
   19460       172618 :       sec.cpp_node (mac->macro);
   19461       172618 :       write_location (sec, mac->m_expansion);
   19462       172618 :       const location_t *locs = mac->macro_locations;
   19463              :       /* There are lots of identical runs.  */
   19464       172618 :       location_t prev = UNKNOWN_LOCATION;
   19465       172618 :       unsigned count = 0;
   19466       172618 :       unsigned runs = 0;
   19467      6564488 :       for (unsigned jx = mac->n_tokens * 2; jx--;)
   19468              :         {
   19469      6391870 :           location_t tok_loc = locs[jx];
   19470      6391870 :           if (tok_loc == prev)
   19471              :             {
   19472      3019235 :               count++;
   19473      3019235 :               continue;
   19474              :             }
   19475      3372635 :           runs++;
   19476      3372635 :           sec.u (count);
   19477      3372635 :           count = 1;
   19478      3372635 :           prev = tok_loc;
   19479      3372635 :           write_location (sec, tok_loc);
   19480              :         }
   19481       172618 :       sec.u (count);
   19482       172618 :       dump (dumper::LOCATION)
   19483            9 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)->%K",
   19484            9 :                  macro_num, identifier (mac->macro),
   19485              :                  runs, mac->n_tokens,
   19486              :                  MAP_START_LOCATION (mac),
   19487            9 :                  MAP_START_LOCATION (mac) + mac->n_tokens,
   19488              :                  iter->remap);
   19489       172618 :       macro_num++;
   19490              :     }
   19491          128 :   gcc_assert (macro_num == info.second);
   19492              : 
   19493          128 :   sec.end (to, to->name (MOD_SNAME_PFX ".mlm"), crc_p);
   19494          128 :   dump.outdent ();
   19495          128 : }
   19496              : 
   19497              : bool
   19498         2990 : module_state::read_ordinary_maps (line_map_uint_t num_ord_locs,
   19499              :                                   unsigned range_bits)
   19500              : {
   19501         2990 :   bytes_in sec;
   19502              : 
   19503         2990 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".olm"))
   19504              :     return false;
   19505         3510 :   dump () && dump ("Reading ordinary location maps");
   19506         2990 :   dump.indent ();
   19507              : 
   19508              :   /* Read the filename table.  */
   19509         2990 :   unsigned len = sec.u ();
   19510         3510 :   dump () && dump ("%u source file names", len);
   19511         2990 :   vec<const char *> filenames;
   19512         2990 :   filenames.create (len);
   19513        22452 :   for (unsigned ix = 0; ix != len; ix++)
   19514              :     {
   19515        16472 :       size_t l;
   19516        16472 :       const char *buf = sec.str (&l);
   19517        16472 :       char *fname = XNEWVEC (char, l + 1);
   19518        16472 :       memcpy (fname, buf, l + 1);
   19519        16472 :       dump (dumper::LOCATION) && dump ("Source file[%u]=%s", ix, fname);
   19520              :       /* We leak these names into the line-map table.  But it
   19521              :          doesn't own them.  */
   19522        16472 :       filenames.quick_push (fname);
   19523              :     }
   19524              : 
   19525         2990 :   line_map_uint_t num_ordinary = sec.loc ();
   19526         3510 :   dump () && dump ("Ordinary maps:%K, range_bits:%u",
   19527              :                    num_ordinary, range_bits);
   19528              : 
   19529         2990 :   location_t offset = line_table->highest_location + 1;
   19530         2990 :   offset += ((loc_one << range_bits) - 1);
   19531         2990 :   offset &= ~((loc_one << range_bits) - 1);
   19532         2990 :   ordinary_locs.first = offset;
   19533              : 
   19534         2990 :   bool propagated = spans.maybe_propagate (this, offset);
   19535         2990 :   line_map_ordinary *maps = static_cast<line_map_ordinary *>
   19536         2990 :     (line_map_new_raw (line_table, false, num_ordinary));
   19537              : 
   19538         2990 :   const line_map_ordinary *base = nullptr;
   19539       294169 :   for (line_map_uint_t ix = 0; ix != num_ordinary && !sec.get_overrun (); ix++)
   19540              :     {
   19541       288189 :       line_map_ordinary *map = &maps[ix];
   19542              : 
   19543       288189 :       location_t offset = sec.loc ();
   19544       288189 :       if (!offset)
   19545              :         {
   19546        31665 :           map->reason = LC_RENAME;
   19547        31665 :           map->sysp = sec.u ();
   19548        31665 :           map->m_range_bits = sec.u ();
   19549        31665 :           map->m_column_and_range_bits = sec.u () + map->m_range_bits;
   19550        31665 :           unsigned fnum = sec.u ();
   19551        63330 :           map->to_file = (fnum < filenames.length () ? filenames[fnum] : "");
   19552        31665 :           map->to_line = sec.u ();
   19553        31665 :           base = map;
   19554              :         }
   19555              :       else
   19556              :         {
   19557       256524 :           *map = *base;
   19558       256524 :           map->to_line += offset >> map->m_column_and_range_bits;
   19559              :         }
   19560       288189 :       location_t remap = sec.loc ();
   19561       288189 :       map->start_location = remap + ordinary_locs.first;
   19562       288189 :       if (base == map)
   19563              :         {
   19564              :           /* Root the outermost map at our location.  */
   19565        31665 :           ordinary_locs.second = remap;
   19566        31665 :           location_t from = read_location (sec);
   19567        31665 :           map->included_from = from != UNKNOWN_LOCATION ? from : loc;
   19568              :         }
   19569              :     }
   19570              : 
   19571         2990 :   ordinary_locs.second = num_ord_locs;
   19572              :   /* highest_location is the one handed out, not the next one to
   19573              :      hand out.  */
   19574         2990 :   line_table->highest_location = ordinary_locs.first + ordinary_locs.second - 1;
   19575              : 
   19576         2990 :   if (line_table->highest_location >= LINE_MAP_MAX_LOCATION_WITH_COLS)
   19577              :     /* We shouldn't run out of locations, as we checked before
   19578              :        starting.  */
   19579            0 :     sec.set_overrun ();
   19580         3510 :   dump () && dump ("Ordinary location [%K,+%K)",
   19581              :                    ordinary_locs.first, ordinary_locs.second);
   19582              : 
   19583         2990 :   if (propagated)
   19584          175 :     spans.close ();
   19585              : 
   19586         2990 :   filenames.release ();
   19587              : 
   19588         2990 :   dump.outdent ();
   19589         2990 :   if (!sec.end (from ()))
   19590              :     return false;
   19591              : 
   19592              :   return true;
   19593         2990 : }
   19594              : 
   19595              : bool
   19596          133 : module_state::read_macro_maps (line_map_uint_t num_macro_locs)
   19597              : {
   19598          133 :   bytes_in sec;
   19599              : 
   19600          133 :   if (!sec.begin (loc, from (), MOD_SNAME_PFX ".mlm"))
   19601              :     return false;
   19602          142 :   dump () && dump ("Reading macro location maps");
   19603          133 :   dump.indent ();
   19604              : 
   19605          133 :   line_map_uint_t num_macros = sec.loc ();
   19606          142 :   dump () && dump ("Macro maps:%K locs:%K",
   19607              :                    num_macros, num_macro_locs);
   19608              : 
   19609          266 :   bool propagated = spans.maybe_propagate (this,
   19610          133 :                                            line_table->highest_location + 1);
   19611              : 
   19612          133 :   location_t offset = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   19613          133 :   macro_locs.second = num_macro_locs;
   19614          133 :   macro_locs.first = offset - num_macro_locs;
   19615              : 
   19616          142 :   dump () && dump ("Macro loc delta %K", offset);
   19617          142 :   dump () && dump ("Macro locations [%K,%K)",
   19618              :                    macro_locs.first, macro_locs.second);
   19619              : 
   19620       175225 :   for (line_map_uint_t ix = 0; ix != num_macros && !sec.get_overrun (); ix++)
   19621              :     {
   19622       175092 :       location_t offset = sec.loc ();
   19623       175092 :       unsigned n_tokens = sec.u ();
   19624       175092 :       cpp_hashnode *node = sec.cpp_node ();
   19625       175092 :       location_t exp_loc = read_location (sec);
   19626              : 
   19627       175092 :       const line_map_macro *macro
   19628       175092 :         = linemap_enter_macro (line_table, node, exp_loc, n_tokens);
   19629       175092 :       if (!macro)
   19630              :         /* We shouldn't run out of locations, as we checked that we
   19631              :            had enough before starting.  */
   19632              :         break;
   19633       175092 :       gcc_checking_assert (MAP_START_LOCATION (macro)
   19634              :                            == offset + macro_locs.first);
   19635              : 
   19636       175092 :       location_t *locs = macro->macro_locations;
   19637       175092 :       location_t tok_loc = UNKNOWN_LOCATION;
   19638       175092 :       unsigned count = sec.u ();
   19639       175092 :       unsigned runs = 0;
   19640      6403262 :       for (unsigned jx = macro->n_tokens * 2; jx-- && !sec.get_overrun ();)
   19641              :         {
   19642      9522917 :           while (!count-- && !sec.get_overrun ())
   19643              :             {
   19644      3294747 :               runs++;
   19645      3294747 :               tok_loc = read_location (sec);
   19646      3294747 :               count = sec.u ();
   19647              :             }
   19648      6228170 :           locs[jx] = tok_loc;
   19649              :         }
   19650       175092 :       if (count)
   19651            0 :         sec.set_overrun ();
   19652       175122 :       dump (dumper::LOCATION)
   19653            0 :         && dump ("Macro:%K %I %u/%u*2 locations [%K,%K)",
   19654              :                  ix, identifier (node), runs, n_tokens,
   19655              :                  MAP_START_LOCATION (macro),
   19656            0 :                  MAP_START_LOCATION (macro) + n_tokens);
   19657              :     }
   19658              : 
   19659          142 :   dump () && dump ("Macro location lwm:%K", macro_locs.first);
   19660          133 :   if (propagated)
   19661            3 :     spans.close ();
   19662              : 
   19663          133 :   dump.outdent ();
   19664          133 :   if (!sec.end (from ()))
   19665              :     return false;
   19666              : 
   19667              :   return true;
   19668          133 : }
   19669              : 
   19670              : /* Serialize the definition of MACRO.  */
   19671              : 
   19672              : void
   19673        78832 : module_state::write_define (bytes_out &sec, const cpp_macro *macro)
   19674              : {
   19675        78832 :   sec.u (macro->count);
   19676              : 
   19677        78832 :   bytes_out::bits_out bits = sec.stream_bits ();
   19678        78832 :   bits.b (macro->fun_like);
   19679        78832 :   bits.b (macro->variadic);
   19680        78832 :   bits.b (macro->syshdr);
   19681        78832 :   bits.bflush ();
   19682              : 
   19683        78832 :   write_location (sec, macro->line);
   19684        78832 :   if (macro->fun_like)
   19685              :     {
   19686         9765 :       sec.u (macro->paramc);
   19687         9765 :       const cpp_hashnode *const *parms = macro->parm.params;
   19688        24598 :       for (unsigned ix = 0; ix != macro->paramc; ix++)
   19689        14833 :         sec.cpp_node (parms[ix]);
   19690              :     }
   19691              : 
   19692        78832 :   unsigned len = 0;
   19693       250105 :   for (unsigned ix = 0; ix != macro->count; ix++)
   19694              :     {
   19695       171273 :       const cpp_token *token = &macro->exp.tokens[ix];
   19696       171273 :       write_location (sec, token->src_loc);
   19697       171273 :       sec.u (token->type);
   19698       171273 :       sec.u (token->flags);
   19699       171273 :       switch (cpp_token_val_index (token))
   19700              :         {
   19701            0 :         default:
   19702            0 :           gcc_unreachable ();
   19703              : 
   19704        12962 :         case CPP_TOKEN_FLD_ARG_NO:
   19705              :           /* An argument reference.  */
   19706        12962 :           sec.u (token->val.macro_arg.arg_no);
   19707        12962 :           sec.cpp_node (token->val.macro_arg.spelling);
   19708        12962 :           break;
   19709              : 
   19710        32963 :         case CPP_TOKEN_FLD_NODE:
   19711              :           /* An identifier.  */
   19712        32963 :           sec.cpp_node (token->val.node.node);
   19713        32963 :           if (token->val.node.spelling == token->val.node.node)
   19714              :             /* The spelling will usually be the same.  so optimize
   19715              :                that.  */
   19716        32963 :             sec.str (NULL, 0);
   19717              :           else
   19718            0 :             sec.cpp_node (token->val.node.spelling);
   19719              :           break;
   19720              : 
   19721              :         case CPP_TOKEN_FLD_NONE:
   19722              :           break;
   19723              : 
   19724        56444 :         case CPP_TOKEN_FLD_STR:
   19725              :           /* A string, number or comment.  Not always NUL terminated,
   19726              :              we stream out in a single concatenation with embedded
   19727              :              NULs as that's a safe default.  */
   19728        56444 :           len += token->val.str.len + 1;
   19729        56444 :           sec.u (token->val.str.len);
   19730        56444 :           break;
   19731              : 
   19732            0 :         case CPP_TOKEN_FLD_SOURCE:
   19733            0 :         case CPP_TOKEN_FLD_TOKEN_NO:
   19734            0 :         case CPP_TOKEN_FLD_PRAGMA:
   19735              :           /* These do not occur inside a macro itself.  */
   19736            0 :           gcc_unreachable ();
   19737              :         }
   19738              :     }
   19739              : 
   19740        78832 :   if (len)
   19741              :     {
   19742        52820 :       char *ptr = reinterpret_cast<char *> (sec.buf (len));
   19743        52820 :       len = 0;
   19744       209381 :       for (unsigned ix = 0; ix != macro->count; ix++)
   19745              :         {
   19746       103741 :           const cpp_token *token = &macro->exp.tokens[ix];
   19747       103741 :           if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19748              :             {
   19749        56444 :               memcpy (ptr + len, token->val.str.text,
   19750        56444 :                       token->val.str.len);
   19751        56444 :               len += token->val.str.len;
   19752        56444 :               ptr[len++] = 0;
   19753              :             }
   19754              :         }
   19755              :     }
   19756        78832 : }
   19757              : 
   19758              : /* Read a macro definition.  */
   19759              : 
   19760              : cpp_macro *
   19761          461 : module_state::read_define (bytes_in &sec, cpp_reader *reader) const
   19762              : {
   19763          461 :   unsigned count = sec.u ();
   19764              :   /* We rely on knowing cpp_reader's hash table is ident_hash, and
   19765              :      its subobject allocator is stringpool_ggc_alloc and that is just
   19766              :      a wrapper for ggc_alloc_atomic.  */
   19767          461 :   cpp_macro *macro
   19768          922 :     = (cpp_macro *)ggc_alloc_atomic (sizeof (cpp_macro)
   19769          461 :                                      + sizeof (cpp_token) * (count - !!count));
   19770          461 :   memset (macro, 0, sizeof (cpp_macro) + sizeof (cpp_token) * (count - !!count));
   19771              : 
   19772          461 :   macro->count = count;
   19773          461 :   macro->kind = cmk_macro;
   19774          461 :   macro->imported_p = true;
   19775              : 
   19776          461 :   bytes_in::bits_in bits = sec.stream_bits ();
   19777          461 :   macro->fun_like = bits.b ();
   19778          461 :   macro->variadic = bits.b ();
   19779          461 :   macro->syshdr = bits.b ();
   19780          461 :   bits.bflush ();
   19781              : 
   19782          461 :   macro->line = read_location (sec);
   19783              : 
   19784          461 :   if (macro->fun_like)
   19785              :     {
   19786           70 :       unsigned paramc = sec.u ();
   19787           70 :       cpp_hashnode **params
   19788           70 :         = (cpp_hashnode **)ggc_alloc_atomic (sizeof (cpp_hashnode *) * paramc);
   19789           70 :       macro->paramc = paramc;
   19790           70 :       macro->parm.params = params;
   19791          149 :       for (unsigned ix = 0; ix != paramc; ix++)
   19792           79 :         params[ix] = sec.cpp_node ();
   19793              :     }
   19794              : 
   19795          461 :   unsigned len = 0;
   19796         1432 :   for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19797              :     {
   19798          971 :       cpp_token *token = &macro->exp.tokens[ix];
   19799          971 :       token->src_loc = read_location (sec);
   19800          971 :       token->type = cpp_ttype (sec.u ());
   19801          971 :       token->flags = sec.u ();
   19802          971 :       switch (cpp_token_val_index (token))
   19803              :         {
   19804            0 :         default:
   19805            0 :           sec.set_overrun ();
   19806            0 :           break;
   19807              : 
   19808           66 :         case CPP_TOKEN_FLD_ARG_NO:
   19809              :           /* An argument reference.  */
   19810           66 :           {
   19811           66 :             unsigned arg_no = sec.u ();
   19812           66 :             if (arg_no - 1 >= macro->paramc)
   19813            0 :               sec.set_overrun ();
   19814           66 :             token->val.macro_arg.arg_no = arg_no;
   19815           66 :             token->val.macro_arg.spelling = sec.cpp_node ();
   19816              :           }
   19817           66 :           break;
   19818              : 
   19819          219 :         case CPP_TOKEN_FLD_NODE:
   19820              :           /* An identifier.  */
   19821          219 :           token->val.node.node = sec.cpp_node ();
   19822          219 :           token->val.node.spelling = sec.cpp_node ();
   19823          219 :           if (!token->val.node.spelling)
   19824          219 :             token->val.node.spelling = token->val.node.node;
   19825              :           break;
   19826              : 
   19827              :         case CPP_TOKEN_FLD_NONE:
   19828              :           break;
   19829              : 
   19830          313 :         case CPP_TOKEN_FLD_STR:
   19831              :           /* A string, number or comment.  */
   19832          313 :           token->val.str.len = sec.u ();
   19833          313 :           len += token->val.str.len + 1;
   19834          313 :           break;
   19835              :         }
   19836              :     }
   19837              : 
   19838          461 :   if (len)
   19839          311 :     if (const char *ptr = reinterpret_cast<const char *> (sec.buf (len)))
   19840              :       {
   19841              :         /* There should be a final NUL.  */
   19842          311 :         if (ptr[len-1])
   19843            0 :           sec.set_overrun ();
   19844              :         /* cpp_alloc_token_string will add a final NUL.  */
   19845          311 :         const unsigned char *buf
   19846          311 :           = cpp_alloc_token_string (reader, (const unsigned char *)ptr, len - 1);
   19847          311 :         len = 0;
   19848         1246 :         for (unsigned ix = 0; ix != count && !sec.get_overrun (); ix++)
   19849              :           {
   19850          624 :             cpp_token *token = &macro->exp.tokens[ix];
   19851          624 :             if (cpp_token_val_index (token) == CPP_TOKEN_FLD_STR)
   19852              :               {
   19853          313 :                 token->val.str.text = buf + len;
   19854          313 :                 len += token->val.str.len;
   19855          313 :                 if (buf[len++])
   19856            0 :                   sec.set_overrun ();
   19857              :               }
   19858              :           }
   19859              :       }
   19860              : 
   19861          461 :   if (sec.get_overrun ())
   19862            0 :     return NULL;
   19863              :   return macro;
   19864          461 : }
   19865              : 
   19866              : /* Exported macro data.  */
   19867              : struct GTY(()) macro_export {
   19868              :   cpp_macro *def;
   19869              :   location_t undef_loc;
   19870              : 
   19871       110667 :   macro_export ()
   19872       110667 :     :def (NULL), undef_loc (UNKNOWN_LOCATION)
   19873              :   {
   19874              :   }
   19875              : };
   19876              : 
   19877              : /* Imported macro data.  */
   19878              : class macro_import {
   19879              : public:
   19880              :   struct slot {
   19881              : #if defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8
   19882              :     int offset;
   19883              : #endif
   19884              :     /* We need to ensure we don't use the LSB for representation, as
   19885              :        that's the union discriminator below.  */
   19886              :     unsigned bits;
   19887              : 
   19888              : #if !(defined (WORDS_BIGENDIAN) && SIZEOF_VOID_P == 8)
   19889              :     int offset;
   19890              : #endif
   19891              : 
   19892              :   public:
   19893              :     enum Layout {
   19894              :       L_DEF = 1,
   19895              :       L_UNDEF = 2,
   19896              :       L_BOTH = 3,
   19897              :       L_MODULE_SHIFT = 2
   19898              :     };
   19899              : 
   19900              :   public:
   19901              :     /* Not a regular ctor, because we put it in a union, and that's
   19902              :        not allowed in C++ 98.  */
   19903       190198 :     static slot ctor (unsigned module, unsigned defness)
   19904              :     {
   19905       190198 :       gcc_checking_assert (defness);
   19906       190198 :       slot s;
   19907       190198 :       s.bits = defness | (module << L_MODULE_SHIFT);
   19908       190198 :       s.offset = -1;
   19909       190198 :       return s;
   19910              :     }
   19911              : 
   19912              :   public:
   19913       159642 :     unsigned get_defness () const
   19914              :     {
   19915       159642 :       return bits & L_BOTH;
   19916              :     }
   19917       113153 :     unsigned get_module () const
   19918              :     {
   19919       113153 :       return bits >> L_MODULE_SHIFT;
   19920              :     }
   19921           12 :     void become_undef ()
   19922              :     {
   19923           12 :       bits &= ~unsigned (L_DEF);
   19924           12 :       bits |= unsigned (L_UNDEF);
   19925              :     }
   19926              :   };
   19927              : 
   19928              : private:
   19929              :   typedef vec<slot, va_heap, vl_embed> ary_t;
   19930              :   union either {
   19931              :     /* Discriminated by bits 0|1 != 0.  The expected case is that
   19932              :        there will be exactly one slot per macro, hence the effort of
   19933              :        packing that.  */
   19934              :     ary_t *ary;
   19935              :     slot single;
   19936              :   } u;
   19937              : 
   19938              : public:
   19939       153764 :   macro_import ()
   19940              :   {
   19941       153764 :     u.ary = NULL;
   19942              :   }
   19943              : 
   19944              : private:
   19945      8704611 :   bool single_p () const
   19946              :   {
   19947      8704611 :     return u.single.bits & slot::L_BOTH;
   19948              :   }
   19949      8858509 :   bool occupied_p () const
   19950              :   {
   19951      8858509 :     return u.ary != NULL;
   19952              :   }
   19953              : 
   19954              : public:
   19955         1341 :   unsigned length () const
   19956              :   {
   19957         1341 :     gcc_checking_assert (occupied_p ());
   19958         1341 :     return single_p () ? 1 : u.ary->length ();
   19959              :   }
   19960      8556636 :   slot &operator[] (unsigned ix)
   19961              :   {
   19962      8556636 :     gcc_checking_assert (occupied_p ());
   19963      8556636 :     if (single_p ())
   19964              :       {
   19965      8449742 :         gcc_checking_assert (!ix);
   19966      8449742 :         return u.single;
   19967              :       }
   19968              :     else
   19969       106894 :       return (*u.ary)[ix];
   19970              :   }
   19971              : 
   19972              : public:
   19973              :   slot &exported ();
   19974              :   slot &append (unsigned module, unsigned defness);
   19975              : };
   19976              : 
   19977              : /* O is a new import to append to the list for.  If we're an empty
   19978              :    set, initialize us.  */
   19979              : 
   19980              : macro_import::slot &
   19981       190198 : macro_import::append (unsigned module, unsigned defness)
   19982              : {
   19983       190198 :   if (!occupied_p ())
   19984              :     {
   19985       153764 :       u.single = slot::ctor (module, defness);
   19986       153764 :       return u.single;
   19987              :     }
   19988              :   else
   19989              :     {
   19990        36434 :       bool single = single_p ();
   19991        36434 :       ary_t *m = single ? NULL : u.ary;
   19992        36434 :       vec_safe_reserve (m, 1 + single);
   19993        36434 :       if (single)
   19994        36431 :         m->quick_push (u.single);
   19995        36434 :       u.ary = m;
   19996        36434 :       return *u.ary->quick_push (slot::ctor (module, defness));
   19997              :     }
   19998              : }
   19999              : 
   20000              : /* We're going to export something.  Make sure the first import slot
   20001              :    is us.  */
   20002              : 
   20003              : macro_import::slot &
   20004       110334 : macro_import::exported ()
   20005              : {
   20006       110334 :   if (occupied_p () && !(*this)[0].get_module ())
   20007              :     {
   20008          134 :       slot &res = (*this)[0];
   20009          134 :       res.bits |= slot::L_DEF;
   20010          134 :       return res;
   20011              :     }
   20012              : 
   20013       110200 :   slot *a = &append (0, slot::L_DEF);
   20014       110200 :   if (!single_p ())
   20015              :     {
   20016        32077 :       slot &f = (*this)[0];
   20017        32077 :       std::swap (f, *a);
   20018        32077 :       a = &f;
   20019              :     }
   20020              :   return *a;
   20021              : }
   20022              : 
   20023              : /* The import (&exported) macros.  cpp_hasnode's deferred field
   20024              :    indexes this array (offset by 1, so zero means 'not present'.  */
   20025              : 
   20026              : static vec<macro_import, va_heap, vl_embed> *macro_imports;
   20027              : 
   20028              : /* The exported macros.  A macro_import slot's zeroth element's offset
   20029              :    indexes this array.  If the zeroth slot is not for module zero,
   20030              :    there is no export.  */
   20031              : 
   20032              : static GTY(()) vec<macro_export, va_gc> *macro_exports;
   20033              : 
   20034              : /* The reachable set of header imports from this TU.  */
   20035              : 
   20036              : static GTY(()) bitmap headers;
   20037              : 
   20038              : /* Get the (possibly empty) macro imports for NODE.  */
   20039              : 
   20040              : static macro_import &
   20041       158255 : get_macro_imports (cpp_hashnode *node)
   20042              : {
   20043       158255 :   if (node->deferred)
   20044         4491 :     return (*macro_imports)[node->deferred - 1];
   20045              : 
   20046       153764 :   vec_safe_reserve (macro_imports, 1);
   20047       153764 :   node->deferred = macro_imports->length () + 1;
   20048       153764 :   return *vec_safe_push (macro_imports, macro_import ());
   20049              : }
   20050              : 
   20051              : /* Get the macro export for export EXP of NODE.  */
   20052              : 
   20053              : static macro_export &
   20054       110334 : get_macro_export (macro_import::slot &slot)
   20055              : {
   20056       110334 :   if (slot.offset >= 0)
   20057          134 :     return (*macro_exports)[slot.offset];
   20058              : 
   20059       110200 :   vec_safe_reserve (macro_exports, 1);
   20060       110200 :   slot.offset = macro_exports->length ();
   20061       110200 :   return *macro_exports->quick_push (macro_export ());
   20062              : }
   20063              : 
   20064              : /* If NODE is an exportable macro, add it to the export set.  */
   20065              : 
   20066              : static int
   20067      4002666 : maybe_add_macro (cpp_reader *, cpp_hashnode *node, void *data_)
   20068              : {
   20069      4002666 :   bool exporting = false;
   20070              : 
   20071      4002666 :   if (cpp_user_macro_p (node))
   20072       514439 :     if (cpp_macro *macro = node->value.macro)
   20073              :       /* Ignore imported, builtins, command line and forced header macros.  */
   20074       513982 :       if (!macro->imported_p
   20075       513982 :           && !macro->lazy && macro->line >= spans.main_start ())
   20076              :         {
   20077        78257 :           gcc_checking_assert (macro->kind == cmk_macro);
   20078              :           /* I don't want to deal with this corner case, that I suspect is
   20079              :              a devil's advocate reading of the standard.  */
   20080        78257 :           gcc_checking_assert (!macro->extra_tokens);
   20081              : 
   20082        78257 :           macro_import::slot &slot = get_macro_imports (node).exported ();
   20083        78257 :           macro_export &exp = get_macro_export (slot);
   20084        78257 :           exp.def = macro;
   20085        78257 :           exporting = true;
   20086              :         }
   20087              : 
   20088      3924409 :   if (!exporting && node->deferred)
   20089              :     {
   20090          613 :       macro_import &imports = (*macro_imports)[node->deferred - 1];
   20091          613 :       macro_import::slot &slot = imports[0];
   20092          613 :       if (!slot.get_module ())
   20093              :         {
   20094          582 :           gcc_checking_assert (slot.get_defness ());
   20095              :           exporting = true;
   20096              :         }
   20097              :     }
   20098              : 
   20099        78257 :   if (exporting)
   20100        78839 :     static_cast<vec<cpp_hashnode *> *> (data_)->safe_push (node);
   20101              : 
   20102      4002666 :   return 1; /* Don't stop.  */
   20103              : }
   20104              : 
   20105              : /* Order cpp_hashnodes A_ and B_ by their exported macro locations.  */
   20106              : 
   20107              : static int
   20108      4126646 : macro_loc_cmp (const void *a_, const void *b_)
   20109              : {
   20110      4126646 :   const cpp_hashnode *node_a = *(const cpp_hashnode *const *)a_;
   20111      4126646 :   macro_import &import_a = (*macro_imports)[node_a->deferred - 1];
   20112      4126646 :   const macro_export &export_a = (*macro_exports)[import_a[0].offset];
   20113      4126646 :   location_t loc_a = export_a.def ? export_a.def->line : export_a.undef_loc;
   20114              : 
   20115      4126646 :   const cpp_hashnode *node_b = *(const cpp_hashnode *const *)b_;
   20116      4126646 :   macro_import &import_b = (*macro_imports)[node_b->deferred - 1];
   20117      4126646 :   const macro_export &export_b = (*macro_exports)[import_b[0].offset];
   20118      4126646 :   location_t loc_b = export_b.def ? export_b.def->line : export_b.undef_loc;
   20119              : 
   20120      4126646 :   if (loc_a < loc_b)
   20121              :     return +1;
   20122      2120794 :   else if (loc_a > loc_b)
   20123              :     return -1;
   20124              :   else
   20125            0 :     return 0;
   20126              : }
   20127              : 
   20128              : /* Gather the macro definitions and undefinitions that we will need to
   20129              :    write out.   */
   20130              : 
   20131              : vec<cpp_hashnode *> *
   20132          909 : module_state::prepare_macros (cpp_reader *reader)
   20133              : {
   20134          909 :   vec<cpp_hashnode *> *macros;
   20135          909 :   vec_alloc (macros, 100);
   20136              : 
   20137          909 :   cpp_forall_identifiers (reader, maybe_add_macro, macros);
   20138              : 
   20139          933 :   dump (dumper::MACRO) && dump ("No more than %u macros", macros->length ());
   20140              : 
   20141          909 :   macros->qsort (macro_loc_cmp);
   20142              : 
   20143              :   // Note the locations.
   20144        80657 :   for (unsigned ix = macros->length (); ix--;)
   20145              :     {
   20146        78839 :       cpp_hashnode *node = (*macros)[ix];
   20147        78839 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20148        78839 :       macro_export &mac = (*macro_exports)[slot.offset];
   20149              : 
   20150        78839 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20151            1 :         continue;
   20152              : 
   20153        78838 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20154           12 :         note_location (mac.undef_loc);
   20155        78838 :       if (mac.def)
   20156              :         {
   20157        78832 :           note_location (mac.def->line);
   20158       328937 :           for (unsigned ix = 0; ix != mac.def->count; ix++)
   20159       171273 :             note_location (mac.def->exp.tokens[ix].src_loc);
   20160              :         }
   20161              :     }
   20162              : 
   20163          909 :   return macros;
   20164              : }
   20165              : 
   20166              : /* Write out the exported defines.  This is two sections, one
   20167              :    containing the definitions, the other a table of node names.  */
   20168              : 
   20169              : unsigned
   20170          909 : module_state::write_macros (elf_out *to, vec<cpp_hashnode *> *macros,
   20171              :                             unsigned *crc_p)
   20172              : {
   20173          976 :   dump () && dump ("Writing macros");
   20174          909 :   dump.indent ();
   20175              : 
   20176              :   /* Write the defs */
   20177          909 :   bytes_out sec (to);
   20178          909 :   sec.begin ();
   20179              : 
   20180          909 :   unsigned count = 0;
   20181        80657 :   for (unsigned ix = macros->length (); ix--;)
   20182              :     {
   20183        78839 :       cpp_hashnode *node = (*macros)[ix];
   20184        78839 :       macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20185        78839 :       gcc_assert (!slot.get_module () && slot.get_defness ());
   20186              : 
   20187        78839 :       macro_export &mac = (*macro_exports)[slot.offset];
   20188        78839 :       gcc_assert (!!(slot.get_defness () & macro_import::slot::L_UNDEF)
   20189              :                   == (mac.undef_loc != UNKNOWN_LOCATION)
   20190              :                   && !!(slot.get_defness () & macro_import::slot::L_DEF)
   20191              :                   == (mac.def != NULL));
   20192              : 
   20193        78839 :       if (IDENTIFIER_KEYWORD_P (identifier (node)))
   20194              :         {
   20195            1 :           warning_at (mac.def->line, 0,
   20196              :                       "not exporting %<#define %E%> as it is a keyword",
   20197              :                       identifier (node));
   20198            1 :           slot.offset = 0;
   20199            1 :           continue;
   20200              :         }
   20201              : 
   20202        78838 :       count++;
   20203        78838 :       slot.offset = sec.pos;
   20204        78838 :       dump (dumper::MACRO)
   20205           24 :         && dump ("Writing macro %s%s%s %I at %u",
   20206           24 :                  slot.get_defness () & macro_import::slot::L_UNDEF
   20207              :                  ? "#undef" : "",
   20208           24 :                  slot.get_defness () == macro_import::slot::L_BOTH
   20209              :                  ? " & " : "",
   20210           24 :                  slot.get_defness () & macro_import::slot::L_DEF
   20211              :                  ? "#define" : "",
   20212              :                  identifier (node), slot.offset);
   20213        78838 :       if (mac.undef_loc != UNKNOWN_LOCATION)
   20214           12 :         write_location (sec, mac.undef_loc);
   20215        78838 :       if (mac.def)
   20216        78832 :         write_define (sec, mac.def);
   20217              :     }
   20218          909 :   if (count)
   20219              :     // We may have ended on a tokenless macro with a very short
   20220              :     // location, that will cause problems reading its bit flags.
   20221          145 :     sec.u (0);
   20222          909 :   sec.end (to, to->name (MOD_SNAME_PFX ".def"), crc_p);
   20223              : 
   20224          909 :   if (count)
   20225              :     {
   20226              :       /* Write the table.  */
   20227          145 :       bytes_out sec (to);
   20228          145 :       sec.begin ();
   20229          145 :       sec.u (count);
   20230              : 
   20231        79128 :       for (unsigned ix = macros->length (); ix--;)
   20232              :         {
   20233        78838 :           const cpp_hashnode *node = (*macros)[ix];
   20234        78838 :           macro_import::slot &slot = (*macro_imports)[node->deferred - 1][0];
   20235              : 
   20236        78838 :           if (slot.offset)
   20237              :             {
   20238        78838 :               sec.cpp_node (node);
   20239        78838 :               sec.u (slot.get_defness ());
   20240        78838 :               sec.u (slot.offset);
   20241              :             }
   20242              :         }
   20243          145 :       sec.end (to, to->name (MOD_SNAME_PFX ".mac"), crc_p);
   20244          145 :     }
   20245              : 
   20246          909 :   dump.outdent ();
   20247          909 :   return count;
   20248          909 : }
   20249              : 
   20250              : bool
   20251          939 : module_state::read_macros ()
   20252              : {
   20253              :   /* Get the def section.  */
   20254          939 :   if (!slurp->macro_defs.begin (loc, from (), MOD_SNAME_PFX ".def"))
   20255              :     return false;
   20256              : 
   20257              :   /* Get the tbl section, if there are defs. */
   20258          939 :   if (slurp->macro_defs.more_p ()
   20259          939 :       && !slurp->macro_tbl.begin (loc, from (), MOD_SNAME_PFX ".mac"))
   20260              :     return false;
   20261              : 
   20262              :   return true;
   20263              : }
   20264              : 
   20265              : /* Install the macro name table.  */
   20266              : 
   20267              : void
   20268          945 : module_state::install_macros ()
   20269              : {
   20270          945 :   bytes_in &sec = slurp->macro_tbl;
   20271          945 :   if (!sec.size)
   20272              :     return;
   20273              : 
   20274          202 :   dump () && dump ("Reading macro table %M", this);
   20275          180 :   dump.indent ();
   20276              : 
   20277          180 :   unsigned count = sec.u ();
   20278          202 :   dump () && dump ("%u macros", count);
   20279        80178 :   while (count--)
   20280              :     {
   20281        79998 :       cpp_hashnode *node = sec.cpp_node ();
   20282        79998 :       macro_import &imp = get_macro_imports (node);
   20283        79998 :       unsigned flags = sec.u () & macro_import::slot::L_BOTH;
   20284        79998 :       if (!flags)
   20285            0 :         sec.set_overrun ();
   20286              : 
   20287        79998 :       if (sec.get_overrun ())
   20288              :         break;
   20289              : 
   20290        79998 :       macro_import::slot &slot = imp.append (mod, flags);
   20291        79998 :       slot.offset = sec.u ();
   20292              : 
   20293        79998 :       dump (dumper::MACRO)
   20294           84 :         && dump ("Read %s macro %s%s%s %I at %u",
   20295           30 :                  imp.length () > 1 ? "add" : "new",
   20296           27 :                  flags & macro_import::slot::L_UNDEF ? "#undef" : "",
   20297              :                  flags == macro_import::slot::L_BOTH ? " & " : "",
   20298           30 :                  flags & macro_import::slot::L_DEF ? "#define" : "",
   20299              :                  identifier (node), slot.offset);
   20300              : 
   20301              :       /* We'll leak an imported definition's TOKEN_FLD_STR's data
   20302              :          here.  But that only happens when we've had to resolve the
   20303              :          deferred macro before this import -- why are you doing
   20304              :          that?  */
   20305        79998 :       if (cpp_macro *cur = cpp_set_deferred_macro (node))
   20306        32065 :         if (!cur->imported_p)
   20307              :           {
   20308        32065 :             macro_import::slot &slot = imp.exported ();
   20309        32065 :             macro_export &exp = get_macro_export (slot);
   20310        32065 :             exp.def = cur;
   20311        79998 :             dump (dumper::MACRO)
   20312            0 :               && dump ("Saving current #define %I", identifier (node));
   20313              :           }
   20314              :     }
   20315              : 
   20316              :   /* We're now done with the table.  */
   20317          180 :   elf_in::release (slurp->from, sec);
   20318              : 
   20319          180 :   dump.outdent ();
   20320              : }
   20321              : 
   20322              : /* Import the transitive macros.  */
   20323              : 
   20324              : void
   20325          903 : module_state::import_macros ()
   20326              : {
   20327          903 :   bitmap_ior_into (headers, slurp->headers);
   20328              : 
   20329          903 :   bitmap_iterator bititer;
   20330          903 :   unsigned bitnum;
   20331         1848 :   EXECUTE_IF_SET_IN_BITMAP (slurp->headers, 0, bitnum, bititer)
   20332          945 :     (*modules)[bitnum]->install_macros ();
   20333          903 : }
   20334              : 
   20335              : /* NODE is being undefined at LOC.  Record it in the export table, if
   20336              :    necessary.  */
   20337              : 
   20338              : void
   20339       337619 : module_state::undef_macro (cpp_reader *, location_t loc, cpp_hashnode *node)
   20340              : {
   20341       337619 :   if (!node->deferred)
   20342              :     /* The macro is not imported, so our undef is irrelevant.  */
   20343              :     return;
   20344              : 
   20345           12 :   unsigned n = dump.push (NULL);
   20346              : 
   20347           12 :   macro_import::slot &slot = (*macro_imports)[node->deferred - 1].exported ();
   20348           12 :   macro_export &exp = get_macro_export (slot);
   20349              : 
   20350           12 :   exp.undef_loc = loc;
   20351           12 :   slot.become_undef ();
   20352           12 :   exp.def = NULL;
   20353              : 
   20354           18 :   dump (dumper::MACRO) && dump ("Recording macro #undef %I", identifier (node));
   20355              : 
   20356           12 :   dump.pop (n);
   20357              : }
   20358              : 
   20359              : /* NODE is a deferred macro node.  Determine the definition and return
   20360              :    it, with NULL if undefined.  May issue diagnostics.
   20361              : 
   20362              :    This can leak memory, when merging declarations -- the string
   20363              :    contents (TOKEN_FLD_STR) of each definition are allocated in
   20364              :    unreclaimable cpp objstack.  Only one will win.  However, I do not
   20365              :    expect this to be common -- mostly macros have a single point of
   20366              :    definition.  Perhaps we could restore the objstack to its position
   20367              :    after the first imported definition (if that wins)?  The macros
   20368              :    themselves are GC'd.  */
   20369              : 
   20370              : cpp_macro *
   20371          437 : module_state::deferred_macro (cpp_reader *reader, location_t loc,
   20372              :                               cpp_hashnode *node)
   20373              : {
   20374          437 :   macro_import &imports = (*macro_imports)[node->deferred - 1];
   20375              : 
   20376          437 :   unsigned n = dump.push (NULL);
   20377          443 :   dump (dumper::MACRO) && dump ("Deferred macro %I", identifier (node));
   20378              : 
   20379          437 :   bitmap visible (BITMAP_GGC_ALLOC ());
   20380              : 
   20381          437 :   if (!((imports[0].get_defness () & macro_import::slot::L_UNDEF)
   20382            0 :         && !imports[0].get_module ()))
   20383              :     {
   20384              :       /* Calculate the set of visible header imports.  */
   20385          437 :       bitmap_copy (visible, headers);
   20386         1182 :       for (unsigned ix = imports.length (); ix--;)
   20387              :         {
   20388          745 :           const macro_import::slot &slot = imports[ix];
   20389          745 :           unsigned mod = slot.get_module ();
   20390          745 :           if ((slot.get_defness () & macro_import::slot::L_UNDEF)
   20391          745 :               && bitmap_bit_p (visible, mod))
   20392              :             {
   20393           12 :               bitmap arg = mod ? (*modules)[mod]->slurp->headers : headers;
   20394           12 :               bitmap_and_compl_into (visible, arg);
   20395           12 :               bitmap_set_bit (visible, mod);
   20396              :             }
   20397              :         }
   20398              :     }
   20399          437 :   bitmap_set_bit (visible, 0);
   20400              : 
   20401              :   /* Now find the macros that are still visible.  */
   20402          437 :   bool failed = false;
   20403          437 :   cpp_macro *def = NULL;
   20404          437 :   vec<macro_export> defs;
   20405          437 :   defs.create (imports.length ());
   20406         1182 :   for (unsigned ix = imports.length (); ix--;)
   20407              :     {
   20408          745 :       const macro_import::slot &slot = imports[ix];
   20409          745 :       unsigned mod = slot.get_module ();
   20410          745 :       if (bitmap_bit_p (visible, mod))
   20411              :         {
   20412          733 :           macro_export *pushed = NULL;
   20413          733 :           if (mod)
   20414              :             {
   20415          467 :               const module_state *imp = (*modules)[mod];
   20416          467 :               bytes_in &sec = imp->slurp->macro_defs;
   20417          467 :               if (!sec.get_overrun ())
   20418              :                 {
   20419          467 :                   dump (dumper::MACRO)
   20420            6 :                     && dump ("Reading macro %s%s%s %I module %M at %u",
   20421            6 :                              slot.get_defness () & macro_import::slot::L_UNDEF
   20422              :                              ? "#undef" : "",
   20423            6 :                              slot.get_defness () == macro_import::slot::L_BOTH
   20424              :                              ? " & " : "",
   20425            6 :                              slot.get_defness () & macro_import::slot::L_DEF
   20426              :                              ? "#define" : "",
   20427            6 :                              identifier (node), imp, slot.offset);
   20428          467 :                   sec.random_access (slot.offset);
   20429              : 
   20430          467 :                   macro_export exp;
   20431          467 :                   if (slot.get_defness () & macro_import::slot::L_UNDEF)
   20432           12 :                     exp.undef_loc = imp->read_location (sec);
   20433          467 :                   if (slot.get_defness () & macro_import::slot::L_DEF)
   20434          461 :                     exp.def = imp->read_define (sec, reader);
   20435          467 :                   if (sec.get_overrun ())
   20436            0 :                     error_at (loc, "macro definitions of %qE corrupted",
   20437            0 :                               imp->name);
   20438              :                   else
   20439          467 :                     pushed = defs.quick_push (exp);
   20440              :                 }
   20441              :             }
   20442              :           else
   20443          266 :             pushed = defs.quick_push ((*macro_exports)[slot.offset]);
   20444          733 :           if (pushed && pushed->def)
   20445              :             {
   20446          727 :               if (!def)
   20447              :                 def = pushed->def;
   20448          293 :               else if (cpp_compare_macros (def, pushed->def))
   20449          745 :                 failed = true;
   20450              :             }
   20451              :         }
   20452              :     }
   20453              : 
   20454          437 :   if (failed)
   20455              :     {
   20456              :       /* If LOC is the first loc, this is the end of file check, which
   20457              :          is a warning.  */
   20458           15 :       auto_diagnostic_group d;
   20459           15 :       if (loc == MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0)))
   20460            9 :         warning_at (loc, OPT_Winvalid_imported_macros,
   20461              :                     "inconsistent imported macro definition %qE",
   20462              :                     identifier (node));
   20463              :       else
   20464            6 :         error_at (loc, "inconsistent imported macro definition %qE",
   20465              :                   identifier (node));
   20466           60 :       for (unsigned ix = defs.length (); ix--;)
   20467              :         {
   20468           30 :           macro_export &exp = defs[ix];
   20469           30 :           if (exp.undef_loc)
   20470            0 :             inform (exp.undef_loc, "%<#undef %E%>", identifier (node));
   20471           30 :           if (exp.def)
   20472           30 :             inform (exp.def->line, "%<#define %s%>",
   20473              :                     cpp_macro_definition (reader, node, exp.def));
   20474              :         }
   20475           15 :       def = NULL;
   20476           15 :     }
   20477              : 
   20478          437 :   defs.release ();
   20479              : 
   20480          437 :   dump.pop (n);
   20481              : 
   20482          437 :   return def;
   20483              : }
   20484              : 
   20485              : /* Stream the static aggregates.  Sadly some headers (ahem:
   20486              :    iostream) contain static vars, and rely on them to run global
   20487              :    ctors.  */
   20488              : unsigned
   20489          909 : module_state::write_inits (elf_out *to, depset::hash &table, unsigned *crc_ptr)
   20490              : {
   20491          909 :   if (!static_aggregates && !tls_aggregates)
   20492              :     return 0;
   20493              : 
   20494           45 :   dump () && dump ("Writing initializers");
   20495           45 :   dump.indent ();
   20496              : 
   20497           45 :   static_aggregates = nreverse (static_aggregates);
   20498           45 :   tls_aggregates = nreverse (tls_aggregates);
   20499              : 
   20500           45 :   unsigned count = 0;
   20501           45 :   trees_out sec (to, this, table, ~0u);
   20502           45 :   sec.begin ();
   20503              : 
   20504           45 :   tree list = static_aggregates;
   20505          135 :   for (int passes = 0; passes != 2; passes++)
   20506              :     {
   20507          258 :       for (tree init = list; init; init = TREE_CHAIN (init))
   20508          168 :         if (TREE_LANG_FLAG_0 (init))
   20509              :           {
   20510          144 :             if (STATIC_INIT_DECOMP_BASE_P (init))
   20511              :               {
   20512              :                 /* Ensure that in the returned result chain if the
   20513              :                    STATIC_INIT_DECOMP_*BASE_P flags are set, there is
   20514              :                    always one or more STATIC_INIT_DECOMP_BASE_P TREE_LIST
   20515              :                    followed by one or more STATIC_INIT_DECOMP_NONBASE_P.  */
   20516           21 :                 int phase = 0;
   20517           21 :                 tree last = NULL_TREE;
   20518           21 :                 for (tree init2 = TREE_CHAIN (init);
   20519          102 :                      init2; init2 = TREE_CHAIN (init2))
   20520              :                   {
   20521          123 :                     if (phase == 0 && STATIC_INIT_DECOMP_BASE_P (init2))
   20522              :                       ;
   20523          102 :                     else if (phase == 0
   20524          123 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20525              :                       {
   20526          102 :                         phase = TREE_LANG_FLAG_0 (init2) ? 2 : 1;
   20527              :                         last = init2;
   20528              :                       }
   20529           81 :                     else if (IN_RANGE (phase, 1, 2)
   20530          162 :                              && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20531              :                       {
   20532           60 :                         if (TREE_LANG_FLAG_0 (init2))
   20533           81 :                           phase = 2;
   20534              :                         last = init2;
   20535              :                       }
   20536              :                     else
   20537              :                       break;
   20538              :                   }
   20539           21 :                 if (phase == 2)
   20540              :                   {
   20541              :                     /* In that case, add markers about it so that the
   20542              :                        STATIC_INIT_DECOMP_BASE_P and
   20543              :                        STATIC_INIT_DECOMP_NONBASE_P flags can be restored.  */
   20544           21 :                     sec.tree_node (build_int_cst (integer_type_node,
   20545           21 :                                                   2 * passes + 1));
   20546           21 :                     phase = 1;
   20547          123 :                     for (tree init2 = init; init2 != TREE_CHAIN (last);
   20548          102 :                          init2 = TREE_CHAIN (init2))
   20549          102 :                       if (TREE_LANG_FLAG_0 (init2))
   20550              :                         {
   20551          102 :                           tree decl = TREE_VALUE (init2);
   20552          102 :                           if (phase == 1
   20553          102 :                               && STATIC_INIT_DECOMP_NONBASE_P (init2))
   20554              :                             {
   20555           21 :                               sec.tree_node (build_int_cst (integer_type_node,
   20556           21 :                                                             2 * passes + 2));
   20557           21 :                               phase = 2;
   20558              :                             }
   20559          102 :                           dump ("Initializer:%u for %N", count, decl);
   20560          102 :                           sec.tree_node (decl);
   20561          102 :                           ++count;
   20562              :                         }
   20563           21 :                     sec.tree_node (integer_zero_node);
   20564           21 :                     init = last;
   20565           21 :                     continue;
   20566           21 :                   }
   20567              :               }
   20568              : 
   20569          123 :             tree decl = TREE_VALUE (init);
   20570              : 
   20571          123 :             dump ("Initializer:%u for %N", count, decl);
   20572          123 :             sec.tree_node (decl);
   20573          123 :             ++count;
   20574              :           }
   20575              : 
   20576           90 :       list = tls_aggregates;
   20577              :     }
   20578              : 
   20579           45 :   sec.end (to, to->name (MOD_SNAME_PFX ".ini"), crc_ptr);
   20580           45 :   dump.outdent ();
   20581              : 
   20582           45 :   return count;
   20583           45 : }
   20584              : 
   20585              : /* We have to defer some post-load processing until we've completed
   20586              :    reading, because they can cause more reading.  */
   20587              : 
   20588              : static void
   20589        12490 : post_load_processing ()
   20590              : {
   20591              :   /* We mustn't cause a GC, our caller should have arranged for that
   20592              :      not to happen.  */
   20593        12490 :   gcc_checking_assert (function_depth);
   20594              : 
   20595        12490 :   if (!post_load_decls)
   20596              :     return;
   20597              : 
   20598         7805 :   tree old_cfd = current_function_decl;
   20599         7805 :   struct function *old_cfun = cfun;
   20600        16773 :   while (post_load_decls->length ())
   20601              :     {
   20602         8968 :       tree decl = post_load_decls->pop ();
   20603              : 
   20604         9023 :       dump () && dump ("Post-load processing of %N", decl);
   20605              : 
   20606         8968 :       if (VAR_P (decl) && DECL_NTTP_OBJECT_P (decl))
   20607              :         {
   20608            9 :           if (!DECL_SIZE (decl))
   20609              :             {
   20610            3 :               push_to_top_level ();
   20611            3 :               cp_finish_decl (decl, DECL_INITIAL (decl), false, NULL_TREE, 0);
   20612            3 :               pop_from_top_level ();
   20613              :             }
   20614            9 :           continue;
   20615              :         }
   20616              : 
   20617         8959 :       gcc_checking_assert (DECL_MAYBE_IN_CHARGE_CDTOR_P (decl));
   20618         8959 :       expand_or_defer_fn (decl);
   20619              :       /* As in module_state::read_cluster.  */
   20620          444 :       if (at_eof && DECL_COMDAT (decl) && DECL_EXTERNAL (decl)
   20621         8987 :           && DECL_NOT_REALLY_EXTERN (decl))
   20622            6 :         DECL_EXTERNAL (decl) = false;
   20623              :     }
   20624              : 
   20625         7805 :   set_cfun (old_cfun);
   20626         7805 :   current_function_decl = old_cfd;
   20627              : }
   20628              : 
   20629              : bool
   20630           45 : module_state::read_inits (unsigned count)
   20631              : {
   20632           45 :   trees_in sec (this);
   20633           45 :   if (!sec.begin (loc, from (), from ()->find (MOD_SNAME_PFX ".ini")))
   20634              :     return false;
   20635           57 :   dump () && dump ("Reading %u initializers", count);
   20636           45 :   dump.indent ();
   20637              : 
   20638           45 :   lazy_snum = ~0u;
   20639           45 :   int decomp_phase = 0;
   20640           45 :   tree *aggrp = NULL;
   20641          270 :   for (unsigned ix = 0; ix != count; ix++)
   20642              :     {
   20643          225 :       tree last = NULL_TREE;
   20644          225 :       if (decomp_phase)
   20645          102 :         last = *aggrp;
   20646              :       /* Merely referencing the decl causes its initializer to be read
   20647              :          and added to the correct list.  */
   20648          225 :       tree decl = sec.tree_node ();
   20649              :       /* module_state::write_inits can add special INTEGER_CST markers in
   20650              :          between the decls.  1 means STATIC_INIT_DECOMP_BASE_P entries
   20651              :          follow in static_aggregates, 2 means STATIC_INIT_DECOMP_NONBASE_P
   20652              :          entries follow in static_aggregates, 3 means
   20653              :          STATIC_INIT_DECOMP_BASE_P entries follow in tls_aggregates,
   20654              :          4 means STATIC_INIT_DECOMP_NONBASE_P follow in tls_aggregates,
   20655              :          0 means end of STATIC_INIT_DECOMP_{,NON}BASE_P sequence.  */
   20656          225 :       if (tree_fits_shwi_p (decl))
   20657              :         {
   20658           63 :           if (sec.get_overrun ())
   20659              :             break;
   20660           63 :           decomp_phase = tree_to_shwi (decl);
   20661           63 :           if (decomp_phase)
   20662              :             {
   20663           42 :               aggrp = decomp_phase > 2 ? &tls_aggregates : &static_aggregates;
   20664              :               last = *aggrp;
   20665              :             }
   20666           63 :           decl = sec.tree_node ();
   20667              :         }
   20668              : 
   20669          225 :       if (sec.get_overrun ())
   20670              :         break;
   20671          225 :       if (decl)
   20672          225 :         dump ("Initializer:%u for %N", ix, decl);
   20673          225 :       if (decomp_phase)
   20674              :         {
   20675          102 :           tree init = *aggrp;
   20676          102 :           gcc_assert (TREE_VALUE (init) == decl && TREE_CHAIN (init) == last);
   20677          102 :           if ((decomp_phase & 1) != 0)
   20678           21 :             STATIC_INIT_DECOMP_BASE_P (init) = 1;
   20679              :           else
   20680           81 :             STATIC_INIT_DECOMP_NONBASE_P (init) = 1;
   20681              :         }
   20682              :     }
   20683           45 :   if (decomp_phase && !sec.get_overrun ())
   20684              :     {
   20685            0 :       tree decl = sec.tree_node ();
   20686            0 :       gcc_assert (integer_zerop (decl));
   20687              :     }
   20688           45 :   lazy_snum = 0;
   20689           45 :   post_load_processing ();
   20690           45 :   dump.outdent ();
   20691           45 :   if (!sec.end (from ()))
   20692              :     return false;
   20693              :   return true;
   20694           45 : }
   20695              : 
   20696              : void
   20697         2815 : module_state::write_counts (elf_out *to, unsigned counts[MSC_HWM],
   20698              :                             unsigned *crc_ptr)
   20699              : {
   20700         2815 :   bytes_out cfg (to);
   20701              : 
   20702         2815 :   cfg.begin ();
   20703              : 
   20704        30965 :   for (unsigned ix = MSC_HWM; ix--;)
   20705        25335 :     cfg.u (counts[ix]);
   20706              : 
   20707         2815 :   if (dump ())
   20708              :     {
   20709          300 :       dump ("Cluster sections are [%u,%u)",
   20710              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20711          300 :       dump ("Bindings %u", counts[MSC_bindings]);
   20712          300 :       dump ("Pendings %u", counts[MSC_pendings]);
   20713          300 :       dump ("Entities %u", counts[MSC_entities]);
   20714          300 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20715          300 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20716          300 :       dump ("Macros %u", counts[MSC_macros]);
   20717          300 :       dump ("Initializers %u", counts[MSC_inits]);
   20718              :     }
   20719              : 
   20720         2815 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cnt"), crc_ptr);
   20721         2815 : }
   20722              : 
   20723              : bool
   20724         2997 : module_state::read_counts (unsigned counts[MSC_HWM])
   20725              : {
   20726         2997 :   bytes_in cfg;
   20727              : 
   20728         2997 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cnt"))
   20729              :     return false;
   20730              : 
   20731        29970 :   for (unsigned ix = MSC_HWM; ix--;)
   20732        26973 :     counts[ix] = cfg.u ();
   20733              : 
   20734         2997 :   if (dump ())
   20735              :     {
   20736          532 :       dump ("Declaration sections are [%u,%u)",
   20737              :             counts[MSC_sec_lwm], counts[MSC_sec_hwm]);
   20738          532 :       dump ("Bindings %u", counts[MSC_bindings]);
   20739          532 :       dump ("Pendings %u", counts[MSC_pendings]);
   20740          532 :       dump ("Entities %u", counts[MSC_entities]);
   20741          532 :       dump ("Namespaces %u", counts[MSC_namespaces]);
   20742          532 :       dump ("Using-directives %u", counts[MSC_using_directives]);
   20743          532 :       dump ("Macros %u", counts[MSC_macros]);
   20744          532 :       dump ("Initializers %u", counts[MSC_inits]);
   20745              :     }
   20746              : 
   20747         2997 :   return cfg.end (from ());
   20748         2997 : }
   20749              : 
   20750              : /* Tool configuration:  MOD_SNAME_PFX .config
   20751              : 
   20752              :    This is data that confirms current state (or fails).  */
   20753              : 
   20754              : void
   20755         2815 : module_state::write_config (elf_out *to, module_state_config &config,
   20756              :                             unsigned inner_crc)
   20757              : {
   20758         2815 :   bytes_out cfg (to);
   20759              : 
   20760         2815 :   cfg.begin ();
   20761              : 
   20762              :   /* Write version and inner crc as u32 values, for easier
   20763              :      debug inspection.  */
   20764         3115 :   dump () && dump ("Writing version=%V, inner_crc=%x",
   20765              :                    MODULE_VERSION, inner_crc);
   20766         2815 :   cfg.u32 (unsigned (MODULE_VERSION));
   20767         2815 :   cfg.u32 (inner_crc);
   20768              : 
   20769         2815 :   cfg.u (to->name (is_header () ? "" : get_flatname ()));
   20770              : 
   20771              :   /* Configuration. */
   20772         3115 :   dump () && dump ("Writing target='%s', host='%s'",
   20773              :                    TARGET_MACHINE, HOST_MACHINE);
   20774         2815 :   unsigned target = to->name (TARGET_MACHINE);
   20775         2815 :   unsigned host = (!strcmp (TARGET_MACHINE, HOST_MACHINE)
   20776              :                    ? target : to->name (HOST_MACHINE));
   20777         2815 :   cfg.u (target);
   20778         2815 :   cfg.u (host);
   20779              : 
   20780         2815 :   cfg.str (config.dialect_str);
   20781         2815 :   cfg.u (extensions);
   20782              : 
   20783              :   /* Global tree information.  We write the globals crc separately,
   20784              :      rather than mix it directly into the overall crc, as it is used
   20785              :      to ensure data match between instances of the compiler, not
   20786              :      integrity of the file.  */
   20787         3115 :   dump () && dump ("Writing globals=%u, crc=%x",
   20788              :                    fixed_trees->length (), global_crc);
   20789         2815 :   cfg.u (fixed_trees->length ());
   20790         2815 :   cfg.u32 (global_crc);
   20791              : 
   20792         2815 :   if (is_partition ())
   20793          208 :     cfg.u (is_interface ());
   20794              : 
   20795         2815 :   cfg.u (config.num_imports);
   20796         2815 :   cfg.u (config.num_partitions);
   20797         2815 :   cfg.u (config.num_entities);
   20798              : 
   20799         2815 :   cfg.loc (config.ordinary_locs);
   20800         2815 :   cfg.loc (config.macro_locs);
   20801         2815 :   cfg.u (config.loc_range_bits);
   20802              : 
   20803         2815 :   cfg.u (config.active_init);
   20804              : 
   20805              :   /* Now generate CRC, we'll have incorporated the inner CRC because
   20806              :      of its serialization above.  */
   20807         2815 :   cfg.end (to, to->name (MOD_SNAME_PFX ".cfg"), &crc);
   20808         3115 :   dump () && dump ("Writing CRC=%x", crc);
   20809         2815 : }
   20810              : 
   20811              : void
   20812           40 : module_state::note_cmi_name ()
   20813              : {
   20814           40 :   if (!cmi_noted_p && filename)
   20815              :     {
   20816           40 :       cmi_noted_p = true;
   20817           40 :       inform (loc, "compiled module file is %qs",
   20818              :               maybe_add_cmi_prefix (filename));
   20819              :     }
   20820           40 : }
   20821              : 
   20822              : bool
   20823         3105 : module_state::read_config (module_state_config &config, bool complain)
   20824              : {
   20825         3105 :   bytes_in cfg;
   20826              : 
   20827         3105 :   if (!cfg.begin (loc, from (), MOD_SNAME_PFX ".cfg"))
   20828              :     return false;
   20829              : 
   20830              :   /* Check version.  */
   20831         3105 :   unsigned my_ver = MODULE_VERSION;
   20832         3105 :   unsigned their_ver = cfg.u32 ();
   20833         3640 :   dump () && dump  (my_ver == their_ver ? "Version %V"
   20834              :                     : "Expecting %V found %V", my_ver, their_ver);
   20835         3105 :   if (their_ver != my_ver)
   20836              :     {
   20837              :       /* The compiler versions differ.  Close enough? */
   20838            0 :       verstr_t my_string, their_string;
   20839              : 
   20840            0 :       version2string (my_ver, my_string);
   20841            0 :       version2string (their_ver, their_string);
   20842              : 
   20843              :       /* Reject when either is non-experimental or when experimental
   20844              :          major versions differ.  */
   20845            0 :       auto_diagnostic_group d;
   20846            0 :       bool reject_p = ((!IS_EXPERIMENTAL (my_ver)
   20847              :                         || !IS_EXPERIMENTAL (their_ver)
   20848            0 :                         || MODULE_MAJOR (my_ver) != MODULE_MAJOR (their_ver))
   20849              :                        /* The 'I know what I'm doing' switch.  */
   20850            0 :                        && !flag_module_version_ignore);
   20851            0 :       bool inform_p = true;
   20852            0 :       if (!complain)
   20853              :         inform_p = false;
   20854            0 :       else if (reject_p)
   20855              :         {
   20856            0 :           cfg.set_overrun ();
   20857            0 :           error_at (loc, "compiled module is %sversion %s",
   20858              :                     IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20859              :                     their_string);
   20860              :         }
   20861              :       else
   20862            0 :         inform_p = warning_at (loc, 0, "compiled module is %sversion %s",
   20863              :                              IS_EXPERIMENTAL (their_ver) ? "experimental " : "",
   20864              :                              their_string);
   20865              : 
   20866            0 :       if (inform_p)
   20867              :         {
   20868            0 :           inform (loc, "compiler is %sversion %s%s%s",
   20869              :                   IS_EXPERIMENTAL (my_ver) ? "experimental " : "",
   20870              :                   my_string,
   20871            0 :                   reject_p ? "" : flag_module_version_ignore
   20872            0 :                   ? ", be it on your own head!" : ", close enough?",
   20873              :                   reject_p ? "" : " \xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf");
   20874            0 :           note_cmi_name ();
   20875              :         }
   20876              : 
   20877            0 :       if (reject_p)
   20878            0 :         goto done;
   20879            0 :     }
   20880              : 
   20881              :   /*  We wrote the inner crc merely to merge it, so simply read it
   20882              :       back and forget it.  */
   20883         3105 :   cfg.u32 ();
   20884              : 
   20885              :   /* Check module name.  */
   20886         3105 :   {
   20887         3105 :     const char *their_name = from ()->name (cfg.u ());
   20888         3105 :     const char *our_name = "";
   20889              : 
   20890         3105 :     if (!is_header ())
   20891         2066 :       our_name = get_flatname ();
   20892              : 
   20893              :     /* Header units can be aliased, so name checking is
   20894              :        inappropriate.  */
   20895         3105 :     if (0 != strcmp (their_name, our_name))
   20896              :       {
   20897            0 :         error_at (loc,
   20898            0 :                   their_name[0] && our_name[0] ? G_("module %qs found")
   20899              :                   : their_name[0]
   20900              :                   ? G_("header module expected, module %qs found")
   20901              :                   : G_("module %qs expected, header module found"),
   20902            0 :                   their_name[0] ? their_name : our_name);
   20903            0 :         cfg.set_overrun ();
   20904            0 :         goto done;
   20905              :       }
   20906              :   }
   20907              : 
   20908              :   /* Check the CRC after the above sanity checks, so that the user is
   20909              :      clued in.  */
   20910         3105 :   {
   20911         3105 :     unsigned e_crc = crc;
   20912         3105 :     crc = cfg.get_crc ();
   20913         3640 :     dump () && dump ("Reading CRC=%x", crc);
   20914              :     /* When not complaining we haven't set directness yet, so ignore the
   20915              :        mismatch. */
   20916         3105 :     if (complain && !is_direct () && crc != e_crc)
   20917              :       {
   20918            3 :         error_at (loc, "module %qs CRC mismatch", get_flatname ());
   20919            3 :         cfg.set_overrun ();
   20920            3 :         goto done;
   20921              :       }
   20922              :   }
   20923              : 
   20924              :   /* Check target & host.  */
   20925         3102 :   {
   20926         3102 :     const char *their_target = from ()->name (cfg.u ());
   20927         3102 :     const char *their_host = from ()->name (cfg.u ());
   20928         3637 :     dump () && dump ("Read target='%s', host='%s'", their_target, their_host);
   20929         3102 :     if (strcmp (their_target, TARGET_MACHINE)
   20930         3102 :         || strcmp (their_host, HOST_MACHINE))
   20931              :       {
   20932            0 :         error_at (loc, "target & host is %qs:%qs, expected %qs:%qs",
   20933              :                   their_target, TARGET_MACHINE, their_host, HOST_MACHINE);
   20934            0 :         cfg.set_overrun ();
   20935            0 :         goto done;
   20936              :       }
   20937              :   }
   20938              : 
   20939              :   /* Check compilation dialect.  This must match.  */
   20940         3102 :   {
   20941         3102 :     const char *their_dialect = cfg.str ();
   20942         3102 :     if (strcmp (their_dialect, config.dialect_str))
   20943              :       {
   20944            1 :         if (complain)
   20945            1 :           error_at (loc, "language dialect differs %qs, expected %qs",
   20946              :                     their_dialect, config.dialect_str);
   20947            1 :         cfg.set_overrun ();
   20948            1 :         goto done;
   20949              :       }
   20950              :   }
   20951              : 
   20952              :   /* Check for extensions.  If they set any, we must have them set
   20953              :      too.  */
   20954         3101 :   {
   20955         3101 :     unsigned ext = cfg.u ();
   20956         3101 :     unsigned allowed = (flag_openmp ? SE_OPENMP | SE_OPENMP_SIMD : 0);
   20957         3101 :     if (flag_openmp_simd)
   20958            3 :       allowed |= SE_OPENMP_SIMD;
   20959         3101 :     if (flag_openacc)
   20960            3 :       allowed |= SE_OPENACC;
   20961              : 
   20962         3101 :     if (unsigned bad = ext & ~allowed)
   20963              :       {
   20964            9 :         if (bad & SE_OPENMP)
   20965            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> to enable");
   20966            6 :         else if (bad & SE_OPENMP_SIMD)
   20967            3 :           error_at (loc, "module contains OpenMP, use %<-fopenmp%> or "
   20968              :                          "%<-fopenmp-simd%> to enable");
   20969            9 :         if (bad & SE_OPENACC)
   20970            3 :           error_at (loc, "module contains OpenACC, use %<-fopenacc%> to "
   20971              :                          "enable");
   20972            9 :         cfg.set_overrun ();
   20973            9 :         goto done;
   20974              :       }
   20975         3092 :     extensions = ext;
   20976              :   }
   20977              : 
   20978              :   /* Check global trees.  */
   20979         3092 :   {
   20980         3092 :     unsigned their_fixed_length = cfg.u ();
   20981         3092 :     unsigned their_fixed_crc = cfg.u32 ();
   20982         3627 :     dump () && dump ("Read globals=%u, crc=%x",
   20983              :                      their_fixed_length, their_fixed_crc);
   20984         3092 :     if (!flag_preprocess_only
   20985         3092 :         && (their_fixed_length != fixed_trees->length ()
   20986         3038 :             || their_fixed_crc != global_crc))
   20987              :       {
   20988            0 :         error_at (loc, "fixed tree mismatch");
   20989            0 :         cfg.set_overrun ();
   20990            0 :         goto done;
   20991              :       }
   20992              :   }
   20993              : 
   20994              :   /* All non-partitions are interfaces.  */
   20995         3092 :   interface_p = !is_partition () || cfg.u ();
   20996              : 
   20997         3092 :   config.num_imports = cfg.u ();
   20998         3092 :   config.num_partitions = cfg.u ();
   20999         3092 :   config.num_entities = cfg.u ();
   21000              : 
   21001         3092 :   config.ordinary_locs = cfg.loc ();
   21002         3092 :   config.macro_locs = cfg.loc ();
   21003         3092 :   config.loc_range_bits = cfg.u ();
   21004              : 
   21005         3092 :   config.active_init = cfg.u ();
   21006              : 
   21007         3105 :  done:
   21008         3105 :   return cfg.end (from ());
   21009         3105 : }
   21010              : 
   21011              : /* Comparator for ordering the Ordered Ordinary Location array.  */
   21012              : 
   21013              : static int
   21014          120 : ool_cmp (const void *a_, const void *b_)
   21015              : {
   21016          120 :   auto *a = *static_cast<const module_state *const *> (a_);
   21017          120 :   auto *b = *static_cast<const module_state *const *> (b_);
   21018          120 :   if (a == b)
   21019              :     return 0;
   21020          120 :   else if (a->ordinary_locs.first < b->ordinary_locs.first)
   21021              :     return -1;
   21022              :   else
   21023           50 :     return +1;
   21024              : }
   21025              : 
   21026              : /* Use ELROND format to record the following sections:
   21027              :      qualified-names        : binding value(s)
   21028              :      MOD_SNAME_PFX.README   : human readable, strings
   21029              :      MOD_SNAME_PFX.ENV      : environment strings, strings
   21030              :      MOD_SNAME_PFX.nms      : namespace hierarchy
   21031              :      MOD_SNAME_PFX.udi      : namespace using-directives
   21032              :      MOD_SNAME_PFX.bnd      : binding table
   21033              :      MOD_SNAME_PFX.spc      : specialization table
   21034              :      MOD_SNAME_PFX.imp      : import table
   21035              :      MOD_SNAME_PFX.ent      : entity table
   21036              :      MOD_SNAME_PFX.prt      : partitions table
   21037              :      MOD_SNAME_PFX.olm      : ordinary line maps
   21038              :      MOD_SNAME_PFX.mlm      : macro line maps
   21039              :      MOD_SNAME_PFX.def      : macro definitions
   21040              :      MOD_SNAME_PFX.mac      : macro index
   21041              :      MOD_SNAME_PFX.ini      : inits
   21042              :      MOD_SNAME_PFX.cnt      : counts
   21043              :      MOD_SNAME_PFX.cfg      : config data
   21044              : */
   21045              : 
   21046              : bool
   21047         2844 : module_state::write_begin (elf_out *to, cpp_reader *reader,
   21048              :                            module_state_config &config, unsigned &crc)
   21049              : {
   21050              :   /* Figure out remapped module numbers, which might elide
   21051              :      partitions.  */
   21052         2844 :   bitmap partitions = NULL;
   21053         2844 :   if (!is_header () && !is_partition ())
   21054         1727 :     partitions = BITMAP_GGC_ALLOC ();
   21055         2844 :   write_init_maps ();
   21056              : 
   21057         2844 :   unsigned mod_hwm = 1;
   21058         6373 :   for (unsigned ix = 1; ix != modules->length (); ix++)
   21059              :     {
   21060          685 :       module_state *imp = (*modules)[ix];
   21061              : 
   21062              :       /* Promote any non-partition direct import from a partition, unless
   21063              :          we're a partition.  */
   21064          625 :       if (!is_partition () && !imp->is_partition ()
   21065         1120 :           && imp->is_partition_direct ())
   21066           12 :         imp->directness = MD_PURVIEW_DIRECT;
   21067              : 
   21068              :       /* Write any import that is not a partition, unless we're a
   21069              :          partition.  */
   21070          685 :       if (!partitions || !imp->is_partition ())
   21071          495 :         imp->remap = mod_hwm++;
   21072              :       else
   21073              :         {
   21074          229 :           dump () && dump ("Partition %M %u", imp, ix);
   21075          190 :           bitmap_set_bit (partitions, ix);
   21076          190 :           imp->remap = 0;
   21077              :           /* All interface partitions must be exported.  */
   21078          190 :           if (imp->is_interface () && !bitmap_bit_p (exports, imp->mod))
   21079              :             {
   21080            3 :               error_at (imp->loc, "interface partition is not exported");
   21081            3 :               bitmap_set_bit (exports, imp->mod);
   21082              :             }
   21083              : 
   21084              :           /* All the partition entities should have been loaded when
   21085              :              loading the partition.  */
   21086          190 :           if (CHECKING_P)
   21087         1245 :             for (unsigned jx = 0; jx != imp->entity_num; jx++)
   21088              :               {
   21089         1055 :                 binding_slot *slot = &(*entity_ary)[imp->entity_lwm + jx];
   21090         1055 :                 gcc_checking_assert (!slot->is_lazy ());
   21091              :               }
   21092              :         }
   21093              : 
   21094          685 :       if (imp->is_direct () && (imp->remap || imp->is_partition ()))
   21095          668 :         note_location (imp->imported_from ());
   21096              :     }
   21097              : 
   21098         2844 :   if (partitions && bitmap_empty_p (partitions))
   21099              :     /* No partitions present.  */
   21100              :     partitions = nullptr;
   21101              : 
   21102              :   /* Find the set of decls we must write out.  */
   21103         2844 :   depset::hash table (DECL_NAMESPACE_BINDINGS (global_namespace)->size () * 8);
   21104              :   /* Add the specializations before the writables, so that we can
   21105              :      detect injected friend specializations.  */
   21106         2844 :   table.add_specializations (true);
   21107         2844 :   table.add_specializations (false);
   21108         2844 :   if (partial_specializations)
   21109              :     {
   21110          216 :       table.add_partial_entities (partial_specializations);
   21111          216 :       partial_specializations = NULL;
   21112              :     }
   21113         2844 :   table.add_namespace_entities (global_namespace, partitions);
   21114         2844 :   if (class_members)
   21115              :     {
   21116           12 :       table.add_class_entities (class_members);
   21117           12 :       class_members = NULL;
   21118              :     }
   21119              : 
   21120              :   /* Now join everything up.  */
   21121         2844 :   table.find_dependencies (this);
   21122              : 
   21123         2844 :   if (!table.finalize_dependencies ())
   21124              :     return false;
   21125              : 
   21126              : #if CHECKING_P
   21127              :   /* We're done verifying at-most once reading, reset to verify
   21128              :      at-most once writing.  */
   21129         2815 :   note_defs = note_defs_table_t::create_ggc (1000);
   21130              : #endif
   21131              : 
   21132              :   /* Determine Strongly Connected Components.  This will also strip any
   21133              :      unnecessary dependencies on imported or TU-local entities.  */
   21134         2815 :   vec<depset *> sccs = table.connect ();
   21135              : 
   21136         2815 :   vec_alloc (ool, modules->length ());
   21137         3493 :   for (unsigned ix = modules->length (); --ix;)
   21138              :     {
   21139          678 :       auto *import = (*modules)[ix];
   21140          678 :       if (import->loadedness > ML_NONE
   21141          678 :           && !(partitions && bitmap_bit_p (partitions, import->mod)))
   21142          488 :         ool->quick_push (import);
   21143              :     }
   21144         2815 :   ool->qsort (ool_cmp);
   21145              : 
   21146         2815 :   write_diagnostic_classification (nullptr, global_dc, nullptr);
   21147              : 
   21148         2815 :   vec<cpp_hashnode *> *macros = nullptr;
   21149         2815 :   if (is_header ())
   21150          909 :     macros = prepare_macros (reader);
   21151              : 
   21152         2815 :   config.num_imports = mod_hwm;
   21153         2815 :   config.num_partitions = modules->length () - mod_hwm;
   21154         2815 :   auto map_info = write_prepare_maps (&config, bool (config.num_partitions));
   21155         2815 :   unsigned counts[MSC_HWM];
   21156         2815 :   memset (counts, 0, sizeof (counts));
   21157              : 
   21158              :   /* depset::cluster is the cluster number,
   21159              :      depset::section is unspecified scratch value.
   21160              : 
   21161              :      The following loops make use of the tarjan property that
   21162              :      dependencies will be earlier in the SCCS array.  */
   21163              : 
   21164              :   /* This first loop determines the number of depsets in each SCC, and
   21165              :      also the number of namespaces we're dealing with.  During the
   21166              :      loop, the meaning of a couple of depset fields now change:
   21167              : 
   21168              :      depset::cluster -> size_of cluster, if first of cluster & !namespace
   21169              :      depset::section -> section number of cluster (if !namespace). */
   21170              : 
   21171         2815 :   unsigned n_spaces = 0;
   21172         2815 :   counts[MSC_sec_lwm] = counts[MSC_sec_hwm] = to->get_section_limit ();
   21173       345799 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21174              :     {
   21175       342984 :       depset **base = &sccs[ix];
   21176              : 
   21177       649248 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21178              :         {
   21179         5301 :           n_spaces++;
   21180         5301 :           size = 1;
   21181              :         }
   21182              :       else
   21183              :         {
   21184              :           /* Count the members in this cluster.  */
   21185      1535939 :           for (size = 1; ix + size < sccs.length (); size++)
   21186      1533464 :             if (base[size]->cluster != base[0]->cluster)
   21187              :               break;
   21188              : 
   21189      1873622 :           for (unsigned jx = 0; jx != size; jx++)
   21190              :             {
   21191              :               /* Set the section number.  */
   21192      1535939 :               base[jx]->cluster = ~(~0u >> 1); /* A bad value.  */
   21193      1535939 :               base[jx]->section = counts[MSC_sec_hwm];
   21194              :             }
   21195              : 
   21196              :           /* Save the size in the first member's cluster slot.  */
   21197       337683 :           base[0]->cluster = size;
   21198              : 
   21199       337683 :           counts[MSC_sec_hwm]++;
   21200              :         }
   21201              :     }
   21202              : 
   21203              :   /* Write the clusters.  Namespace decls are put in the spaces array.
   21204              :      The meaning of depset::cluster changes to provide the
   21205              :      unnamed-decl count of the depset's decl (and remains zero for
   21206              :      non-decls and non-unnamed).  */
   21207         2815 :   unsigned bytes = 0;
   21208         2815 :   vec<depset *> spaces;
   21209         2815 :   spaces.create (n_spaces);
   21210              : 
   21211       348614 :   for (unsigned size, ix = 0; ix < sccs.length (); ix += size)
   21212              :     {
   21213       342984 :       depset **base = &sccs[ix];
   21214              : 
   21215       342984 :       if (base[0]->get_entity_kind () == depset::EK_NAMESPACE)
   21216              :         {
   21217         5301 :           tree decl = base[0]->get_entity ();
   21218         5301 :           if (decl == global_namespace)
   21219         2555 :             base[0]->cluster = 0;
   21220         2746 :           else if (!base[0]->is_import ())
   21221              :             {
   21222         2746 :               base[0]->cluster = counts[MSC_entities]++;
   21223         2746 :               spaces.quick_push (base[0]);
   21224         2746 :               counts[MSC_namespaces]++;
   21225         2746 :               if (CHECKING_P)
   21226              :                 {
   21227              :                   /* Add it to the entity map, such that we can tell it is
   21228              :                      part of us.  */
   21229         2746 :                   bool existed;
   21230         2746 :                   unsigned *slot = &entity_map->get_or_insert
   21231         2746 :                     (DECL_UID (decl), &existed);
   21232         2746 :                   if (existed)
   21233              :                     /* It must have come from a partition.  */
   21234            0 :                     gcc_checking_assert
   21235              :                       (import_entity_module (*slot)->is_partition ());
   21236         2746 :                   *slot = ~base[0]->cluster;
   21237              :                 }
   21238       343124 :               dump (dumper::CLUSTER) && dump ("Cluster namespace %N", decl);
   21239              :             }
   21240              :           size = 1;
   21241              :         }
   21242              :       else
   21243              :         {
   21244       337683 :           size = base[0]->cluster;
   21245              : 
   21246              :           /* Cluster is now used to number entities.  */
   21247       337683 :           base[0]->cluster = ~(~0u >> 1); /* A bad value.  */
   21248              : 
   21249       337683 :           sort_cluster (&table, base, size);
   21250              : 
   21251              :           /* Record the section for consistency checking during stream
   21252              :              out -- we don't want to start writing decls in different
   21253              :              sections.  */
   21254       337683 :           table.section = base[0]->section;
   21255       337683 :           bytes += write_cluster (to, base, size, table, counts, &crc);
   21256       337683 :           table.section = 0;
   21257              :         }
   21258              :     }
   21259              : 
   21260              :   /* depset::cluster - entity number (on entities)
   21261              :      depset::section - cluster number  */
   21262              :   /* We'd better have written as many sections and found as many
   21263              :      namespaces as we predicted.  */
   21264         5630 :   gcc_assert (counts[MSC_sec_hwm] == to->get_section_limit ()
   21265              :               && spaces.length () == counts[MSC_namespaces]);
   21266              : 
   21267              :   /* Write the entities.  None happens if we contain namespaces or
   21268              :      nothing. */
   21269         2815 :   config.num_entities = counts[MSC_entities];
   21270         2815 :   if (counts[MSC_entities])
   21271         2549 :     write_entities (to, sccs, counts[MSC_entities], &crc);
   21272              : 
   21273              :   /* Write the namespaces.  */
   21274         2815 :   if (counts[MSC_namespaces])
   21275          597 :     write_namespaces (to, spaces, counts[MSC_namespaces], &crc);
   21276              : 
   21277              :   /* Write any using-directives.  */
   21278         2815 :   if (counts[MSC_namespaces])
   21279          597 :     counts[MSC_using_directives]
   21280          597 :       = write_using_directives (to, table, spaces, &crc);
   21281              : 
   21282              :   /* Write the bindings themselves.  */
   21283         2815 :   counts[MSC_bindings] = write_bindings (to, sccs, &crc);
   21284              : 
   21285              :   /* Write the unnamed.  */
   21286         2815 :   counts[MSC_pendings] = write_pendings (to, sccs, table, &crc);
   21287              : 
   21288              :   /* Write the import table.  */
   21289         2815 :   if (config.num_imports > 1)
   21290          458 :     write_imports (to, &crc);
   21291              : 
   21292              :   /* Write elided partition table.  */
   21293         2815 :   if (config.num_partitions)
   21294          136 :     write_partitions (to, config.num_partitions, &crc);
   21295              : 
   21296              :   /* Write the line maps.  */
   21297         2815 :   if (config.ordinary_locs)
   21298              :     {
   21299         2716 :       write_ordinary_maps (to, map_info, bool (config.num_partitions), &crc);
   21300         2716 :       write_diagnostic_classification (to, global_dc, &crc);
   21301              :     }
   21302         2815 :   if (config.macro_locs)
   21303          128 :     write_macro_maps (to, map_info, &crc);
   21304              : 
   21305         2815 :   if (is_header ())
   21306              :     {
   21307          909 :       counts[MSC_macros] = write_macros (to, macros, &crc);
   21308          909 :       counts[MSC_inits] = write_inits (to, table, &crc);
   21309          909 :       vec_free (macros);
   21310              :     }
   21311              : 
   21312         2815 :   unsigned clusters = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21313         2815 :   dump () && dump ("Wrote %u clusters, average %u bytes/cluster",
   21314          300 :                    clusters, (bytes + clusters / 2) / (clusters + !clusters));
   21315         2815 :   trees_out::instrument ();
   21316              : 
   21317         2815 :   write_counts (to, counts, &crc);
   21318              : 
   21319         2815 :   spaces.release ();
   21320         2815 :   sccs.release ();
   21321              : 
   21322         2815 :   vec_free (macro_loc_remap);
   21323         2815 :   vec_free (ord_loc_remap);
   21324         2815 :   vec_free (ool);
   21325              : 
   21326              :   // FIXME:QOI:  Have a command line switch to control more detailed
   21327              :   // information (which might leak data you do not want to leak).
   21328              :   // Perhaps (some of) the write_readme contents should also be
   21329              :   // so-controlled.
   21330         2815 :   if (false)
   21331              :     write_env (to);
   21332              : 
   21333         2815 :   return true;
   21334         2844 : }
   21335              : 
   21336              : // Finish module writing after we've emitted all dynamic initializers.
   21337              : 
   21338              : void
   21339         2815 : module_state::write_end (elf_out *to, cpp_reader *reader,
   21340              :                          module_state_config &config, unsigned &crc)
   21341              : {
   21342              :   /* And finish up.  */
   21343         2815 :   write_config (to, config, crc);
   21344              : 
   21345              :   /* Human-readable info.  */
   21346         2815 :   write_readme (to, reader, config.dialect_str);
   21347              : 
   21348         3115 :   dump () && dump ("Wrote %u sections", to->get_section_limit ());
   21349         2815 : }
   21350              : 
   21351              : /* Initial read of a CMI.  Checks config, loads up imports and line
   21352              :    maps.  */
   21353              : 
   21354              : bool
   21355         3061 : module_state::read_initial (cpp_reader *reader)
   21356              : {
   21357         3061 :   module_state_config config;
   21358         3061 :   bool ok = true;
   21359              : 
   21360         3061 :   if (ok && !read_config (config))
   21361              :     ok = false;
   21362              : 
   21363         3048 :   bool have_locs = ok && read_prepare_maps (&config);
   21364              : 
   21365              :   /* Ordinary maps before the imports.  */
   21366         3048 :   if (!(have_locs && config.ordinary_locs))
   21367           71 :     ordinary_locs.first = line_table->highest_location + 1;
   21368         2990 :   else if (!read_ordinary_maps (config.ordinary_locs, config.loc_range_bits))
   21369              :     ok = false;
   21370              : 
   21371              :   /* Allocate the REMAP vector.  */
   21372         3061 :   slurp->alloc_remap (config.num_imports);
   21373              : 
   21374         3061 :   if (ok)
   21375              :     {
   21376              :       /* Read the import table.  Decrement current to stop this CMI
   21377              :          from being evicted during the import. */
   21378         3048 :       slurp->current--;
   21379         3048 :       if (config.num_imports > 1 && !read_imports (reader, line_table))
   21380              :         ok = false;
   21381         3048 :       slurp->current++;
   21382              :     }
   21383              : 
   21384              :   /* Read the elided partition table, if we're the primary partition.  */
   21385         3048 :   if (ok && config.num_partitions && is_module ()
   21386         3075 :       && !read_partitions (config.num_partitions))
   21387              :     ok = false;
   21388              : 
   21389              :   /* Determine the module's number.  */
   21390         3061 :   gcc_checking_assert (mod == MODULE_UNKNOWN);
   21391         3061 :   gcc_checking_assert (this != this_module ());
   21392              : 
   21393         3061 :   {
   21394              :     /* Allocate space in the entities array now -- that array must be
   21395              :        monotonically in step with the modules array.  */
   21396         3061 :     entity_lwm = vec_safe_length (entity_ary);
   21397         3061 :     entity_num = config.num_entities;
   21398         3061 :     gcc_checking_assert (modules->length () == 1
   21399              :                          || modules->last ()->entity_lwm <= entity_lwm);
   21400         3061 :     vec_safe_reserve (entity_ary, config.num_entities);
   21401              : 
   21402         3061 :     binding_slot slot;
   21403         3061 :     slot.u.binding = NULL_TREE;
   21404      1276692 :     for (unsigned count = config.num_entities; count--;)
   21405      1273631 :       entity_ary->quick_push (slot);
   21406              :   }
   21407              : 
   21408              :   /* We'll run out of other resources before we run out of module
   21409              :      indices.  */
   21410         3061 :   mod = modules->length ();
   21411         3061 :   vec_safe_push (modules, this);
   21412              : 
   21413              :   /* We always import and export ourselves. */
   21414         3061 :   bitmap_set_bit (imports, mod);
   21415         3061 :   bitmap_set_bit (exports, mod);
   21416              : 
   21417         3061 :   if (ok)
   21418         3048 :     (*slurp->remap)[0] = mod << 1;
   21419         3593 :   dump () && dump ("Assigning %M module number %u", this, mod);
   21420              : 
   21421              :   /* We should not have been frozen during the importing done by
   21422              :      read_config.  */
   21423         3061 :   gcc_assert (!from ()->is_frozen ());
   21424              : 
   21425              :   /* Macro maps after the imports.  */
   21426         3061 :   if (!(ok && have_locs && config.macro_locs))
   21427         2928 :     macro_locs.first = LINEMAPS_MACRO_LOWEST_LOCATION (line_table);
   21428          133 :   else if (!read_macro_maps (config.macro_locs))
   21429              :     ok = false;
   21430              : 
   21431              :   /* Diagnostic classification streaming needs to come after reading
   21432              :      macro maps to handle _Pragmas in macros.  */
   21433         3048 :   if (ok && have_locs && config.ordinary_locs
   21434         6051 :       && !read_diagnostic_classification (global_dc))
   21435              :     ok = false;
   21436              : 
   21437              :   /* Note whether there's an active initializer.  */
   21438         3061 :   active_init_p = !is_header () && bool (config.active_init);
   21439              : 
   21440         3061 :   gcc_assert (slurp->current == ~0u);
   21441         3061 :   return ok;
   21442              : }
   21443              : 
   21444              : /* Read a preprocessor state.  */
   21445              : 
   21446              : bool
   21447          945 : module_state::read_preprocessor (bool outermost)
   21448              : {
   21449          945 :   gcc_checking_assert (is_header () && slurp
   21450              :                        && slurp->remap_module (0) == mod);
   21451              : 
   21452          945 :   if (loadedness == ML_PREPROCESSOR)
   21453            6 :     return !(from () && from ()->get_error ());
   21454              : 
   21455          939 :   bool ok = true;
   21456              : 
   21457              :   /* Read direct header imports.  */
   21458          939 :   unsigned len = slurp->remap->length ();
   21459          981 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21460              :     {
   21461           42 :       unsigned map = (*slurp->remap)[ix];
   21462           42 :       if (map & 1)
   21463              :         {
   21464           42 :           module_state *import = (*modules)[map >> 1];
   21465           42 :           if (import->is_header ())
   21466              :             {
   21467           42 :               ok = import->read_preprocessor (false);
   21468           42 :               bitmap_ior_into (slurp->headers, import->slurp->headers);
   21469              :             }
   21470              :         }
   21471              :     }
   21472              : 
   21473              :   /* Record as a direct header.  */
   21474          939 :   if (ok)
   21475          939 :     bitmap_set_bit (slurp->headers, mod);
   21476              : 
   21477          939 :   if (ok && !read_macros ())
   21478              :     ok = false;
   21479              : 
   21480          939 :   loadedness = ML_PREPROCESSOR;
   21481          939 :   announce ("macros");
   21482              : 
   21483          939 :   if (flag_preprocess_only)
   21484              :     /* We're done with the string table.  */
   21485           39 :     from ()->release ();
   21486              : 
   21487          939 :   return check_read (outermost, ok);
   21488              : }
   21489              : 
   21490              : /* Read language state.  */
   21491              : 
   21492              : bool
   21493         3084 : module_state::read_language (bool outermost)
   21494              : {
   21495         3084 :   gcc_checking_assert (!lazy_snum);
   21496              : 
   21497         3084 :   if (loadedness == ML_LANGUAGE)
   21498           87 :     return !(slurp && from () && from ()->get_error ());
   21499              : 
   21500         2997 :   gcc_checking_assert (slurp && slurp->current == ~0u
   21501              :                        && slurp->remap_module (0) == mod);
   21502              : 
   21503         2997 :   bool ok = true;
   21504              : 
   21505              :   /* Read direct imports.  */
   21506         2997 :   unsigned len = slurp->remap->length ();
   21507         3400 :   for (unsigned ix = 1; ok && ix != len; ix++)
   21508              :     {
   21509          403 :       unsigned map = (*slurp->remap)[ix];
   21510          403 :       if (map & 1)
   21511              :         {
   21512          400 :           module_state *import = (*modules)[map >> 1];
   21513          400 :           if (!import->read_language (false))
   21514          403 :             ok = false;
   21515              :         }
   21516              :     }
   21517              : 
   21518         2997 :   unsigned counts[MSC_HWM];
   21519              : 
   21520         2997 :   if (ok && !read_counts (counts))
   21521              :     ok = false;
   21522              : 
   21523         2997 :   function_depth++; /* Prevent unexpected GCs.  */
   21524              : 
   21525         2997 :   if (ok && counts[MSC_entities] != entity_num)
   21526              :     ok = false;
   21527         2997 :   if (ok && counts[MSC_entities]
   21528         2768 :       && !read_entities (counts[MSC_entities],
   21529              :                          counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21530              :     ok = false;
   21531              : 
   21532              :   /* Read the namespace hierarchy.  */
   21533         2997 :   if (ok && counts[MSC_namespaces]
   21534         3609 :       && !read_namespaces (counts[MSC_namespaces]))
   21535              :     ok = false;
   21536              : 
   21537              :   /* Read any using-directives.  */
   21538         2997 :   if (ok && counts[MSC_using_directives]
   21539         3159 :       && !read_using_directives (counts[MSC_using_directives]))
   21540              :     ok = false;
   21541              : 
   21542         2997 :   if (ok && !read_bindings (counts[MSC_bindings],
   21543              :                             counts[MSC_sec_lwm], counts[MSC_sec_hwm]))
   21544              :     ok = false;
   21545              : 
   21546              :   /* And unnamed.  */
   21547         2997 :   if (ok && counts[MSC_pendings] && !read_pendings (counts[MSC_pendings]))
   21548              :     ok = false;
   21549              : 
   21550         2997 :   if (ok)
   21551              :     {
   21552         2997 :       slurp->remaining = counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21553         2997 :       available_clusters += counts[MSC_sec_hwm] - counts[MSC_sec_lwm];
   21554              :     }
   21555              : 
   21556         2997 :   if (!flag_module_lazy
   21557         2997 :       || (is_partition ()
   21558          247 :           && module_interface_p ()
   21559          217 :           && !module_partition_p ()))
   21560              :     {
   21561              :       /* Read the sections in forward order, so that dependencies are read
   21562              :          first.  See note about tarjan_connect.  */
   21563          557 :       ggc_collect ();
   21564              : 
   21565          557 :       lazy_snum = ~0u;
   21566              : 
   21567          557 :       unsigned hwm = counts[MSC_sec_hwm];
   21568       176819 :       for (unsigned ix = counts[MSC_sec_lwm]; ok && ix != hwm; ix++)
   21569       176262 :         if (!load_section (ix, NULL))
   21570              :           {
   21571              :             ok = false;
   21572              :             break;
   21573              :           }
   21574          557 :       lazy_snum = 0;
   21575          557 :       post_load_processing ();
   21576              : 
   21577          557 :       ggc_collect ();
   21578              : 
   21579          557 :       if (ok && CHECKING_P)
   21580       680001 :         for (unsigned ix = 0; ix != entity_num; ix++)
   21581       679444 :           gcc_assert (!(*entity_ary)[ix + entity_lwm].is_lazy ());
   21582              :     }
   21583              : 
   21584              :   // If the import is a header-unit, we need to register initializers
   21585              :   // of any static objects it contains (looking at you _Ioinit).
   21586              :   // Notice, the ordering of these initializers will be that of a
   21587              :   // dynamic initializer at this point in the current TU.  (Other
   21588              :   // instances of these objects in other TUs will be initialized as
   21589              :   // part of that TU's global initializers.)
   21590         2997 :   if (ok && counts[MSC_inits] && !read_inits (counts[MSC_inits]))
   21591              :     ok = false;
   21592              : 
   21593         2997 :   function_depth--;
   21594              : 
   21595         3364 :   announce (flag_module_lazy ? "lazy" : "imported");
   21596         2997 :   loadedness = ML_LANGUAGE;
   21597              : 
   21598         2997 :   gcc_assert (slurp->current == ~0u);
   21599              : 
   21600              :   /* We're done with the string table.  */
   21601         2997 :   from ()->release ();
   21602              : 
   21603         2997 :   return check_read (outermost, ok);
   21604              : }
   21605              : 
   21606              : bool
   21607       215723 : module_state::maybe_defrost ()
   21608              : {
   21609       215723 :   bool ok = true;
   21610       215723 :   if (from ()->is_frozen ())
   21611              :     {
   21612            9 :       if (lazy_open >= lazy_limit)
   21613            3 :         freeze_an_elf ();
   21614           18 :       dump () && dump ("Defrosting '%s'", filename);
   21615            9 :       ok = from ()->defrost (maybe_add_cmi_prefix (filename));
   21616            9 :       lazy_open++;
   21617              :     }
   21618              : 
   21619       215723 :   return ok;
   21620              : }
   21621              : 
   21622              : /* Load section SNUM, dealing with laziness.  It doesn't matter if we
   21623              :    have multiple concurrent loads, because we do not use TREE_VISITED
   21624              :    when reading back in.  */
   21625              : 
   21626              : bool
   21627       215723 : module_state::load_section (unsigned snum, binding_slot *mslot)
   21628              : {
   21629       215723 :   if (from ()->get_error ())
   21630              :     return false;
   21631              : 
   21632       215723 :   if (snum >= slurp->current)
   21633            0 :     from ()->set_error (elf::E_BAD_LAZY);
   21634       215723 :   else if (maybe_defrost ())
   21635              :     {
   21636       215723 :       unsigned old_current = slurp->current;
   21637       215723 :       slurp->current = snum;
   21638       215723 :       slurp->lru = 0;  /* Do not swap out.  */
   21639       215723 :       slurp->remaining--;
   21640       215723 :       read_cluster (snum);
   21641       215723 :       slurp->lru = ++lazy_lru;
   21642       215723 :       slurp->current = old_current;
   21643              :     }
   21644              : 
   21645       215723 :   if (mslot && mslot->is_lazy ())
   21646              :     {
   21647              :       /* Oops, the section didn't set this slot.  */
   21648            0 :       from ()->set_error (elf::E_BAD_DATA);
   21649            0 :       *mslot = NULL_TREE;
   21650              :     }
   21651              : 
   21652       215723 :   bool ok = !from ()->get_error ();
   21653       215723 :   if (!ok)
   21654              :     {
   21655            0 :       error_at (loc, "failed to read compiled module cluster %u: %s",
   21656            0 :                 snum, from ()->get_error (filename));
   21657            0 :       note_cmi_name ();
   21658              :     }
   21659              : 
   21660       215723 :   maybe_completed_reading ();
   21661              : 
   21662       215723 :   return ok;
   21663              : }
   21664              : 
   21665              : void
   21666       222704 : module_state::maybe_completed_reading ()
   21667              : {
   21668       222704 :   if (loadedness == ML_LANGUAGE && slurp->current == ~0u && !slurp->remaining)
   21669              :     {
   21670         2484 :       lazy_open--;
   21671              :       /* We no longer need the macros, all tokenizing has been done.  */
   21672         2484 :       slurp->release_macros ();
   21673              : 
   21674         2484 :       from ()->end ();
   21675         2484 :       slurp->close ();
   21676         2484 :       slurped ();
   21677              :     }
   21678       222704 : }
   21679              : 
   21680              : /* After a reading operation, make sure things are still ok.  If not,
   21681              :    emit an error and clean up.  */
   21682              : 
   21683              : bool
   21684         7018 : module_state::check_read (bool outermost, bool ok)
   21685              : {
   21686         7018 :   gcc_checking_assert (!outermost || slurp->current == ~0u);
   21687              : 
   21688         7018 :   if (!ok)
   21689           34 :     from ()->set_error ();
   21690              : 
   21691         7018 :   if (int e = from ()->get_error ())
   21692              :     {
   21693           40 :       auto_diagnostic_group d;
   21694           40 :       error_at (loc, "failed to read compiled module: %s",
   21695           40 :                 from ()->get_error (filename));
   21696           40 :       note_cmi_name ();
   21697              : 
   21698           40 :       if (e == EMFILE
   21699           40 :           || e == ENFILE
   21700              : #if MAPPED_READING
   21701           40 :           || e == ENOMEM
   21702              : #endif
   21703              :           || false)
   21704            0 :         inform (loc, "consider using %<-fno-module-lazy%>,"
   21705              :                 " increasing %<-param-lazy-modules=%u%> value,"
   21706              :                 " or increasing the per-process file descriptor limit",
   21707              :                 param_lazy_modules);
   21708           40 :       else if (e == ENOENT)
   21709           21 :         inform (loc, "imports must be built before being imported");
   21710              : 
   21711           40 :       if (outermost)
   21712           37 :         fatal_error (loc, "returning to the gate for a mechanical issue");
   21713              : 
   21714            3 :       ok = false;
   21715            3 :     }
   21716              : 
   21717         6981 :   maybe_completed_reading ();
   21718              : 
   21719         6981 :   return ok;
   21720              : }
   21721              : 
   21722              : /* Return the IDENTIFIER_NODE naming module IX.  This is the name
   21723              :    including dots.  */
   21724              : 
   21725              : char const *
   21726          408 : module_name (unsigned ix, bool header_ok)
   21727              : {
   21728          408 :   if (modules)
   21729              :     {
   21730          408 :       module_state *imp = (*modules)[ix];
   21731              : 
   21732          408 :       if (ix && !imp->name)
   21733            0 :         imp = imp->parent;
   21734              : 
   21735          408 :       if (header_ok || !imp->is_header ())
   21736          408 :         return imp->get_flatname ();
   21737              :     }
   21738              : 
   21739              :   return NULL;
   21740              : }
   21741              : 
   21742              : /* Return the bitmap describing what modules are imported.  Remember,
   21743              :    we always import ourselves.  */
   21744              : 
   21745              : bitmap
   21746       197490 : get_import_bitmap ()
   21747              : {
   21748       197490 :   return this_module ()->imports;
   21749              : }
   21750              : 
   21751              : /* Get the original decl for an instantiation at TINST, or NULL_TREE
   21752              :    if we're not an instantiation.  */
   21753              : 
   21754              : static tree
   21755       273427 : orig_decl_for_instantiation (tinst_level *tinst)
   21756              : {
   21757       273427 :   if (!tinst || TREE_CODE (tinst->tldcl) == TEMPLATE_FOR_STMT)
   21758              :     return NULL_TREE;
   21759              : 
   21760       105442 :   tree decl = tinst->tldcl;
   21761       105442 :   if (TREE_CODE (decl) == TREE_LIST)
   21762            0 :     decl = TREE_PURPOSE (decl);
   21763       105442 :   if (TYPE_P (decl))
   21764        18756 :     decl = TYPE_NAME (decl);
   21765              :   return decl;
   21766              : }
   21767              : 
   21768              : /* Return the visible imports and path of instantiation for an
   21769              :    instantiation at TINST.  If TINST is nullptr, we're not in an
   21770              :    instantiation, and thus will return the visible imports of the
   21771              :    current TU (and NULL *PATH_MAP_P).   We cache the information on
   21772              :    the tinst level itself.  */
   21773              : 
   21774              : static bitmap
   21775       165430 : path_of_instantiation (tinst_level *tinst, bitmap *path_map_p)
   21776              : {
   21777       165430 :   gcc_checking_assert (modules_p ());
   21778              : 
   21779       165430 :   tree decl = orig_decl_for_instantiation (tinst);
   21780       165430 :   if (!decl)
   21781              :     {
   21782        74361 :       gcc_assert (!tinst || !tinst->next);
   21783              :       /* Not inside an instantiation, just the regular case.  */
   21784        74361 :       *path_map_p = nullptr;
   21785        74361 :       return get_import_bitmap ();
   21786              :     }
   21787              : 
   21788        91069 :   if (!tinst->path)
   21789              :     {
   21790              :       /* Calculate.  */
   21791        27138 :       bitmap visible = path_of_instantiation (tinst->next, path_map_p);
   21792        27138 :       bitmap path_map = *path_map_p;
   21793              : 
   21794        27138 :       if (!path_map)
   21795              :         {
   21796         3649 :           path_map = BITMAP_GGC_ALLOC ();
   21797         3649 :           bitmap_set_bit (path_map, 0);
   21798              :         }
   21799              : 
   21800        27138 :       if (unsigned mod = get_originating_module (decl))
   21801         2368 :         if (!bitmap_bit_p (path_map, mod))
   21802              :           {
   21803              :             /* This is brand new information!  */
   21804          145 :             bitmap new_path = BITMAP_GGC_ALLOC ();
   21805          145 :             bitmap_copy (new_path, path_map);
   21806          145 :             bitmap_set_bit (new_path, mod);
   21807          145 :             path_map = new_path;
   21808              : 
   21809          145 :             bitmap imports = (*modules)[mod]->imports;
   21810          145 :             if (bitmap_intersect_compl_p (imports, visible))
   21811              :               {
   21812              :                 /* IMPORTS contains additional modules to VISIBLE.  */
   21813           23 :                 bitmap new_visible = BITMAP_GGC_ALLOC ();
   21814              : 
   21815           23 :                 bitmap_ior (new_visible, visible, imports);
   21816           23 :                 visible = new_visible;
   21817              :               }
   21818              :           }
   21819              : 
   21820        27138 :       tinst->path = path_map;
   21821        27138 :       tinst->visible = visible;
   21822              :     }
   21823              : 
   21824        91069 :   *path_map_p = tinst->path;
   21825        91069 :   return tinst->visible;
   21826              : }
   21827              : 
   21828              : /* Return the bitmap describing what modules are visible along the
   21829              :    path of instantiation.  If we're not an instantiation, this will be
   21830              :    the visible imports of the TU.  *PATH_MAP_P is filled in with the
   21831              :    modules owning the instantiation path -- we see the module-linkage
   21832              :    entities of those modules.  */
   21833              : 
   21834              : bitmap
   21835     34012324 : visible_instantiation_path (bitmap *path_map_p)
   21836              : {
   21837     34012324 :   if (!modules_p ())
   21838              :     return NULL;
   21839              : 
   21840       138292 :   return path_of_instantiation (current_instantiation (), path_map_p);
   21841              : }
   21842              : 
   21843              : /* Returns the bitmap describing what modules were visible from the
   21844              :    module that the current instantiation originated from.  If we're
   21845              :    not an instantiation, returns NULL.  *MODULE_P is filled in with
   21846              :    the originating module of the definition for this instantiation.  */
   21847              : 
   21848              : bitmap
   21849       107997 : visible_from_instantiation_origination (unsigned *module_p)
   21850              : {
   21851       107997 :   if (!modules_p ())
   21852              :     return NULL;
   21853              : 
   21854       107997 :   tree decl = orig_decl_for_instantiation (current_instantiation ());
   21855       107997 :   if (!decl)
   21856              :     return NULL;
   21857              : 
   21858        14373 :   *module_p = get_originating_module (decl);
   21859        14373 :   return (*modules)[*module_p]->imports;
   21860              : }
   21861              : 
   21862              : /* We've just directly imported IMPORT.  Update our import/export
   21863              :    bitmaps.  IS_EXPORT is true if we're reexporting the OTHER.  */
   21864              : 
   21865              : void
   21866         3151 : module_state::set_import (module_state const *import, bool is_export)
   21867              : {
   21868         3151 :   gcc_checking_assert (this != import);
   21869              : 
   21870              :   /* We see IMPORT's exports (which includes IMPORT).  If IMPORT is
   21871              :      the primary interface or a partition we'll see its imports.  */
   21872         3151 :   bitmap_ior_into (imports, import->is_module () || import->is_partition ()
   21873              :                    ? import->imports : import->exports);
   21874              : 
   21875         3151 :   if (is_export)
   21876              :     /* We'll export OTHER's exports.  */
   21877          486 :     bitmap_ior_into (exports, import->exports);
   21878         3151 : }
   21879              : 
   21880              : /* Return the declaring entity of DECL.  That is the decl determining
   21881              :    how to decorate DECL with module information.  Returns NULL_TREE if
   21882              :    it's the global module.  */
   21883              : 
   21884              : tree
   21885    112813141 : get_originating_module_decl (tree decl)
   21886              : {
   21887              :   /* An enumeration constant.  */
   21888    112813141 :   if (TREE_CODE (decl) == CONST_DECL
   21889        10860 :       && DECL_CONTEXT (decl)
   21890    112824001 :       && (TREE_CODE (DECL_CONTEXT (decl)) == ENUMERAL_TYPE))
   21891         9567 :     decl = TYPE_NAME (DECL_CONTEXT (decl));
   21892    112803574 :   else if (TREE_CODE (decl) == FIELD_DECL
   21893    112761336 :            || TREE_CODE (decl) == USING_DECL
   21894    225538747 :            || CONST_DECL_USING_P (decl))
   21895              :     {
   21896        69694 :       decl = DECL_CONTEXT (decl);
   21897        69694 :       if (TREE_CODE (decl) != FUNCTION_DECL)
   21898        69694 :         decl = TYPE_NAME (decl);
   21899              :     }
   21900              : 
   21901    112813141 :   gcc_checking_assert (TREE_CODE (decl) == TEMPLATE_DECL
   21902              :                        || TREE_CODE (decl) == FUNCTION_DECL
   21903              :                        || TREE_CODE (decl) == TYPE_DECL
   21904              :                        || TREE_CODE (decl) == VAR_DECL
   21905              :                        || TREE_CODE (decl) == CONCEPT_DECL
   21906              :                        || TREE_CODE (decl) == NAMESPACE_DECL);
   21907              : 
   21908    115783057 :   for (;;)
   21909              :     {
   21910              :       /* Uninstantiated template friends are owned by the befriending
   21911              :          class -- not their context.  */
   21912    114298099 :       if (TREE_CODE (decl) == TEMPLATE_DECL
   21913    114298099 :           && DECL_UNINSTANTIATED_TEMPLATE_FRIEND_P (decl))
   21914        14077 :         decl = TYPE_NAME (DECL_CHAIN (decl));
   21915              : 
   21916              :       /* An imported temploid friend is attached to the same module the
   21917              :          befriending class was.  */
   21918    114298099 :       if (imported_temploid_friends)
   21919      4285751 :         if (tree *slot = imported_temploid_friends->get (decl))
   21920          519 :           decl = *slot;
   21921              : 
   21922    114298099 :       int use;
   21923    114298099 :       if (tree ti = node_template_info (decl, use))
   21924              :         {
   21925      1099871 :           decl = TI_TEMPLATE (ti);
   21926      1099871 :           if (TREE_CODE (decl) != TEMPLATE_DECL)
   21927              :             {
   21928              :               /* A friend template specialization.  */
   21929           60 :               gcc_checking_assert (OVL_P (decl) || identifier_p (decl));
   21930           60 :               return global_namespace;
   21931              :             }
   21932              :         }
   21933              :       else
   21934              :         {
   21935    113198228 :           tree ctx = CP_DECL_CONTEXT (decl);
   21936    113198228 :           if (TREE_CODE (ctx) == NAMESPACE_DECL)
   21937              :             break;
   21938              : 
   21939       385147 :           if (TYPE_P (ctx))
   21940              :             {
   21941       345866 :               ctx = TYPE_NAME (ctx);
   21942       345866 :               if (!ctx)
   21943              :                 {
   21944              :                   /* Some kind of internal type.  */
   21945            0 :                   gcc_checking_assert (DECL_ARTIFICIAL (decl));
   21946            0 :                   return global_namespace;
   21947              :                 }
   21948              :             }
   21949       385147 :           decl = ctx;
   21950              :         }
   21951      1484958 :     }
   21952              : 
   21953    112813081 :   return decl;
   21954              : }
   21955              : 
   21956              : /* If DECL is imported, return which module imported it, or 0 for the current
   21957              :    module.  Except that if GLOBAL_M1, return -1 for decls attached to the
   21958              :    global module.  */
   21959              : 
   21960              : int
   21961      1703107 : get_originating_module (tree decl, bool global_m1)
   21962              : {
   21963      1703107 :   tree owner = get_originating_module_decl (decl);
   21964      1703107 :   tree not_tmpl = STRIP_TEMPLATE (owner);
   21965              : 
   21966      1703107 :   if (!DECL_LANG_SPECIFIC (not_tmpl))
   21967       514331 :     return global_m1 ? -1 : 0;
   21968              : 
   21969      2264982 :   if (global_m1 && !DECL_MODULE_ATTACH_P (not_tmpl))
   21970              :     return -1;
   21971              : 
   21972       120962 :   int mod = !DECL_MODULE_IMPORT_P (not_tmpl) ? 0 : get_importing_module (owner);
   21973       120962 :   gcc_checking_assert (!global_m1 || !(*modules)[mod]->is_header ());
   21974              :   return mod;
   21975              : }
   21976              : 
   21977              : /* DECL is imported, return which module imported it.
   21978              :    If FLEXIBLE, return -1 if not found, otherwise checking ICE.  */
   21979              : 
   21980              : unsigned
   21981        82027 : get_importing_module (tree decl, bool flexible)
   21982              : {
   21983        82027 :   unsigned index = import_entity_index (decl, flexible);
   21984        82027 :   if (index == ~(~0u >> 1))
   21985              :     return -1;
   21986        31427 :   module_state *module = import_entity_module (index);
   21987              : 
   21988        31427 :   return module->mod;
   21989              : }
   21990              : 
   21991              : /* Is it permissible to redeclare OLDDECL with NEWDECL.
   21992              : 
   21993              :    If NEWDECL is NULL, assumes that OLDDECL will be redeclared using
   21994              :    the current scope's module and attachment.  */
   21995              : 
   21996              : bool
   21997       227121 : module_may_redeclare (tree olddecl, tree newdecl)
   21998              : {
   21999       227121 :   tree decl = olddecl;
   22000       307595 :   for (;;)
   22001              :     {
   22002       267358 :       tree ctx = CP_DECL_CONTEXT (decl);
   22003       267358 :       if (TREE_CODE (ctx) == NAMESPACE_DECL)
   22004              :         // Found the namespace-scope decl.
   22005              :         break;
   22006        41759 :       if (!CLASS_TYPE_P (ctx))
   22007              :         // We've met a non-class scope.  Such a thing is not
   22008              :         // reopenable, so we must be ok.
   22009              :         return true;
   22010        40237 :       decl = TYPE_NAME (ctx);
   22011        40237 :     }
   22012              : 
   22013       225599 :   int use_tpl = 0;
   22014       225599 :   if (node_template_info (STRIP_TEMPLATE (decl), use_tpl) && use_tpl)
   22015              :     // Specializations of any kind can be redeclared anywhere.
   22016              :     // FIXME: Should we be checking this in more places on the scope chain?
   22017              :     return true;
   22018              : 
   22019       164180 :   module_state *old_mod = get_primary (this_module ());
   22020       164180 :   module_state *new_mod = old_mod;
   22021              : 
   22022       164180 :   tree old_origin = get_originating_module_decl (decl);
   22023       164180 :   tree old_inner = STRIP_TEMPLATE (old_origin);
   22024       164180 :   bool olddecl_attached_p = (DECL_LANG_SPECIFIC (old_inner)
   22025       253242 :                              && DECL_MODULE_ATTACH_P (old_inner));
   22026       253242 :   if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22027              :     {
   22028         8816 :       unsigned index = import_entity_index (old_origin);
   22029         8816 :       old_mod = get_primary (import_entity_module (index));
   22030              :     }
   22031              : 
   22032       164180 :   bool newdecl_attached_p = module_attach_p ();
   22033       164180 :   if (newdecl)
   22034              :     {
   22035        46887 :       tree new_origin = get_originating_module_decl (newdecl);
   22036        46887 :       tree new_inner = STRIP_TEMPLATE (new_origin);
   22037        46887 :       newdecl_attached_p = (DECL_LANG_SPECIFIC (new_inner)
   22038        90095 :                             && DECL_MODULE_ATTACH_P (new_inner));
   22039        90095 :       if (DECL_LANG_SPECIFIC (new_inner) && DECL_MODULE_IMPORT_P (new_inner))
   22040              :         {
   22041          139 :           unsigned index = import_entity_index (new_origin);
   22042          139 :           new_mod = get_primary (import_entity_module (index));
   22043              :         }
   22044              :     }
   22045              : 
   22046              :   /* Module attachment needs to match.  */
   22047       164180 :   if (olddecl_attached_p == newdecl_attached_p)
   22048              :     {
   22049       164036 :       if (!olddecl_attached_p)
   22050              :         /* Both are GM entities, OK.  */
   22051              :         return true;
   22052              : 
   22053         1446 :       if (new_mod == old_mod)
   22054              :         /* Both attached to same named module, OK.  */
   22055              :         return true;
   22056              :     }
   22057              : 
   22058              :   /* Attached to different modules, error.  */
   22059          171 :   decl = newdecl ? newdecl : olddecl;
   22060          171 :   location_t loc = newdecl ? DECL_SOURCE_LOCATION (newdecl) : input_location;
   22061          171 :   if (DECL_IS_UNDECLARED_BUILTIN (olddecl))
   22062              :     {
   22063            3 :       if (newdecl_attached_p)
   22064            3 :         error_at (loc, "declaring %qD in module %qs conflicts with builtin "
   22065              :                   "in global module", decl, new_mod->get_flatname ());
   22066              :       else
   22067            0 :         error_at (loc, "declaration %qD conflicts with builtin", decl);
   22068              :     }
   22069          333 :   else if (DECL_LANG_SPECIFIC (old_inner) && DECL_MODULE_IMPORT_P (old_inner))
   22070              :     {
   22071          153 :       auto_diagnostic_group d;
   22072          153 :       if (newdecl_attached_p)
   22073           75 :         error_at (loc, "redeclaring %qD in module %qs conflicts with import",
   22074              :                   decl, new_mod->get_flatname ());
   22075              :       else
   22076           78 :         error_at (loc, "redeclaring %qD in global module conflicts with import",
   22077              :                   decl);
   22078              : 
   22079          153 :       if (olddecl_attached_p)
   22080          105 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22081              :                 "import declared attached to module %qs",
   22082              :                 old_mod->get_flatname ());
   22083              :       else
   22084           48 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22085              :                 "import declared in global module");
   22086          153 :     }
   22087              :   else
   22088              :     {
   22089           15 :       auto_diagnostic_group d;
   22090           15 :       if (newdecl_attached_p)
   22091           15 :         error_at (loc, "conflicting declaration of %qD in module %qs",
   22092              :                   decl, new_mod->get_flatname ());
   22093              :       else
   22094            0 :         error_at (loc, "conflicting declaration of %qD in global module",
   22095              :                   decl);
   22096              : 
   22097           15 :       if (olddecl_attached_p)
   22098            0 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22099              :                 "previously declared in module %qs",
   22100              :                 old_mod->get_flatname ());
   22101              :       else
   22102           15 :         inform (DECL_SOURCE_LOCATION (olddecl),
   22103              :                 "previously declared in global module");
   22104           15 :     }
   22105              :   return false;
   22106              : }
   22107              : 
   22108              : /* DECL is being created by this TU.  Record it came from here.  We
   22109              :    record module purview, so we can see if partial or explicit
   22110              :    specialization needs to be written out, even though its purviewness
   22111              :    comes from the most general template.  */
   22112              : 
   22113              : void
   22114    978211748 : set_instantiating_module (tree decl)
   22115              : {
   22116    978211748 :   gcc_assert (TREE_CODE (decl) == FUNCTION_DECL
   22117              :               || VAR_P (decl)
   22118              :               || TREE_CODE (decl) == TYPE_DECL
   22119              :               || TREE_CODE (decl) == CONCEPT_DECL
   22120              :               || TREE_CODE (decl) == TEMPLATE_DECL
   22121              :               || TREE_CODE (decl) == CONST_DECL
   22122              :               || (TREE_CODE (decl) == NAMESPACE_DECL
   22123              :                   && DECL_NAMESPACE_ALIAS (decl)));
   22124              : 
   22125    978211748 :   if (!modules_p ())
   22126              :     return;
   22127              : 
   22128      4030386 :   decl = STRIP_TEMPLATE (decl);
   22129              : 
   22130      4030386 :   if (!DECL_LANG_SPECIFIC (decl) && module_purview_p ())
   22131       376401 :     retrofit_lang_decl (decl);
   22132              : 
   22133      4030386 :   if (DECL_LANG_SPECIFIC (decl))
   22134              :     {
   22135      3060635 :       DECL_MODULE_PURVIEW_P (decl) = module_purview_p ();
   22136              :       /* If this was imported, we'll still be in the entity_hash.  */
   22137      3060635 :       DECL_MODULE_IMPORT_P (decl) = false;
   22138              :     }
   22139              : }
   22140              : 
   22141              : /* If DECL is a class member, whose class is not defined in this TU
   22142              :    (it was imported), remember this decl.  */
   22143              : 
   22144              : void
   22145       159069 : set_defining_module (tree decl)
   22146              : {
   22147       159069 :   gcc_checking_assert (!DECL_LANG_SPECIFIC (decl)
   22148              :                        || !DECL_MODULE_IMPORT_P (decl));
   22149              : 
   22150       159069 :   if (module_maybe_has_cmi_p ())
   22151              :     {
   22152              :       /* We need to track all declarations within a module, not just those
   22153              :          in the module purview, because we don't necessarily know yet if
   22154              :          this module will require a CMI while in the global fragment.  */
   22155       110307 :       tree ctx = DECL_CONTEXT (decl);
   22156       110307 :       if (ctx
   22157       110307 :           && (TREE_CODE (ctx) == RECORD_TYPE || TREE_CODE (ctx) == UNION_TYPE)
   22158        23894 :           && DECL_LANG_SPECIFIC (TYPE_NAME (ctx))
   22159       121872 :           && DECL_MODULE_IMPORT_P (TYPE_NAME (ctx)))
   22160              :         {
   22161              :           /* This entity's context is from an import.  We may need to
   22162              :              record this entity to make sure we emit it in the CMI.
   22163              :              Template specializations are in the template hash tables,
   22164              :              so we don't need to record them here as well.  */
   22165           39 :           int use_tpl = -1;
   22166           39 :           tree ti = node_template_info (decl, use_tpl);
   22167           39 :           if (use_tpl <= 0)
   22168              :             {
   22169           39 :               if (ti)
   22170              :                 {
   22171           21 :                   gcc_checking_assert (!use_tpl);
   22172              :                   /* Get to the TEMPLATE_DECL.  */
   22173           21 :                   decl = TI_TEMPLATE (ti);
   22174              :                 }
   22175              : 
   22176              :               /* Record it on the class_members list.  */
   22177           39 :               vec_safe_push (class_members, decl);
   22178              :             }
   22179              :         }
   22180              :     }
   22181       159069 : }
   22182              : 
   22183              : /* Also remember DECL if it's a newly declared class template partial
   22184              :    specialization, because these are not necessarily added to the
   22185              :    instantiation tables.  */
   22186              : 
   22187              : void
   22188      8395692 : set_defining_module_for_partial_spec (tree decl)
   22189              : {
   22190      8395692 :   if (module_maybe_has_cmi_p ()
   22191        27555 :       && DECL_IMPLICIT_TYPEDEF_P (decl)
   22192      8418864 :       && CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (decl)))
   22193        23172 :     vec_safe_push (partial_specializations, decl);
   22194      8395692 : }
   22195              : 
   22196              : /* Record that DECL is declared in this TU, and note attachment and
   22197              :    exporting for namespace-scope entities.  FRIEND_P is true if
   22198              :    this is a friend declaration.  */
   22199              : 
   22200              : void
   22201    315359011 : set_originating_module (tree decl, bool friend_p ATTRIBUTE_UNUSED)
   22202              : {
   22203    315359011 :   set_instantiating_module (decl);
   22204              : 
   22205              :   /* DECL_CONTEXT may not be set yet when we're called for
   22206              :      non-namespace-scope entities.  */
   22207    315359011 :   if (!DECL_CONTEXT (decl) || !DECL_NAMESPACE_SCOPE_P (decl))
   22208              :     return;
   22209              : 
   22210    117253435 :   gcc_checking_assert (friend_p || decl == get_originating_module_decl (decl));
   22211              : 
   22212    117253435 :   if (module_attach_p ())
   22213              :     {
   22214         4342 :       retrofit_lang_decl (decl);
   22215         4342 :       DECL_MODULE_ATTACH_P (decl) = true;
   22216              :     }
   22217              : 
   22218              :   /* It is ill-formed to export a declaration with internal linkage.  However,
   22219              :      at the point this function is called we don't yet always know whether this
   22220              :      declaration has internal linkage; instead we defer this check for callers
   22221              :      to do once visibility has been determined.  */
   22222    117253435 :   if (module_exporting_p ())
   22223       159195 :     DECL_MODULE_EXPORT_P (decl) = true;
   22224              : }
   22225              : 
   22226              : /* True if it's OK to redefine DECL under P1811 because it is attached to the
   22227              :    global module and its definition was imported; [basic.def.odr]/12 "there can
   22228              :    be more than one definition...provided that each definition appears in a
   22229              :    different translation unit"
   22230              : 
   22231              :    This does not depend on whether DECL was already defined, so that for
   22232              :    instance it can affect redefinition of default arguments.  This should only
   22233              :    be used when we notice a redefinition.  */
   22234              : 
   22235              : bool
   22236    351702622 : redefinable_import_p (tree decl)
   22237              : {
   22238    351702622 :   if (!modules_p () || named_module_attach_p ())
   22239              :     return false;
   22240              : 
   22241      1070095 :   decl = STRIP_TEMPLATE (decl);
   22242              : 
   22243      1070095 :   if (TREE_CODE (decl) == TYPE_DECL
   22244        10614 :       && !DECL_IMPLICIT_TYPEDEF_P (decl)
   22245      1072660 :       && !TYPE_DECL_FOR_LINKAGE_PURPOSES_P (decl))
   22246              :     /* Typedefs are always redefinable, use normal handling.  */
   22247              :     return false;
   22248              : 
   22249      1067545 :   return (DECL_LANG_SPECIFIC (decl)
   22250      1065948 :           && DECL_MODULE_ENTITY_P (decl)
   22251              :           /* Use this instead of DECL_MODULE_IMPORT_P so we don't get confused
   22252              :              by partitions or set_instantiating_module.  */
   22253              :           /* FIXME this also means we allow multiple redefinitions.  */
   22254      1137744 :           && get_importing_module (decl, /*flexible*/true) > 0);
   22255              : }
   22256              : 
   22257              : /* Checks whether DECL within a module unit has valid linkage for its kind.
   22258              :    Must be called after visibility for DECL has been finalised.  */
   22259              : 
   22260              : void
   22261    396062889 : check_module_decl_linkage (tree decl)
   22262              : {
   22263    396062889 :   if (!module_has_cmi_p ())
   22264              :     return;
   22265              : 
   22266              :   /* A header unit shall not contain a definition of a non-inline function
   22267              :      or variable (not template) whose name has external linkage.  */
   22268       536632 :   if (header_module_p ()
   22269       484373 :       && !processing_template_decl
   22270       160000 :       && ((TREE_CODE (decl) == FUNCTION_DECL
   22271        94537 :            && !DECL_DECLARED_INLINE_P (decl))
   22272       123973 :           || (TREE_CODE (decl) == VAR_DECL
   22273        37538 :               && !DECL_INLINE_VAR_P (decl)))
   22274        45287 :       && decl_defined_p (decl)
   22275         4476 :       && !(DECL_LANG_SPECIFIC (decl)
   22276         4476 :            && DECL_TEMPLATE_INSTANTIATION (decl))
   22277       541108 :       && DECL_EXTERNAL_LINKAGE_P (decl))
   22278           60 :     error_at (DECL_SOURCE_LOCATION (decl),
   22279              :               "external linkage definition of %qD in header module must "
   22280              :               "be declared %<inline%>", decl);
   22281              : 
   22282              :   /* An internal-linkage declaration cannot be generally be exported.
   22283              :      But it's OK to export any declaration from a header unit, including
   22284              :      internal linkage declarations.  */
   22285       536632 :   if (!header_module_p () && DECL_MODULE_EXPORT_P (decl))
   22286              :     {
   22287              :       /* Let's additionally treat any exported declaration within an
   22288              :          internal namespace as exporting a declaration with internal
   22289              :          linkage, as this would also implicitly export the internal
   22290              :          linkage namespace.  */
   22291         2035 :       if (decl_anon_ns_mem_p (decl))
   22292              :         {
   22293           50 :           error_at (DECL_SOURCE_LOCATION (decl),
   22294              :                     "exporting declaration %qD declared in unnamed namespace",
   22295              :                     decl);
   22296           50 :           DECL_MODULE_EXPORT_P (decl) = false;
   22297              :         }
   22298         1985 :       else if (decl_linkage (decl) == lk_internal)
   22299              :         {
   22300           17 :           error_at (DECL_SOURCE_LOCATION (decl),
   22301              :                     "exporting declaration %qD with internal linkage", decl);
   22302           17 :           DECL_MODULE_EXPORT_P (decl) = false;
   22303              :         }
   22304              :     }
   22305              : }
   22306              : 
   22307              : /* Given a scope CTX, find the scope we want to attach the key to,
   22308              :    or NULL if no key scope is required.  */
   22309              : 
   22310              : static tree
   22311         3170 : adjust_key_scope (tree ctx)
   22312              : {
   22313              :   /* For members, key it to the containing type to handle deduplication
   22314              :      correctly.  For fields, this is necessary as FIELD_DECLs have no
   22315              :      dep and so would only be streamed after the lambda type, defeating
   22316              :      our ability to merge them.
   22317              : 
   22318              :      Other class-scope key decls might depend on the type of the lambda
   22319              :      but be within the same cluster; we need to ensure that we never
   22320              :      first see the key decl while streaming the lambda type as merging
   22321              :      would then fail when comparing the partially-streamed lambda type
   22322              :      of the key decl with the existing (PR c++/122310).
   22323              : 
   22324              :      Perhaps sort_cluster can be adjusted to handle this better, but
   22325              :      this is a simple workaround (and might down on the number of
   22326              :      entries in keyed_table as a bonus).  */
   22327         5331 :   while (!DECL_NAMESPACE_SCOPE_P (ctx))
   22328         2161 :     if (DECL_CLASS_SCOPE_P (ctx))
   22329         2047 :       ctx = TYPE_NAME (DECL_CONTEXT (ctx));
   22330              :     else
   22331          114 :       ctx = DECL_CONTEXT (ctx);
   22332              : 
   22333         3170 :   return ctx;
   22334              : }
   22335              : 
   22336              : /* DECL is keyed to CTX for odr purposes.  */
   22337              : 
   22338              : void
   22339      1926758 : maybe_key_decl (tree ctx, tree decl)
   22340              : {
   22341      1926758 :   if (!modules_p ())
   22342              :     return;
   22343              : 
   22344              :   /* We only need to deal here with decls attached to var, field,
   22345              :      parm, type, function, or concept decls.  */
   22346        13150 :   if (TREE_CODE (ctx) != VAR_DECL
   22347        13150 :       && TREE_CODE (ctx) != FIELD_DECL
   22348              :       && TREE_CODE (ctx) != PARM_DECL
   22349              :       && TREE_CODE (ctx) != TYPE_DECL
   22350              :       && TREE_CODE (ctx) != FUNCTION_DECL
   22351              :       && TREE_CODE (ctx) != CONCEPT_DECL)
   22352              :     return;
   22353              : 
   22354        26297 :   gcc_checking_assert (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22355              :                        || TREE_CODE (ctx) == FUNCTION_DECL);
   22356              : 
   22357              :   /* We don't need to use the keyed map for functions with definitions,
   22358              :      as we can instead use the MK_local_type handling for streaming.  */
   22359        13150 :   if (TREE_CODE (ctx) == FUNCTION_DECL
   22360        13150 :       && (has_definition (ctx)
   22361              :           /* If we won't be streaming this definition there's also no
   22362              :              need to record the key, as it will not be useful for merging
   22363              :              (this function is non-inline and so a matching declaration
   22364              :              will always be an ODR violation anyway).  */
   22365          120 :           || !module_maybe_has_cmi_p ()))
   22366              :     return;
   22367              : 
   22368          714 :   ctx = adjust_key_scope (ctx);
   22369              : 
   22370          714 :   if (!keyed_table)
   22371          166 :     keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22372              : 
   22373          714 :   auto &vec = keyed_table->get_or_insert (ctx);
   22374          714 :   if (!vec.length ())
   22375              :     {
   22376          657 :       retrofit_lang_decl (ctx);
   22377          657 :       DECL_MODULE_KEYED_DECLS_P (ctx) = true;
   22378              :     }
   22379          714 :   if (CHECKING_P)
   22380          891 :     for (tree t : vec)
   22381           63 :       gcc_checking_assert (t != decl);
   22382              : 
   22383          714 :   vec.safe_push (decl);
   22384              : }
   22385              : 
   22386              : /* Find the scope that the local type or lambda DECL is keyed to, if any.  */
   22387              : 
   22388              : static tree
   22389         2492 : get_keyed_decl_scope (tree decl)
   22390              : {
   22391         2492 :   gcc_checking_assert (DECL_IMPLICIT_TYPEDEF_P (STRIP_TEMPLATE (decl)));
   22392              : 
   22393         7476 :   tree scope = (LAMBDA_TYPE_P (TREE_TYPE (decl))
   22394         4756 :                 ? LAMBDA_TYPE_EXTRA_SCOPE (TREE_TYPE (decl))
   22395         2492 :                 : CP_DECL_CONTEXT (decl));
   22396         2492 :   if (!scope)
   22397              :     return NULL_TREE;
   22398              : 
   22399         2456 :   gcc_checking_assert (TREE_CODE (scope) == VAR_DECL
   22400              :                        || TREE_CODE (scope) == FIELD_DECL
   22401              :                        || TREE_CODE (scope) == PARM_DECL
   22402              :                        || TREE_CODE (scope) == TYPE_DECL
   22403              :                        || (TREE_CODE (scope) == FUNCTION_DECL
   22404              :                            && !has_definition (scope))
   22405              :                        || TREE_CODE (scope) == CONCEPT_DECL);
   22406              : 
   22407         2456 :   scope = adjust_key_scope (scope);
   22408              : 
   22409         4912 :   gcc_checking_assert (scope
   22410              :                        && DECL_LANG_SPECIFIC (scope)
   22411              :                        && DECL_MODULE_KEYED_DECLS_P (scope));
   22412              :   return scope;
   22413              : }
   22414              : 
   22415              : /* DECL is an instantiated friend that should be attached to the same
   22416              :    module that ORIG is.  */
   22417              : 
   22418              : void
   22419      2725943 : propagate_defining_module (tree decl, tree orig)
   22420              : {
   22421      2725943 :   if (!modules_p ())
   22422              :     return;
   22423              : 
   22424         8171 :   tree not_tmpl = STRIP_TEMPLATE (orig);
   22425        16285 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_ATTACH_P (not_tmpl))
   22426              :     {
   22427          126 :       tree inner = STRIP_TEMPLATE (decl);
   22428          126 :       retrofit_lang_decl (inner);
   22429          126 :       DECL_MODULE_ATTACH_P (inner) = true;
   22430              :     }
   22431              : 
   22432        16285 :   if (DECL_LANG_SPECIFIC (not_tmpl) && DECL_MODULE_IMPORT_P (not_tmpl))
   22433              :     {
   22434          277 :       bool exists = imported_temploid_friends->put (decl, orig);
   22435              : 
   22436              :       /* We should only be called if lookup for an existing decl
   22437              :          failed, in which case there shouldn't already be an entry
   22438              :          in the map.  */
   22439          277 :       gcc_assert (!exists);
   22440              :     }
   22441              : }
   22442              : 
   22443              : /* NEWDECL matched with OLDDECL, transfer defining module information
   22444              :    onto OLDDECL.  We've already validated attachment matches.  */
   22445              : 
   22446              : void
   22447     20313467 : transfer_defining_module (tree olddecl, tree newdecl)
   22448              : {
   22449     20313467 :   if (!modules_p ())
   22450              :     return;
   22451              : 
   22452        74869 :   tree old_inner = STRIP_TEMPLATE (olddecl);
   22453        74869 :   tree new_inner = STRIP_TEMPLATE (newdecl);
   22454              : 
   22455        74869 :   if (DECL_LANG_SPECIFIC (new_inner))
   22456              :     {
   22457        73830 :       retrofit_lang_decl (old_inner);
   22458        73830 :       if (DECL_MODULE_PURVIEW_P (new_inner))
   22459        22937 :         DECL_MODULE_PURVIEW_P (old_inner) = true;
   22460        73830 :       if (!DECL_MODULE_IMPORT_P (new_inner))
   22461        73830 :         DECL_MODULE_IMPORT_P (old_inner) = false;
   22462              :     }
   22463              : 
   22464        74869 :   if (tree *p = imported_temploid_friends->get (newdecl))
   22465              :     {
   22466           58 :       tree orig = *p;
   22467           58 :       tree &slot = imported_temploid_friends->get_or_insert (olddecl);
   22468           58 :       if (!slot)
   22469           41 :         slot = orig;
   22470           17 :       else if (slot != orig)
   22471              :         /* This can happen when multiple classes declare the same
   22472              :            friend function (e.g. g++.dg/modules/tpl-friend-4);
   22473              :            make sure we at least attach to the same module.  */
   22474            3 :         gcc_checking_assert (get_originating_module (slot)
   22475              :                              == get_originating_module (orig));
   22476              :     }
   22477              : }
   22478              : 
   22479              : /* DECL is being freed, clear data we don't need anymore.  */
   22480              : 
   22481              : void
   22482        53017 : remove_defining_module (tree decl)
   22483              : {
   22484        53017 :   if (!modules_p ())
   22485              :     return;
   22486              : 
   22487        53017 :   if (imported_temploid_friends)
   22488        53017 :     imported_temploid_friends->remove (decl);
   22489              : }
   22490              : 
   22491              : /* Create the flat name string.  It is simplest to have it handy.  */
   22492              : 
   22493              : void
   22494         6507 : module_state::set_flatname ()
   22495              : {
   22496         6507 :   gcc_checking_assert (!flatname);
   22497         6507 :   if (parent)
   22498              :     {
   22499          704 :       auto_vec<tree,5> ids;
   22500          704 :       size_t len = 0;
   22501          704 :       char const *primary = NULL;
   22502          704 :       size_t pfx_len = 0;
   22503              : 
   22504          704 :       for (module_state *probe = this;
   22505         1711 :            probe;
   22506         1007 :            probe = probe->parent)
   22507         1543 :         if (is_partition () && !probe->is_partition ())
   22508              :           {
   22509          536 :             primary = probe->get_flatname ();
   22510          536 :             pfx_len = strlen (primary);
   22511          536 :             break;
   22512              :           }
   22513              :         else
   22514              :           {
   22515         1007 :             ids.safe_push (probe->name);
   22516         1007 :             len += IDENTIFIER_LENGTH (probe->name) + 1;
   22517              :           }
   22518              : 
   22519          704 :       char *flat = XNEWVEC (char, pfx_len + len + is_partition ());
   22520          704 :       flatname = flat;
   22521              : 
   22522          704 :       if (primary)
   22523              :         {
   22524          536 :           memcpy (flat, primary, pfx_len);
   22525          536 :           flat += pfx_len;
   22526          536 :           *flat++ = ':';
   22527              :         }
   22528              : 
   22529         1711 :       for (unsigned len = 0; ids.length ();)
   22530              :         {
   22531         1007 :           if (len)
   22532          303 :             flat[len++] = '.';
   22533         1007 :           tree elt = ids.pop ();
   22534         1007 :           unsigned l = IDENTIFIER_LENGTH (elt);
   22535         1007 :           memcpy (flat + len, IDENTIFIER_POINTER (elt), l + 1);
   22536         1007 :           len += l;
   22537              :         }
   22538          704 :     }
   22539         5803 :   else if (is_header ())
   22540         1931 :     flatname = TREE_STRING_POINTER (name);
   22541              :   else
   22542         3872 :     flatname = IDENTIFIER_POINTER (name);
   22543         6507 : }
   22544              : 
   22545              : /* Open the GCM file and prepare to read.  Return whether that was
   22546              :    successful.  */
   22547              : 
   22548              : bool
   22549         3129 : module_state::open_slurp (cpp_reader *reader)
   22550              : {
   22551         3129 :   if (slurp)
   22552              :     return true;
   22553              : 
   22554         3085 :   if (lazy_open >= lazy_limit)
   22555            9 :     freeze_an_elf ();
   22556              : 
   22557         3085 :   int fd = -1;
   22558         3085 :   int e = ENOENT;
   22559         3085 :   if (filename)
   22560              :     {
   22561         3085 :       const char *file = maybe_add_cmi_prefix (filename);
   22562         3617 :       dump () && dump ("CMI is %s", file);
   22563         3085 :       if (note_module_cmi_yes || inform_cmi_p)
   22564           12 :         inform (loc, "reading CMI %qs", file);
   22565              :       /* Add the CMI file to the dependency tracking. */
   22566         3085 :       if (cpp_get_deps (reader))
   22567           15 :         deps_add_dep (cpp_get_deps (reader), file);
   22568         3085 :       fd = open (file, O_RDONLY | O_CLOEXEC | O_BINARY);
   22569         3085 :       e = errno;
   22570              :     }
   22571              : 
   22572         3085 :   gcc_checking_assert (!slurp);
   22573         6146 :   slurp = new slurping (new elf_in (fd, e));
   22574              : 
   22575         3085 :   bool ok = from ()->begin (loc);
   22576         3085 :   if (ok)
   22577              :     {
   22578         3061 :       lazy_open++;
   22579         3061 :       slurp->lru = ++lazy_lru;
   22580              :     }
   22581              :   return ok;
   22582              : }
   22583              : 
   22584              : /* Return whether importing this GCM would work without an error in
   22585              :    read_config.  */
   22586              : 
   22587              : bool
   22588           50 : module_state::check_importable (cpp_reader *reader)
   22589              : {
   22590           50 :   if (loadedness > ML_CONFIG)
   22591              :     return true;
   22592           47 :   if (!open_slurp (reader))
   22593              :     return false;
   22594           44 :   module_state_config config;
   22595           44 :   return read_config (config, /*complain*/false);
   22596              : }
   22597              : 
   22598              : /* Read the CMI file for a module.  */
   22599              : 
   22600              : bool
   22601         3082 : module_state::do_import (cpp_reader *reader, bool outermost)
   22602              : {
   22603         3082 :   gcc_assert (global_namespace == current_scope () && loadedness == ML_NONE);
   22604              : 
   22605              :   /* If this TU is a partition of the module we're importing,
   22606              :      that module is the primary module interface.  */
   22607         3082 :   if (this_module ()->is_partition ()
   22608         3139 :       && this == get_primary (this_module ()))
   22609            9 :     module_p = true;
   22610              : 
   22611         3082 :   loc = linemap_module_loc (line_table, loc, get_flatname ());
   22612              : 
   22613         3082 :   bool ok = open_slurp (reader);
   22614         3082 :   if (!from ()->get_error ())
   22615              :     {
   22616         3061 :       announce ("importing");
   22617         3061 :       loadedness = ML_CONFIG;
   22618         3061 :       ok = read_initial (reader);
   22619              :     }
   22620              : 
   22621         3082 :   gcc_assert (slurp->current == ~0u);
   22622              : 
   22623         3082 :   return check_read (outermost, ok);
   22624              : }
   22625              : 
   22626              : /* Attempt to increase the file descriptor limit.  */
   22627              : 
   22628              : static bool
   22629         5042 : try_increase_lazy (unsigned want)
   22630              : {
   22631         5042 :   gcc_checking_assert (lazy_open >= lazy_limit);
   22632              : 
   22633              :   /* If we're increasing, saturate at hard limit.  */
   22634         5042 :   if (want > lazy_hard_limit && lazy_limit < lazy_hard_limit)
   22635         5042 :     want = lazy_hard_limit;
   22636              : 
   22637              : #if HAVE_SETRLIMIT
   22638         5042 :   if ((!lazy_limit || !param_lazy_modules)
   22639         5030 :       && lazy_hard_limit
   22640         5030 :       && want <= lazy_hard_limit)
   22641              :     {
   22642         5030 :       struct rlimit rlimit;
   22643         5030 :       rlimit.rlim_cur = want + LAZY_HEADROOM;
   22644         5030 :       rlimit.rlim_max = lazy_hard_limit + LAZY_HEADROOM;
   22645         5030 :       if (!setrlimit (RLIMIT_NOFILE, &rlimit))
   22646         5030 :         lazy_limit = want;
   22647              :     }
   22648              : #endif
   22649              : 
   22650         5042 :   return lazy_open < lazy_limit;
   22651              : }
   22652              : 
   22653              : /* Pick a victim module to freeze its reader.  */
   22654              : 
   22655              : void
   22656           12 : module_state::freeze_an_elf ()
   22657              : {
   22658           12 :   if (try_increase_lazy (lazy_open * 2))
   22659              :     return;
   22660              : 
   22661           12 :   module_state *victim = NULL;
   22662           42 :   for (unsigned ix = modules->length (); ix--;)
   22663              :     {
   22664           30 :       module_state *candidate = (*modules)[ix];
   22665           30 :       if (candidate && candidate->slurp && candidate->slurp->lru
   22666           18 :           && candidate->from ()->is_freezable ()
   22667           39 :           && (!victim || victim->slurp->lru > candidate->slurp->lru))
   22668           30 :         victim = candidate;
   22669              :     }
   22670              : 
   22671           12 :   if (victim)
   22672              :     {
   22673           18 :       dump () && dump ("Freezing '%s'", victim->filename);
   22674            9 :       if (victim->slurp->macro_defs.size)
   22675              :         /* Save the macro definitions to a buffer.  */
   22676            0 :         victim->from ()->preserve (victim->slurp->macro_defs);
   22677            9 :       if (victim->slurp->macro_tbl.size)
   22678              :         /* Save the macro definitions to a buffer.  */
   22679            0 :         victim->from ()->preserve (victim->slurp->macro_tbl);
   22680            9 :       victim->from ()->freeze ();
   22681            9 :       lazy_open--;
   22682              :     }
   22683              :   else
   22684            3 :     dump () && dump ("No module available for freezing");
   22685              : }
   22686              : 
   22687              : /* Load the lazy slot *MSLOT, INDEX'th slot of the module.  */
   22688              : 
   22689              : bool
   22690        35480 : module_state::lazy_load (unsigned index, binding_slot *mslot)
   22691              : {
   22692        35480 :   unsigned n = dump.push (this);
   22693              : 
   22694        35480 :   gcc_checking_assert (function_depth);
   22695              : 
   22696        35480 :   unsigned cookie = mslot->get_lazy ();
   22697        35480 :   unsigned snum = cookie >> 2;
   22698        35888 :   dump () && dump ("Loading entity %M[%u] section:%u", this, index, snum);
   22699              : 
   22700        35480 :   bool ok = load_section (snum, mslot);
   22701              : 
   22702        35480 :   dump.pop (n);
   22703              : 
   22704        35480 :   return ok;
   22705              : }
   22706              : 
   22707              : /* Load MOD's binding for NS::ID into *MSLOT.  *MSLOT contains the
   22708              :    lazy cookie.  OUTER is true if this is the outermost lazy, (used
   22709              :    for diagnostics).  */
   22710              : 
   22711              : void
   22712         3981 : lazy_load_binding (unsigned mod, tree ns, tree id, binding_slot *mslot)
   22713              : {
   22714         3981 :   int count = errorcount + warningcount;
   22715              : 
   22716         3981 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22717              : 
   22718              :   /* Make sure lazy loading from a template context behaves as if
   22719              :      from a non-template context.  */
   22720         3981 :   processing_template_decl_sentinel ptds;
   22721              : 
   22722              :   /* Stop GC happening, even in outermost loads (because our caller
   22723              :      could well be building up a lookup set).  */
   22724         3981 :   function_depth++;
   22725              : 
   22726         3981 :   gcc_checking_assert (mod);
   22727         3981 :   module_state *module = (*modules)[mod];
   22728         3981 :   unsigned n = dump.push (module);
   22729              : 
   22730         3981 :   unsigned snum = mslot->get_lazy ();
   22731         4336 :   dump () && dump ("Lazily binding %P@%N section:%u", ns, id,
   22732              :                    module->name, snum);
   22733              : 
   22734         3981 :   bool ok = !recursive_lazy (snum);
   22735         3981 :   if (ok)
   22736              :     {
   22737         3981 :       ok = module->load_section (snum, mslot);
   22738         3981 :       lazy_snum = 0;
   22739         3981 :       post_load_processing ();
   22740              :     }
   22741              : 
   22742         3981 :   dump.pop (n);
   22743              : 
   22744         3981 :   function_depth--;
   22745              : 
   22746         3981 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22747              : 
   22748         3981 :   if (!ok)
   22749            0 :     fatal_error (input_location,
   22750            0 :                  module->is_header ()
   22751              :                  ? G_("failed to load binding %<%E%s%E%>")
   22752              :                  : G_("failed to load binding %<%E%s%E@%s%>"),
   22753            0 :                  ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22754              :                  module->get_flatname ());
   22755              : 
   22756         3981 :   if (count != errorcount + warningcount)
   22757           27 :     inform (input_location,
   22758           27 :             module->is_header ()
   22759              :             ? G_("during load of binding %<%E%s%E%>")
   22760              :             : G_("during load of binding %<%E%s%E@%s%>"),
   22761           27 :             ns, &"::"[ns == global_namespace ? 2 : 0], id,
   22762              :             module->get_flatname ());
   22763         3981 : }
   22764              : 
   22765              : /* Load any pending entities keyed to NS and NAME.
   22766              :    Used to find pending types if we don't yet have a decl built.  */
   22767              : 
   22768              : void
   22769     40525942 : lazy_load_pendings (tree ns, tree name)
   22770              : {
   22771              :   /* Make sure lazy loading from a template context behaves as if
   22772              :      from a non-template context.  */
   22773     40525942 :   processing_template_decl_sentinel ptds;
   22774              : 
   22775     40525942 :   pending_key key;
   22776     40525942 :   key.ns = ns;
   22777     40525942 :   key.id = name;
   22778              : 
   22779     40525942 :   auto *pending_vec = pending_table ? pending_table->get (key) : nullptr;
   22780     40520908 :   if (!pending_vec)
   22781     40518035 :     return;
   22782              : 
   22783         7907 :   int count = errorcount + warningcount;
   22784              : 
   22785         7907 :   bool timer_running = timevar_cond_start (TV_MODULE_IMPORT);
   22786         7907 :   bool ok = !recursive_lazy ();
   22787         7907 :   if (ok)
   22788              :     {
   22789         7907 :       function_depth++; /* Prevent GC */
   22790         7907 :       unsigned n = dump.push (NULL);
   22791         8411 :       dump () && dump ("Reading %u pending entities keyed to %P",
   22792              :                        pending_vec->length (), key.ns, key.id);
   22793        88146 :       for (unsigned ix = pending_vec->length (); ix--;)
   22794              :         {
   22795        72332 :           unsigned index = (*pending_vec)[ix];
   22796        72332 :           binding_slot *slot = &(*entity_ary)[index];
   22797              : 
   22798        72332 :           if (slot->is_lazy ())
   22799              :             {
   22800         3288 :               module_state *import = import_entity_module (index);
   22801         3288 :               if (!import->lazy_load (index - import->entity_lwm, slot))
   22802        72332 :                 ok = false;
   22803              :             }
   22804        69044 :           else if (dump ())
   22805              :             {
   22806          363 :               module_state *import = import_entity_module (index);
   22807        72332 :               dump () && dump ("Entity %M[%u] already loaded",
   22808          363 :                                import, index - import->entity_lwm);
   22809              :             }
   22810              :         }
   22811              : 
   22812         7907 :       pending_table->remove (key);
   22813         7907 :       dump.pop (n);
   22814         7907 :       lazy_snum = 0;
   22815         7907 :       post_load_processing ();
   22816         7907 :       function_depth--;
   22817              :     }
   22818              : 
   22819         7907 :   timevar_cond_stop (TV_MODULE_IMPORT, timer_running);
   22820              : 
   22821         7907 :   if (!ok)
   22822            0 :     fatal_error (input_location, "failed to load pendings for %<%E%s%E%>",
   22823            0 :                  key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22824              : 
   22825         7907 :   if (count != errorcount + warningcount)
   22826            0 :     inform (input_location, "during load of pendings for %<%E%s%E%>",
   22827            0 :             key.ns, &"::"[key.ns == global_namespace ? 2 : 0], key.id);
   22828     40525942 : }
   22829              : 
   22830              : /* Load any pending entities keyed to the top-key of DECL.  */
   22831              : 
   22832              : void
   22833     40426536 : lazy_load_pendings (tree decl)
   22834              : {
   22835     40426536 :   tree key_decl;
   22836     40426536 :   tree ns = find_pending_key (decl, &key_decl);
   22837     40426536 :   return lazy_load_pendings (ns, DECL_NAME (key_decl));
   22838              : }
   22839              : 
   22840              : static void
   22841         2800 : direct_import (module_state *import, cpp_reader *reader)
   22842              : {
   22843         2800 :   timevar_start (TV_MODULE_IMPORT);
   22844         2800 :   unsigned n = dump.push (import);
   22845              : 
   22846         2800 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22847         2800 :   if (import->loadedness == ML_NONE)
   22848         1854 :     if (!import->do_import (reader, true))
   22849            0 :       gcc_unreachable ();
   22850              : 
   22851         2763 :   this_module ()->set_import (import, import->exported_p);
   22852              : 
   22853         2763 :   if (import->loadedness < ML_LANGUAGE)
   22854              :     {
   22855         2684 :       if (!keyed_table)
   22856         2360 :         keyed_table = new keyed_map_t (EXPERIMENT (1, 400));
   22857         2684 :       import->read_language (true);
   22858              :     }
   22859              : 
   22860         2763 :   dump.pop (n);
   22861         2763 :   timevar_stop (TV_MODULE_IMPORT);
   22862         2763 : }
   22863              : 
   22864              : /* Import module IMPORT.  */
   22865              : 
   22866              : void
   22867         2564 : import_module (module_state *import, location_t from_loc, bool exporting_p,
   22868              :                tree, cpp_reader *reader)
   22869              : {
   22870              :   /* A non-partition implementation unit has no name.  */
   22871         2564 :   if (!this_module ()->name && this_module ()->parent == import)
   22872              :     {
   22873            3 :       auto_diagnostic_group d;
   22874            3 :       error_at (from_loc, "import of %qs within its own implementation unit",
   22875              :                 import->get_flatname());
   22876            3 :       inform (import->loc, "module declared here");
   22877            3 :       return;
   22878            3 :     }
   22879              : 
   22880         2561 :   if (!import->check_circular_import (from_loc))
   22881              :     return;
   22882              : 
   22883         2555 :   if (!import->is_header () && current_lang_depth ())
   22884              :     /* Only header units should appear inside language
   22885              :        specifications.  The std doesn't specify this, but I think
   22886              :        that's an error in resolving US 033, because language linkage
   22887              :        is also our escape clause to getting things into the global
   22888              :        module, so we don't want to confuse things by having to think
   22889              :        about whether 'extern "C++" { import foo; }' puts foo's
   22890              :        contents into the global module all of a sudden.  */
   22891            6 :     warning (0, "import of named module %qs inside language-linkage block",
   22892              :              import->get_flatname ());
   22893              : 
   22894         2555 :   if (exporting_p || module_exporting_p ())
   22895          331 :     import->exported_p = true;
   22896              : 
   22897         2555 :   if (import->loadedness != ML_NONE)
   22898              :     {
   22899          943 :       from_loc = ordinary_loc_of (line_table, from_loc);
   22900          943 :       linemap_module_reparent (line_table, import->loc, from_loc);
   22901              :     }
   22902              : 
   22903         2555 :   gcc_checking_assert (import->is_direct () && import->has_location ());
   22904              : 
   22905         2555 :   direct_import (import, reader);
   22906              : }
   22907              : 
   22908              : /* Declare the name of the current module to be NAME.  EXPORTING_p is
   22909              :    true if this TU is the exporting module unit.  */
   22910              : 
   22911              : void
   22912         3222 : declare_module (module_state *module, location_t from_loc, bool exporting_p,
   22913              :                 tree, cpp_reader *reader)
   22914              : {
   22915         3222 :   gcc_assert (global_namespace == current_scope ());
   22916              : 
   22917         3222 :   module_state *current = this_module ();
   22918         3222 :   if (module_purview_p () || module->loadedness > ML_CONFIG)
   22919              :     {
   22920            6 :       auto_diagnostic_group d;
   22921           12 :       error_at (from_loc, module_purview_p ()
   22922              :                 ? G_("module already declared")
   22923              :                 : G_("module already imported"));
   22924            6 :       if (module_purview_p ())
   22925            0 :         module = current;
   22926           12 :       inform (module->loc, module_purview_p ()
   22927              :               ? G_("module %qs declared here")
   22928              :               : G_("module %qs imported here"),
   22929              :               module->get_flatname ());
   22930            6 :       return;
   22931            6 :     }
   22932              : 
   22933         3216 :   gcc_checking_assert (module->is_module ());
   22934         3216 :   gcc_checking_assert (module->is_direct () && module->has_location ());
   22935              : 
   22936              :   /* Yer a module, 'arry.  */
   22937         3216 :   module_kind = module->is_header () ? MK_HEADER : MK_NAMED | MK_ATTACH;
   22938              : 
   22939              :   // Even in header units, we consider the decls to be purview
   22940         3216 :   module_kind |= MK_PURVIEW;
   22941              : 
   22942         3216 :   if (module->is_partition ())
   22943          220 :     module_kind |= MK_PARTITION;
   22944         3216 :   if (exporting_p)
   22945              :     {
   22946         2900 :       module->interface_p = true;
   22947         2900 :       module_kind |= MK_INTERFACE;
   22948              :     }
   22949              : 
   22950         3216 :   if (module_has_cmi_p ())
   22951              :     {
   22952              :       /* Copy the importing information we may have already done.  We
   22953              :          do not need to separate out the imports that only happen in
   22954              :          the GMF, in spite of what the literal wording of the std
   22955              :          might imply.  See p2191, the core list had a discussion
   22956              :          where the module implementors agreed that the GMF of a named
   22957              :          module is invisible to importers.  */
   22958         2971 :       module->imports = current->imports;
   22959              : 
   22960         2971 :       module->mod = 0;
   22961         2971 :       (*modules)[0] = module;
   22962              :     }
   22963              :   else
   22964              :     {
   22965          245 :       module->interface_p = true;
   22966          245 :       current->parent = module; /* So mangler knows module identity. */
   22967          245 :       direct_import (module, reader);
   22968              :     }
   22969              : }
   22970              : 
   22971              : /* Return true IFF we must emit a module global initializer function
   22972              :    (which will be called by importers' init code).  */
   22973              : 
   22974              : bool
   22975       106917 : module_global_init_needed ()
   22976              : {
   22977       106917 :   return module_has_cmi_p () && !header_module_p ();
   22978              : }
   22979              : 
   22980              : /* Calculate which, if any, import initializers need calling.  */
   22981              : 
   22982              : bool
   22983        99305 : module_determine_import_inits ()
   22984              : {
   22985        99305 :   if (!modules || header_module_p ())
   22986              :     return false;
   22987              : 
   22988              :   /* Prune active_init_p.  We need the same bitmap allocation
   22989              :      scheme as for the imports member.  */
   22990         3931 :   function_depth++; /* Disable GC.  */
   22991         3931 :   bitmap covered_imports (BITMAP_GGC_ALLOC ());
   22992              : 
   22993         3931 :   bool any = false;
   22994              : 
   22995              :   /* Because indirect imports are before their direct import, and
   22996              :      we're scanning the array backwards, we only need one pass!  */
   22997         6829 :   for (unsigned ix = modules->length (); --ix;)
   22998              :     {
   22999         2898 :       module_state *import = (*modules)[ix];
   23000              : 
   23001         2898 :       if (!import->active_init_p)
   23002              :         ;
   23003           64 :       else if (bitmap_bit_p (covered_imports, ix))
   23004            9 :         import->active_init_p = false;
   23005              :       else
   23006              :         {
   23007              :           /* Everything this imports is therefore handled by its
   23008              :              initializer, so doesn't need initializing by us.  */
   23009           55 :           bitmap_ior_into (covered_imports, import->imports);
   23010           55 :           any = true;
   23011              :         }
   23012              :     }
   23013         3931 :   function_depth--;
   23014              : 
   23015         3931 :   return any;
   23016              : }
   23017              : 
   23018              : /* Emit calls to each direct import's global initializer.  Including
   23019              :    direct imports of directly imported header units.  The initializers
   23020              :    of (static) entities in header units will be called by their
   23021              :    importing modules (for the instance contained within that), or by
   23022              :    the current TU (for the instances we've brought in).  Of course
   23023              :    such header unit behaviour is evil, but iostream went through that
   23024              :    door some time ago.  */
   23025              : 
   23026              : void
   23027           55 : module_add_import_initializers ()
   23028              : {
   23029           55 :   if (!modules || header_module_p ())
   23030            0 :     return;
   23031              : 
   23032           55 :   tree fntype = build_function_type (void_type_node, void_list_node);
   23033           55 :   releasing_vec args;  // There are no args
   23034              : 
   23035          125 :   for (unsigned ix = modules->length (); --ix;)
   23036              :     {
   23037           70 :       module_state *import = (*modules)[ix];
   23038           70 :       if (import->active_init_p)
   23039              :         {
   23040           55 :           tree name = mangle_module_global_init (ix);
   23041           55 :           tree fndecl = build_lang_decl (FUNCTION_DECL, name, fntype);
   23042              : 
   23043           55 :           DECL_CONTEXT (fndecl) = FROB_CONTEXT (global_namespace);
   23044           55 :           SET_DECL_ASSEMBLER_NAME (fndecl, name);
   23045           55 :           TREE_PUBLIC (fndecl) = true;
   23046           55 :           determine_visibility (fndecl);
   23047              : 
   23048           55 :           tree call = cp_build_function_call_vec (fndecl, &args,
   23049              :                                                   tf_warning_or_error);
   23050           55 :           finish_expr_stmt (call);
   23051              :         }
   23052              :     }
   23053           55 : }
   23054              : 
   23055              : /* NAME & LEN are a preprocessed header name, possibly including the
   23056              :    surrounding "" or <> characters.  Return the raw string name of the
   23057              :    module to which it refers.  This will be an absolute path, or begin
   23058              :    with ./, so it is immediately distinguishable from a (non-header
   23059              :    unit) module name.  If READER is non-null, ask the preprocessor to
   23060              :    locate the header to which it refers using the appropriate include
   23061              :    path.  Note that we do never do \ processing of the string, as that
   23062              :    matches the preprocessor's behaviour.  */
   23063              : 
   23064              : static const char *
   23065        25962 : canonicalize_header_name (cpp_reader *reader, location_t loc, bool unquoted,
   23066              :                           const char *str, size_t &len_r)
   23067              : {
   23068        25962 :   size_t len = len_r;
   23069        25962 :   static char *buf = 0;
   23070        25962 :   static size_t alloc = 0;
   23071              : 
   23072        25962 :   if (!unquoted)
   23073              :     {
   23074            4 :       gcc_checking_assert (len >= 2
   23075              :                            && ((reader && str[0] == '<' && str[len-1] == '>')
   23076              :                                || (str[0] == '"' && str[len-1] == '"')));
   23077            4 :       str += 1;
   23078            4 :       len -= 2;
   23079              :     }
   23080              : 
   23081        25962 :   if (reader)
   23082              :     {
   23083            4 :       gcc_assert (!unquoted);
   23084              : 
   23085            4 :       if (len >= alloc)
   23086              :         {
   23087            4 :           alloc = len + 1;
   23088            4 :           buf = XRESIZEVEC (char, buf, alloc);
   23089              :         }
   23090            4 :       memcpy (buf, str, len);
   23091            4 :       buf[len] = 0;
   23092              : 
   23093            8 :       if (const char *hdr
   23094            4 :           = cpp_probe_header_unit (reader, buf, str[-1] == '<', loc))
   23095              :         {
   23096            4 :           len = strlen (hdr);
   23097            4 :           str = hdr;
   23098              :         }
   23099              :       else
   23100            0 :         str = buf;
   23101              :     }
   23102              : 
   23103        25962 :   if (!(str[0] == '.' ? IS_DIR_SEPARATOR (str[1]) : IS_ABSOLUTE_PATH (str)))
   23104              :     {
   23105              :       /* Prepend './'  */
   23106            9 :       if (len + 3 > alloc)
   23107              :         {
   23108            9 :           alloc = len + 3;
   23109            9 :           buf = XRESIZEVEC (char, buf, alloc);
   23110              :         }
   23111              : 
   23112            9 :       buf[0] = '.';
   23113            9 :       buf[1] = DIR_SEPARATOR;
   23114            9 :       memmove (buf + 2, str, len);
   23115            9 :       len += 2;
   23116            9 :       buf[len] = 0;
   23117            9 :       str = buf;
   23118              :     }
   23119              : 
   23120        25962 :   len_r = len;
   23121        25962 :   return str;
   23122              : }
   23123              : 
   23124              : /* Set the CMI name from a cody packet.  Issue an error if
   23125              :    ill-formed.  */
   23126              : 
   23127         5905 : void module_state::set_filename (const Cody::Packet &packet)
   23128              : {
   23129         5905 :   if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23130              :     {
   23131              :       /* If we've seen this import before we better have the same CMI.  */
   23132         5902 :       const std::string &path = packet.GetString ();
   23133         5902 :       if (!filename)
   23134         5899 :         filename = xstrdup (packet.GetString ().c_str ());
   23135            3 :       else if (filename != path)
   23136            0 :         error_at (loc, "mismatching compiled module interface: "
   23137              :                   "had %qs, got %qs", filename, path.c_str ());
   23138              :     }
   23139              :   else
   23140              :     {
   23141            3 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23142            3 :       fatal_error (loc, "unknown compiled module interface: %s",
   23143            3 :                    packet.GetString ().c_str ());
   23144              :     }
   23145         5902 : }
   23146              : 
   23147              : /* The list of importable headers from C++ Table 24.  */
   23148              : 
   23149              : static const char *
   23150              : importable_headers[] =
   23151              :   {
   23152              :     "algorithm", "any", "array", "atomic",
   23153              :     "barrier", "bit", "bitset",
   23154              :     "charconv", "chrono", "compare", "complex", "concepts",
   23155              :     "condition_variable", "contracts", "coroutine",
   23156              :     "debugging", "deque",
   23157              :     "exception", "execution", "expected",
   23158              :     "filesystem", "flat_map", "flat_set", "format", "forward_list",
   23159              :     "fstream", "functional", "future",
   23160              :     "generator",
   23161              :     "hazard_pointer", "hive",
   23162              :     "initializer_list", "inplace_vector", "iomanip", "ios", "iosfwd",
   23163              :     "iostream", "istream", "iterator",
   23164              :     "latch", "limits", "linalg", "list", "locale",
   23165              :     "map", "mdspan", "memory", "memory_resource", "meta", "mutex",
   23166              :     "new", "numbers", "numeric",
   23167              :     "optional", "ostream",
   23168              :     "print",
   23169              :     "queue",
   23170              :     "random", "ranges", "ratio", "rcu", "regex",
   23171              :     "scoped_allocator", "semaphore", "set", "shared_mutex", "simd",
   23172              :     "source_location", "span", "spanstream", "sstream", "stack", "stacktrace",
   23173              :     "stdexcept", "stdfloat", "stop_token", "streambuf", "string",
   23174              :     "string_view", "syncstream", "system_error",
   23175              :     "text_encoding", "thread", "tuple", "type_traits", "typeindex", "typeinfo",
   23176              :     "unordered_map", "unordered_set",
   23177              :     "utility",
   23178              :     "valarray", "variant", "vector", "version"
   23179              :   };
   23180              : 
   23181              : /* True iff <name> is listed as an importable standard header.  */
   23182              : 
   23183              : static bool
   23184        23654 : is_importable_header (const char *name)
   23185              : {
   23186        23654 :   unsigned lo = 0;
   23187        23654 :   unsigned hi = ARRAY_SIZE (importable_headers);
   23188       179448 :   while (hi > lo)
   23189              :     {
   23190       158575 :       unsigned mid = (lo + hi)/2;
   23191       158575 :       int cmp = strcmp (name, importable_headers[mid]);
   23192       158575 :       if (cmp > 0)
   23193        36567 :         lo = mid + 1;
   23194       122008 :       else if (cmp < 0)
   23195              :         hi = mid;
   23196              :       else
   23197              :         return true;
   23198              :     }
   23199              :   return false;
   23200              : }
   23201              : 
   23202              : /* Figure out whether to treat HEADER as an include or an import.  */
   23203              : 
   23204              : static char *
   23205        25029 : maybe_translate_include (cpp_reader *reader, line_maps *lmaps, location_t loc,
   23206              :                          _cpp_file *file, bool angle, const char **alternate)
   23207              : {
   23208        25029 :   if (!modules_p () || !flag_module_include_translate)
   23209              :     {
   23210              :       /* Turn off.  */
   23211            4 :       cpp_get_callbacks (reader)->translate_include = NULL;
   23212            4 :       return nullptr;
   23213              :     }
   23214              : 
   23215        25025 :   const char *path = _cpp_get_file_path (file);
   23216              : 
   23217        25025 :   dump.push (NULL);
   23218              : 
   23219        26550 :   dump () && dump ("Checking include translation '%s'", path);
   23220        25025 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23221              : 
   23222        25025 :   size_t len = strlen (path);
   23223        25025 :   path = canonicalize_header_name (NULL, loc, true, path, len);
   23224        25025 :   auto packet = mapper->IncludeTranslate (path, Cody::Flags::None, len);
   23225              : 
   23226        25025 :   enum class xlate_kind {
   23227              :     unknown, text, import, invalid
   23228        25025 :   } translate = xlate_kind::unknown;
   23229              : 
   23230        25025 :   if (packet.GetCode () == Cody::Client::PC_BOOL)
   23231        24978 :     translate = packet.GetInteger () ? xlate_kind::text : xlate_kind::unknown;
   23232           47 :   else if (packet.GetCode () == Cody::Client::PC_PATHNAME)
   23233              :     {
   23234              :       /* Record the CMI name for when we do the import.
   23235              :          We may already know about this import, but libcpp doesn't yet.  */
   23236           47 :       module_state *import = get_module (build_string (len, path));
   23237           47 :       import->set_filename (packet);
   23238           47 :       if (import->check_importable (reader))
   23239              :         translate = xlate_kind::import;
   23240              :       else
   23241            0 :         translate = xlate_kind::invalid;
   23242              :     }
   23243              :   else
   23244              :     {
   23245            0 :       gcc_checking_assert (packet.GetCode () == Cody::Client::PC_ERROR);
   23246            0 :       error_at (loc, "cannot determine %<#include%> translation of %s: %s",
   23247            0 :                 path, packet.GetString ().c_str ());
   23248              :     }
   23249              : 
   23250        25025 :   bool note = (translate == xlate_kind::invalid);
   23251        25025 :   if (note_include_translate_yes && translate == xlate_kind::import)
   23252              :     note = true;
   23253        25022 :   else if (note_include_translate_no && translate == xlate_kind::unknown)
   23254              :     note = true;
   23255        25019 :   else if (note_includes)
   23256              :     /* We do not expect the note_includes vector to be large, so O(N)
   23257              :        iteration.  */
   23258          434 :     for (unsigned ix = note_includes->length (); !note && ix--;)
   23259          217 :       if (!strcmp ((*note_includes)[ix], path))
   23260            1 :         note = true;
   23261              : 
   23262              :   /* Maybe try importing a different header instead.  */
   23263        25025 :   if (alternate && translate == xlate_kind::unknown)
   23264              :     {
   23265        24577 :       const char *fname = _cpp_get_file_name (file);
   23266              :       /* Redirect importable <name> to <bits/stdc++.h>.  */
   23267              :       /* ??? Generalize to use a .json.  */
   23268        24577 :       expanded_location eloc = expand_location (loc);
   23269        27344 :       auto indir = [](const char *f, const char *d)
   23270              :       {
   23271         2767 :         if (!filename_ncmp (f, d, strlen (d))) return true;
   23272              :         /* Also check canonical paths (c++/123879).  */
   23273          929 :         auto cf = lrealpath (f); auto cd = lrealpath (d);
   23274          929 :         bool r = cf && cd && !filename_ncmp (cf, cd, strlen (cd));
   23275          929 :         free (cf); free (cd);
   23276          929 :         return r;
   23277              :       };
   23278        23654 :       if (angle && is_importable_header (fname)
   23279              :           /* Exclude <version> which often goes with import std.  */
   23280         2781 :           && strcmp (fname, "version") != 0
   23281              :           /* Don't redirect #includes between headers under the same include
   23282              :              path directory (i.e. between library headers); if the import
   23283              :              brings in the current file we then get redefinition errors.  */
   23284         2767 :           && !indir (eloc.file, _cpp_get_file_dir (file)->name)
   23285              :           /* ??? These are needed when running a toolchain from the build
   23286              :              directory, because libsupc++ headers aren't linked into
   23287              :              libstdc++-v3/include with the other headers.  */
   23288          851 :           && !strstr (eloc.file, "libstdc++-v3/include")
   23289        25066 :           && !strstr (eloc.file, "libsupc++"))
   23290          406 :         *alternate = "bits/stdc++.h";
   23291              :     }
   23292              : 
   23293        25025 :   if (note)
   23294           10 :     inform (loc, translate == xlate_kind::import
   23295              :             ? G_("include %qs translated to import")
   23296              :             : translate == xlate_kind::invalid
   23297            3 :             ? G_("import of %qs failed, falling back to include")
   23298              :             : G_("include %qs processed textually"), path);
   23299              : 
   23300        28072 :   dump () && dump (translate == xlate_kind::import
   23301              :                    ? "Translating include to import"
   23302              :                    : "Keeping include as include");
   23303        25025 :   dump.pop (0);
   23304              : 
   23305        25025 :   if (translate != xlate_kind::import)
   23306              :     return nullptr;
   23307              : 
   23308              :   /* Create the translation text.  */
   23309           47 :   loc = ordinary_loc_of (lmaps, loc);
   23310           47 :   const line_map_ordinary *map
   23311           47 :     = linemap_check_ordinary (linemap_lookup (lmaps, loc));
   23312           47 :   unsigned col = SOURCE_COLUMN (map, loc);
   23313           47 :   col -= (col != 0); /* Columns are 1-based.  */
   23314              : 
   23315           47 :   unsigned alloc = len + col + 60;
   23316           47 :   char *res = XNEWVEC (char, alloc);
   23317              : 
   23318           47 :   strcpy (res, "__import");
   23319           47 :   unsigned actual = 8;
   23320           47 :   if (col > actual)
   23321              :     {
   23322              :       /* Pad out so the filename appears at the same position.  */
   23323           44 :       memset (res + actual, ' ', col - actual);
   23324           44 :       actual = col;
   23325              :     }
   23326              :   /* No need to encode characters, that's not how header names are
   23327              :      handled.  */
   23328           47 :   actual += snprintf (res + actual, alloc - actual,
   23329              :                       "\"%s\" [[__translated]];\n", path);
   23330           47 :   gcc_checking_assert (actual < alloc);
   23331              : 
   23332              :   /* cpplib will delete the buffer.  */
   23333              :   return res;
   23334        25025 : }
   23335              : 
   23336              : static void
   23337          933 : begin_header_unit (cpp_reader *reader)
   23338              : {
   23339              :   /* Set the module header name from the main_input_filename.  */
   23340          933 :   const char *main = main_input_filename;
   23341          933 :   size_t len = strlen (main);
   23342          933 :   main = canonicalize_header_name (NULL, 0, true, main, len);
   23343          933 :   module_state *module = get_module (build_string (len, main));
   23344              : 
   23345          933 :   preprocess_module (module, cpp_main_loc (reader), false, false, true, reader);
   23346          933 : }
   23347              : 
   23348              : /* We've just properly entered the main source file.  I.e. after the
   23349              :    command line, builtins and forced headers.  Record the line map and
   23350              :    location of this map.  Note we may be called more than once.  The
   23351              :    first call sticks.  */
   23352              : 
   23353              : void
   23354       101257 : module_begin_main_file (cpp_reader *reader, line_maps *lmaps,
   23355              :                        const line_map_ordinary *map)
   23356              : {
   23357       101257 :   gcc_checking_assert (lmaps == line_table);
   23358       101257 :   if (modules_p () && !spans.init_p ())
   23359              :     {
   23360         5030 :       unsigned n = dump.push (NULL);
   23361         5030 :       spans.init (lmaps, map);
   23362         5030 :       dump.pop (n);
   23363         5030 :       if (flag_header_unit && !cpp_get_options (reader)->preprocessed)
   23364              :         {
   23365              :           /* Tell the preprocessor this is an include file.  */
   23366          924 :           cpp_retrofit_as_include (reader);
   23367          924 :           begin_header_unit (reader);
   23368              :         }
   23369              :     }
   23370       101257 : }
   23371              : 
   23372              : /* Process the pending_import queue, making sure we know the
   23373              :    filenames.   */
   23374              : 
   23375              : static void
   23376         5945 : name_pending_imports (cpp_reader *reader)
   23377              : {
   23378         5945 :   auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23379              : 
   23380         5945 :   if (!vec_safe_length (pending_imports))
   23381              :     /* Not doing anything.  */
   23382              :     return;
   23383              : 
   23384         5089 :   timevar_start (TV_MODULE_MAPPER);
   23385              : 
   23386         5089 :   auto n = dump.push (NULL);
   23387         5711 :   dump () && dump ("Resolving direct import names");
   23388         5089 :   bool want_deps = (bool (mapper->get_flags () & Cody::Flags::NameOnly)
   23389         5089 :                     || cpp_get_deps (reader));
   23390         5089 :   bool any = false;
   23391              : 
   23392        11129 :   for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23393              :     {
   23394         6040 :       module_state *module = (*pending_imports)[ix];
   23395         6040 :       gcc_checking_assert (module->is_direct ());
   23396         6040 :       if (!module->filename && !module->visited_p)
   23397              :         {
   23398         5930 :           bool export_p = (module->is_module ()
   23399         5930 :                            && (module->is_partition ()
   23400         3074 :                                || module->is_exported ()));
   23401              : 
   23402         5930 :           Cody::Flags flags = Cody::Flags::None;
   23403         5930 :           if (flag_preprocess_only
   23404         5930 :               && !(module->is_header () && !export_p))
   23405              :             {
   23406          141 :               if (!want_deps)
   23407           72 :                 continue;
   23408              :               flags = Cody::Flags::NameOnly;
   23409              :             }
   23410              : 
   23411         5858 :           if (!any)
   23412              :             {
   23413         5016 :               any = true;
   23414         5016 :               mapper->Cork ();
   23415              :             }
   23416         5858 :           if (export_p)
   23417         3025 :             mapper->ModuleExport (module->get_flatname (), flags);
   23418              :           else
   23419         2833 :             mapper->ModuleImport (module->get_flatname (), flags);
   23420         5858 :           module->visited_p = true;
   23421              :         }
   23422              :     }
   23423              : 
   23424         5089 :   if (any)
   23425              :     {
   23426         5016 :       auto response = mapper->Uncork ();
   23427         5016 :       auto r_iter = response.begin ();
   23428        10965 :       for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23429              :         {
   23430         5952 :           module_state *module = (*pending_imports)[ix];
   23431         5952 :           if (module->visited_p)
   23432              :             {
   23433         5858 :               module->visited_p = false;
   23434         5858 :               gcc_checking_assert (!module->filename);
   23435              : 
   23436         5858 :               module->set_filename (*r_iter);
   23437         5855 :               ++r_iter;
   23438              :             }
   23439              :         }
   23440         5013 :     }
   23441              : 
   23442         5086 :   dump.pop (n);
   23443              : 
   23444         5086 :   timevar_stop (TV_MODULE_MAPPER);
   23445              : }
   23446              : 
   23447              : /* We've just lexed a module-specific control line for MODULE.  Mark
   23448              :    the module as a direct import, and possibly load up its macro
   23449              :    state.  Returns the primary module, if this is a module
   23450              :    declaration.  */
   23451              : /* Perhaps we should offer a preprocessing mode where we read the
   23452              :    directives from the header unit, rather than require the header's
   23453              :    CMI.  */
   23454              : 
   23455              : module_state *
   23456         6059 : preprocess_module (module_state *module, location_t from_loc,
   23457              :                    bool in_purview, bool is_import, bool is_export,
   23458              :                    cpp_reader *reader)
   23459              : {
   23460         6059 :   if (!is_import)
   23461              :     {
   23462         3378 :       if (in_purview || module->loc)
   23463              :         {
   23464              :           /* We've already seen a module declaration.  If only preprocessing
   23465              :              then we won't complain in declare_module, so complain here.  */
   23466           42 :           if (flag_preprocess_only)
   23467            6 :             error_at (from_loc,
   23468              :                       in_purview
   23469              :                       ? G_("module already declared")
   23470              :                       : G_("module already imported"));
   23471              :           /* Always pretend this was an import to aid error recovery.  */
   23472              :           is_import = true;
   23473              :         }
   23474              :       else
   23475              :         {
   23476              :           /* Record it is the module.  */
   23477         3336 :           module->module_p = true;
   23478         3336 :           if (is_export)
   23479              :             {
   23480         3002 :               module->exported_p = true;
   23481         3002 :               module->interface_p = true;
   23482              :             }
   23483              :         }
   23484              :     }
   23485              : 
   23486         6059 :   if (module->directness < MD_DIRECT + in_purview)
   23487              :     {
   23488              :       /* Mark as a direct import.  */
   23489         6013 :       module->directness = module_directness (MD_DIRECT + in_purview);
   23490              : 
   23491              :       /* Set the location to be most informative for users.  */
   23492         6013 :       from_loc = ordinary_loc_of (line_table, from_loc);
   23493         6013 :       if (module->loadedness != ML_NONE)
   23494            6 :         linemap_module_reparent (line_table, module->loc, from_loc);
   23495              :       else
   23496              :         {
   23497              :           /* Don't overwrite the location if we're importing ourselves
   23498              :              after already having seen a module-declaration.  */
   23499         6007 :           if (!(is_import && module->is_module ()))
   23500         5977 :             module->loc = from_loc;
   23501         6007 :           if (!module->flatname)
   23502         5968 :             module->set_flatname ();
   23503              :         }
   23504              :     }
   23505              : 
   23506         6059 :   auto desired = ML_CONFIG;
   23507         6059 :   if (is_import
   23508         2723 :       && module->is_header ()
   23509         6987 :       && (!cpp_get_options (reader)->preprocessed
   23510            3 :           || cpp_get_options (reader)->directives_only))
   23511              :     /* We need preprocessor state now.  */
   23512              :     desired = ML_PREPROCESSOR;
   23513              : 
   23514         6059 :   if (!is_import || module->loadedness < desired)
   23515              :     {
   23516         6040 :       vec_safe_push (pending_imports, module);
   23517              : 
   23518         6040 :       if (desired == ML_PREPROCESSOR)
   23519              :         {
   23520          909 :           unsigned n = dump.push (NULL);
   23521              : 
   23522         1108 :           dump () && dump ("Reading %M preprocessor state", module);
   23523          909 :           name_pending_imports (reader);
   23524              : 
   23525              :           /* Preserve the state of the line-map.  */
   23526          909 :           auto pre_hwm = LINEMAPS_ORDINARY_USED (line_table);
   23527              : 
   23528              :           /* We only need to close the span, if we're going to emit a
   23529              :              CMI.  But that's a little tricky -- our token scanner
   23530              :              needs to be smarter -- and this isn't much state.
   23531              :              Remember, we've not parsed anything at this point, so
   23532              :              our module state flags are inadequate.  */
   23533          909 :           spans.maybe_init ();
   23534          909 :           spans.close ();
   23535              : 
   23536          909 :           timevar_start (TV_MODULE_IMPORT);
   23537              : 
   23538              :           /* Load the config of each pending import -- we must assign
   23539              :              module numbers monotonically.  */
   23540         2914 :           for (unsigned ix = 0; ix != pending_imports->length (); ix++)
   23541              :             {
   23542         1096 :               auto *import = (*pending_imports)[ix];
   23543         1274 :               if (!(import->is_module ()
   23544          178 :                     && (import->is_partition () || import->is_exported ()))
   23545          924 :                   && import->loadedness == ML_NONE
   23546         2020 :                   && (!flag_preprocess_only
   23547           51 :                       || (import->is_header ()
   23548              :                           /* Allow a missing/unimportable GCM with -MG.
   23549              :                              FIXME We should also try falling back to #include
   23550              :                              before giving up entirely.  */
   23551           42 :                           && (!cpp_get_options (reader)->deps.missing_files
   23552            3 :                               || import->check_importable (reader)))))
   23553              :                 {
   23554          912 :                   unsigned n = dump.push (import);
   23555          912 :                   import->do_import (reader, true);
   23556          912 :                   dump.pop (n);
   23557              :                 }
   23558              :             }
   23559          909 :           vec_free (pending_imports);
   23560              : 
   23561              :           /* Restore the line-map state.  */
   23562          909 :           spans.open (linemap_module_restore (line_table, pre_hwm));
   23563              : 
   23564              :           /* Now read the preprocessor state of this particular
   23565              :              import.  */
   23566          909 :           if (module->loadedness == ML_CONFIG
   23567          909 :               && module->read_preprocessor (true))
   23568          903 :             module->import_macros ();
   23569              : 
   23570          909 :           timevar_stop (TV_MODULE_IMPORT);
   23571              : 
   23572          909 :           dump.pop (n);
   23573              :         }
   23574              :     }
   23575              : 
   23576         6059 :   return is_import ? NULL : get_primary (module);
   23577              : }
   23578              : 
   23579              : /* We've completed phase-4 translation.  Emit any dependency
   23580              :    information for the not-yet-loaded direct imports, and fill in
   23581              :    their file names.  We'll have already loaded up the direct header
   23582              :    unit wavefront.  */
   23583              : 
   23584              : void
   23585         5036 : preprocessed_module (cpp_reader *reader)
   23586              : {
   23587         5036 :   unsigned n = dump.push (NULL);
   23588              : 
   23589         5622 :   dump () && dump ("Completed phase-4 (tokenization) processing");
   23590              : 
   23591         5036 :   name_pending_imports (reader);
   23592         5033 :   vec_free (pending_imports);
   23593              : 
   23594         5033 :   spans.maybe_init ();
   23595         5033 :   spans.close ();
   23596              : 
   23597         5033 :   using iterator = hash_table<module_state_hash>::iterator;
   23598         5033 :   if (mkdeps *deps = cpp_get_deps (reader))
   23599              :     {
   23600              :       /* Walk the module hash, informing the dependency machinery.  */
   23601           57 :       iterator end = modules_hash->end ();
   23602          228 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23603              :         {
   23604          114 :           module_state *module = *iter;
   23605              : 
   23606          114 :           if (module->is_direct ())
   23607              :             {
   23608           90 :               if (module->is_module ()
   23609           90 :                   && (module->is_interface () || module->is_partition ()))
   23610           36 :                 deps_add_module_target (deps, module->get_flatname (),
   23611           36 :                                         maybe_add_cmi_prefix (module->filename),
   23612              :                                         module->is_header (),
   23613              :                                         module->is_exported ());
   23614              :               else
   23615           54 :                 deps_add_module_dep (deps, module->get_flatname ());
   23616              :             }
   23617              :         }
   23618              :     }
   23619              : 
   23620         5033 :   if (flag_header_unit && !flag_preprocess_only)
   23621              :     {
   23622              :       /* Find the main module -- remember, it's not yet in the module
   23623              :          array.  */
   23624          921 :       iterator end = modules_hash->end ();
   23625         1896 :       for (iterator iter = modules_hash->begin (); iter != end; ++iter)
   23626              :         {
   23627          975 :           module_state *module = *iter;
   23628          975 :           if (module->is_module ())
   23629              :             {
   23630          921 :               declare_module (module, cpp_main_loc (reader), true, NULL, reader);
   23631          921 :               module_kind |= MK_EXPORTING;
   23632          921 :               break;
   23633              :             }
   23634              :         }
   23635              :     }
   23636              : 
   23637         5033 :   dump.pop (n);
   23638         5033 : }
   23639              : 
   23640              : /* VAL is a global tree, add it to the global vec if it is
   23641              :    interesting.  Add some of its targets, if they too are
   23642              :    interesting.  We do not add identifiers, as they can be re-found
   23643              :    via the identifier hash table.  There is a cost to the number of
   23644              :    global trees.  */
   23645              : 
   23646              : static int
   23647      3120740 : maybe_add_global (tree val, unsigned &crc)
   23648              : {
   23649      3120740 :   int v = 0;
   23650              : 
   23651      3120740 :   if (val && !(identifier_p (val) || TREE_VISITED (val)))
   23652              :     {
   23653       960298 :       TREE_VISITED (val) = true;
   23654       960298 :       crc = crc32_unsigned (crc, fixed_trees->length ());
   23655       960298 :       vec_safe_push (fixed_trees, val);
   23656       960298 :       v++;
   23657              : 
   23658       960298 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPED))
   23659       960298 :         v += maybe_add_global (TREE_TYPE (val), crc);
   23660       960298 :       if (CODE_CONTAINS_STRUCT (TREE_CODE (val), TS_TYPE_COMMON))
   23661       629498 :         v += maybe_add_global (TYPE_NAME (val), crc);
   23662              :     }
   23663              : 
   23664      3120740 :   return v;
   23665              : }
   23666              : 
   23667              : /* Initialize module state.  Create the hash table, determine the
   23668              :    global trees.  Create the module for current TU.  */
   23669              : 
   23670              : void
   23671         5036 : init_modules (cpp_reader *reader)
   23672              : {
   23673              :   /* PCH should not be reachable because of lang-specs, but the
   23674              :      user could have overridden that.  */
   23675         5036 :   if (pch_file)
   23676            0 :     fatal_error (input_location,
   23677              :                  "C++ modules are incompatible with precompiled headers");
   23678              : 
   23679         5036 :   if (cpp_get_options (reader)->traditional)
   23680            0 :     fatal_error (input_location,
   23681              :                  "C++ modules are incompatible with traditional preprocessing");
   23682              : 
   23683              :   /* :: is always exported.  */
   23684         5036 :   DECL_MODULE_EXPORT_P (global_namespace) = true;
   23685              : 
   23686         5036 :   modules_hash = hash_table<module_state_hash>::create_ggc (31);
   23687         5036 :   vec_safe_reserve (modules, 20);
   23688              : 
   23689              :   /* Create module for current TU.  */
   23690         5036 :   module_state *current
   23691         5036 :     = new (ggc_alloc<module_state> ()) module_state (NULL_TREE, NULL, false);
   23692         5036 :   current->mod = 0;
   23693         5036 :   bitmap_set_bit (current->imports, 0);
   23694         5036 :   modules->quick_push (current);
   23695              : 
   23696         5036 :   gcc_checking_assert (!fixed_trees);
   23697              : 
   23698         5036 :   headers = BITMAP_GGC_ALLOC ();
   23699              : 
   23700         5036 :   if (note_includes)
   23701              :     /* Canonicalize header names.  */
   23702            2 :     for (unsigned ix = 0; ix != note_includes->length (); ix++)
   23703              :       {
   23704            1 :         const char *hdr = (*note_includes)[ix];
   23705            1 :         size_t len = strlen (hdr);
   23706              : 
   23707            1 :         bool system = hdr[0] == '<';
   23708            1 :         bool user = hdr[0] == '"';
   23709            1 :         bool delimed = system || user;
   23710              : 
   23711            1 :         if (len <= (delimed ? 2 : 0)
   23712            1 :             || (delimed && hdr[len-1] != (system ? '>' : '"')))
   23713            0 :           error ("invalid header name %qs", hdr);
   23714              : 
   23715            1 :         hdr = canonicalize_header_name (delimed ? reader : NULL,
   23716              :                                         0, !delimed, hdr, len);
   23717            1 :         char *path = XNEWVEC (char, len + 1);
   23718            1 :         memcpy (path, hdr, len);
   23719            1 :         path[len] = 0;
   23720              : 
   23721            1 :         (*note_includes)[ix] = path;
   23722              :       }
   23723              : 
   23724         5036 :   if (note_cmis)
   23725              :     /* Canonicalize & mark module names.  */
   23726           12 :     for (unsigned ix = 0; ix != note_cmis->length (); ix++)
   23727              :       {
   23728            6 :         const char *name = (*note_cmis)[ix];
   23729            6 :         size_t len = strlen (name);
   23730              : 
   23731            6 :         bool is_system = name[0] == '<';
   23732            6 :         bool is_user = name[0] == '"';
   23733            6 :         bool is_pathname = false;
   23734            6 :         if (!(is_system || is_user))
   23735           12 :           for (unsigned ix = len; !is_pathname && ix--;)
   23736            9 :             is_pathname = IS_DIR_SEPARATOR (name[ix]);
   23737            6 :         if (is_system || is_user || is_pathname)
   23738              :           {
   23739            3 :             if (len <= (is_pathname ? 0 : 2)
   23740            3 :                 || (!is_pathname && name[len-1] != (is_system ? '>' : '"')))
   23741              :               {
   23742            0 :                 error ("invalid header name %qs", name);
   23743            0 :                 continue;
   23744              :               }
   23745              :             else
   23746            3 :               name = canonicalize_header_name (is_pathname ? nullptr : reader,
   23747              :                                                0, is_pathname, name, len);
   23748              :           }
   23749            6 :         if (auto module = get_module (name))
   23750            6 :           module->inform_cmi_p = 1;
   23751              :         else
   23752            0 :           error ("invalid module name %qs", name);
   23753              :       }
   23754              : 
   23755         5036 :   dump.push (NULL);
   23756              : 
   23757              :   /* Determine lazy handle bound.  */
   23758         5036 :   {
   23759         5036 :     unsigned limit = 1000;
   23760              : #if HAVE_GETRLIMIT
   23761         5036 :     struct rlimit rlimit;
   23762         5036 :     if (!getrlimit (RLIMIT_NOFILE, &rlimit))
   23763              :       {
   23764         5036 :         lazy_hard_limit = (rlimit.rlim_max < 1000000
   23765         5036 :                            ? unsigned (rlimit.rlim_max) : 1000000);
   23766         5036 :         lazy_hard_limit = (lazy_hard_limit > LAZY_HEADROOM
   23767         5036 :                            ? lazy_hard_limit - LAZY_HEADROOM : 0);
   23768         5036 :         if (rlimit.rlim_cur < limit)
   23769            0 :           limit = unsigned (rlimit.rlim_cur);
   23770              :       }
   23771              : #endif
   23772         5036 :     limit = limit > LAZY_HEADROOM ? limit - LAZY_HEADROOM : 1;
   23773              : 
   23774         5036 :     if (unsigned parm = param_lazy_modules)
   23775              :       {
   23776         5036 :         if (parm <= limit || !lazy_hard_limit || !try_increase_lazy (parm))
   23777            6 :           lazy_limit = parm;
   23778              :       }
   23779              :     else
   23780            0 :       lazy_limit = limit;
   23781              :   }
   23782              : 
   23783         5036 :   if (dump ())
   23784              :     {
   23785          586 :       verstr_t ver;
   23786          586 :       version2string (MODULE_VERSION, ver);
   23787          586 :       dump ("Source: %s", main_input_filename);
   23788          586 :       dump ("Compiler: %s", version_string);
   23789          586 :       dump ("Modules: %s", ver);
   23790          586 :       dump ("Checking: %s",
   23791              : #if CHECKING_P
   23792              :             "checking"
   23793              : #elif ENABLE_ASSERT_CHECKING
   23794              :             "asserting"
   23795              : #else
   23796              :             "release"
   23797              : #endif
   23798              :             );
   23799          586 :       dump ("Compiled by: "
   23800              : #ifdef __GNUC__
   23801              :             "GCC %d.%d, %s", __GNUC__, __GNUC_MINOR__,
   23802              : #ifdef __OPTIMIZE__
   23803              :             "optimizing"
   23804              : #else
   23805              :             "not optimizing"
   23806              : #endif
   23807              : #else
   23808              :             "not GCC"
   23809              : #endif
   23810              :             );
   23811          586 :       dump ("Reading: %s", MAPPED_READING ? "mmap" : "fileio");
   23812          586 :       dump ("Writing: %s", MAPPED_WRITING ? "mmap" : "fileio");
   23813          586 :       dump ("Lazy limit: %u", lazy_limit);
   23814          586 :       dump ("Lazy hard limit: %u", lazy_hard_limit);
   23815          586 :       dump ("");
   23816              :     }
   23817              : 
   23818              :   /* Construct the global tree array.  This is an array of unique
   23819              :      global trees (& types).  Do this now, rather than lazily, as
   23820              :      some global trees are lazily created and we don't want that to
   23821              :      mess with our syndrome of fixed trees.  */
   23822         5036 :   unsigned crc = 0;
   23823         5036 :   vec_alloc (fixed_trees, 250);
   23824              : 
   23825         5622 :   dump () && dump ("+Creating globals");
   23826              :   /* Insert the TRANSLATION_UNIT_DECL.  */
   23827         5036 :   TREE_VISITED (DECL_CONTEXT (global_namespace)) = true;
   23828         5036 :   fixed_trees->quick_push (DECL_CONTEXT (global_namespace));
   23829        35252 :   for (unsigned jx = 0; global_tree_arys[jx].first; jx++)
   23830              :     {
   23831        25180 :       const tree *ptr = global_tree_arys[jx].first;
   23832        25180 :       unsigned limit = global_tree_arys[jx].second;
   23833              : 
   23834      1541016 :       for (unsigned ix = 0; ix != limit; ix++, ptr++)
   23835              :         {
   23836      1515836 :           !(ix & 31) && dump ("") && dump ("+\t%u:%u:", jx, ix);
   23837      1515836 :           unsigned v = maybe_add_global (*ptr, crc);
   23838      1692222 :           dump () && dump ("+%u", v);
   23839              :         }
   23840              :     }
   23841              :   /* OS- and machine-specific types are dynamically registered at
   23842              :      runtime, so cannot be part of global_tree_arys.  */
   23843         5036 :   registered_builtin_types && dump ("") && dump ("+\tB:");
   23844        20144 :   for (tree t = registered_builtin_types; t; t = TREE_CHAIN (t))
   23845              :     {
   23846        15108 :       unsigned v = maybe_add_global (TREE_VALUE (t), crc);
   23847        16866 :       dump () && dump ("+%u", v);
   23848              :     }
   23849         5036 :   global_crc = crc32_unsigned (crc, fixed_trees->length ());
   23850         5036 :   dump ("") && dump ("Created %u unique globals, crc=%x",
   23851              :                      fixed_trees->length (), global_crc);
   23852       970370 :   for (unsigned ix = fixed_trees->length (); ix--;)
   23853       965334 :     TREE_VISITED ((*fixed_trees)[ix]) = false;
   23854              : 
   23855         5036 :   dump.pop (0);
   23856              : 
   23857         5036 :   if (!flag_module_lazy)
   23858              :     /* Get the mapper now, if we're not being lazy.  */
   23859          320 :     get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   23860              : 
   23861         5036 :   if (!flag_preprocess_only)
   23862              :     {
   23863         4892 :       pending_table = new pending_map_t (EXPERIMENT (1, 400));
   23864         4892 :       entity_map = new entity_map_t (EXPERIMENT (1, 400));
   23865         4892 :       vec_safe_reserve (entity_ary, EXPERIMENT (1, 400));
   23866         4892 :       imported_temploid_friends
   23867         4892 :         = decl_tree_cache_map::create_ggc (EXPERIMENT (1, 400));
   23868              :     }
   23869              : 
   23870              : #if CHECKING_P
   23871         5036 :   note_defs = note_defs_table_t::create_ggc (1000);
   23872              : #endif
   23873              : 
   23874         5036 :   if (flag_header_unit && cpp_get_options (reader)->preprocessed)
   23875            9 :     begin_header_unit (reader);
   23876              : 
   23877              :   /* Collect here to make sure things are tagged correctly (when
   23878              :      aggressively GC'd).  */
   23879         5036 :   ggc_collect ();
   23880         5036 : }
   23881              : 
   23882              : /* If NODE is a deferred macro, load it.  */
   23883              : 
   23884              : static int
   23885        83275 : load_macros (cpp_reader *reader, cpp_hashnode *node, void *)
   23886              : {
   23887        83275 :   location_t main_loc
   23888        83275 :     = MAP_START_LOCATION (LINEMAPS_ORDINARY_MAP_AT (line_table, 0));
   23889              : 
   23890        83275 :   if (cpp_user_macro_p (node)
   23891        83275 :       && !node->value.macro)
   23892              :     {
   23893           72 :       cpp_macro *macro = cpp_get_deferred_macro (reader, node, main_loc);
   23894           72 :       dump () && dump ("Loaded macro #%s %I",
   23895              :                        macro ? "define" : "undef", identifier (node));
   23896              :     }
   23897              : 
   23898        83275 :   return 1;
   23899              : }
   23900              : 
   23901              : /* At the end of tokenizing, we no longer need the macro tables of
   23902              :    imports.  But the user might have requested some checking.  */
   23903              : 
   23904              : void
   23905        99518 : maybe_check_all_macros (cpp_reader *reader)
   23906              : {
   23907        99518 :   if (!warn_imported_macros)
   23908              :     return;
   23909              : 
   23910              :   /* Force loading of any remaining deferred macros.  This will
   23911              :      produce diagnostics if they are ill-formed.  */
   23912           21 :   unsigned n = dump.push (NULL);
   23913           21 :   cpp_forall_identifiers (reader, load_macros, NULL);
   23914           21 :   dump.pop (n);
   23915              : }
   23916              : 
   23917              : // State propagated from finish_module_processing to fini_modules
   23918              : 
   23919              : struct module_processing_cookie
   23920              : {
   23921              :   elf_out out;
   23922              :   module_state_config config;
   23923              :   char *cmi_name;
   23924              :   char *tmp_name;
   23925              :   unsigned crc;
   23926              :   bool began;
   23927              : 
   23928         2965 :   module_processing_cookie (char *cmi, char *tmp, int fd, int e)
   23929         2965 :     : out (fd, e), cmi_name (cmi), tmp_name (tmp), crc (0), began (false)
   23930              :   {
   23931              :   }
   23932         2965 :   ~module_processing_cookie ()
   23933              :   {
   23934         2965 :     XDELETEVEC (tmp_name);
   23935         2965 :     XDELETEVEC (cmi_name);
   23936         2965 :   }
   23937              : };
   23938              : 
   23939              : /* Write the CMI, if we're a module interface.  */
   23940              : 
   23941              : void *
   23942        99305 : finish_module_processing (cpp_reader *reader)
   23943              : {
   23944        99305 :   module_processing_cookie *cookie = nullptr;
   23945              : 
   23946        99305 :   if (header_module_p ())
   23947          921 :     module_kind &= ~MK_EXPORTING;
   23948              : 
   23949        99305 :   if (!modules || !this_module ()->name)
   23950              :     {
   23951        96337 :       if (flag_module_only)
   23952            6 :         warning (0, "%<-fmodule-only%> used for non-interface");
   23953              :     }
   23954         2968 :   else if (!flag_syntax_only)
   23955              :     {
   23956         2965 :       int fd = -1;
   23957         2965 :       int e = -1;
   23958              : 
   23959         2965 :       timevar_start (TV_MODULE_EXPORT);
   23960              : 
   23961              :       /* Force a valid but empty line map at the end.  This simplifies
   23962              :          the line table preparation and writing logic.  */
   23963         2965 :       linemap_add (line_table, LC_ENTER, false, "", 0);
   23964              : 
   23965              :       /* We write to a tmpname, and then atomically rename.  */
   23966         2965 :       char *cmi_name = NULL;
   23967         2965 :       char *tmp_name = NULL;
   23968         2965 :       module_state *state = this_module ();
   23969              : 
   23970         2965 :       unsigned n = dump.push (state);
   23971         2965 :       state->announce ("creating");
   23972         2965 :       if (state->filename)
   23973              :         {
   23974         2965 :           size_t len = 0;
   23975         2965 :           cmi_name = xstrdup (maybe_add_cmi_prefix (state->filename, &len));
   23976         2965 :           tmp_name = XNEWVEC (char, len + 3);
   23977         2965 :           memcpy (tmp_name, cmi_name, len);
   23978         2965 :           strcpy (&tmp_name[len], "~");
   23979              : 
   23980         2965 :           if (!errorcount)
   23981           41 :             for (unsigned again = 2; ; again--)
   23982              :               {
   23983         2891 :                 fd = open (tmp_name,
   23984              :                            O_RDWR | O_CREAT | O_TRUNC | O_CLOEXEC | O_BINARY,
   23985              :                            S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
   23986         2891 :                 e = errno;
   23987         2891 :                 if (fd >= 0 || !again || e != ENOENT)
   23988              :                   break;
   23989           41 :                 create_dirs (tmp_name);
   23990              :               }
   23991         2965 :           if (note_module_cmi_yes || state->inform_cmi_p)
   23992            3 :             inform (state->loc, "writing CMI %qs", cmi_name);
   23993         3265 :           dump () && dump ("CMI is %s", cmi_name);
   23994              :         }
   23995              : 
   23996         2965 :       cookie = new module_processing_cookie (cmi_name, tmp_name, fd, e);
   23997              : 
   23998         2965 :       if (errorcount)
   23999              :         /* Don't write the module if we have reported errors.  */;
   24000         2850 :       else if (erroneous_templates
   24001         2850 :                && !erroneous_templates->is_empty ())
   24002              :         {
   24003              :           /* Don't write the module if it contains an erroneous template.
   24004              :              Also emit notes about where errors occurred in case
   24005              :              -Wno-template-body was passed.  */
   24006            6 :           auto_diagnostic_group d;
   24007            6 :           error_at (state->loc, "not writing module %qs due to errors "
   24008              :                     "in template bodies", state->get_flatname ());
   24009            6 :           if (!warn_template_body)
   24010            3 :             inform (state->loc, "enable %<-Wtemplate-body%> for more details");
   24011           12 :           for (auto e : *erroneous_templates)
   24012            6 :             inform (e.second, "first error in %qD appeared here", e.first);
   24013            6 :         }
   24014         2844 :       else if (cookie->out.begin ())
   24015              :         {
   24016              :           /* So crashes finger-point the module decl.  */
   24017         2844 :           iloc_sentinel ils = state->loc;
   24018         2844 :           if (state->write_begin (&cookie->out, reader, cookie->config,
   24019         2844 :                                   cookie->crc))
   24020         2815 :             cookie->began = true;
   24021         2844 :         }
   24022              : 
   24023         2965 :       dump.pop (n);
   24024         2965 :       timevar_stop (TV_MODULE_EXPORT);
   24025              : 
   24026         2965 :       ggc_collect ();
   24027              :     }
   24028              : 
   24029        99305 :   if (modules)
   24030              :     {
   24031         4852 :       unsigned n = dump.push (NULL);
   24032         5438 :       dump () && dump ("Imported %u modules", modules->length () - 1);
   24033         5438 :       dump () && dump ("Containing %u clusters", available_clusters);
   24034         4852 :       dump () && dump ("Loaded %u clusters (%u%%)", loaded_clusters,
   24035          586 :                        (loaded_clusters * 100 + available_clusters / 2) /
   24036          586 :                        (available_clusters + !available_clusters));
   24037         4852 :       dump.pop (n);
   24038              :     }
   24039              : 
   24040        99305 :   return cookie;
   24041              : }
   24042              : 
   24043              : // Do the final emission of a module.  At this point we know whether
   24044              : // the module static initializer is a NOP or not.
   24045              : 
   24046              : static void
   24047         2965 : late_finish_module (cpp_reader *reader,  module_processing_cookie *cookie,
   24048              :                     bool init_fn_non_empty)
   24049              : {
   24050         2965 :   timevar_start (TV_MODULE_EXPORT);
   24051              : 
   24052         2965 :   module_state *state = this_module ();
   24053         2965 :   unsigned n = dump.push (state);
   24054         2965 :   state->announce ("finishing");
   24055              : 
   24056         2965 :   cookie->config.active_init = init_fn_non_empty;
   24057         2965 :   if (cookie->began)
   24058         2815 :     state->write_end (&cookie->out, reader, cookie->config, cookie->crc);
   24059              : 
   24060         2965 :   if (cookie->out.end () && cookie->cmi_name)
   24061              :     {
   24062              :       /* Some OS's do not replace NEWNAME if it already exists.
   24063              :          This'll have a race condition in erroneous concurrent
   24064              :          builds.  */
   24065         2850 :       unlink (cookie->cmi_name);
   24066         2850 :       if (rename (cookie->tmp_name, cookie->cmi_name))
   24067              :         {
   24068            0 :           dump () && dump ("Rename ('%s','%s') errno=%u",
   24069            0 :                            cookie->tmp_name, cookie->cmi_name, errno);
   24070            0 :           cookie->out.set_error (errno);
   24071              :         }
   24072              :     }
   24073              : 
   24074         2965 :   if (cookie->out.get_error () && cookie->began)
   24075              :     {
   24076            0 :       error_at (state->loc, "failed to write compiled module: %s",
   24077            0 :                 cookie->out.get_error (state->filename));
   24078            0 :       state->note_cmi_name ();
   24079              :     }
   24080              : 
   24081         2965 :   if (!errorcount)
   24082              :     {
   24083         2809 :       auto *mapper = get_mapper (cpp_main_loc (reader), cpp_get_deps (reader));
   24084         2809 :       mapper->ModuleCompiled (state->get_flatname ());
   24085              :     }
   24086          156 :   else if (cookie->cmi_name)
   24087              :     {
   24088              :       /* We failed, attempt to erase all evidence we even tried.  */
   24089          156 :       unlink (cookie->tmp_name);
   24090          156 :       unlink (cookie->cmi_name);
   24091              :     }
   24092              : 
   24093         2965 :   delete cookie;
   24094         2965 :   dump.pop (n);
   24095         2965 :   timevar_stop (TV_MODULE_EXPORT);
   24096         2965 : }
   24097              : 
   24098              : void
   24099        99305 : fini_modules (cpp_reader *reader, void *cookie, bool has_inits)
   24100              : {
   24101        99305 :   if (cookie)
   24102         2965 :     late_finish_module (reader,
   24103              :                         static_cast<module_processing_cookie *> (cookie),
   24104              :                         has_inits);
   24105              : 
   24106              :   /* We're done with the macro tables now.  */
   24107        99305 :   vec_free (macro_exports);
   24108        99305 :   vec_free (macro_imports);
   24109        99305 :   headers = NULL;
   24110              : 
   24111              :   /* We're now done with everything but the module names.  */
   24112        99305 :   set_cmi_repo (NULL);
   24113        99305 :   if (mapper)
   24114              :     {
   24115         4852 :       timevar_start (TV_MODULE_MAPPER);
   24116         4852 :       module_client::close_module_client (0, mapper);
   24117         4852 :       mapper = nullptr;
   24118         4852 :       timevar_stop (TV_MODULE_MAPPER);
   24119              :     }
   24120        99305 :   module_state_config::release ();
   24121              : 
   24122              : #if CHECKING_P
   24123        99305 :   note_defs = NULL;
   24124              : #endif
   24125              : 
   24126        99305 :   if (modules)
   24127         7855 :     for (unsigned ix = modules->length (); --ix;)
   24128         3003 :       if (module_state *state = (*modules)[ix])
   24129         3003 :         state->release ();
   24130              : 
   24131              :   /* No need to lookup modules anymore.  */
   24132        99305 :   modules_hash = NULL;
   24133              : 
   24134              :   /* Or entity array.  We still need the entity map to find import numbers.  */
   24135        99305 :   vec_free (entity_ary);
   24136        99305 :   entity_ary = NULL;
   24137              : 
   24138              :   /* Or remember any pending entities.  */
   24139       104157 :   delete pending_table;
   24140        99305 :   pending_table = NULL;
   24141              : 
   24142              :   /* Or any keys -- Let it go!  */
   24143       101831 :   delete keyed_table;
   24144        99305 :   keyed_table = NULL;
   24145              : 
   24146              :   /* Allow a GC, we've possibly made much data unreachable.  */
   24147        99305 :   ggc_collect ();
   24148        99305 : }
   24149              : 
   24150              : /* If CODE is a module option, handle it & return true.  Otherwise
   24151              :    return false.  For unknown reasons I cannot get the option
   24152              :    generation machinery to set fmodule-mapper or -fmodule-header to
   24153              :    make a string type option variable.  */
   24154              : 
   24155              : bool
   24156      1969417 : handle_module_option (unsigned code, const char *str, int)
   24157              : {
   24158      1969417 :   auto hdr = CMS_header;
   24159              : 
   24160      1969417 :   switch (opt_code (code))
   24161              :     {
   24162           48 :     case OPT_fmodule_mapper_:
   24163           48 :       module_mapper_name = str;
   24164           48 :       return true;
   24165              : 
   24166           12 :     case OPT_fmodule_header_:
   24167           12 :       {
   24168           12 :         if (!strcmp (str, "user"))
   24169              :           hdr = CMS_user;
   24170           12 :         else if (!strcmp (str, "system"))
   24171              :           hdr = CMS_system;
   24172              :         else
   24173            0 :           error ("unknown header kind %qs", str);
   24174              :       }
   24175              :       /* Fallthrough.  */
   24176              : 
   24177          936 :     case OPT_fmodule_header:
   24178          936 :       flag_header_unit = hdr;
   24179          936 :       flag_modules = 1;
   24180          936 :       return true;
   24181              : 
   24182            1 :     case OPT_flang_info_include_translate_:
   24183            1 :       vec_safe_push (note_includes, str);
   24184            1 :       return true;
   24185              : 
   24186            6 :     case OPT_flang_info_module_cmi_:
   24187            6 :       vec_safe_push (note_cmis, str);
   24188            6 :       return true;
   24189              : 
   24190              :     default:
   24191              :       return false;
   24192              :     }
   24193              : }
   24194              : 
   24195              : /* Set preprocessor callbacks and options for modules.  */
   24196              : 
   24197              : void
   24198       101060 : module_preprocess_options (cpp_reader *reader)
   24199              : {
   24200       101060 :   gcc_checking_assert (!lang_hooks.preprocess_undef);
   24201       101060 :   if (modules_p ())
   24202              :     {
   24203         5036 :       auto *cb = cpp_get_callbacks (reader);
   24204              : 
   24205         5036 :       cb->translate_include = maybe_translate_include;
   24206         5036 :       cb->user_deferred_macro = module_state::deferred_macro;
   24207         5036 :       if (flag_header_unit)
   24208              :         {
   24209              :           /* If the preprocessor hook is already in use, that
   24210              :              implementation will call the undef langhook.  */
   24211          933 :           if (cb->undef)
   24212            0 :             lang_hooks.preprocess_undef = module_state::undef_macro;
   24213              :           else
   24214          933 :             cb->undef = module_state::undef_macro;
   24215              :         }
   24216         5036 :       auto *opt = cpp_get_options (reader);
   24217         5036 :       opt->module_directives = true;
   24218         5036 :       if (flag_no_output)
   24219           18 :         opt->directives_only = true;
   24220         5036 :       if (opt->main_search == CMS_none)
   24221         5030 :         opt->main_search = cpp_main_search (flag_header_unit);
   24222              :     }
   24223       101060 : }
   24224              : 
   24225              : #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.